blob: b05c6d9b3be7839bb3ee331cfa2786fbecbdc98b [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 Nikula7bd688c2013-11-08 16:48:56 +0200464 struct drm_device *dev = connector->base.dev;
Carsten Emde4dca20e2012-03-15 15:56:26 +0100465 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200466 struct intel_panel *panel = &connector->panel;
467
468 WARN_ON(panel->backlight.max == 0);
Carsten Emde4dca20e2012-03-15 15:56:26 +0100469
Jani Nikulad330a952014-01-21 11:24:25 +0200470 if (i915.invert_brightness < 0)
Carsten Emde4dca20e2012-03-15 15:56:26 +0100471 return val;
472
Jani Nikulad330a952014-01-21 11:24:25 +0200473 if (i915.invert_brightness > 0 ||
Jani Nikulad6540632013-04-12 15:18:36 +0300474 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200475 return panel->backlight.max - val;
Jani Nikulad6540632013-04-12 15:18:36 +0300476 }
Carsten Emde7bd90902012-03-15 15:56:25 +0100477
478 return val;
479}
480
Jani Nikula437b15b2015-09-04 16:55:13 +0300481static u32 lpt_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100482{
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100483 struct drm_device *dev = connector->base.dev;
484 struct drm_i915_private *dev_priv = dev->dev_private;
485
486 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
487}
488
Jani Nikula7bd688c2013-11-08 16:48:56 +0200489static u32 pch_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100490{
Jani Nikula7bd688c2013-11-08 16:48:56 +0200491 struct drm_device *dev = connector->base.dev;
492 struct drm_i915_private *dev_priv = dev->dev_private;
493
494 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
495}
496
497static u32 i9xx_get_backlight(struct intel_connector *connector)
498{
499 struct drm_device *dev = connector->base.dev;
500 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula636baeb2013-11-08 16:49:02 +0200501 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200502 u32 val;
503
504 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
505 if (INTEL_INFO(dev)->gen < 4)
506 val >>= 1;
507
Jani Nikula636baeb2013-11-08 16:49:02 +0200508 if (panel->backlight.combination_mode) {
Jani Nikula7bd688c2013-11-08 16:48:56 +0200509 u8 lbpc;
510
511 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
512 val *= lbpc;
513 }
514
515 return val;
516}
517
518static u32 _vlv_get_backlight(struct drm_device *dev, enum pipe pipe)
519{
520 struct drm_i915_private *dev_priv = dev->dev_private;
521
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200522 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
523 return 0;
524
Jani Nikula7bd688c2013-11-08 16:48:56 +0200525 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
526}
527
528static u32 vlv_get_backlight(struct intel_connector *connector)
529{
530 struct drm_device *dev = connector->base.dev;
531 enum pipe pipe = intel_get_pipe_from_connector(connector);
532
533 return _vlv_get_backlight(dev, pipe);
534}
535
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530536static u32 bxt_get_backlight(struct intel_connector *connector)
537{
538 struct drm_device *dev = connector->base.dev;
Sunil Kamath022e4e52015-09-30 22:34:57 +0530539 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530540 struct drm_i915_private *dev_priv = dev->dev_private;
541
Sunil Kamath022e4e52015-09-30 22:34:57 +0530542 return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530543}
544
Shobhit Kumarb029e662015-06-26 14:32:10 +0530545static u32 pwm_get_backlight(struct intel_connector *connector)
546{
547 struct intel_panel *panel = &connector->panel;
548 int duty_ns;
549
550 duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
551 return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
552}
553
Jani Nikula7bd688c2013-11-08 16:48:56 +0200554static u32 intel_panel_get_backlight(struct intel_connector *connector)
555{
556 struct drm_device *dev = connector->base.dev;
Chris Wilsona9573552010-08-22 13:18:16 +0100557 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200558 struct intel_panel *panel = &connector->panel;
559 u32 val = 0;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300560
Daniel Vetter07f11d42014-09-15 14:35:09 +0200561 mutex_lock(&dev_priv->backlight_lock);
Chris Wilsona9573552010-08-22 13:18:16 +0100562
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200563 if (panel->backlight.enabled) {
Jani Nikula5507fae2015-09-14 14:03:48 +0300564 val = panel->backlight.get(connector);
Ville Syrjälä2d72f6c2014-11-07 15:18:45 +0200565 val = intel_panel_compute_brightness(connector, val);
566 }
Jani Nikula8ba2d182013-04-12 15:18:37 +0300567
Daniel Vetter07f11d42014-09-15 14:35:09 +0200568 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300569
Chris Wilsona9573552010-08-22 13:18:16 +0100570 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
571 return val;
572}
573
Jani Nikula437b15b2015-09-04 16:55:13 +0300574static void lpt_set_backlight(struct intel_connector *connector, u32 level)
Ben Widawskyf8e10062013-11-11 11:12:57 +0200575{
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100576 struct drm_device *dev = connector->base.dev;
Ben Widawskyf8e10062013-11-11 11:12:57 +0200577 struct drm_i915_private *dev_priv = dev->dev_private;
578 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
579 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
580}
581
Jani Nikula7bd688c2013-11-08 16:48:56 +0200582static void pch_set_backlight(struct intel_connector *connector, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100583{
Jani Nikula7bd688c2013-11-08 16:48:56 +0200584 struct drm_device *dev = connector->base.dev;
Chris Wilsona9573552010-08-22 13:18:16 +0100585 struct drm_i915_private *dev_priv = dev->dev_private;
586 u32 tmp;
587
Jani Nikula7bd688c2013-11-08 16:48:56 +0200588 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
589 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
590}
Chris Wilsona9573552010-08-22 13:18:16 +0100591
Jani Nikula7bd688c2013-11-08 16:48:56 +0200592static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
593{
594 struct drm_device *dev = connector->base.dev;
595 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200596 struct intel_panel *panel = &connector->panel;
Jani Nikulab329b322013-11-08 16:48:57 +0200597 u32 tmp, mask;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100598
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200599 WARN_ON(panel->backlight.max == 0);
600
Jani Nikula636baeb2013-11-08 16:49:02 +0200601 if (panel->backlight.combination_mode) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100602 u8 lbpc;
603
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200604 lbpc = level * 0xfe / panel->backlight.max + 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100605 level /= lbpc;
606 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
607 }
608
Jani Nikulab329b322013-11-08 16:48:57 +0200609 if (IS_GEN4(dev)) {
610 mask = BACKLIGHT_DUTY_CYCLE_MASK;
611 } else {
Chris Wilsona9573552010-08-22 13:18:16 +0100612 level <<= 1;
Jani Nikulab329b322013-11-08 16:48:57 +0200613 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
614 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200615
Jani Nikulab329b322013-11-08 16:48:57 +0200616 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200617 I915_WRITE(BLC_PWM_CTL, tmp | level);
618}
619
620static void vlv_set_backlight(struct intel_connector *connector, u32 level)
621{
622 struct drm_device *dev = connector->base.dev;
623 struct drm_i915_private *dev_priv = dev->dev_private;
624 enum pipe pipe = intel_get_pipe_from_connector(connector);
625 u32 tmp;
626
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200627 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
628 return;
629
Jani Nikula7bd688c2013-11-08 16:48:56 +0200630 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
631 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
632}
633
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530634static void bxt_set_backlight(struct intel_connector *connector, u32 level)
635{
636 struct drm_device *dev = connector->base.dev;
637 struct drm_i915_private *dev_priv = dev->dev_private;
Sunil Kamath022e4e52015-09-30 22:34:57 +0530638 struct intel_panel *panel = &connector->panel;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530639
Sunil Kamath022e4e52015-09-30 22:34:57 +0530640 I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530641}
642
Shobhit Kumarb029e662015-06-26 14:32:10 +0530643static void pwm_set_backlight(struct intel_connector *connector, u32 level)
644{
645 struct intel_panel *panel = &connector->panel;
646 int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
647
648 pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
649}
650
Jani Nikula7bd688c2013-11-08 16:48:56 +0200651static void
652intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
653{
Jani Nikula5507fae2015-09-14 14:03:48 +0300654 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200655
656 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
657
658 level = intel_panel_compute_brightness(connector, level);
Jani Nikula5507fae2015-09-14 14:03:48 +0300659 panel->backlight.set(connector, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100660}
Chris Wilson47356eb2011-01-11 17:06:04 +0000661
Jani Nikula6dda7302014-06-24 18:27:40 +0300662/* set backlight brightness to level in range [0..max], scaling wrt hw min */
663static void intel_panel_set_backlight(struct intel_connector *connector,
664 u32 user_level, u32 user_max)
Takashi Iwaif52c6192011-10-14 11:45:40 +0200665{
Jesse Barnes752aa882013-10-31 18:55:49 +0200666 struct drm_device *dev = connector->base.dev;
Takashi Iwaif52c6192011-10-14 11:45:40 +0200667 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +0200668 struct intel_panel *panel = &connector->panel;
Jani Nikula6dda7302014-06-24 18:27:40 +0300669 u32 hw_level;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300670
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200671 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +0200672 return;
673
Daniel Vetter07f11d42014-09-15 14:35:09 +0200674 mutex_lock(&dev_priv->backlight_lock);
Jani Nikulad6540632013-04-12 15:18:36 +0300675
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200676 WARN_ON(panel->backlight.max == 0);
Jani Nikulad6540632013-04-12 15:18:36 +0300677
Jani Nikula6dda7302014-06-24 18:27:40 +0300678 hw_level = scale_user_to_hw(connector, user_level, user_max);
679 panel->backlight.level = hw_level;
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200680
Jani Nikula58c68772013-11-08 16:48:54 +0200681 if (panel->backlight.enabled)
Jani Nikula6dda7302014-06-24 18:27:40 +0300682 intel_panel_actually_set_backlight(connector, hw_level);
683
Daniel Vetter07f11d42014-09-15 14:35:09 +0200684 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikula6dda7302014-06-24 18:27:40 +0300685}
686
687/* set backlight brightness to level in range [0..max], assuming hw min is
688 * respected.
689 */
690void intel_panel_set_backlight_acpi(struct intel_connector *connector,
691 u32 user_level, u32 user_max)
692{
693 struct drm_device *dev = connector->base.dev;
694 struct drm_i915_private *dev_priv = dev->dev_private;
695 struct intel_panel *panel = &connector->panel;
696 enum pipe pipe = intel_get_pipe_from_connector(connector);
697 u32 hw_level;
Jani Nikula6dda7302014-06-24 18:27:40 +0300698
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200699 /*
700 * INVALID_PIPE may occur during driver init because
701 * connection_mutex isn't held across the entire backlight
702 * setup + modeset readout, and the BIOS can issue the
703 * requests at any time.
704 */
Jani Nikula6dda7302014-06-24 18:27:40 +0300705 if (!panel->backlight.present || pipe == INVALID_PIPE)
706 return;
707
Daniel Vetter07f11d42014-09-15 14:35:09 +0200708 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula6dda7302014-06-24 18:27:40 +0300709
710 WARN_ON(panel->backlight.max == 0);
711
712 hw_level = clamp_user_to_hw(connector, user_level, user_max);
713 panel->backlight.level = hw_level;
714
715 if (panel->backlight.device)
716 panel->backlight.device->props.brightness =
717 scale_hw_to_user(connector,
718 panel->backlight.level,
719 panel->backlight.device->props.max_brightness);
720
721 if (panel->backlight.enabled)
722 intel_panel_actually_set_backlight(connector, hw_level);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200723
Daniel Vetter07f11d42014-09-15 14:35:09 +0200724 mutex_unlock(&dev_priv->backlight_lock);
Takashi Iwaif52c6192011-10-14 11:45:40 +0200725}
726
Jani Nikula437b15b2015-09-04 16:55:13 +0300727static void lpt_disable_backlight(struct intel_connector *connector)
728{
729 struct drm_device *dev = connector->base.dev;
730 struct drm_i915_private *dev_priv = dev->dev_private;
731 u32 tmp;
732
733 intel_panel_actually_set_backlight(connector, 0);
734
735 tmp = I915_READ(BLC_PWM_PCH_CTL1);
736 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
737}
738
Jani Nikula7bd688c2013-11-08 16:48:56 +0200739static void pch_disable_backlight(struct intel_connector *connector)
740{
741 struct drm_device *dev = connector->base.dev;
742 struct drm_i915_private *dev_priv = dev->dev_private;
743 u32 tmp;
744
Jani Nikula3bd712e2013-11-08 16:48:59 +0200745 intel_panel_actually_set_backlight(connector, 0);
746
Jani Nikula7bd688c2013-11-08 16:48:56 +0200747 tmp = I915_READ(BLC_PWM_CPU_CTL2);
748 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
749
750 tmp = I915_READ(BLC_PWM_PCH_CTL1);
751 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
752}
753
Jani Nikula3bd712e2013-11-08 16:48:59 +0200754static void i9xx_disable_backlight(struct intel_connector *connector)
755{
756 intel_panel_actually_set_backlight(connector, 0);
757}
758
Jani Nikula7bd688c2013-11-08 16:48:56 +0200759static void i965_disable_backlight(struct intel_connector *connector)
760{
761 struct drm_device *dev = connector->base.dev;
762 struct drm_i915_private *dev_priv = dev->dev_private;
763 u32 tmp;
764
Jani Nikula3bd712e2013-11-08 16:48:59 +0200765 intel_panel_actually_set_backlight(connector, 0);
766
Jani Nikula7bd688c2013-11-08 16:48:56 +0200767 tmp = I915_READ(BLC_PWM_CTL2);
768 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
769}
770
771static void vlv_disable_backlight(struct intel_connector *connector)
772{
773 struct drm_device *dev = connector->base.dev;
774 struct drm_i915_private *dev_priv = dev->dev_private;
775 enum pipe pipe = intel_get_pipe_from_connector(connector);
776 u32 tmp;
777
Ville Syrjälä23ec0a82014-11-07 11:15:59 +0200778 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
779 return;
780
Jani Nikula3bd712e2013-11-08 16:48:59 +0200781 intel_panel_actually_set_backlight(connector, 0);
782
Jani Nikula7bd688c2013-11-08 16:48:56 +0200783 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
784 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
785}
786
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530787static void bxt_disable_backlight(struct intel_connector *connector)
788{
789 struct drm_device *dev = connector->base.dev;
790 struct drm_i915_private *dev_priv = dev->dev_private;
Sunil Kamath022e4e52015-09-30 22:34:57 +0530791 struct intel_panel *panel = &connector->panel;
792 u32 tmp, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530793
794 intel_panel_actually_set_backlight(connector, 0);
795
Sunil Kamath022e4e52015-09-30 22:34:57 +0530796 tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
797 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
798 tmp & ~BXT_BLC_PWM_ENABLE);
799
800 if (panel->backlight.controller == 1) {
801 val = I915_READ(UTIL_PIN_CTL);
802 val &= ~UTIL_PIN_ENABLE;
803 I915_WRITE(UTIL_PIN_CTL, val);
804 }
Vandana Kannan0fb890c2015-05-05 14:51:56 +0530805}
806
Shobhit Kumarb029e662015-06-26 14:32:10 +0530807static void pwm_disable_backlight(struct intel_connector *connector)
808{
809 struct intel_panel *panel = &connector->panel;
810
811 /* Disable the backlight */
812 pwm_config(panel->backlight.pwm, 0, CRC_PMIC_PWM_PERIOD_NS);
813 usleep_range(2000, 3000);
814 pwm_disable(panel->backlight.pwm);
815}
816
Jesse Barnes752aa882013-10-31 18:55:49 +0200817void intel_panel_disable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +0000818{
Jesse Barnes752aa882013-10-31 18:55:49 +0200819 struct drm_device *dev = connector->base.dev;
Chris Wilson47356eb2011-01-11 17:06:04 +0000820 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +0200821 struct intel_panel *panel = &connector->panel;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300822
Ville Syrjälä260d8f92014-11-07 15:20:23 +0200823 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +0200824 return;
825
Jani Nikula3f577572013-07-25 14:31:30 +0300826 /*
Lukas Wunner5389e912015-09-04 21:06:15 +0200827 * Do not disable backlight on the vga_switcheroo path. When switching
Jani Nikula3f577572013-07-25 14:31:30 +0300828 * away from i915, the other client may depend on i915 to handle the
829 * backlight. This will leave the backlight on unnecessarily when
830 * another client is not activated.
831 */
832 if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
833 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
834 return;
835 }
836
Daniel Vetter07f11d42014-09-15 14:35:09 +0200837 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000838
Jani Nikulaab656bb2014-08-13 12:10:12 +0300839 if (panel->backlight.device)
840 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
Jani Nikula58c68772013-11-08 16:48:54 +0200841 panel->backlight.enabled = false;
Jani Nikula5507fae2015-09-14 14:03:48 +0300842 panel->backlight.disable(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300843
Daniel Vetter07f11d42014-09-15 14:35:09 +0200844 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +0000845}
846
Jani Nikula437b15b2015-09-04 16:55:13 +0300847static void lpt_enable_backlight(struct intel_connector *connector)
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100848{
849 struct drm_device *dev = connector->base.dev;
850 struct drm_i915_private *dev_priv = dev->dev_private;
851 struct intel_panel *panel = &connector->panel;
852 u32 pch_ctl1, pch_ctl2;
Chris Wilson47356eb2011-01-11 17:06:04 +0000853
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100854 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
855 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
856 DRM_DEBUG_KMS("pch backlight already enabled\n");
857 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
858 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
Chris Wilson47356eb2011-01-11 17:06:04 +0000859 }
860
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100861 pch_ctl2 = panel->backlight.max << 16;
862 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
863
864 pch_ctl1 = 0;
865 if (panel->backlight.active_low_pwm)
866 pch_ctl1 |= BLM_PCH_POLARITY;
867
Jani Nikulae6b26272014-10-10 17:53:33 +0300868 /* After LPT, override is the default. */
869 if (HAS_PCH_LPT(dev_priv))
870 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100871
872 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
873 POSTING_READ(BLC_PWM_PCH_CTL1);
874 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
875
876 /* This won't stick until the above enable. */
877 intel_panel_actually_set_backlight(connector, panel->backlight.level);
878}
879
Jani Nikula7bd688c2013-11-08 16:48:56 +0200880static void pch_enable_backlight(struct intel_connector *connector)
881{
882 struct drm_device *dev = connector->base.dev;
883 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200884 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200885 enum pipe pipe = intel_get_pipe_from_connector(connector);
886 enum transcoder cpu_transcoder =
887 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
Jani Nikulab35684b2013-11-14 12:13:41 +0200888 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200889
Jani Nikulab35684b2013-11-14 12:13:41 +0200890 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
891 if (cpu_ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000892 DRM_DEBUG_KMS("cpu backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200893 cpu_ctl2 &= ~BLM_PWM_ENABLE;
894 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200895 }
Jani Nikula3bd712e2013-11-08 16:48:59 +0200896
Jani Nikulab35684b2013-11-14 12:13:41 +0200897 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
898 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
899 DRM_DEBUG_KMS("pch backlight already enabled\n");
900 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
901 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
902 }
903
904 if (cpu_transcoder == TRANSCODER_EDP)
905 cpu_ctl2 = BLM_TRANSCODER_EDP;
906 else
907 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
908 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
909 POSTING_READ(BLC_PWM_CPU_CTL2);
910 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
911
912 /* This won't stick until the above enable. */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200913 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +0200914
915 pch_ctl2 = panel->backlight.max << 16;
916 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
917
Jani Nikulab35684b2013-11-14 12:13:41 +0200918 pch_ctl1 = 0;
919 if (panel->backlight.active_low_pwm)
920 pch_ctl1 |= BLM_PCH_POLARITY;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100921
Jani Nikulab35684b2013-11-14 12:13:41 +0200922 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
923 POSTING_READ(BLC_PWM_PCH_CTL1);
924 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200925}
926
927static void i9xx_enable_backlight(struct intel_connector *connector)
928{
Jani Nikulab35684b2013-11-14 12:13:41 +0200929 struct drm_device *dev = connector->base.dev;
930 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200931 struct intel_panel *panel = &connector->panel;
Jani Nikulab35684b2013-11-14 12:13:41 +0200932 u32 ctl, freq;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200933
Jani Nikulab35684b2013-11-14 12:13:41 +0200934 ctl = I915_READ(BLC_PWM_CTL);
935 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
Scot Doyle813008c2014-08-19 02:07:13 +0000936 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200937 I915_WRITE(BLC_PWM_CTL, 0);
938 }
939
940 freq = panel->backlight.max;
941 if (panel->backlight.combination_mode)
942 freq /= 0xff;
943
944 ctl = freq << 17;
Jani Nikulab6ab66a2014-02-25 13:11:47 +0200945 if (panel->backlight.combination_mode)
Jani Nikulab35684b2013-11-14 12:13:41 +0200946 ctl |= BLM_LEGACY_MODE;
947 if (IS_PINEVIEW(dev) && panel->backlight.active_low_pwm)
948 ctl |= BLM_POLARITY_PNV;
949
950 I915_WRITE(BLC_PWM_CTL, ctl);
951 POSTING_READ(BLC_PWM_CTL);
952
953 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200954 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula2059ac32015-06-26 14:18:56 +0300955
956 /*
957 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
958 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
959 * that has backlight.
960 */
961 if (IS_GEN2(dev))
962 I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200963}
964
965static void i965_enable_backlight(struct intel_connector *connector)
966{
967 struct drm_device *dev = connector->base.dev;
968 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200969 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200970 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +0200971 u32 ctl, ctl2, freq;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200972
Jani Nikulab35684b2013-11-14 12:13:41 +0200973 ctl2 = I915_READ(BLC_PWM_CTL2);
974 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000975 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200976 ctl2 &= ~BLM_PWM_ENABLE;
977 I915_WRITE(BLC_PWM_CTL2, ctl2);
978 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200979
Jani Nikulab35684b2013-11-14 12:13:41 +0200980 freq = panel->backlight.max;
981 if (panel->backlight.combination_mode)
982 freq /= 0xff;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200983
Jani Nikulab35684b2013-11-14 12:13:41 +0200984 ctl = freq << 16;
985 I915_WRITE(BLC_PWM_CTL, ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200986
Jani Nikulab35684b2013-11-14 12:13:41 +0200987 ctl2 = BLM_PIPE(pipe);
988 if (panel->backlight.combination_mode)
989 ctl2 |= BLM_COMBINATION_MODE;
990 if (panel->backlight.active_low_pwm)
991 ctl2 |= BLM_POLARITY_I965;
992 I915_WRITE(BLC_PWM_CTL2, ctl2);
993 POSTING_READ(BLC_PWM_CTL2);
994 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
Jani Nikula2e7eeeb2014-06-09 18:24:34 +0300995
996 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200997}
998
999static void vlv_enable_backlight(struct intel_connector *connector)
1000{
1001 struct drm_device *dev = connector->base.dev;
1002 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +02001003 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001004 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +02001005 u32 ctl, ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001006
Ville Syrjälä23ec0a82014-11-07 11:15:59 +02001007 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1008 return;
1009
Jani Nikulab35684b2013-11-14 12:13:41 +02001010 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
1011 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +00001012 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +02001013 ctl2 &= ~BLM_PWM_ENABLE;
1014 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1015 }
Jani Nikula7bd688c2013-11-08 16:48:56 +02001016
Jani Nikulab35684b2013-11-14 12:13:41 +02001017 ctl = panel->backlight.max << 16;
1018 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001019
Jani Nikulab35684b2013-11-14 12:13:41 +02001020 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +02001021 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +02001022
1023 ctl2 = 0;
1024 if (panel->backlight.active_low_pwm)
1025 ctl2 |= BLM_POLARITY_I965;
1026 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
1027 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
1028 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001029}
1030
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301031static void bxt_enable_backlight(struct intel_connector *connector)
1032{
1033 struct drm_device *dev = connector->base.dev;
1034 struct drm_i915_private *dev_priv = dev->dev_private;
1035 struct intel_panel *panel = &connector->panel;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301036 enum pipe pipe = intel_get_pipe_from_connector(connector);
1037 u32 pwm_ctl, val;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301038
Sunil Kamath022e4e52015-09-30 22:34:57 +05301039 /* To use 2nd set of backlight registers, utility pin has to be
1040 * enabled with PWM mode.
1041 * The field should only be changed when the utility pin is disabled
1042 */
1043 if (panel->backlight.controller == 1) {
1044 val = I915_READ(UTIL_PIN_CTL);
1045 if (val & UTIL_PIN_ENABLE) {
1046 DRM_DEBUG_KMS("util pin already enabled\n");
1047 val &= ~UTIL_PIN_ENABLE;
1048 I915_WRITE(UTIL_PIN_CTL, val);
1049 }
1050
1051 val = 0;
1052 if (panel->backlight.util_pin_active_low)
1053 val |= UTIL_PIN_POLARITY;
1054 I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
1055 UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1056 }
1057
1058 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301059 if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1060 DRM_DEBUG_KMS("backlight already enabled\n");
1061 pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
Sunil Kamath022e4e52015-09-30 22:34:57 +05301062 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1063 pwm_ctl);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301064 }
1065
Sunil Kamath022e4e52015-09-30 22:34:57 +05301066 I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
1067 panel->backlight.max);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301068
1069 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1070
1071 pwm_ctl = 0;
1072 if (panel->backlight.active_low_pwm)
1073 pwm_ctl |= BXT_BLC_PWM_POLARITY;
1074
Sunil Kamath022e4e52015-09-30 22:34:57 +05301075 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
1076 POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1077 I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
1078 pwm_ctl | BXT_BLC_PWM_ENABLE);
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301079}
1080
Shobhit Kumarb029e662015-06-26 14:32:10 +05301081static void pwm_enable_backlight(struct intel_connector *connector)
1082{
1083 struct intel_panel *panel = &connector->panel;
1084
1085 pwm_enable(panel->backlight.pwm);
1086 intel_panel_actually_set_backlight(connector, panel->backlight.level);
1087}
1088
Jesse Barnes752aa882013-10-31 18:55:49 +02001089void intel_panel_enable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +00001090{
Jesse Barnes752aa882013-10-31 18:55:49 +02001091 struct drm_device *dev = connector->base.dev;
Chris Wilson47356eb2011-01-11 17:06:04 +00001092 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +02001093 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +02001094 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +03001095
Ville Syrjälä260d8f92014-11-07 15:20:23 +02001096 if (!panel->backlight.present)
Jesse Barnes752aa882013-10-31 18:55:49 +02001097 return;
1098
Damien Lespiau6f2bcce2013-10-16 12:29:54 +01001099 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
Chris Wilson540b5d02013-10-13 12:56:31 +01001100
Daniel Vetter07f11d42014-09-15 14:35:09 +02001101 mutex_lock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001102
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001103 WARN_ON(panel->backlight.max == 0);
1104
Jeremiah Mahler13f3fbe2015-01-12 11:01:03 -08001105 if (panel->backlight.level <= panel->backlight.min) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001106 panel->backlight.level = panel->backlight.max;
Jani Nikula58c68772013-11-08 16:48:54 +02001107 if (panel->backlight.device)
1108 panel->backlight.device->props.brightness =
Jani Nikula6dda7302014-06-24 18:27:40 +03001109 scale_hw_to_user(connector,
1110 panel->backlight.level,
1111 panel->backlight.device->props.max_brightness);
Jani Nikulab6b3ba52013-03-12 11:44:15 +02001112 }
Chris Wilson47356eb2011-01-11 17:06:04 +00001113
Jani Nikula5507fae2015-09-14 14:03:48 +03001114 panel->backlight.enable(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001115 panel->backlight.enabled = true;
Jani Nikulaab656bb2014-08-13 12:10:12 +03001116 if (panel->backlight.device)
1117 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
Jani Nikula8ba2d182013-04-12 15:18:37 +03001118
Daniel Vetter07f11d42014-09-15 14:35:09 +02001119 mutex_unlock(&dev_priv->backlight_lock);
Chris Wilson47356eb2011-01-11 17:06:04 +00001120}
1121
Jani Nikula912e8b12013-09-18 17:19:45 +03001122#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001123static int intel_backlight_device_update_status(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001124{
Jesse Barnes752aa882013-10-31 18:55:49 +02001125 struct intel_connector *connector = bl_get_data(bd);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001126 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +02001127 struct drm_device *dev = connector->base.dev;
1128
Rob Clark51fd3712013-11-19 12:10:12 -05001129 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Chris Wilson540b5d02013-10-13 12:56:31 +01001130 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
1131 bd->props.brightness, bd->props.max_brightness);
Jesse Barnes752aa882013-10-31 18:55:49 +02001132 intel_panel_set_backlight(connector, bd->props.brightness,
Jani Nikulad6540632013-04-12 15:18:36 +03001133 bd->props.max_brightness);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001134
1135 /*
1136 * Allow flipping bl_power as a sub-state of enabled. Sadly the
1137 * backlight class device does not make it easy to to differentiate
1138 * between callbacks for brightness and bl_power, so our backlight_power
1139 * callback needs to take this into account.
1140 */
1141 if (panel->backlight.enabled) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001142 if (panel->backlight.power) {
Jani Nikulae6755fb2014-08-12 17:11:42 +03001143 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
1144 bd->props.brightness != 0;
Jani Nikula5507fae2015-09-14 14:03:48 +03001145 panel->backlight.power(connector, enable);
Jani Nikulaab656bb2014-08-13 12:10:12 +03001146 }
1147 } else {
1148 bd->props.power = FB_BLANK_POWERDOWN;
1149 }
1150
Rob Clark51fd3712013-11-19 12:10:12 -05001151 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001152 return 0;
1153}
1154
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001155static int intel_backlight_device_get_brightness(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001156{
Jesse Barnes752aa882013-10-31 18:55:49 +02001157 struct intel_connector *connector = bl_get_data(bd);
1158 struct drm_device *dev = connector->base.dev;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001159 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula6dda7302014-06-24 18:27:40 +03001160 u32 hw_level;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001161 int ret;
Jesse Barnes752aa882013-10-31 18:55:49 +02001162
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001163 intel_runtime_pm_get(dev_priv);
Rob Clark51fd3712013-11-19 12:10:12 -05001164 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Jani Nikula6dda7302014-06-24 18:27:40 +03001165
1166 hw_level = intel_panel_get_backlight(connector);
1167 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1168
Rob Clark51fd3712013-11-19 12:10:12 -05001169 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001170 intel_runtime_pm_put(dev_priv);
Jesse Barnes752aa882013-10-31 18:55:49 +02001171
Jani Nikula7bd688c2013-11-08 16:48:56 +02001172 return ret;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001173}
1174
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001175static const struct backlight_ops intel_backlight_device_ops = {
1176 .update_status = intel_backlight_device_update_status,
1177 .get_brightness = intel_backlight_device_get_brightness,
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001178};
1179
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001180static int intel_backlight_device_register(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001181{
Jani Nikula58c68772013-11-08 16:48:54 +02001182 struct intel_panel *panel = &connector->panel;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001183 struct backlight_properties props;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001184
Jani Nikula58c68772013-11-08 16:48:54 +02001185 if (WARN_ON(panel->backlight.device))
Jani Nikuladc652f92013-04-12 15:18:38 +03001186 return -ENODEV;
1187
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001188 if (!panel->backlight.present)
1189 return 0;
1190
Jani Nikula6dda7302014-06-24 18:27:40 +03001191 WARN_ON(panel->backlight.max == 0);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001192
Corentin Charyaf437cf2012-05-22 10:29:46 +01001193 memset(&props, 0, sizeof(props));
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001194 props.type = BACKLIGHT_RAW;
Jani Nikula6dda7302014-06-24 18:27:40 +03001195
1196 /*
1197 * Note: Everything should work even if the backlight device max
1198 * presented to the userspace is arbitrarily chosen.
1199 */
Jani Nikula7bd688c2013-11-08 16:48:56 +02001200 props.max_brightness = panel->backlight.max;
Jani Nikula6dda7302014-06-24 18:27:40 +03001201 props.brightness = scale_hw_to_user(connector,
1202 panel->backlight.level,
1203 props.max_brightness);
Jani Nikula58c68772013-11-08 16:48:54 +02001204
Jani Nikulaab656bb2014-08-13 12:10:12 +03001205 if (panel->backlight.enabled)
1206 props.power = FB_BLANK_UNBLANK;
1207 else
1208 props.power = FB_BLANK_POWERDOWN;
1209
Jani Nikula58c68772013-11-08 16:48:54 +02001210 /*
1211 * Note: using the same name independent of the connector prevents
1212 * registration of multiple backlight devices in the driver.
1213 */
1214 panel->backlight.device =
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001215 backlight_device_register("intel_backlight",
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001216 connector->base.kdev,
1217 connector,
1218 &intel_backlight_device_ops, &props);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001219
Jani Nikula58c68772013-11-08 16:48:54 +02001220 if (IS_ERR(panel->backlight.device)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001221 DRM_ERROR("Failed to register backlight: %ld\n",
Jani Nikula58c68772013-11-08 16:48:54 +02001222 PTR_ERR(panel->backlight.device));
1223 panel->backlight.device = NULL;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001224 return -ENODEV;
1225 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001226
1227 DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
1228 connector->base.name);
1229
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001230 return 0;
1231}
1232
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001233static void intel_backlight_device_unregister(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001234{
Jani Nikula58c68772013-11-08 16:48:54 +02001235 struct intel_panel *panel = &connector->panel;
1236
1237 if (panel->backlight.device) {
1238 backlight_device_unregister(panel->backlight.device);
1239 panel->backlight.device = NULL;
Jani Nikuladc652f92013-04-12 15:18:38 +03001240 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001241}
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001242#else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1243static int intel_backlight_device_register(struct intel_connector *connector)
1244{
1245 return 0;
1246}
1247static void intel_backlight_device_unregister(struct intel_connector *connector)
1248{
1249}
1250#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1251
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001252/*
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001253 * SPT: This value represents the period of the PWM stream in clock periods
1254 * multiplied by 16 (default increment) or 128 (alternate increment selected in
1255 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
1256 */
1257static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1258{
1259 struct drm_device *dev = connector->base.dev;
1260 struct drm_i915_private *dev_priv = dev->dev_private;
1261 u32 mul, clock;
1262
1263 if (I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY)
1264 mul = 128;
1265 else
1266 mul = 16;
1267
1268 clock = MHz(24);
1269
1270 return clock / (pwm_freq_hz * mul);
1271}
1272
1273/*
1274 * LPT: This value represents the period of the PWM stream in clock periods
1275 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
1276 * LPT SOUTH_CHICKEN2 register bit 5).
1277 */
1278static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1279{
1280 struct drm_device *dev = connector->base.dev;
1281 struct drm_i915_private *dev_priv = dev->dev_private;
1282 u32 mul, clock;
1283
1284 if (I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY)
1285 mul = 16;
1286 else
1287 mul = 128;
1288
1289 if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE)
1290 clock = MHz(135); /* LPT:H */
1291 else
1292 clock = MHz(24); /* LPT:LP */
1293
1294 return clock / (pwm_freq_hz * mul);
1295}
1296
1297/*
1298 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
1299 * display raw clocks multiplied by 128.
1300 */
1301static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1302{
1303 struct drm_device *dev = connector->base.dev;
1304 int clock = MHz(intel_pch_rawclk(dev));
1305
1306 return clock / (pwm_freq_hz * 128);
1307}
1308
1309/*
1310 * Gen2: This field determines the number of time base events (display core
1311 * clock frequency/32) in total for a complete cycle of modulated backlight
1312 * control.
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001313 *
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001314 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
1315 * divided by 32.
1316 */
1317static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1318{
1319 struct drm_device *dev = connector->base.dev;
1320 struct drm_i915_private *dev_priv = dev->dev_private;
1321 int clock;
1322
1323 if (IS_PINEVIEW(dev))
1324 clock = intel_hrawclk(dev);
1325 else
1326 clock = 1000 * dev_priv->display.get_display_clock_speed(dev);
1327
1328 return clock / (pwm_freq_hz * 32);
1329}
1330
1331/*
1332 * Gen4: This value represents the period of the PWM stream in display core
1333 * clocks multiplied by 128.
1334 */
1335static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1336{
1337 struct drm_device *dev = connector->base.dev;
1338 struct drm_i915_private *dev_priv = dev->dev_private;
1339 int clock = 1000 * dev_priv->display.get_display_clock_speed(dev);
1340
1341 return clock / (pwm_freq_hz * 128);
1342}
1343
1344/*
1345 * VLV: This value represents the period of the PWM stream in display core
1346 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
1347 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
1348 */
1349static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
1350{
1351 struct drm_device *dev = connector->base.dev;
1352 struct drm_i915_private *dev_priv = dev->dev_private;
1353 int clock;
1354
1355 if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1356 if (IS_CHERRYVIEW(dev))
1357 return KHz(19200) / (pwm_freq_hz * 16);
1358 else
1359 return MHz(25) / (pwm_freq_hz * 16);
1360 } else {
1361 clock = intel_hrawclk(dev);
1362 return MHz(clock) / (pwm_freq_hz * 128);
1363 }
1364}
1365
1366static u32 get_backlight_max_vbt(struct intel_connector *connector)
1367{
1368 struct drm_device *dev = connector->base.dev;
1369 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula5507fae2015-09-14 14:03:48 +03001370 struct intel_panel *panel = &connector->panel;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001371 u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
1372 u32 pwm;
1373
1374 if (!pwm_freq_hz) {
1375 DRM_DEBUG_KMS("backlight frequency not specified in VBT\n");
1376 return 0;
1377 }
1378
Jani Nikula5507fae2015-09-14 14:03:48 +03001379 if (!panel->backlight.hz_to_pwm) {
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001380 DRM_DEBUG_KMS("backlight frequency setting from VBT currently not supported on this platform\n");
1381 return 0;
1382 }
1383
Jani Nikula5507fae2015-09-14 14:03:48 +03001384 pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001385 if (!pwm) {
1386 DRM_DEBUG_KMS("backlight frequency conversion failed\n");
1387 return 0;
1388 }
1389
1390 DRM_DEBUG_KMS("backlight frequency %u Hz from VBT\n", pwm_freq_hz);
1391
1392 return pwm;
1393}
1394
1395/*
1396 * Note: The setup hooks can't assume pipe is set!
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001397 */
Jani Nikula6dda7302014-06-24 18:27:40 +03001398static u32 get_backlight_min_vbt(struct intel_connector *connector)
1399{
1400 struct drm_device *dev = connector->base.dev;
1401 struct drm_i915_private *dev_priv = dev->dev_private;
1402 struct intel_panel *panel = &connector->panel;
Jani Nikulae1c412e2014-11-05 14:46:31 +02001403 int min;
Jani Nikula6dda7302014-06-24 18:27:40 +03001404
1405 WARN_ON(panel->backlight.max == 0);
1406
Jani Nikulae1c412e2014-11-05 14:46:31 +02001407 /*
1408 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1409 * to problems. There are such machines out there. Either our
1410 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1411 * against this by letting the minimum be at most (arbitrarily chosen)
1412 * 25% of the max.
1413 */
1414 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1415 if (min != dev_priv->vbt.backlight.min_brightness) {
1416 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1417 dev_priv->vbt.backlight.min_brightness, min);
1418 }
1419
Jani Nikula6dda7302014-06-24 18:27:40 +03001420 /* vbt value is a coefficient in range [0..255] */
Jani Nikulae1c412e2014-11-05 14:46:31 +02001421 return scale(min, 0, 255, 0, panel->backlight.max);
Jani Nikula6dda7302014-06-24 18:27:40 +03001422}
1423
Jani Nikula437b15b2015-09-04 16:55:13 +03001424static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001425{
1426 struct drm_device *dev = connector->base.dev;
1427 struct drm_i915_private *dev_priv = dev->dev_private;
1428 struct intel_panel *panel = &connector->panel;
1429 u32 pch_ctl1, pch_ctl2, val;
1430
1431 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1432 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1433
1434 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1435 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001436
1437 if (!panel->backlight.max)
1438 panel->backlight.max = get_backlight_max_vbt(connector);
1439
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001440 if (!panel->backlight.max)
1441 return -ENODEV;
1442
Jani Nikula6dda7302014-06-24 18:27:40 +03001443 panel->backlight.min = get_backlight_min_vbt(connector);
1444
Jani Nikula437b15b2015-09-04 16:55:13 +03001445 val = lpt_get_backlight(connector);
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001446 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1447
1448 panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
1449 panel->backlight.level != 0;
1450
1451 return 0;
1452}
1453
Ville Syrjälä6517d272014-11-07 11:16:02 +02001454static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001455{
Jani Nikula636baeb2013-11-08 16:49:02 +02001456 struct drm_device *dev = connector->base.dev;
1457 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001458 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001459 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001460
Jani Nikula636baeb2013-11-08 16:49:02 +02001461 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1462 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1463
1464 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1465 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001466
1467 if (!panel->backlight.max)
1468 panel->backlight.max = get_backlight_max_vbt(connector);
1469
Jani Nikula7bd688c2013-11-08 16:48:56 +02001470 if (!panel->backlight.max)
1471 return -ENODEV;
1472
Jani Nikula6dda7302014-06-24 18:27:40 +03001473 panel->backlight.min = get_backlight_min_vbt(connector);
1474
Jani Nikula7bd688c2013-11-08 16:48:56 +02001475 val = pch_get_backlight(connector);
1476 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1477
Jani Nikula636baeb2013-11-08 16:49:02 +02001478 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1479 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1480 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1481
Jani Nikula7bd688c2013-11-08 16:48:56 +02001482 return 0;
1483}
1484
Ville Syrjälä6517d272014-11-07 11:16:02 +02001485static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001486{
Jani Nikula636baeb2013-11-08 16:49:02 +02001487 struct drm_device *dev = connector->base.dev;
1488 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001489 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001490 u32 ctl, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001491
Jani Nikula636baeb2013-11-08 16:49:02 +02001492 ctl = I915_READ(BLC_PWM_CTL);
1493
Jani Nikulab6ab66a2014-02-25 13:11:47 +02001494 if (IS_GEN2(dev) || IS_I915GM(dev) || IS_I945GM(dev))
Jani Nikula636baeb2013-11-08 16:49:02 +02001495 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1496
1497 if (IS_PINEVIEW(dev))
1498 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1499
1500 panel->backlight.max = ctl >> 17;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001501
1502 if (!panel->backlight.max) {
1503 panel->backlight.max = get_backlight_max_vbt(connector);
1504 panel->backlight.max >>= 1;
1505 }
Jani Nikula636baeb2013-11-08 16:49:02 +02001506
Jani Nikula7bd688c2013-11-08 16:48:56 +02001507 if (!panel->backlight.max)
1508 return -ENODEV;
1509
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001510 if (panel->backlight.combination_mode)
1511 panel->backlight.max *= 0xff;
1512
Jani Nikula6dda7302014-06-24 18:27:40 +03001513 panel->backlight.min = get_backlight_min_vbt(connector);
1514
Jani Nikula7bd688c2013-11-08 16:48:56 +02001515 val = i9xx_get_backlight(connector);
1516 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1517
Jani Nikula636baeb2013-11-08 16:49:02 +02001518 panel->backlight.enabled = panel->backlight.level != 0;
1519
Jani Nikula7bd688c2013-11-08 16:48:56 +02001520 return 0;
1521}
1522
Ville Syrjälä6517d272014-11-07 11:16:02 +02001523static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001524{
Jani Nikula636baeb2013-11-08 16:49:02 +02001525 struct drm_device *dev = connector->base.dev;
1526 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001527 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001528 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001529
Jani Nikula636baeb2013-11-08 16:49:02 +02001530 ctl2 = I915_READ(BLC_PWM_CTL2);
1531 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1532 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1533
1534 ctl = I915_READ(BLC_PWM_CTL);
1535 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001536
1537 if (!panel->backlight.max)
1538 panel->backlight.max = get_backlight_max_vbt(connector);
Jani Nikula636baeb2013-11-08 16:49:02 +02001539
Jani Nikula7bd688c2013-11-08 16:48:56 +02001540 if (!panel->backlight.max)
1541 return -ENODEV;
1542
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001543 if (panel->backlight.combination_mode)
1544 panel->backlight.max *= 0xff;
1545
Jani Nikula6dda7302014-06-24 18:27:40 +03001546 panel->backlight.min = get_backlight_min_vbt(connector);
1547
Jani Nikula7bd688c2013-11-08 16:48:56 +02001548 val = i9xx_get_backlight(connector);
1549 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1550
Jani Nikula636baeb2013-11-08 16:49:02 +02001551 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1552 panel->backlight.level != 0;
1553
Jani Nikula7bd688c2013-11-08 16:48:56 +02001554 return 0;
1555}
1556
Ville Syrjälä6517d272014-11-07 11:16:02 +02001557static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001558{
1559 struct drm_device *dev = connector->base.dev;
1560 struct drm_i915_private *dev_priv = dev->dev_private;
1561 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001562 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001563
Ville Syrjälä6517d272014-11-07 11:16:02 +02001564 if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
1565 return -ENODEV;
1566
1567 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001568 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1569
Ville Syrjälä6517d272014-11-07 11:16:02 +02001570 ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
Jani Nikula636baeb2013-11-08 16:49:02 +02001571 panel->backlight.max = ctl >> 16;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001572
1573 if (!panel->backlight.max)
1574 panel->backlight.max = get_backlight_max_vbt(connector);
1575
Jani Nikula7bd688c2013-11-08 16:48:56 +02001576 if (!panel->backlight.max)
1577 return -ENODEV;
1578
Jani Nikula6dda7302014-06-24 18:27:40 +03001579 panel->backlight.min = get_backlight_min_vbt(connector);
1580
Ville Syrjälä6517d272014-11-07 11:16:02 +02001581 val = _vlv_get_backlight(dev, pipe);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001582 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1583
Jani Nikula636baeb2013-11-08 16:49:02 +02001584 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1585 panel->backlight.level != 0;
1586
Jani Nikula7bd688c2013-11-08 16:48:56 +02001587 return 0;
1588}
1589
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301590static int
1591bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1592{
1593 struct drm_device *dev = connector->base.dev;
1594 struct drm_i915_private *dev_priv = dev->dev_private;
1595 struct intel_panel *panel = &connector->panel;
1596 u32 pwm_ctl, val;
1597
Sunil Kamath022e4e52015-09-30 22:34:57 +05301598 /*
1599 * For BXT hard coding the Backlight controller to 0.
1600 * TODO : Read the controller value from VBT and generalize
1601 */
1602 panel->backlight.controller = 0;
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301603
Sunil Kamath022e4e52015-09-30 22:34:57 +05301604 pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
1605
1606 /* Keeping the check if controller 1 is to be programmed.
1607 * This will come into affect once the VBT parsing
1608 * is fixed for controller selection, and controller 1 is used
1609 * for a prticular display configuration.
1610 */
1611 if (panel->backlight.controller == 1) {
1612 val = I915_READ(UTIL_PIN_CTL);
1613 panel->backlight.util_pin_active_low =
1614 val & UTIL_PIN_POLARITY;
1615 }
1616
1617 panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1618 panel->backlight.max =
1619 I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001620
1621 if (!panel->backlight.max)
1622 panel->backlight.max = get_backlight_max_vbt(connector);
1623
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301624 if (!panel->backlight.max)
1625 return -ENODEV;
1626
1627 val = bxt_get_backlight(connector);
1628 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1629
1630 panel->backlight.enabled = (pwm_ctl & BXT_BLC_PWM_ENABLE) &&
1631 panel->backlight.level != 0;
1632
1633 return 0;
1634}
1635
Shobhit Kumarb029e662015-06-26 14:32:10 +05301636static int pwm_setup_backlight(struct intel_connector *connector,
1637 enum pipe pipe)
1638{
1639 struct drm_device *dev = connector->base.dev;
1640 struct intel_panel *panel = &connector->panel;
1641 int retval;
1642
1643 /* Get the PWM chip for backlight control */
1644 panel->backlight.pwm = pwm_get(dev->dev, "pwm_backlight");
1645 if (IS_ERR(panel->backlight.pwm)) {
1646 DRM_ERROR("Failed to own the pwm chip\n");
1647 panel->backlight.pwm = NULL;
1648 return -ENODEV;
1649 }
1650
1651 retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
1652 CRC_PMIC_PWM_PERIOD_NS);
1653 if (retval < 0) {
1654 DRM_ERROR("Failed to configure the pwm chip\n");
1655 pwm_put(panel->backlight.pwm);
1656 panel->backlight.pwm = NULL;
1657 return retval;
1658 }
1659
1660 panel->backlight.min = 0; /* 0% */
1661 panel->backlight.max = 100; /* 100% */
1662 panel->backlight.level = DIV_ROUND_UP(
1663 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
1664 CRC_PMIC_PWM_PERIOD_NS);
1665 panel->backlight.enabled = panel->backlight.level != 0;
1666
1667 return 0;
1668}
1669
Ville Syrjälä6517d272014-11-07 11:16:02 +02001670int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001671{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001672 struct drm_device *dev = connector->dev;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001673 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001674 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001675 struct intel_panel *panel = &intel_connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001676 int ret;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001677
Jani Nikulac6759492014-04-09 11:31:37 +03001678 if (!dev_priv->vbt.backlight.present) {
Scot Doyle9c72cc62014-07-03 23:27:50 +00001679 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1680 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1681 } else {
1682 DRM_DEBUG_KMS("no backlight present per VBT\n");
1683 return 0;
1684 }
Jani Nikulac6759492014-04-09 11:31:37 +03001685 }
1686
Jani Nikula5507fae2015-09-14 14:03:48 +03001687 /* ensure intel_panel has been initialized first */
1688 if (WARN_ON(!panel->backlight.setup))
1689 return -ENODEV;
1690
Jani Nikula7bd688c2013-11-08 16:48:56 +02001691 /* set level and max in panel struct */
Daniel Vetter07f11d42014-09-15 14:35:09 +02001692 mutex_lock(&dev_priv->backlight_lock);
Jani Nikula5507fae2015-09-14 14:03:48 +03001693 ret = panel->backlight.setup(intel_connector, pipe);
Daniel Vetter07f11d42014-09-15 14:35:09 +02001694 mutex_unlock(&dev_priv->backlight_lock);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001695
Jani Nikula7bd688c2013-11-08 16:48:56 +02001696 if (ret) {
1697 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001698 connector->name);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001699 return ret;
1700 }
1701
Jani Nikulac91c9f32013-11-08 16:48:55 +02001702 panel->backlight.present = true;
1703
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001704 DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
1705 connector->name,
Jani Nikulac445b3b2013-11-08 16:49:01 +02001706 panel->backlight.enabled ? "enabled" : "disabled",
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001707 panel->backlight.level, panel->backlight.max);
Jani Nikulac445b3b2013-11-08 16:49:01 +02001708
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001709 return 0;
1710}
1711
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001712void intel_panel_destroy_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001713{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001714 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikulac91c9f32013-11-08 16:48:55 +02001715 struct intel_panel *panel = &intel_connector->panel;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001716
Shobhit Kumarb029e662015-06-26 14:32:10 +05301717 /* dispose of the pwm */
1718 if (panel->backlight.pwm)
1719 pwm_put(panel->backlight.pwm);
1720
Jani Nikulac91c9f32013-11-08 16:48:55 +02001721 panel->backlight.present = false;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001722}
Jani Nikula1d508702012-10-19 14:51:49 +03001723
Jani Nikula7bd688c2013-11-08 16:48:56 +02001724/* Set up chip specific backlight functions */
Jani Nikula5507fae2015-09-14 14:03:48 +03001725static void
1726intel_panel_init_backlight_funcs(struct intel_panel *panel)
Jani Nikula7bd688c2013-11-08 16:48:56 +02001727{
Jani Nikula5507fae2015-09-14 14:03:48 +03001728 struct intel_connector *intel_connector =
1729 container_of(panel, struct intel_connector, panel);
1730 struct drm_device *dev = intel_connector->base.dev;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001731 struct drm_i915_private *dev_priv = dev->dev_private;
1732
Vandana Kannan0fb890c2015-05-05 14:51:56 +05301733 if (IS_BROXTON(dev)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001734 panel->backlight.setup = bxt_setup_backlight;
1735 panel->backlight.enable = bxt_enable_backlight;
1736 panel->backlight.disable = bxt_disable_backlight;
1737 panel->backlight.set = bxt_set_backlight;
1738 panel->backlight.get = bxt_get_backlight;
Jani Nikula437b15b2015-09-04 16:55:13 +03001739 } else if (HAS_PCH_LPT(dev) || HAS_PCH_SPT(dev)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001740 panel->backlight.setup = lpt_setup_backlight;
1741 panel->backlight.enable = lpt_enable_backlight;
1742 panel->backlight.disable = lpt_disable_backlight;
1743 panel->backlight.set = lpt_set_backlight;
1744 panel->backlight.get = lpt_get_backlight;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001745 if (HAS_PCH_LPT(dev))
Jani Nikula5507fae2015-09-14 14:03:48 +03001746 panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
Jani Nikulaaa17cdb2015-09-04 16:55:14 +03001747 else
Jani Nikula5507fae2015-09-14 14:03:48 +03001748 panel->backlight.hz_to_pwm = spt_hz_to_pwm;
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001749 } else if (HAS_PCH_SPLIT(dev)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001750 panel->backlight.setup = pch_setup_backlight;
1751 panel->backlight.enable = pch_enable_backlight;
1752 panel->backlight.disable = pch_disable_backlight;
1753 panel->backlight.set = pch_set_backlight;
1754 panel->backlight.get = pch_get_backlight;
1755 panel->backlight.hz_to_pwm = pch_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001756 } else if (IS_VALLEYVIEW(dev)) {
Shobhit Kumarb029e662015-06-26 14:32:10 +05301757 if (dev_priv->vbt.has_mipi) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001758 panel->backlight.setup = pwm_setup_backlight;
1759 panel->backlight.enable = pwm_enable_backlight;
1760 panel->backlight.disable = pwm_disable_backlight;
1761 panel->backlight.set = pwm_set_backlight;
1762 panel->backlight.get = pwm_get_backlight;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301763 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001764 panel->backlight.setup = vlv_setup_backlight;
1765 panel->backlight.enable = vlv_enable_backlight;
1766 panel->backlight.disable = vlv_disable_backlight;
1767 panel->backlight.set = vlv_set_backlight;
1768 panel->backlight.get = vlv_get_backlight;
1769 panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
Shobhit Kumarb029e662015-06-26 14:32:10 +05301770 }
Jani Nikula7bd688c2013-11-08 16:48:56 +02001771 } else if (IS_GEN4(dev)) {
Jani Nikula5507fae2015-09-14 14:03:48 +03001772 panel->backlight.setup = i965_setup_backlight;
1773 panel->backlight.enable = i965_enable_backlight;
1774 panel->backlight.disable = i965_disable_backlight;
1775 panel->backlight.set = i9xx_set_backlight;
1776 panel->backlight.get = i9xx_get_backlight;
1777 panel->backlight.hz_to_pwm = i965_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001778 } else {
Jani Nikula5507fae2015-09-14 14:03:48 +03001779 panel->backlight.setup = i9xx_setup_backlight;
1780 panel->backlight.enable = i9xx_enable_backlight;
1781 panel->backlight.disable = i9xx_disable_backlight;
1782 panel->backlight.set = i9xx_set_backlight;
1783 panel->backlight.get = i9xx_get_backlight;
1784 panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001785 }
1786}
1787
Jani Nikuladd06f902012-10-19 14:51:50 +03001788int intel_panel_init(struct intel_panel *panel,
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301789 struct drm_display_mode *fixed_mode,
1790 struct drm_display_mode *downclock_mode)
Jani Nikula1d508702012-10-19 14:51:49 +03001791{
Jani Nikula5507fae2015-09-14 14:03:48 +03001792 intel_panel_init_backlight_funcs(panel);
1793
Jani Nikuladd06f902012-10-19 14:51:50 +03001794 panel->fixed_mode = fixed_mode;
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301795 panel->downclock_mode = downclock_mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03001796
Jani Nikula1d508702012-10-19 14:51:49 +03001797 return 0;
1798}
1799
1800void intel_panel_fini(struct intel_panel *panel)
1801{
Jani Nikuladd06f902012-10-19 14:51:50 +03001802 struct intel_connector *intel_connector =
1803 container_of(panel, struct intel_connector, panel);
1804
1805 if (panel->fixed_mode)
1806 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
Vandana Kannanec9ed192013-12-10 13:37:36 +05301807
1808 if (panel->downclock_mode)
1809 drm_mode_destroy(intel_connector->base.dev,
1810 panel->downclock_mode);
Jani Nikula1d508702012-10-19 14:51:49 +03001811}
Ville Syrjälä0962c3c2014-11-07 15:19:46 +02001812
1813void intel_backlight_register(struct drm_device *dev)
1814{
1815 struct intel_connector *connector;
1816
1817 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1818 intel_backlight_device_register(connector);
1819}
1820
1821void intel_backlight_unregister(struct drm_device *dev)
1822{
1823 struct intel_connector *connector;
1824
1825 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head)
1826 intel_backlight_device_unregister(connector);
1827}