blob: a0788763757b0d650ab25d213aea9d1b5c8c032d [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
51 * @dev: drm device
52 * @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 *
59intel_find_panel_downclock(struct drm_device *dev,
60 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)
97 return drm_mode_duplicate(dev, tmp_mode);
98 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 &&
113 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100114 goto done;
115
116 switch (fitting_mode) {
117 case DRM_MODE_SCALE_CENTER:
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300118 width = pipe_config->pipe_src_w;
119 height = pipe_config->pipe_src_h;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300120 x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
121 y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100122 break;
123
124 case DRM_MODE_SCALE_ASPECT:
125 /* Scale but preserve the aspect ratio */
126 {
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300127 u32 scaled_width = adjusted_mode->crtc_hdisplay
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200128 * pipe_config->pipe_src_h;
129 u32 scaled_height = pipe_config->pipe_src_w
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300130 * adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100131 if (scaled_width > scaled_height) { /* pillar */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300132 width = scaled_height / pipe_config->pipe_src_h;
Adam Jackson302983e2011-07-13 16:32:32 -0400133 if (width & 1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400134 width++;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300135 x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100136 y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300137 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100138 } else if (scaled_width < scaled_height) { /* letter */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300139 height = scaled_width / pipe_config->pipe_src_w;
Adam Jackson302983e2011-07-13 16:32:32 -0400140 if (height & 1)
141 height++;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300142 y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100143 x = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300144 width = adjusted_mode->crtc_hdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100145 } else {
146 x = y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300147 width = adjusted_mode->crtc_hdisplay;
148 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100149 }
150 }
151 break;
152
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100153 case DRM_MODE_SCALE_FULLSCREEN:
154 x = y = 0;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300155 width = adjusted_mode->crtc_hdisplay;
156 height = adjusted_mode->crtc_vdisplay;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100157 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700158
159 default:
160 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
161 return;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100162 }
163
164done:
Jesse Barnesb074cec2013-04-25 12:55:02 -0700165 pipe_config->pch_pfit.pos = (x << 16) | y;
166 pipe_config->pch_pfit.size = (width << 16) | height;
Chris Wilsonfd4daa92013-08-27 17:04:17 +0100167 pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100168}
Chris Wilsona9573552010-08-22 13:18:16 +0100169
Jesse Barnes2dd24552013-04-25 12:55:01 -0700170static void
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300171centre_horizontally(struct drm_display_mode *adjusted_mode,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700172 int width)
173{
174 u32 border, sync_pos, blank_width, sync_width;
175
176 /* keep the hsync and hblank widths constant */
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300177 sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
178 blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700179 sync_pos = (blank_width - sync_width + 1) / 2;
180
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300181 border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700182 border += border & 1; /* make the border even */
183
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300184 adjusted_mode->crtc_hdisplay = width;
185 adjusted_mode->crtc_hblank_start = width + border;
186 adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700187
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300188 adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
189 adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700190}
191
192static void
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300193centre_vertically(struct drm_display_mode *adjusted_mode,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700194 int height)
195{
196 u32 border, sync_pos, blank_width, sync_width;
197
198 /* keep the vsync and vblank widths constant */
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300199 sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
200 blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700201 sync_pos = (blank_width - sync_width + 1) / 2;
202
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300203 border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700204
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300205 adjusted_mode->crtc_vdisplay = height;
206 adjusted_mode->crtc_vblank_start = height + border;
207 adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700208
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300209 adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
210 adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700211}
212
213static inline u32 panel_fitter_scaling(u32 source, u32 target)
214{
215 /*
216 * Floating point operation is not supported. So the FACTOR
217 * is defined, which can avoid the floating point computation
218 * when calculating the panel ratio.
219 */
220#define ACCURACY 12
221#define FACTOR (1 << ACCURACY)
222 u32 ratio = source * FACTOR / target;
223 return (FACTOR * ratio + FACTOR/2) / FACTOR;
224}
225
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200226static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200227 u32 *pfit_control)
228{
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300229 const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300230 u32 scaled_width = adjusted_mode->crtc_hdisplay *
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200231 pipe_config->pipe_src_h;
232 u32 scaled_height = pipe_config->pipe_src_w *
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300233 adjusted_mode->crtc_vdisplay;
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200234
235 /* 965+ is easy, it does everything in hw */
236 if (scaled_width > scaled_height)
237 *pfit_control |= PFIT_ENABLE |
238 PFIT_SCALING_PILLAR;
239 else if (scaled_width < scaled_height)
240 *pfit_control |= PFIT_ENABLE |
241 PFIT_SCALING_LETTER;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300242 else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200243 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
244}
245
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200246static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200247 u32 *pfit_control, u32 *pfit_pgm_ratios,
248 u32 *border)
249{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200250 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300251 u32 scaled_width = adjusted_mode->crtc_hdisplay *
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200252 pipe_config->pipe_src_h;
253 u32 scaled_height = pipe_config->pipe_src_w *
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300254 adjusted_mode->crtc_vdisplay;
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200255 u32 bits;
256
257 /*
258 * For earlier chips we have to calculate the scaling
259 * ratio by hand and program it into the
260 * PFIT_PGM_RATIO register
261 */
262 if (scaled_width > scaled_height) { /* pillar */
263 centre_horizontally(adjusted_mode,
264 scaled_height /
265 pipe_config->pipe_src_h);
266
267 *border = LVDS_BORDER_ENABLE;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300268 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200269 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300270 adjusted_mode->crtc_vdisplay);
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200271
272 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
273 bits << PFIT_VERT_SCALE_SHIFT);
274 *pfit_control |= (PFIT_ENABLE |
275 VERT_INTERP_BILINEAR |
276 HORIZ_INTERP_BILINEAR);
277 }
278 } else if (scaled_width < scaled_height) { /* letter */
279 centre_vertically(adjusted_mode,
280 scaled_width /
281 pipe_config->pipe_src_w);
282
283 *border = LVDS_BORDER_ENABLE;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300284 if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200285 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300286 adjusted_mode->crtc_hdisplay);
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200287
288 *pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
289 bits << PFIT_VERT_SCALE_SHIFT);
290 *pfit_control |= (PFIT_ENABLE |
291 VERT_INTERP_BILINEAR |
292 HORIZ_INTERP_BILINEAR);
293 }
294 } else {
295 /* Aspects match, Let hw scale both directions */
296 *pfit_control |= (PFIT_ENABLE |
297 VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
298 VERT_INTERP_BILINEAR |
299 HORIZ_INTERP_BILINEAR);
300 }
301}
302
Jesse Barnes2dd24552013-04-25 12:55:01 -0700303void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200304 struct intel_crtc_state *pipe_config,
Jesse Barnes2dd24552013-04-25 12:55:01 -0700305 int fitting_mode)
306{
307 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700308 u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300309 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700310
311 /* Native modes don't need fitting */
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300312 if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
313 adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
Jesse Barnes2dd24552013-04-25 12:55:01 -0700314 goto out;
315
316 switch (fitting_mode) {
317 case DRM_MODE_SCALE_CENTER:
318 /*
319 * For centered modes, we have to calculate border widths &
320 * heights and modify the values programmed into the CRTC.
321 */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300322 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
323 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700324 border = LVDS_BORDER_ENABLE;
325 break;
326 case DRM_MODE_SCALE_ASPECT:
327 /* Scale but preserve the aspect ratio */
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200328 if (INTEL_INFO(dev)->gen >= 4)
329 i965_scale_aspect(pipe_config, &pfit_control);
330 else
331 i9xx_scale_aspect(pipe_config, &pfit_control,
332 &pfit_pgm_ratios, &border);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700333 break;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700334 case DRM_MODE_SCALE_FULLSCREEN:
335 /*
336 * Full scaling, even if it changes the aspect ratio.
337 * Fortunately this is all done for us in hw.
338 */
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300339 if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
340 pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
Jesse Barnes2dd24552013-04-25 12:55:01 -0700341 pfit_control |= PFIT_ENABLE;
342 if (INTEL_INFO(dev)->gen >= 4)
343 pfit_control |= PFIT_SCALING_AUTO;
344 else
345 pfit_control |= (VERT_AUTO_SCALE |
346 VERT_INTERP_BILINEAR |
347 HORIZ_AUTO_SCALE |
348 HORIZ_INTERP_BILINEAR);
349 }
350 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700351 default:
352 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
353 return;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700354 }
355
356 /* 965+ wants fuzzy fitting */
357 /* FIXME: handle multiple panels by failing gracefully */
358 if (INTEL_INFO(dev)->gen >= 4)
359 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
360 PFIT_FILTER_FUZZY);
361
362out:
363 if ((pfit_control & PFIT_ENABLE) == 0) {
364 pfit_control = 0;
365 pfit_pgm_ratios = 0;
366 }
367
Daniel Vetter6b89cdd2014-07-09 22:35:53 +0200368 /* Make sure pre-965 set dither correctly for 18bpp panels. */
369 if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
370 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
371
Daniel Vetter2deefda2013-04-25 22:52:17 +0200372 pipe_config->gmch_pfit.control = pfit_control;
373 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
Daniel Vetter68fc8742013-04-25 22:52:16 +0200374 pipe_config->gmch_pfit.lvds_border_bits = border;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700375}
376
Jani Nikula525997e2014-04-29 23:30:48 +0300377enum drm_connector_status
378intel_panel_detect(struct drm_device *dev)
379{
380 struct drm_i915_private *dev_priv = dev->dev_private;
381
382 /* Assume that the BIOS does not lie through the OpRegion... */
383 if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
Williams, Dan J115719f2015-10-12 21:12:57 +0000384 return *dev_priv->opregion.lid_state & 0x1 ?
Jani Nikula525997e2014-04-29 23:30:48 +0300385 connector_status_connected :
386 connector_status_disconnected;
387 }
388
389 switch (i915.panel_ignore_lid) {
390 case -2:
391 return connector_status_connected;
392 case -1:
393 return connector_status_disconnected;
394 default:
395 return connector_status_unknown;
396 }
397}
398
Jani Nikula6dda7302014-06-24 18:27:40 +0300399/**
400 * scale - scale values from one range to another
401 *
402 * @source_val: value in range [@source_min..@source_max]
403 *
404 * Return @source_val in range [@source_min..@source_max] scaled to range
405 * [@target_min..@target_max].
406 */
407static uint32_t scale(uint32_t source_val,
408 uint32_t source_min, uint32_t source_max,
409 uint32_t target_min, uint32_t target_max)
410{
411 uint64_t target_val;
412
413 WARN_ON(source_min > source_max);
414 WARN_ON(target_min > target_max);
415
416 /* defensive */
417 source_val = clamp(source_val, source_min, source_max);
418
419 /* avoid overflows */
U. Artie Eoff673e7bb2014-09-29 15:49:32 -0700420 target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
421 (target_max - target_min), source_max - source_min);
Jani Nikula6dda7302014-06-24 18:27:40 +0300422 target_val += target_min;
423
424 return target_val;
425}
426
427/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
428static inline u32 scale_user_to_hw(struct intel_connector *connector,
429 u32 user_level, u32 user_max)
430{
431 struct intel_panel *panel = &connector->panel;
432
433 return scale(user_level, 0, user_max,
434 panel->backlight.min, panel->backlight.max);
435}
436
437/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
438 * to [hw_min..hw_max]. */
439static inline u32 clamp_user_to_hw(struct intel_connector *connector,
440 u32 user_level, u32 user_max)
441{
442 struct intel_panel *panel = &connector->panel;
443 u32 hw_level;
444
445 hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
446 hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
447
448 return hw_level;
449}
450
451/* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
452static inline u32 scale_hw_to_user(struct intel_connector *connector,
453 u32 hw_level, u32 user_max)
454{
455 struct intel_panel *panel = &connector->panel;
456
457 return scale(hw_level, panel->backlight.min, panel->backlight.max,
458 0, user_max);
459}
460
Jani Nikula7bd688c2013-11-08 16:48:56 +0200461static u32 intel_panel_compute_brightness(struct intel_connector *connector,
462 u32 val)
Carsten Emde7bd90902012-03-15 15:56:25 +0100463{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200464 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200465 struct intel_panel *panel = &connector->panel;
466
467 WARN_ON(panel->backlight.max == 0);
Carsten Emde4dca20e2012-03-15 15:56:26 +0100468
Jani Nikulad330a952014-01-21 11:24:25 +0200469 if (i915.invert_brightness < 0)
Carsten Emde4dca20e2012-03-15 15:56:26 +0100470 return val;
471
Jani Nikulad330a952014-01-21 11:24:25 +0200472 if (i915.invert_brightness > 0 ||
Jani Nikulad6540632013-04-12 15:18:36 +0300473 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200474 return panel->backlight.max - val;
Jani Nikulad6540632013-04-12 15:18:36 +0300475 }
Carsten Emde7bd90902012-03-15 15:56:25 +0100476
477 return val;
478}
479
Jani Nikula437b15b2015-09-04 16:55:13 +0300480static u32 lpt_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100481{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200482 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100483
484 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
485}
486
Jani Nikula7bd688c2013-11-08 16:48:56 +0200487static u32 pch_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100488{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200489 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200490
491 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
492}
493
494static u32 i9xx_get_backlight(struct intel_connector *connector)
495{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200496 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula636baeb2013-11-08 16:49:02 +0200497 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200498 u32 val;
499
500 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
Jani Nikulae6cb3722015-12-18 10:51:35 +0200501 if (INTEL_INFO(dev_priv)->gen < 4)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200502 val >>= 1;
503
Jani Nikula636baeb2013-11-08 16:49:02 +0200504 if (panel->backlight.combination_mode) {
Jani Nikula7bd688c2013-11-08 16:48:56 +0200505 u8 lbpc;
506
Joonas Lahtinene10fa552016-04-15 12:03:39 +0300507 pci_read_config_byte(dev_priv->dev->pdev, LBPC, &lbpc);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200508 val *= lbpc;
509 }
510
511 return val;
512}
513
Jani Nikulae6cb3722015-12-18 10:51:35 +0200514static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
Jani Nikula7bd688c2013-11-08 16:48:56 +0200515{
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200516 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
517 return 0;
518
Jani Nikula7bd688c2013-11-08 16:48:56 +0200519 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
520}
521
522static u32 vlv_get_backlight(struct intel_connector *connector)
523{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200524 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200525 enum pipe pipe = intel_get_pipe_from_connector(connector);
526
Jani Nikulae6cb3722015-12-18 10:51:35 +0200527 return _vlv_get_backlight(dev_priv, pipe);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200528}
529
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530530static u32 bxt_get_backlight(struct intel_connector *connector)
531{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200532 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530533 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530534
Sunil Kamath022e4e52015-09-30 22:34:57 +0530535 return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530536}
537
Shobhit Kumarb029e662015-06-26 14:32:10 +0530538static u32 pwm_get_backlight(struct intel_connector *connector)
539{
540 struct intel_panel *panel = &connector->panel;
541 int duty_ns;
542
543 duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
544 return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
545}
546
Jani Nikula7bd688c2013-11-08 16:48:56 +0200547static u32 intel_panel_get_backlight(struct intel_connector *connector)
548{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200549 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200550 struct intel_panel *panel = &connector->panel;
551 u32 val = 0;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300552
Daniel Vetter07f11d42014-09-15 14:35:09 +0200553 mutex_lock(&dev_priv->backlight_lock);
Chris Wilsona9573552010-08-22 13:18:16 +0100554
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200555 if (panel->backlight.enabled) {
Jani Nikula5507fae2015-09-14 14:03:48 +0300556 val = panel->backlight.get(connector);
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200557 val = intel_panel_compute_brightness(connector, val);
558 }
Jani Nikula8ba2d182013-04-12 15:18:37 +0300559
Daniel Vetter07f11d42014-09-15 14:35:09 +0200560 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300561
Chris Wilsona9573552010-08-22 13:18:16 +0100562 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
563 return val;
564}
565
Jani Nikula437b15b2015-09-04 16:55:13 +0300566static void lpt_set_backlight(struct intel_connector *connector, u32 level)
Ben Widawskyf8e10062013-11-11 11:12:57 +0200567{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200568 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Ben Widawskyf8e10062013-11-11 11:12:57 +0200569 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
570 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
571}
572
Jani Nikula7bd688c2013-11-08 16:48:56 +0200573static void pch_set_backlight(struct intel_connector *connector, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100574{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200575 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Chris Wilsona9573552010-08-22 13:18:16 +0100576 u32 tmp;
577
Jani Nikula7bd688c2013-11-08 16:48:56 +0200578 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
579 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
580}
Chris Wilsona9573552010-08-22 13:18:16 +0100581
Jani Nikula7bd688c2013-11-08 16:48:56 +0200582static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
583{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200584 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200585 struct intel_panel *panel = &connector->panel;
Jani Nikulab329b322013-11-08 16:48:57 +0200586 u32 tmp, mask;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100587
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200588 WARN_ON(panel->backlight.max == 0);
589
Jani Nikula636baeb2013-11-08 16:49:02 +0200590 if (panel->backlight.combination_mode) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100591 u8 lbpc;
592
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200593 lbpc = level * 0xfe / panel->backlight.max + 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100594 level /= lbpc;
Joonas Lahtinene10fa552016-04-15 12:03:39 +0300595 pci_write_config_byte(dev_priv->dev->pdev, LBPC, lbpc);
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100596 }
597
Jani Nikulae6cb3722015-12-18 10:51:35 +0200598 if (IS_GEN4(dev_priv)) {
Jani Nikulab329b322013-11-08 16:48:57 +0200599 mask = BACKLIGHT_DUTY_CYCLE_MASK;
600 } else {
Chris Wilsona9573552010-08-22 13:18:16 +0100601 level <<= 1;
Jani Nikulab329b322013-11-08 16:48:57 +0200602 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
603 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200604
Jani Nikulab329b322013-11-08 16:48:57 +0200605 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200606 I915_WRITE(BLC_PWM_CTL, tmp | level);
607}
608
609static void vlv_set_backlight(struct intel_connector *connector, u32 level)
610{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200611 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200612 enum pipe pipe = intel_get_pipe_from_connector(connector);
613 u32 tmp;
614
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200615 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
616 return;
617
Jani Nikula7bd688c2013-11-08 16:48:56 +0200618 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
619 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
620}
621
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530622static void bxt_set_backlight(struct intel_connector *connector, u32 level)
623{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200624 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530625 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530626
Sunil Kamath022e4e52015-09-30 22:34:57 +0530627 I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530628}
629
Shobhit Kumarb029e662015-06-26 14:32:10 +0530630static void pwm_set_backlight(struct intel_connector *connector, u32 level)
631{
632 struct intel_panel *panel = &connector->panel;
633 int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
634
635 pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
636}
637
Jani Nikula7bd688c2013-11-08 16:48:56 +0200638static void
639intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
640{
Jani Nikula5507fae2015-09-14 14:03:48 +0300641 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200642
643 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
644
645 level = intel_panel_compute_brightness(connector, level);
Jani Nikula5507fae2015-09-14 14:03:48 +0300646 panel->backlight.set(connector, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100647}
Chris Wilson47356eb2011-01-11 17:06:04 +0000648
Jani Nikula6dda7302014-06-24 18:27:40 +0300649/* set backlight brightness to level in range [0..max], scaling wrt hw min */
650static void intel_panel_set_backlight(struct intel_connector *connector,
651 u32 user_level, u32 user_max)
Takashi Iwaif52c6192011-10-14 11:45:40 +0200652{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200653 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula58c68772013-11-08 16:48:54 +0200654 struct intel_panel *panel = &connector->panel;
Jani Nikula6dda7302014-06-24 18:27:40 +0300655 u32 hw_level;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300656
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200657 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +0200658 return;
659
Daniel Vetter07f11d42014-09-15 14:35:09 +0200660 mutex_lock(&dev_priv->backlight_lock);
Jani Nikulad6540632013-04-12 15:18:36 +0300661
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200662 WARN_ON(panel->backlight.max == 0);
Jani Nikulad6540632013-04-12 15:18:36 +0300663
Jani Nikula6dda7302014-06-24 18:27:40 +0300664 hw_level = scale_user_to_hw(connector, user_level, user_max);
665 panel->backlight.level = hw_level;
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200666
Jani Nikula58c68772013-11-08 16:48:54 +0200667 if (panel->backlight.enabled)
Jani Nikula6dda7302014-06-24 18:27:40 +0300668 intel_panel_actually_set_backlight(connector, hw_level);
669
Daniel Vetter07f11d42014-09-15 14:35:09 +0200670 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikula6dda7302014-06-24 18:27:40 +0300671}
672
673/* set backlight brightness to level in range [0..max], assuming hw min is
674 * respected.
675 */
676void intel_panel_set_backlight_acpi(struct intel_connector *connector,
677 u32 user_level, u32 user_max)
678{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200679 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula6dda7302014-06-24 18:27:40 +0300680 struct intel_panel *panel = &connector->panel;
681 enum pipe pipe = intel_get_pipe_from_connector(connector);
682 u32 hw_level;
Jani Nikula6dda7302014-06-24 18:27:40 +0300683
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200684 /*
685 * INVALID_PIPE may occur during driver init because
686 * connection_mutex isn't held across the entire backlight
687 * setup + modeset readout, and the BIOS can issue the
688 * requests at any time.
689 */
Jani Nikula6dda7302014-06-24 18:27:40 +0300690 if (!panel->backlight.present || pipe == INVALID_PIPE)
691 return;
692
Daniel Vetter07f11d42014-09-15 14:35:09 +0200693 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula6dda7302014-06-24 18:27:40 +0300694
695 WARN_ON(panel->backlight.max == 0);
696
697 hw_level = clamp_user_to_hw(connector, user_level, user_max);
698 panel->backlight.level = hw_level;
699
700 if (panel->backlight.device)
701 panel->backlight.device->props.brightness =
702 scale_hw_to_user(connector,
703 panel->backlight.level,
704 panel->backlight.device->props.max_brightness);
705
706 if (panel->backlight.enabled)
707 intel_panel_actually_set_backlight(connector, hw_level);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200708
Daniel Vetter07f11d42014-09-15 14:35:09 +0200709 mutex_unlock(&dev_priv->backlight_lock);
Takashi Iwaif52c6192011-10-14 11:45:40 +0200710}
711
Jani Nikula437b15b2015-09-04 16:55:13 +0300712static void lpt_disable_backlight(struct intel_connector *connector)
713{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200714 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula437b15b2015-09-04 16:55:13 +0300715 u32 tmp;
716
717 intel_panel_actually_set_backlight(connector, 0);
718
Jani Nikula6675bce2015-10-28 13:57:09 +0200719 /*
720 * Although we don't support or enable CPU PWM with LPT/SPT based
721 * systems, it may have been enabled prior to loading the
722 * driver. Disable to avoid warnings on LCPLL disable.
723 *
724 * This needs rework if we need to add support for CPU PWM on PCH split
725 * platforms.
726 */
727 tmp = I915_READ(BLC_PWM_CPU_CTL2);
728 if (tmp & BLM_PWM_ENABLE) {
729 DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
730 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
731 }
732
Jani Nikula437b15b2015-09-04 16:55:13 +0300733 tmp = I915_READ(BLC_PWM_PCH_CTL1);
734 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
735}
736
Jani Nikula7bd688c2013-11-08 16:48:56 +0200737static void pch_disable_backlight(struct intel_connector *connector)
738{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200739 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200740 u32 tmp;
741
Jani Nikula3bd712e2013-11-08 16:48:59 +0200742 intel_panel_actually_set_backlight(connector, 0);
743
Jani Nikula7bd688c2013-11-08 16:48:56 +0200744 tmp = I915_READ(BLC_PWM_CPU_CTL2);
745 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
746
747 tmp = I915_READ(BLC_PWM_PCH_CTL1);
748 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
749}
750
Jani Nikula3bd712e2013-11-08 16:48:59 +0200751static void i9xx_disable_backlight(struct intel_connector *connector)
752{
753 intel_panel_actually_set_backlight(connector, 0);
754}
755
Jani Nikula7bd688c2013-11-08 16:48:56 +0200756static void i965_disable_backlight(struct intel_connector *connector)
757{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200758 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200759 u32 tmp;
760
Jani Nikula3bd712e2013-11-08 16:48:59 +0200761 intel_panel_actually_set_backlight(connector, 0);
762
Jani Nikula7bd688c2013-11-08 16:48:56 +0200763 tmp = I915_READ(BLC_PWM_CTL2);
764 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
765}
766
767static void vlv_disable_backlight(struct intel_connector *connector)
768{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200769 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200770 enum pipe pipe = intel_get_pipe_from_connector(connector);
771 u32 tmp;
772
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200773 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
774 return;
775
Jani Nikula3bd712e2013-11-08 16:48:59 +0200776 intel_panel_actually_set_backlight(connector, 0);
777
Jani Nikula7bd688c2013-11-08 16:48:56 +0200778 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
779 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
780}
781
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530782static void bxt_disable_backlight(struct intel_connector *connector)
783{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200784 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Sunil Kamath022e4e52015-09-30 22:34:57 +0530785 struct intel_panel *panel = &connector->panel;
786 u32 tmp, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530787
788 intel_panel_actually_set_backlight(connector, 0);
789
Sunil Kamath022e4e52015-09-30 22:34:57 +0530790 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
791 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
792 tmp & ~BXT_BLC_PWM_ENABLE);
793
794 if (panel->backlight.controller == 1) {
795 val = I915_READ(UTIL_PIN_CTL);
796 val &= ~UTIL_PIN_ENABLE;
797 I915_WRITE(UTIL_PIN_CTL, val);
798 }
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530799}
800
Shobhit Kumarb029e662015-06-26 14:32:10 +0530801static void pwm_disable_backlight(struct intel_connector *connector)
802{
803 struct intel_panel *panel = &connector->panel;
804
805 /* Disable the backlight */
806 pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
807 usleep_range(2000, 3000);
808 pwm_disable(panel->backlight.pwm);
809}
810
Jesse Barnes752aa882013-10-31 18:55:49 +0200811void intel_panel_disable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +0000812{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200813 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula58c68772013-11-08 16:48:54 +0200814 struct intel_panel *panel = &connector->panel;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300815
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200816 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +0200817 return;
818
Jani Nikula3f577572013-07-25 14:31:30 +0300819 /*
Lukas Wunner5389e912015-09-04 21:06:15 +0200820 * Do not disable backlight on the vga_switcheroo path. When switching
Jani Nikula3f577572013-07-25 14:31:30 +0300821 * away from i915, the other client may depend on i915 to handle the
822 * backlight. This will leave the backlight on unnecessarily when
823 * another client is not activated.
824 */
Jani Nikulae6cb3722015-12-18 10:51:35 +0200825 if (dev_priv->dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
Jani Nikula3f577572013-07-25 14:31:30 +0300826 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
827 return;
828 }
829
Daniel Vetter07f11d42014-09-15 14:35:09 +0200830 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000831
Jani Nikulaab656bb2014-08-13 12:10:12 +0300832 if (panel->backlight.device)
833 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
Jani Nikula58c68772013-11-08 16:48:54 +0200834 panel->backlight.enabled = false;
Jani Nikula5507fae2015-09-14 14:03:48 +0300835 panel->backlight.disable(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300836
Daniel Vetter07f11d42014-09-15 14:35:09 +0200837 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000838}
839
Jani Nikula437b15b2015-09-04 16:55:13 +0300840static void lpt_enable_backlight(struct intel_connector *connector)
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100841{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200842 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100843 struct intel_panel *panel = &connector->panel;
844 u32 pch_ctl1, pch_ctl2;
Chris Wilson47356eb2011-01-11 17:06:04 +0000845
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100846 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
847 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
848 DRM_DEBUG_KMS("pch backlight already enabled\n");
849 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
850 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
Chris Wilson47356eb2011-01-11 17:06:04 +0000851 }
852
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100853 pch_ctl2 = panel->backlight.max << 16;
854 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
855
856 pch_ctl1 = 0;
857 if (panel->backlight.active_low_pwm)
858 pch_ctl1 |= BLM_PCH_POLARITY;
859
Jani Nikulae6b26272014-10-10 17:53:33 +0300860 /* After LPT, override is the default. */
861 if (HAS_PCH_LPT(dev_priv))
862 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100863
864 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
865 POSTING_READ(BLC_PWM_PCH_CTL1);
866 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
867
868 /* This won't stick until the above enable. */
869 intel_panel_actually_set_backlight(connector, panel->backlight.level);
870}
871
Jani Nikula7bd688c2013-11-08 16:48:56 +0200872static void pch_enable_backlight(struct intel_connector *connector)
873{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200874 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200875 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200876 enum pipe pipe = intel_get_pipe_from_connector(connector);
877 enum transcoder cpu_transcoder =
878 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
Jani Nikulab35684b2013-11-14 12:13:41 +0200879 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200880
Jani Nikulab35684b2013-11-14 12:13:41 +0200881 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
882 if (cpu_ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000883 DRM_DEBUG_KMS("cpu backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200884 cpu_ctl2 &= ~BLM_PWM_ENABLE;
885 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200886 }
Jani Nikula3bd712e2013-11-08 16:48:59 +0200887
Jani Nikulab35684b2013-11-14 12:13:41 +0200888 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
889 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
890 DRM_DEBUG_KMS("pch backlight already enabled\n");
891 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
892 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
893 }
894
895 if (cpu_transcoder == TRANSCODER_EDP)
896 cpu_ctl2 = BLM_TRANSCODER_EDP;
897 else
898 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
899 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
900 POSTING_READ(BLC_PWM_CPU_CTL2);
901 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
902
903 /* This won't stick until the above enable. */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200904 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +0200905
906 pch_ctl2 = panel->backlight.max << 16;
907 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
908
Jani Nikulab35684b2013-11-14 12:13:41 +0200909 pch_ctl1 = 0;
910 if (panel->backlight.active_low_pwm)
911 pch_ctl1 |= BLM_PCH_POLARITY;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100912
Jani Nikulab35684b2013-11-14 12:13:41 +0200913 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
914 POSTING_READ(BLC_PWM_PCH_CTL1);
915 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200916}
917
918static void i9xx_enable_backlight(struct intel_connector *connector)
919{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200920 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200921 struct intel_panel *panel = &connector->panel;
Jani Nikulab35684b2013-11-14 12:13:41 +0200922 u32 ctl, freq;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200923
Jani Nikulab35684b2013-11-14 12:13:41 +0200924 ctl = I915_READ(BLC_PWM_CTL);
925 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
Scot Doyle813008c2014-08-19 02:07:13 +0000926 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200927 I915_WRITE(BLC_PWM_CTL, 0);
928 }
929
930 freq = panel->backlight.max;
931 if (panel->backlight.combination_mode)
932 freq /= 0xff;
933
934 ctl = freq << 17;
Jani Nikulab6ab66aa2014-02-25 13:11:47 +0200935 if (panel->backlight.combination_mode)
Jani Nikulab35684b2013-11-14 12:13:41 +0200936 ctl |= BLM_LEGACY_MODE;
Jani Nikulae6cb3722015-12-18 10:51:35 +0200937 if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
Jani Nikulab35684b2013-11-14 12:13:41 +0200938 ctl |= BLM_POLARITY_PNV;
939
940 I915_WRITE(BLC_PWM_CTL, ctl);
941 POSTING_READ(BLC_PWM_CTL);
942
943 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200944 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula2059ac32015-06-26 14:18:56 +0300945
946 /*
947 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
948 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
949 * that has backlight.
950 */
Jani Nikulae6cb3722015-12-18 10:51:35 +0200951 if (IS_GEN2(dev_priv))
Jani Nikula2059ac32015-06-26 14:18:56 +0300952 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200953}
954
955static void i965_enable_backlight(struct intel_connector *connector)
956{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200957 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200958 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200959 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +0200960 u32 ctl, ctl2, freq;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200961
Jani Nikulab35684b2013-11-14 12:13:41 +0200962 ctl2 = I915_READ(BLC_PWM_CTL2);
963 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000964 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200965 ctl2 &= ~BLM_PWM_ENABLE;
966 I915_WRITE(BLC_PWM_CTL2, ctl2);
967 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200968
Jani Nikulab35684b2013-11-14 12:13:41 +0200969 freq = panel->backlight.max;
970 if (panel->backlight.combination_mode)
971 freq /= 0xff;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200972
Jani Nikulab35684b2013-11-14 12:13:41 +0200973 ctl = freq << 16;
974 I915_WRITE(BLC_PWM_CTL, ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200975
Jani Nikulab35684b2013-11-14 12:13:41 +0200976 ctl2 = BLM_PIPE(pipe);
977 if (panel->backlight.combination_mode)
978 ctl2 |= BLM_COMBINATION_MODE;
979 if (panel->backlight.active_low_pwm)
980 ctl2 |= BLM_POLARITY_I965;
981 I915_WRITE(BLC_PWM_CTL2, ctl2);
982 POSTING_READ(BLC_PWM_CTL2);
983 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
Jani Nikula2e7eeeb2014-06-09 18:24:34 +0300984
985 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200986}
987
988static void vlv_enable_backlight(struct intel_connector *connector)
989{
Jani Nikulae6cb3722015-12-18 10:51:35 +0200990 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200991 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200992 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +0200993 u32 ctl, ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200994
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200995 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
996 return;
997
Jani Nikulab35684b2013-11-14 12:13:41 +0200998 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
999 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +00001000 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +02001001 ctl2 &= ~BLM_PWM_ENABLE;
1002 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1003 }
Jani Nikula7bd688c2013-11-08 16:48:56 +02001004
Jani Nikulab35684b2013-11-14 12:13:41 +02001005 ctl = panel->backlight.max << 16;
1006 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001007
Jani Nikulab35684b2013-11-14 12:13:41 +02001008 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +02001009 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +02001010
1011 ctl2 = 0;
1012 if (panel->backlight.active_low_pwm)
1013 ctl2 |= BLM_POLARITY_I965;
1014 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1015 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
1016 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001017}
1018
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301019static void bxt_enable_backlight(struct intel_connector *connector)
1020{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001021 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301022 struct intel_panel *panel = &connector->panel;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301023 enum pipe pipe = intel_get_pipe_from_connector(connector);
1024 u32 pwm_ctl, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301025
Sunil Kamath022e4e52015-09-30 22:34:57 +05301026 /* To use 2nd set of backlight registers, utility pin has to be
1027 * enabled with PWM mode.
1028 * The field should only be changed when the utility pin is disabled
1029 */
1030 if (panel->backlight.controller == 1) {
1031 val = I915_READ(UTIL_PIN_CTL);
1032 if (val & UTIL_PIN_ENABLE) {
1033 DRM_DEBUG_KMS("util pin already enabled\n");
1034 val &= ~UTIL_PIN_ENABLE;
1035 I915_WRITE(UTIL_PIN_CTL, val);
1036 }
1037
1038 val = 0;
1039 if (panel->backlight.util_pin_active_low)
1040 val |= UTIL_PIN_POLARITY;
1041 I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1042 UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1043 }
1044
1045 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301046 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1047 DRM_DEBUG_KMS("backlight already enabled\n");
1048 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301049 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1050 pwm_ctl);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301051 }
1052
Sunil Kamath022e4e52015-09-30 22:34:57 +05301053 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1054 panel->backlight.max);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301055
1056 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1057
1058 pwm_ctl = 0;
1059 if (panel->backlight.active_low_pwm)
1060 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1061
Sunil Kamath022e4e52015-09-30 22:34:57 +05301062 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1063 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1064 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1065 pwm_ctl | BXT_BLC_PWM_ENABLE);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301066}
1067
Shobhit Kumarb029e662015-06-26 14:32:10 +05301068static void pwm_enable_backlight(struct intel_connector *connector)
1069{
1070 struct intel_panel *panel = &connector->panel;
1071
1072 pwm_enable(panel->backlight.pwm);
1073 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1074}
1075
Jesse Barnes752aa882013-10-31 18:55:49 +02001076void intel_panel_enable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +00001077{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001078 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula58c68772013-11-08 16:48:54 +02001079 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +02001080 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +03001081
Ville Syrjälä260d8f92014-11-07 15:20:23 +02001082 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +02001083 return;
1084
Damien Lespiau6f2bcce2013-10-16 12:29:54 +01001085 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
Chris Wilson540b5d02013-10-13 12:56:31 +01001086
Daniel Vetter07f11d42014-09-15 14:35:09 +02001087 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001088
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001089 WARN_ON(panel->backlight.max == 0);
1090
Jeremiah Mahler13f3fbe2015-01-12 11:01:03 -08001091 if (panel->backlight.level <= panel->backlight.min) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001092 panel->backlight.level = panel->backlight.max;
Jani Nikula58c68772013-11-08 16:48:54 +02001093 if (panel->backlight.device)
1094 panel->backlight.device->props.brightness =
Jani Nikula6dda7302014-06-24 18:27:40 +03001095 scale_hw_to_user(connector,
1096 panel->backlight.level,
1097 panel->backlight.device->props.max_brightness);
Jani Nikulab6b3ba52013-03-12 11:44:15 +02001098 }
Chris Wilson47356eb2011-01-11 17:06:04 +00001099
Jani Nikula5507fae2015-09-14 14:03:48 +03001100 panel->backlight.enable(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001101 panel->backlight.enabled = true;
Jani Nikulaab656bb2014-08-13 12:10:12 +03001102 if (panel->backlight.device)
1103 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
Jani Nikula8ba2d182013-04-12 15:18:37 +03001104
Daniel Vetter07f11d42014-09-15 14:35:09 +02001105 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001106}
1107
Jani Nikula912e8b12013-09-18 17:19:45 +03001108#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001109static int intel_backlight_device_update_status(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001110{
Jesse Barnes752aa882013-10-31 18:55:49 +02001111 struct intel_connector *connector = bl_get_data(bd);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001112 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +02001113 struct drm_device *dev = connector->base.dev;
1114
Rob Clark51fd3712013-11-19 12:10:12 -05001115 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Chris Wilson540b5d02013-10-13 12:56:31 +01001116 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1117 bd->props.brightness, bd->props.max_brightness);
Jesse Barnes752aa882013-10-31 18:55:49 +02001118 intel_panel_set_backlight(connector, bd->props.brightness,
Jani Nikulad6540632013-04-12 15:18:36 +03001119 bd->props.max_brightness);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001120
1121 /*
1122 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1123 * backlight class device does not make it easy to to differentiate
1124 * between callbacks for brightness and bl_power, so our backlight_power
1125 * callback needs to take this into account.
1126 */
1127 if (panel->backlight.enabled) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001128 if (panel->backlight.power) {
Jani Nikulae6755fb2014-08-12 17:11:42 +03001129 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1130 bd->props.brightness != 0;
Jani Nikula5507fae2015-09-14 14:03:48 +03001131 panel->backlight.power(connector, enable);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001132 }
1133 } else {
1134 bd->props.power = FB_BLANK_POWERDOWN;
1135 }
1136
Rob Clark51fd3712013-11-19 12:10:12 -05001137 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001138 return 0;
1139}
1140
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001141static int intel_backlight_device_get_brightness(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001142{
Jesse Barnes752aa882013-10-31 18:55:49 +02001143 struct intel_connector *connector = bl_get_data(bd);
1144 struct drm_device *dev = connector->base.dev;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001145 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula6dda7302014-06-24 18:27:40 +03001146 u32 hw_level;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001147 int ret;
Jesse Barnes752aa882013-10-31 18:55:49 +02001148
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001149 intel_runtime_pm_get(dev_priv);
Rob Clark51fd3712013-11-19 12:10:12 -05001150 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Jani Nikula6dda7302014-06-24 18:27:40 +03001151
1152 hw_level = intel_panel_get_backlight(connector);
1153 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1154
Rob Clark51fd3712013-11-19 12:10:12 -05001155 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001156 intel_runtime_pm_put(dev_priv);
Jesse Barnes752aa882013-10-31 18:55:49 +02001157
Jani Nikula7bd688c2013-11-08 16:48:56 +02001158 return ret;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001159}
1160
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001161static const struct backlight_ops intel_backlight_device_ops = {
1162 .update_status = intel_backlight_device_update_status,
1163 .get_brightness = intel_backlight_device_get_brightness,
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001164};
1165
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001166static int intel_backlight_device_register(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001167{
Jani Nikula58c68772013-11-08 16:48:54 +02001168 struct intel_panel *panel = &connector->panel;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001169 struct backlight_properties props;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001170
Jani Nikula58c68772013-11-08 16:48:54 +02001171 if (WARN_ON(panel->backlight.device))
Jani Nikuladc652f92013-04-12 15:18:38 +03001172 return -ENODEV;
1173
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001174 if (!panel->backlight.present)
1175 return 0;
1176
Jani Nikula6dda7302014-06-24 18:27:40 +03001177 WARN_ON(panel->backlight.max == 0);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001178
Corentin Charyaf437cf2012-05-22 10:29:46 +01001179 memset(&props, 0, sizeof(props));
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001180 props.type = BACKLIGHT_RAW;
Jani Nikula6dda7302014-06-24 18:27:40 +03001181
1182 /*
1183 * Note: Everything should work even if the backlight device max
1184 * presented to the userspace is arbitrarily chosen.
1185 */
Jani Nikula7bd688c2013-11-08 16:48:56 +02001186 props.max_brightness = panel->backlight.max;
Jani Nikula6dda7302014-06-24 18:27:40 +03001187 props.brightness = scale_hw_to_user(connector,
1188 panel->backlight.level,
1189 props.max_brightness);
Jani Nikula58c68772013-11-08 16:48:54 +02001190
Jani Nikulaab656bb2014-08-13 12:10:12 +03001191 if (panel->backlight.enabled)
1192 props.power = FB_BLANK_UNBLANK;
1193 else
1194 props.power = FB_BLANK_POWERDOWN;
1195
Jani Nikula58c68772013-11-08 16:48:54 +02001196 /*
1197 * Note: using the same name independent of the connector prevents
1198 * registration of multiple backlight devices in the driver.
1199 */
1200 panel->backlight.device =
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001201 backlight_device_register("intel_backlight",
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001202 connector->base.kdev,
1203 connector,
1204 &intel_backlight_device_ops, &props);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001205
Jani Nikula58c68772013-11-08 16:48:54 +02001206 if (IS_ERR(panel->backlight.device)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001207 DRM_ERROR("Failed to register backlight: %ld\n",
Jani Nikula58c68772013-11-08 16:48:54 +02001208 PTR_ERR(panel->backlight.device));
1209 panel->backlight.device = NULL;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001210 return -ENODEV;
1211 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001212
1213 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1214 connector->base.name);
1215
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001216 return 0;
1217}
1218
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001219static void intel_backlight_device_unregister(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001220{
Jani Nikula58c68772013-11-08 16:48:54 +02001221 struct intel_panel *panel = &connector->panel;
1222
1223 if (panel->backlight.device) {
1224 backlight_device_unregister(panel->backlight.device);
1225 panel->backlight.device = NULL;
Jani Nikuladc652f92013-04-12 15:18:38 +03001226 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001227}
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001228#else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1229static int intel_backlight_device_register(struct intel_connector *connector)
1230{
1231 return 0;
1232}
1233static void intel_backlight_device_unregister(struct intel_connector *connector)
1234{
1235}
1236#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1237
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001238/*
Jani Nikula2dd69822015-12-01 10:23:50 +02001239 * BXT: PWM clock frequency = 19.2 MHz.
1240 */
1241static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1242{
Ville Syrjälä37f22482016-03-02 17:22:18 +02001243 return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
Jani Nikula2dd69822015-12-01 10:23:50 +02001244}
1245
1246/*
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001247 * SPT: This value represents the period of the PWM stream in clock periods
1248 * multiplied by 16 (default increment) or 128 (alternate increment selected in
1249 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1250 */
1251static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1252{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001253 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001254 u32 mul;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001255
1256 if (I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY)
1257 mul = 128;
1258 else
1259 mul = 16;
1260
Ville Syrjälä37f22482016-03-02 17:22:18 +02001261 return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001262}
1263
1264/*
1265 * LPT: This value represents the period of the PWM stream in clock periods
1266 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1267 * LPT SOUTH_CHICKEN2 register bit 5).
1268 */
1269static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1270{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001271 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001272 u32 mul, clock;
1273
1274 if (I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY)
1275 mul = 16;
1276 else
1277 mul = 128;
1278
Ville Syrjälä56f5f702015-11-30 16:23:44 +02001279 if (HAS_PCH_LPT_H(dev_priv))
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001280 clock = MHz(135); /* LPT:H */
1281 else
1282 clock = MHz(24); /* LPT:LP */
1283
Ville Syrjälä37f22482016-03-02 17:22:18 +02001284 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001285}
1286
1287/*
1288 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1289 * display raw clocks multiplied by 128.
1290 */
1291static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1292{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001293 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001294
Ville Syrjälä37f22482016-03-02 17:22:18 +02001295 return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001296}
1297
1298/*
1299 * Gen2: This field determines the number of time base events (display core
1300 * clock frequency/32) in total for a complete cycle of modulated backlight
1301 * control.
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001302 *
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001303 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1304 * divided by 32.
1305 */
1306static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1307{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001308 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001309 int clock;
1310
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001311 if (IS_PINEVIEW(dev_priv))
1312 clock = KHz(dev_priv->rawclk_freq);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001313 else
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001314 clock = KHz(dev_priv->cdclk_freq);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001315
Ville Syrjälä37f22482016-03-02 17:22:18 +02001316 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001317}
1318
1319/*
1320 * Gen4: This value represents the period of the PWM stream in display core
Ville Syrjälä83d83392015-11-30 16:23:43 +02001321 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
1322 *
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001323 */
1324static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1325{
1326 struct drm_device *dev = connector->base.dev;
1327 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä83d83392015-11-30 16:23:43 +02001328 int clock;
1329
1330 if (IS_G4X(dev_priv))
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001331 clock = KHz(dev_priv->rawclk_freq);
Ville Syrjälä83d83392015-11-30 16:23:43 +02001332 else
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001333 clock = KHz(dev_priv->cdclk_freq);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001334
Ville Syrjälä37f22482016-03-02 17:22:18 +02001335 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001336}
1337
1338/*
1339 * VLV: This value represents the period of the PWM stream in display core
1340 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1341 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1342 */
1343static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1344{
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001345 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1346 int mul, clock;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001347
1348 if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001349 if (IS_CHERRYVIEW(dev_priv))
1350 clock = KHz(19200);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001351 else
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001352 clock = MHz(25);
1353 mul = 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001354 } else {
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001355 clock = KHz(dev_priv->rawclk_freq);
1356 mul = 128;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001357 }
Ville Syrjäläe7dc33f2016-03-02 17:22:13 +02001358
Ville Syrjälä37f22482016-03-02 17:22:18 +02001359 return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001360}
1361
1362static u32 get_backlight_max_vbt(struct intel_connector *connector)
1363{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001364 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula5507fae2015-09-14 14:03:48 +03001365 struct intel_panel *panel = &connector->panel;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001366 u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1367 u32 pwm;
1368
Jani Nikula50752222015-12-01 10:23:51 +02001369 if (!panel->backlight.hz_to_pwm) {
1370 DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001371 return 0;
1372 }
1373
Jani Nikula50752222015-12-01 10:23:51 +02001374 if (pwm_freq_hz) {
1375 DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
1376 pwm_freq_hz);
1377 } else {
1378 pwm_freq_hz = 200;
1379 DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
1380 pwm_freq_hz);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001381 }
1382
Jani Nikula5507fae2015-09-14 14:03:48 +03001383 pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001384 if (!pwm) {
1385 DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1386 return 0;
1387 }
1388
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001389 return pwm;
1390}
1391
1392/*
1393 * Note: The setup hooks can't assume pipe is set!
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001394 */
Jani Nikula6dda7302014-06-24 18:27:40 +03001395static u32 get_backlight_min_vbt(struct intel_connector *connector)
1396{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001397 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula6dda7302014-06-24 18:27:40 +03001398 struct intel_panel *panel = &connector->panel;
Jani Nikulae1c412e2014-11-05 14:46:31 +02001399 int min;
Jani Nikula6dda7302014-06-24 18:27:40 +03001400
1401 WARN_ON(panel->backlight.max == 0);
1402
Jani Nikulae1c412e2014-11-05 14:46:31 +02001403 /*
1404 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1405 * to problems. There are such machines out there. Either our
1406 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1407 * against this by letting the minimum be at most (arbitrarily chosen)
1408 * 25% of the max.
1409 */
1410 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1411 if (min != dev_priv->vbt.backlight.min_brightness) {
1412 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1413 dev_priv->vbt.backlight.min_brightness, min);
1414 }
1415
Jani Nikula6dda7302014-06-24 18:27:40 +03001416 /* vbt value is a coefficient in range [0..255] */
Jani Nikulae1c412e2014-11-05 14:46:31 +02001417 return scale(min, 0, 255, 0, panel->backlight.max);
Jani Nikula6dda7302014-06-24 18:27:40 +03001418}
1419
Jani Nikula437b15b2015-09-04 16:55:13 +03001420static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001421{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001422 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001423 struct intel_panel *panel = &connector->panel;
1424 u32 pch_ctl1, pch_ctl2, val;
1425
1426 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1427 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1428
1429 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1430 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001431
1432 if (!panel->backlight.max)
1433 panel->backlight.max = get_backlight_max_vbt(connector);
1434
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001435 if (!panel->backlight.max)
1436 return -ENODEV;
1437
Jani Nikula6dda7302014-06-24 18:27:40 +03001438 panel->backlight.min = get_backlight_min_vbt(connector);
1439
Jani Nikula437b15b2015-09-04 16:55:13 +03001440 val = lpt_get_backlight(connector);
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001441 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1442
1443 panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
1444 panel->backlight.level != 0;
1445
1446 return 0;
1447}
1448
Ville Syrjälä6517d272014-11-07 11:16:02 +02001449static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001450{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001451 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001452 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001453 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001454
Jani Nikula636baeb2013-11-08 16:49:02 +02001455 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1456 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1457
1458 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1459 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001460
1461 if (!panel->backlight.max)
1462 panel->backlight.max = get_backlight_max_vbt(connector);
1463
Jani Nikula7bd688c2013-11-08 16:48:56 +02001464 if (!panel->backlight.max)
1465 return -ENODEV;
1466
Jani Nikula6dda7302014-06-24 18:27:40 +03001467 panel->backlight.min = get_backlight_min_vbt(connector);
1468
Jani Nikula7bd688c2013-11-08 16:48:56 +02001469 val = pch_get_backlight(connector);
1470 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1471
Jani Nikula636baeb2013-11-08 16:49:02 +02001472 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1473 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1474 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1475
Jani Nikula7bd688c2013-11-08 16:48:56 +02001476 return 0;
1477}
1478
Ville Syrjälä6517d272014-11-07 11:16:02 +02001479static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001480{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001481 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001482 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001483 u32 ctl, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001484
Jani Nikula636baeb2013-11-08 16:49:02 +02001485 ctl = I915_READ(BLC_PWM_CTL);
1486
Jani Nikulae6cb3722015-12-18 10:51:35 +02001487 if (IS_GEN2(dev_priv) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula636baeb2013-11-08 16:49:02 +02001488 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1489
Jani Nikulae6cb3722015-12-18 10:51:35 +02001490 if (IS_PINEVIEW(dev_priv))
Jani Nikula636baeb2013-11-08 16:49:02 +02001491 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1492
1493 panel->backlight.max = ctl >> 17;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001494
1495 if (!panel->backlight.max) {
1496 panel->backlight.max = get_backlight_max_vbt(connector);
1497 panel->backlight.max >>= 1;
1498 }
Jani Nikula636baeb2013-11-08 16:49:02 +02001499
Jani Nikula7bd688c2013-11-08 16:48:56 +02001500 if (!panel->backlight.max)
1501 return -ENODEV;
1502
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001503 if (panel->backlight.combination_mode)
1504 panel->backlight.max *= 0xff;
1505
Jani Nikula6dda7302014-06-24 18:27:40 +03001506 panel->backlight.min = get_backlight_min_vbt(connector);
1507
Jani Nikula7bd688c2013-11-08 16:48:56 +02001508 val = i9xx_get_backlight(connector);
1509 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1510
Jani Nikula636baeb2013-11-08 16:49:02 +02001511 panel->backlight.enabled = panel->backlight.level != 0;
1512
Jani Nikula7bd688c2013-11-08 16:48:56 +02001513 return 0;
1514}
1515
Ville Syrjälä6517d272014-11-07 11:16:02 +02001516static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001517{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001518 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001519 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001520 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001521
Jani Nikula636baeb2013-11-08 16:49:02 +02001522 ctl2 = I915_READ(BLC_PWM_CTL2);
1523 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1524 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1525
1526 ctl = I915_READ(BLC_PWM_CTL);
1527 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001528
1529 if (!panel->backlight.max)
1530 panel->backlight.max = get_backlight_max_vbt(connector);
Jani Nikula636baeb2013-11-08 16:49:02 +02001531
Jani Nikula7bd688c2013-11-08 16:48:56 +02001532 if (!panel->backlight.max)
1533 return -ENODEV;
1534
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001535 if (panel->backlight.combination_mode)
1536 panel->backlight.max *= 0xff;
1537
Jani Nikula6dda7302014-06-24 18:27:40 +03001538 panel->backlight.min = get_backlight_min_vbt(connector);
1539
Jani Nikula7bd688c2013-11-08 16:48:56 +02001540 val = i9xx_get_backlight(connector);
1541 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1542
Jani Nikula636baeb2013-11-08 16:49:02 +02001543 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1544 panel->backlight.level != 0;
1545
Jani Nikula7bd688c2013-11-08 16:48:56 +02001546 return 0;
1547}
1548
Ville Syrjälä6517d272014-11-07 11:16:02 +02001549static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001550{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001551 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001552 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001553 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001554
Ville Syrjälä6517d272014-11-07 11:16:02 +02001555 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1556 return -ENODEV;
1557
1558 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001559 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1560
Ville Syrjälä6517d272014-11-07 11:16:02 +02001561 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001562 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001563
1564 if (!panel->backlight.max)
1565 panel->backlight.max = get_backlight_max_vbt(connector);
1566
Jani Nikula7bd688c2013-11-08 16:48:56 +02001567 if (!panel->backlight.max)
1568 return -ENODEV;
1569
Jani Nikula6dda7302014-06-24 18:27:40 +03001570 panel->backlight.min = get_backlight_min_vbt(connector);
1571
Jani Nikulae6cb3722015-12-18 10:51:35 +02001572 val = _vlv_get_backlight(dev_priv, pipe);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001573 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1574
Jani Nikula636baeb2013-11-08 16:49:02 +02001575 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1576 panel->backlight.level != 0;
1577
Jani Nikula7bd688c2013-11-08 16:48:56 +02001578 return 0;
1579}
1580
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301581static int
1582bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1583{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001584 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301585 struct intel_panel *panel = &connector->panel;
1586 u32 pwm_ctl, val;
1587
Sunil Kamath022e4e52015-09-30 22:34:57 +05301588 /*
1589 * For BXT hard coding the Backlight controller to 0.
1590 * TODO : Read the controller value from VBT and generalize
1591 */
1592 panel->backlight.controller = 0;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301593
Sunil Kamath022e4e52015-09-30 22:34:57 +05301594 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1595
1596 /* Keeping the check if controller 1 is to be programmed.
1597 * This will come into affect once the VBT parsing
1598 * is fixed for controller selection, and controller 1 is used
1599 * for a prticular display configuration.
1600 */
1601 if (panel->backlight.controller == 1) {
1602 val = I915_READ(UTIL_PIN_CTL);
1603 panel->backlight.util_pin_active_low =
1604 val & UTIL_PIN_POLARITY;
1605 }
1606
1607 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1608 panel->backlight.max =
1609 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001610
1611 if (!panel->backlight.max)
1612 panel->backlight.max = get_backlight_max_vbt(connector);
1613
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301614 if (!panel->backlight.max)
1615 return -ENODEV;
1616
1617 val = bxt_get_backlight(connector);
1618 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1619
1620 panel->backlight.enabled = (pwm_ctl & BXT_BLC_PWM_ENABLE) &&
1621 panel->backlight.level != 0;
1622
1623 return 0;
1624}
1625
Shobhit Kumarb029e662015-06-26 14:32:10 +05301626static int pwm_setup_backlight(struct intel_connector *connector,
1627 enum pipe pipe)
1628{
1629 struct drm_device *dev = connector->base.dev;
1630 struct intel_panel *panel = &connector->panel;
1631 int retval;
1632
1633 /* Get the PWM chip for backlight control */
1634 panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1635 if (IS_ERR(panel->backlight.pwm)) {
1636 DRM_ERROR("Failed to own the pwm chip\n");
1637 panel->backlight.pwm = NULL;
1638 return -ENODEV;
1639 }
1640
1641 retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1642 CRC_PMIC_PWM_PERIOD_NS);
1643 if (retval < 0) {
1644 DRM_ERROR("Failed to configure the pwm chip\n");
1645 pwm_put(panel->backlight.pwm);
1646 panel->backlight.pwm = NULL;
1647 return retval;
1648 }
1649
1650 panel->backlight.min = 0; /* 0% */
1651 panel->backlight.max = 100; /* 100% */
1652 panel->backlight.level = DIV_ROUND_UP(
1653 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1654 CRC_PMIC_PWM_PERIOD_NS);
1655 panel->backlight.enabled = panel->backlight.level != 0;
1656
1657 return 0;
1658}
1659
Ville Syrjälä6517d272014-11-07 11:16:02 +02001660int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001661{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001662 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001663 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001664 struct intel_panel *panel = &intel_connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001665 int ret;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001666
Jani Nikulac6759492014-04-09 11:31:37 +03001667 if (!dev_priv->vbt.backlight.present) {
Scot Doyle9c72cc62014-07-03 23:27:50 +00001668 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1669 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1670 } else {
1671 DRM_DEBUG_KMS("no backlight present per VBT\n");
1672 return 0;
1673 }
Jani Nikulac6759492014-04-09 11:31:37 +03001674 }
1675
Jani Nikula5507fae2015-09-14 14:03:48 +03001676 /* ensure intel_panel has been initialized first */
1677 if (WARN_ON(!panel->backlight.setup))
1678 return -ENODEV;
1679
Jani Nikula7bd688c2013-11-08 16:48:56 +02001680 /* set level and max in panel struct */
Daniel Vetter07f11d42014-09-15 14:35:09 +02001681 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula5507fae2015-09-14 14:03:48 +03001682 ret = panel->backlight.setup(intel_connector, pipe);
Daniel Vetter07f11d42014-09-15 14:35:09 +02001683 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001684
Jani Nikula7bd688c2013-11-08 16:48:56 +02001685 if (ret) {
1686 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001687 connector->name);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001688 return ret;
1689 }
1690
Jani Nikulac91c9f32013-11-08 16:48:55 +02001691 panel->backlight.present = true;
1692
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001693 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1694 connector->name,
Jani Nikulac445b3b2013-11-08 16:49:01 +02001695 panel->backlight.enabled ? "enabled" : "disabled",
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001696 panel->backlight.level, panel->backlight.max);
Jani Nikulac445b3b2013-11-08 16:49:01 +02001697
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001698 return 0;
1699}
1700
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001701void intel_panel_destroy_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001702{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001703 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikulac91c9f32013-11-08 16:48:55 +02001704 struct intel_panel *panel = &intel_connector->panel;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001705
Shobhit Kumarb029e662015-06-26 14:32:10 +05301706 /* dispose of the pwm */
1707 if (panel->backlight.pwm)
1708 pwm_put(panel->backlight.pwm);
1709
Jani Nikulac91c9f32013-11-08 16:48:55 +02001710 panel->backlight.present = false;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001711}
Jani Nikula1d508702012-10-19 14:51:49 +03001712
Jani Nikula7bd688c2013-11-08 16:48:56 +02001713/* Set up chip specific backlight functions */
Jani Nikula5507fae2015-09-14 14:03:48 +03001714static void
1715intel_panel_init_backlight_funcs(struct intel_panel *panel)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001716{
Jani Nikulae6cb3722015-12-18 10:51:35 +02001717 struct intel_connector *connector =
Jani Nikula5507fae2015-09-14 14:03:48 +03001718 container_of(panel, struct intel_connector, panel);
Jani Nikulae6cb3722015-12-18 10:51:35 +02001719 struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001720
Jani Nikulae6cb3722015-12-18 10:51:35 +02001721 if (IS_BROXTON(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001722 panel->backlight.setup = bxt_setup_backlight;
1723 panel->backlight.enable = bxt_enable_backlight;
1724 panel->backlight.disable = bxt_disable_backlight;
1725 panel->backlight.set = bxt_set_backlight;
1726 panel->backlight.get = bxt_get_backlight;
Jani Nikula2dd69822015-12-01 10:23:50 +02001727 panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001728 } else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_SPT(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001729 panel->backlight.setup = lpt_setup_backlight;
1730 panel->backlight.enable = lpt_enable_backlight;
1731 panel->backlight.disable = lpt_disable_backlight;
1732 panel->backlight.set = lpt_set_backlight;
1733 panel->backlight.get = lpt_get_backlight;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001734 if (HAS_PCH_LPT(dev_priv))
Jani Nikula5507fae2015-09-14 14:03:48 +03001735 panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001736 else
Jani Nikula5507fae2015-09-14 14:03:48 +03001737 panel->backlight.hz_to_pwm = spt_hz_to_pwm;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001738 } else if (HAS_PCH_SPLIT(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001739 panel->backlight.setup = pch_setup_backlight;
1740 panel->backlight.enable = pch_enable_backlight;
1741 panel->backlight.disable = pch_disable_backlight;
1742 panel->backlight.set = pch_set_backlight;
1743 panel->backlight.get = pch_get_backlight;
1744 panel->backlight.hz_to_pwm = pch_hz_to_pwm;
Jani Nikulae6cb3722015-12-18 10:51:35 +02001745 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Jani Nikula92c45652016-03-16 12:43:33 +02001746 if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001747 panel->backlight.setup = pwm_setup_backlight;
1748 panel->backlight.enable = pwm_enable_backlight;
1749 panel->backlight.disable = pwm_disable_backlight;
1750 panel->backlight.set = pwm_set_backlight;
1751 panel->backlight.get = pwm_get_backlight;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301752 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001753 panel->backlight.setup = vlv_setup_backlight;
1754 panel->backlight.enable = vlv_enable_backlight;
1755 panel->backlight.disable = vlv_disable_backlight;
1756 panel->backlight.set = vlv_set_backlight;
1757 panel->backlight.get = vlv_get_backlight;
1758 panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301759 }
Jani Nikulae6cb3722015-12-18 10:51:35 +02001760 } else if (IS_GEN4(dev_priv)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001761 panel->backlight.setup = i965_setup_backlight;
1762 panel->backlight.enable = i965_enable_backlight;
1763 panel->backlight.disable = i965_disable_backlight;
1764 panel->backlight.set = i9xx_set_backlight;
1765 panel->backlight.get = i9xx_get_backlight;
1766 panel->backlight.hz_to_pwm = i965_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001767 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001768 panel->backlight.setup = i9xx_setup_backlight;
1769 panel->backlight.enable = i9xx_enable_backlight;
1770 panel->backlight.disable = i9xx_disable_backlight;
1771 panel->backlight.set = i9xx_set_backlight;
1772 panel->backlight.get = i9xx_get_backlight;
1773 panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001774 }
1775}
1776
Jani Nikuladd06f902012-10-19 14:51:50 +03001777int intel_panel_init(struct intel_panel *panel,
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301778 struct drm_display_mode *fixed_mode,
1779 struct drm_display_mode *downclock_mode)
Jani Nikula1d508702012-10-19 14:51:49 +03001780{
Jani Nikula5507fae2015-09-14 14:03:48 +03001781 intel_panel_init_backlight_funcs(panel);
1782
Jani Nikuladd06f902012-10-19 14:51:50 +03001783 panel->fixed_mode = fixed_mode;
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301784 panel->downclock_mode = downclock_mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03001785
Jani Nikula1d508702012-10-19 14:51:49 +03001786 return 0;
1787}
1788
1789void intel_panel_fini(struct intel_panel *panel)
1790{
Jani Nikuladd06f902012-10-19 14:51:50 +03001791 struct intel_connector *intel_connector =
1792 container_of(panel, struct intel_connector, panel);
1793
1794 if (panel->fixed_mode)
1795 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
Vandana Kannanec9ed192013-12-10 13:37:36 +05301796
1797 if (panel->downclock_mode)
1798 drm_mode_destroy(intel_connector->base.dev,
1799 panel->downclock_mode);
Jani Nikula1d508702012-10-19 14:51:49 +03001800}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001801
1802void intel_backlight_register(struct drm_device *dev)
1803{
1804 struct intel_connector *connector;
1805
Jani Nikula19c80542015-12-16 12:48:16 +02001806 for_each_intel_connector(dev, connector)
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001807 intel_backlight_device_register(connector);
1808}
1809
1810void intel_backlight_unregister(struct drm_device *dev)
1811{
1812 struct intel_connector *connector;
1813
Jani Nikula19c80542015-12-16 12:48:16 +02001814 for_each_intel_connector(dev, connector)
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001815 intel_backlight_device_unregister(connector);
1816}