blob: 41b3be217493b9a26315e2c69968f1c080161fe6 [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
Carsten Emde7bd90902012-03-15 15:56:25 +010033#include <linux/moduleparam.h>
Chris Wilson1d8e1c72010-08-07 11:01:28 +010034#include "intel_drv.h"
35
36void
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030037intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
Chris Wilson1d8e1c72010-08-07 11:01:28 +010038 struct drm_display_mode *adjusted_mode)
39{
Ville Syrjälä4c6df4b2013-09-02 21:13:39 +030040 drm_mode_copy(adjusted_mode, fixed_mode);
Imre Deaka52690e2013-08-27 12:24:09 +030041
42 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson1d8e1c72010-08-07 11:01:28 +010043}
44
Jani Nikula525997e2014-04-29 23:30:48 +030045/**
46 * intel_find_panel_downclock - find the reduced downclock for LVDS in EDID
47 * @dev: drm device
48 * @fixed_mode : panel native mode
49 * @connector: LVDS/eDP connector
50 *
51 * Return downclock_avail
52 * Find the reduced downclock for LVDS/eDP in EDID.
53 */
54struct drm_display_mode *
55intel_find_panel_downclock(struct drm_device *dev,
56 struct drm_display_mode *fixed_mode,
57 struct drm_connector *connector)
58{
59 struct drm_display_mode *scan, *tmp_mode;
60 int temp_downclock;
61
62 temp_downclock = fixed_mode->clock;
63 tmp_mode = NULL;
64
65 list_for_each_entry(scan, &connector->probed_modes, head) {
66 /*
67 * If one mode has the same resolution with the fixed_panel
68 * mode while they have the different refresh rate, it means
69 * that the reduced downclock is found. In such
70 * case we can set the different FPx0/1 to dynamically select
71 * between low and high frequency.
72 */
73 if (scan->hdisplay == fixed_mode->hdisplay &&
74 scan->hsync_start == fixed_mode->hsync_start &&
75 scan->hsync_end == fixed_mode->hsync_end &&
76 scan->htotal == fixed_mode->htotal &&
77 scan->vdisplay == fixed_mode->vdisplay &&
78 scan->vsync_start == fixed_mode->vsync_start &&
79 scan->vsync_end == fixed_mode->vsync_end &&
80 scan->vtotal == fixed_mode->vtotal) {
81 if (scan->clock < temp_downclock) {
82 /*
83 * The downclock is already found. But we
84 * expect to find the lower downclock.
85 */
86 temp_downclock = scan->clock;
87 tmp_mode = scan;
88 }
89 }
90 }
91
92 if (temp_downclock < fixed_mode->clock)
93 return drm_mode_duplicate(dev, tmp_mode);
94 else
95 return NULL;
96}
97
Chris Wilson1d8e1c72010-08-07 11:01:28 +010098/* adjusted_mode has been preset to be the panel's fixed mode */
99void
Jesse Barnesb074cec2013-04-25 12:55:02 -0700100intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
101 struct intel_crtc_config *pipe_config,
102 int fitting_mode)
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100103{
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300104 struct drm_display_mode *adjusted_mode;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100105 int x, y, width, height;
106
Jesse Barnesb074cec2013-04-25 12:55:02 -0700107 adjusted_mode = &pipe_config->adjusted_mode;
108
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100109 x = y = width = height = 0;
110
111 /* Native modes don't need fitting */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300112 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
113 adjusted_mode->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;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100120 x = (adjusted_mode->hdisplay - width + 1)/2;
121 y = (adjusted_mode->vdisplay - height + 1)/2;
122 break;
123
124 case DRM_MODE_SCALE_ASPECT:
125 /* Scale but preserve the aspect ratio */
126 {
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200127 u32 scaled_width = adjusted_mode->hdisplay
128 * pipe_config->pipe_src_h;
129 u32 scaled_height = pipe_config->pipe_src_w
130 * adjusted_mode->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++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100135 x = (adjusted_mode->hdisplay - width + 1) / 2;
136 y = 0;
137 height = adjusted_mode->vdisplay;
138 } 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++;
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100142 y = (adjusted_mode->vdisplay - height + 1) / 2;
143 x = 0;
144 width = adjusted_mode->hdisplay;
145 } else {
146 x = y = 0;
147 width = adjusted_mode->hdisplay;
148 height = adjusted_mode->vdisplay;
149 }
150 }
151 break;
152
Chris Wilson1d8e1c72010-08-07 11:01:28 +0100153 case DRM_MODE_SCALE_FULLSCREEN:
154 x = y = 0;
155 width = adjusted_mode->hdisplay;
156 height = adjusted_mode->vdisplay;
157 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
171centre_horizontally(struct drm_display_mode *mode,
172 int width)
173{
174 u32 border, sync_pos, blank_width, sync_width;
175
176 /* keep the hsync and hblank widths constant */
177 sync_width = mode->crtc_hsync_end - mode->crtc_hsync_start;
178 blank_width = mode->crtc_hblank_end - mode->crtc_hblank_start;
179 sync_pos = (blank_width - sync_width + 1) / 2;
180
181 border = (mode->hdisplay - width + 1) / 2;
182 border += border & 1; /* make the border even */
183
184 mode->crtc_hdisplay = width;
185 mode->crtc_hblank_start = width + border;
186 mode->crtc_hblank_end = mode->crtc_hblank_start + blank_width;
187
188 mode->crtc_hsync_start = mode->crtc_hblank_start + sync_pos;
189 mode->crtc_hsync_end = mode->crtc_hsync_start + sync_width;
190}
191
192static void
193centre_vertically(struct drm_display_mode *mode,
194 int height)
195{
196 u32 border, sync_pos, blank_width, sync_width;
197
198 /* keep the vsync and vblank widths constant */
199 sync_width = mode->crtc_vsync_end - mode->crtc_vsync_start;
200 blank_width = mode->crtc_vblank_end - mode->crtc_vblank_start;
201 sync_pos = (blank_width - sync_width + 1) / 2;
202
203 border = (mode->vdisplay - height + 1) / 2;
204
205 mode->crtc_vdisplay = height;
206 mode->crtc_vblank_start = height + border;
207 mode->crtc_vblank_end = mode->crtc_vblank_start + blank_width;
208
209 mode->crtc_vsync_start = mode->crtc_vblank_start + sync_pos;
210 mode->crtc_vsync_end = mode->crtc_vsync_start + sync_width;
211}
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
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200226static void i965_scale_aspect(struct intel_crtc_config *pipe_config,
227 u32 *pfit_control)
228{
229 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
230 u32 scaled_width = adjusted_mode->hdisplay *
231 pipe_config->pipe_src_h;
232 u32 scaled_height = pipe_config->pipe_src_w *
233 adjusted_mode->vdisplay;
234
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;
242 else if (adjusted_mode->hdisplay != pipe_config->pipe_src_w)
243 *pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
244}
245
246static void i9xx_scale_aspect(struct intel_crtc_config *pipe_config,
247 u32 *pfit_control, u32 *pfit_pgm_ratios,
248 u32 *border)
249{
250 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
251 u32 scaled_width = adjusted_mode->hdisplay *
252 pipe_config->pipe_src_h;
253 u32 scaled_height = pipe_config->pipe_src_w *
254 adjusted_mode->vdisplay;
255 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;
268 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay) {
269 bits = panel_fitter_scaling(pipe_config->pipe_src_h,
270 adjusted_mode->vdisplay);
271
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;
284 if (pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
285 bits = panel_fitter_scaling(pipe_config->pipe_src_w,
286 adjusted_mode->hdisplay);
287
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,
304 struct intel_crtc_config *pipe_config,
305 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ä37327ab2013-09-04 18:25:28 +0300309 struct drm_display_mode *adjusted_mode;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700310
Jesse Barnes2dd24552013-04-25 12:55:01 -0700311 adjusted_mode = &pipe_config->adjusted_mode;
312
313 /* Native modes don't need fitting */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300314 if (adjusted_mode->hdisplay == pipe_config->pipe_src_w &&
315 adjusted_mode->vdisplay == pipe_config->pipe_src_h)
Jesse Barnes2dd24552013-04-25 12:55:01 -0700316 goto out;
317
318 switch (fitting_mode) {
319 case DRM_MODE_SCALE_CENTER:
320 /*
321 * For centered modes, we have to calculate border widths &
322 * heights and modify the values programmed into the CRTC.
323 */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300324 centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
325 centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700326 border = LVDS_BORDER_ENABLE;
327 break;
328 case DRM_MODE_SCALE_ASPECT:
329 /* Scale but preserve the aspect ratio */
Daniel Vetter9084e7d2013-09-16 23:43:45 +0200330 if (INTEL_INFO(dev)->gen >= 4)
331 i965_scale_aspect(pipe_config, &pfit_control);
332 else
333 i9xx_scale_aspect(pipe_config, &pfit_control,
334 &pfit_pgm_ratios, &border);
Jesse Barnes2dd24552013-04-25 12:55:01 -0700335 break;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700336 case DRM_MODE_SCALE_FULLSCREEN:
337 /*
338 * Full scaling, even if it changes the aspect ratio.
339 * Fortunately this is all done for us in hw.
340 */
Ville Syrjälä37327ab2013-09-04 18:25:28 +0300341 if (pipe_config->pipe_src_h != adjusted_mode->vdisplay ||
342 pipe_config->pipe_src_w != adjusted_mode->hdisplay) {
Jesse Barnes2dd24552013-04-25 12:55:01 -0700343 pfit_control |= PFIT_ENABLE;
344 if (INTEL_INFO(dev)->gen >= 4)
345 pfit_control |= PFIT_SCALING_AUTO;
346 else
347 pfit_control |= (VERT_AUTO_SCALE |
348 VERT_INTERP_BILINEAR |
349 HORIZ_AUTO_SCALE |
350 HORIZ_INTERP_BILINEAR);
351 }
352 break;
Jesse Barnesab3e67f2013-04-25 12:55:03 -0700353 default:
354 WARN(1, "bad panel fit mode: %d\n", fitting_mode);
355 return;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700356 }
357
358 /* 965+ wants fuzzy fitting */
359 /* FIXME: handle multiple panels by failing gracefully */
360 if (INTEL_INFO(dev)->gen >= 4)
361 pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
362 PFIT_FILTER_FUZZY);
363
364out:
365 if ((pfit_control & PFIT_ENABLE) == 0) {
366 pfit_control = 0;
367 pfit_pgm_ratios = 0;
368 }
369
Daniel Vetter6b89cdd2014-07-09 22:35:53 +0200370 /* Make sure pre-965 set dither correctly for 18bpp panels. */
371 if (INTEL_INFO(dev)->gen < 4 && pipe_config->pipe_bpp == 18)
372 pfit_control |= PANEL_8TO6_DITHER_ENABLE;
373
Daniel Vetter2deefda2013-04-25 22:52:17 +0200374 pipe_config->gmch_pfit.control = pfit_control;
375 pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
Daniel Vetter68fc8742013-04-25 22:52:16 +0200376 pipe_config->gmch_pfit.lvds_border_bits = border;
Jesse Barnes2dd24552013-04-25 12:55:01 -0700377}
378
Jani Nikula525997e2014-04-29 23:30:48 +0300379enum drm_connector_status
380intel_panel_detect(struct drm_device *dev)
381{
382 struct drm_i915_private *dev_priv = dev->dev_private;
383
384 /* Assume that the BIOS does not lie through the OpRegion... */
385 if (!i915.panel_ignore_lid && dev_priv->opregion.lid_state) {
386 return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
387 connector_status_connected :
388 connector_status_disconnected;
389 }
390
391 switch (i915.panel_ignore_lid) {
392 case -2:
393 return connector_status_connected;
394 case -1:
395 return connector_status_disconnected;
396 default:
397 return connector_status_unknown;
398 }
399}
400
Jani Nikula6dda7302014-06-24 18:27:40 +0300401/**
402 * scale - scale values from one range to another
403 *
404 * @source_val: value in range [@source_min..@source_max]
405 *
406 * Return @source_val in range [@source_min..@source_max] scaled to range
407 * [@target_min..@target_max].
408 */
409static uint32_t scale(uint32_t source_val,
410 uint32_t source_min, uint32_t source_max,
411 uint32_t target_min, uint32_t target_max)
412{
413 uint64_t target_val;
414
415 WARN_ON(source_min > source_max);
416 WARN_ON(target_min > target_max);
417
418 /* defensive */
419 source_val = clamp(source_val, source_min, source_max);
420
421 /* avoid overflows */
U. Artie Eoff673e7bb2014-09-29 15:49:32 -0700422 target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
423 (target_max - target_min), source_max - source_min);
Jani Nikula6dda7302014-06-24 18:27:40 +0300424 target_val += target_min;
425
426 return target_val;
427}
428
429/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
430static inline u32 scale_user_to_hw(struct intel_connector *connector,
431 u32 user_level, u32 user_max)
432{
433 struct intel_panel *panel = &connector->panel;
434
435 return scale(user_level, 0, user_max,
436 panel->backlight.min, panel->backlight.max);
437}
438
439/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
440 * to [hw_min..hw_max]. */
441static inline u32 clamp_user_to_hw(struct intel_connector *connector,
442 u32 user_level, u32 user_max)
443{
444 struct intel_panel *panel = &connector->panel;
445 u32 hw_level;
446
447 hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
448 hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
449
450 return hw_level;
451}
452
453/* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
454static inline u32 scale_hw_to_user(struct intel_connector *connector,
455 u32 hw_level, u32 user_max)
456{
457 struct intel_panel *panel = &connector->panel;
458
459 return scale(hw_level, panel->backlight.min, panel->backlight.max,
460 0, user_max);
461}
462
Jani Nikula7bd688c2013-11-08 16:48:56 +0200463static u32 intel_panel_compute_brightness(struct intel_connector *connector,
464 u32 val)
Carsten Emde7bd90902012-03-15 15:56:25 +0100465{
Jani Nikula7bd688c2013-11-08 16:48:56 +0200466 struct drm_device *dev = connector->base.dev;
Carsten Emde4dca20e2012-03-15 15:56:26 +0100467 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200468 struct intel_panel *panel = &connector->panel;
469
470 WARN_ON(panel->backlight.max == 0);
Carsten Emde4dca20e2012-03-15 15:56:26 +0100471
Jani Nikulad330a952014-01-21 11:24:25 +0200472 if (i915.invert_brightness < 0)
Carsten Emde4dca20e2012-03-15 15:56:26 +0100473 return val;
474
Jani Nikulad330a952014-01-21 11:24:25 +0200475 if (i915.invert_brightness > 0 ||
Jani Nikulad6540632013-04-12 15:18:36 +0300476 dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200477 return panel->backlight.max - val;
Jani Nikulad6540632013-04-12 15:18:36 +0300478 }
Carsten Emde7bd90902012-03-15 15:56:25 +0100479
480 return val;
481}
482
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100483static u32 bdw_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100484{
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100485 struct drm_device *dev = connector->base.dev;
486 struct drm_i915_private *dev_priv = dev->dev_private;
487
488 return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
489}
490
Jani Nikula7bd688c2013-11-08 16:48:56 +0200491static u32 pch_get_backlight(struct intel_connector *connector)
Chris Wilsona9573552010-08-22 13:18:16 +0100492{
Jani Nikula7bd688c2013-11-08 16:48:56 +0200493 struct drm_device *dev = connector->base.dev;
494 struct drm_i915_private *dev_priv = dev->dev_private;
495
496 return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
497}
498
499static u32 i9xx_get_backlight(struct intel_connector *connector)
500{
501 struct drm_device *dev = connector->base.dev;
502 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula636baeb2013-11-08 16:49:02 +0200503 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200504 u32 val;
505
506 val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
507 if (INTEL_INFO(dev)->gen < 4)
508 val >>= 1;
509
Jani Nikula636baeb2013-11-08 16:49:02 +0200510 if (panel->backlight.combination_mode) {
Jani Nikula7bd688c2013-11-08 16:48:56 +0200511 u8 lbpc;
512
513 pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
514 val *= lbpc;
515 }
516
517 return val;
518}
519
520static u32 _vlv_get_backlight(struct drm_device *dev, enum pipe pipe)
521{
522 struct drm_i915_private *dev_priv = dev->dev_private;
523
524 return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
525}
526
527static u32 vlv_get_backlight(struct intel_connector *connector)
528{
529 struct drm_device *dev = connector->base.dev;
530 enum pipe pipe = intel_get_pipe_from_connector(connector);
531
532 return _vlv_get_backlight(dev, pipe);
533}
534
535static u32 intel_panel_get_backlight(struct intel_connector *connector)
536{
537 struct drm_device *dev = connector->base.dev;
Chris Wilsona9573552010-08-22 13:18:16 +0100538 struct drm_i915_private *dev_priv = dev->dev_private;
539 u32 val;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300540 unsigned long flags;
541
Jani Nikula58c68772013-11-08 16:48:54 +0200542 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
Chris Wilsona9573552010-08-22 13:18:16 +0100543
Jani Nikula7bd688c2013-11-08 16:48:56 +0200544 val = dev_priv->display.get_backlight(connector);
545 val = intel_panel_compute_brightness(connector, val);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300546
Jani Nikula58c68772013-11-08 16:48:54 +0200547 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300548
Chris Wilsona9573552010-08-22 13:18:16 +0100549 DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
550 return val;
551}
552
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100553static void bdw_set_backlight(struct intel_connector *connector, u32 level)
Ben Widawskyf8e10062013-11-11 11:12:57 +0200554{
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100555 struct drm_device *dev = connector->base.dev;
Ben Widawskyf8e10062013-11-11 11:12:57 +0200556 struct drm_i915_private *dev_priv = dev->dev_private;
557 u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
558 I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
559}
560
Jani Nikula7bd688c2013-11-08 16:48:56 +0200561static void pch_set_backlight(struct intel_connector *connector, u32 level)
Chris Wilsona9573552010-08-22 13:18:16 +0100562{
Jani Nikula7bd688c2013-11-08 16:48:56 +0200563 struct drm_device *dev = connector->base.dev;
Chris Wilsona9573552010-08-22 13:18:16 +0100564 struct drm_i915_private *dev_priv = dev->dev_private;
565 u32 tmp;
566
Jani Nikula7bd688c2013-11-08 16:48:56 +0200567 tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
568 I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
569}
Chris Wilsona9573552010-08-22 13:18:16 +0100570
Jani Nikula7bd688c2013-11-08 16:48:56 +0200571static void i9xx_set_backlight(struct intel_connector *connector, u32 level)
572{
573 struct drm_device *dev = connector->base.dev;
574 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200575 struct intel_panel *panel = &connector->panel;
Jani Nikulab329b322013-11-08 16:48:57 +0200576 u32 tmp, mask;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100577
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200578 WARN_ON(panel->backlight.max == 0);
579
Jani Nikula636baeb2013-11-08 16:49:02 +0200580 if (panel->backlight.combination_mode) {
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100581 u8 lbpc;
582
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200583 lbpc = level * 0xfe / panel->backlight.max + 1;
Takashi Iwaiba3820a2011-03-10 14:02:12 +0100584 level /= lbpc;
585 pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
586 }
587
Jani Nikulab329b322013-11-08 16:48:57 +0200588 if (IS_GEN4(dev)) {
589 mask = BACKLIGHT_DUTY_CYCLE_MASK;
590 } else {
Chris Wilsona9573552010-08-22 13:18:16 +0100591 level <<= 1;
Jani Nikulab329b322013-11-08 16:48:57 +0200592 mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
593 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200594
Jani Nikulab329b322013-11-08 16:48:57 +0200595 tmp = I915_READ(BLC_PWM_CTL) & ~mask;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200596 I915_WRITE(BLC_PWM_CTL, tmp | level);
597}
598
599static void vlv_set_backlight(struct intel_connector *connector, u32 level)
600{
601 struct drm_device *dev = connector->base.dev;
602 struct drm_i915_private *dev_priv = dev->dev_private;
603 enum pipe pipe = intel_get_pipe_from_connector(connector);
604 u32 tmp;
605
606 tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
607 I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
608}
609
610static void
611intel_panel_actually_set_backlight(struct intel_connector *connector, u32 level)
612{
613 struct drm_device *dev = connector->base.dev;
614 struct drm_i915_private *dev_priv = dev->dev_private;
615
616 DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
617
618 level = intel_panel_compute_brightness(connector, level);
619 dev_priv->display.set_backlight(connector, level);
Chris Wilsona9573552010-08-22 13:18:16 +0100620}
Chris Wilson47356eb2011-01-11 17:06:04 +0000621
Jani Nikula6dda7302014-06-24 18:27:40 +0300622/* set backlight brightness to level in range [0..max], scaling wrt hw min */
623static void intel_panel_set_backlight(struct intel_connector *connector,
624 u32 user_level, u32 user_max)
Takashi Iwaif52c6192011-10-14 11:45:40 +0200625{
Jesse Barnes752aa882013-10-31 18:55:49 +0200626 struct drm_device *dev = connector->base.dev;
Takashi Iwaif52c6192011-10-14 11:45:40 +0200627 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +0200628 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +0200629 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikula6dda7302014-06-24 18:27:40 +0300630 u32 hw_level;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300631 unsigned long flags;
632
Ville Syrjälädc5a4362014-01-16 18:27:15 +0200633 if (!panel->backlight.present || pipe == INVALID_PIPE)
Jesse Barnes752aa882013-10-31 18:55:49 +0200634 return;
635
Jani Nikula58c68772013-11-08 16:48:54 +0200636 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
Jani Nikulad6540632013-04-12 15:18:36 +0300637
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200638 WARN_ON(panel->backlight.max == 0);
Jani Nikulad6540632013-04-12 15:18:36 +0300639
Jani Nikula6dda7302014-06-24 18:27:40 +0300640 hw_level = scale_user_to_hw(connector, user_level, user_max);
641 panel->backlight.level = hw_level;
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200642
Jani Nikula58c68772013-11-08 16:48:54 +0200643 if (panel->backlight.enabled)
Jani Nikula6dda7302014-06-24 18:27:40 +0300644 intel_panel_actually_set_backlight(connector, hw_level);
645
646 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
647}
648
649/* set backlight brightness to level in range [0..max], assuming hw min is
650 * respected.
651 */
652void intel_panel_set_backlight_acpi(struct intel_connector *connector,
653 u32 user_level, u32 user_max)
654{
655 struct drm_device *dev = connector->base.dev;
656 struct drm_i915_private *dev_priv = dev->dev_private;
657 struct intel_panel *panel = &connector->panel;
658 enum pipe pipe = intel_get_pipe_from_connector(connector);
659 u32 hw_level;
660 unsigned long flags;
661
662 if (!panel->backlight.present || pipe == INVALID_PIPE)
663 return;
664
665 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
666
667 WARN_ON(panel->backlight.max == 0);
668
669 hw_level = clamp_user_to_hw(connector, user_level, user_max);
670 panel->backlight.level = hw_level;
671
672 if (panel->backlight.device)
673 panel->backlight.device->props.brightness =
674 scale_hw_to_user(connector,
675 panel->backlight.level,
676 panel->backlight.device->props.max_brightness);
677
678 if (panel->backlight.enabled)
679 intel_panel_actually_set_backlight(connector, hw_level);
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200680
Jani Nikula58c68772013-11-08 16:48:54 +0200681 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
Takashi Iwaif52c6192011-10-14 11:45:40 +0200682}
683
Jani Nikula7bd688c2013-11-08 16:48:56 +0200684static void pch_disable_backlight(struct intel_connector *connector)
685{
686 struct drm_device *dev = connector->base.dev;
687 struct drm_i915_private *dev_priv = dev->dev_private;
688 u32 tmp;
689
Jani Nikula3bd712e2013-11-08 16:48:59 +0200690 intel_panel_actually_set_backlight(connector, 0);
691
Jani Nikula7bd688c2013-11-08 16:48:56 +0200692 tmp = I915_READ(BLC_PWM_CPU_CTL2);
693 I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
694
695 tmp = I915_READ(BLC_PWM_PCH_CTL1);
696 I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
697}
698
Jani Nikula3bd712e2013-11-08 16:48:59 +0200699static void i9xx_disable_backlight(struct intel_connector *connector)
700{
701 intel_panel_actually_set_backlight(connector, 0);
702}
703
Jani Nikula7bd688c2013-11-08 16:48:56 +0200704static void i965_disable_backlight(struct intel_connector *connector)
705{
706 struct drm_device *dev = connector->base.dev;
707 struct drm_i915_private *dev_priv = dev->dev_private;
708 u32 tmp;
709
Jani Nikula3bd712e2013-11-08 16:48:59 +0200710 intel_panel_actually_set_backlight(connector, 0);
711
Jani Nikula7bd688c2013-11-08 16:48:56 +0200712 tmp = I915_READ(BLC_PWM_CTL2);
713 I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
714}
715
716static void vlv_disable_backlight(struct intel_connector *connector)
717{
718 struct drm_device *dev = connector->base.dev;
719 struct drm_i915_private *dev_priv = dev->dev_private;
720 enum pipe pipe = intel_get_pipe_from_connector(connector);
721 u32 tmp;
722
Jani Nikula3bd712e2013-11-08 16:48:59 +0200723 intel_panel_actually_set_backlight(connector, 0);
724
Jani Nikula7bd688c2013-11-08 16:48:56 +0200725 tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
726 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
727}
728
Jesse Barnes752aa882013-10-31 18:55:49 +0200729void intel_panel_disable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +0000730{
Jesse Barnes752aa882013-10-31 18:55:49 +0200731 struct drm_device *dev = connector->base.dev;
Chris Wilson47356eb2011-01-11 17:06:04 +0000732 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +0200733 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +0200734 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300735 unsigned long flags;
736
Ville Syrjälädc5a4362014-01-16 18:27:15 +0200737 if (!panel->backlight.present || pipe == INVALID_PIPE)
Jesse Barnes752aa882013-10-31 18:55:49 +0200738 return;
739
Jani Nikula3f577572013-07-25 14:31:30 +0300740 /*
741 * Do not disable backlight on the vgaswitcheroo path. When switching
742 * away from i915, the other client may depend on i915 to handle the
743 * backlight. This will leave the backlight on unnecessarily when
744 * another client is not activated.
745 */
746 if (dev->switch_power_state == DRM_SWITCH_POWER_CHANGING) {
747 DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
748 return;
749 }
750
Jani Nikula58c68772013-11-08 16:48:54 +0200751 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000752
Jani Nikulaab656bb2014-08-13 12:10:12 +0300753 if (panel->backlight.device)
754 panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
Jani Nikula58c68772013-11-08 16:48:54 +0200755 panel->backlight.enabled = false;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200756 dev_priv->display.disable_backlight(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300757
Jani Nikula58c68772013-11-08 16:48:54 +0200758 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000759}
760
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100761static void bdw_enable_backlight(struct intel_connector *connector)
762{
763 struct drm_device *dev = connector->base.dev;
764 struct drm_i915_private *dev_priv = dev->dev_private;
765 struct intel_panel *panel = &connector->panel;
766 u32 pch_ctl1, pch_ctl2;
Chris Wilson47356eb2011-01-11 17:06:04 +0000767
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100768 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
769 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
770 DRM_DEBUG_KMS("pch backlight already enabled\n");
771 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
772 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
Chris Wilson47356eb2011-01-11 17:06:04 +0000773 }
774
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100775 pch_ctl2 = panel->backlight.max << 16;
776 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
777
778 pch_ctl1 = 0;
779 if (panel->backlight.active_low_pwm)
780 pch_ctl1 |= BLM_PCH_POLARITY;
781
782 /* BDW always uses the pch pwm controls. */
783 pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
784
785 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
786 POSTING_READ(BLC_PWM_PCH_CTL1);
787 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
788
789 /* This won't stick until the above enable. */
790 intel_panel_actually_set_backlight(connector, panel->backlight.level);
791}
792
Jani Nikula7bd688c2013-11-08 16:48:56 +0200793static void pch_enable_backlight(struct intel_connector *connector)
794{
795 struct drm_device *dev = connector->base.dev;
796 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200797 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200798 enum pipe pipe = intel_get_pipe_from_connector(connector);
799 enum transcoder cpu_transcoder =
800 intel_pipe_to_cpu_transcoder(dev_priv, pipe);
Jani Nikulab35684b2013-11-14 12:13:41 +0200801 u32 cpu_ctl2, pch_ctl1, pch_ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200802
Jani Nikulab35684b2013-11-14 12:13:41 +0200803 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
804 if (cpu_ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000805 DRM_DEBUG_KMS("cpu backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200806 cpu_ctl2 &= ~BLM_PWM_ENABLE;
807 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200808 }
Jani Nikula3bd712e2013-11-08 16:48:59 +0200809
Jani Nikulab35684b2013-11-14 12:13:41 +0200810 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
811 if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
812 DRM_DEBUG_KMS("pch backlight already enabled\n");
813 pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
814 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
815 }
816
817 if (cpu_transcoder == TRANSCODER_EDP)
818 cpu_ctl2 = BLM_TRANSCODER_EDP;
819 else
820 cpu_ctl2 = BLM_PIPE(cpu_transcoder);
821 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
822 POSTING_READ(BLC_PWM_CPU_CTL2);
823 I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
824
825 /* This won't stick until the above enable. */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200826 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +0200827
828 pch_ctl2 = panel->backlight.max << 16;
829 I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
830
Jani Nikulab35684b2013-11-14 12:13:41 +0200831 pch_ctl1 = 0;
832 if (panel->backlight.active_low_pwm)
833 pch_ctl1 |= BLM_PCH_POLARITY;
Daniel Vetter96ab4c72013-11-14 15:17:41 +0100834
Jani Nikulab35684b2013-11-14 12:13:41 +0200835 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
836 POSTING_READ(BLC_PWM_PCH_CTL1);
837 I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
Jani Nikula3bd712e2013-11-08 16:48:59 +0200838}
839
840static void i9xx_enable_backlight(struct intel_connector *connector)
841{
Jani Nikulab35684b2013-11-14 12:13:41 +0200842 struct drm_device *dev = connector->base.dev;
843 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200844 struct intel_panel *panel = &connector->panel;
Jani Nikulab35684b2013-11-14 12:13:41 +0200845 u32 ctl, freq;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200846
Jani Nikulab35684b2013-11-14 12:13:41 +0200847 ctl = I915_READ(BLC_PWM_CTL);
848 if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
Scot Doyle813008c2014-08-19 02:07:13 +0000849 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200850 I915_WRITE(BLC_PWM_CTL, 0);
851 }
852
853 freq = panel->backlight.max;
854 if (panel->backlight.combination_mode)
855 freq /= 0xff;
856
857 ctl = freq << 17;
Jani Nikulab6ab66a2014-02-25 13:11:47 +0200858 if (panel->backlight.combination_mode)
Jani Nikulab35684b2013-11-14 12:13:41 +0200859 ctl |= BLM_LEGACY_MODE;
860 if (IS_PINEVIEW(dev) && panel->backlight.active_low_pwm)
861 ctl |= BLM_POLARITY_PNV;
862
863 I915_WRITE(BLC_PWM_CTL, ctl);
864 POSTING_READ(BLC_PWM_CTL);
865
866 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200867 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200868}
869
870static void i965_enable_backlight(struct intel_connector *connector)
871{
872 struct drm_device *dev = connector->base.dev;
873 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200874 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200875 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +0200876 u32 ctl, ctl2, freq;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200877
Jani Nikulab35684b2013-11-14 12:13:41 +0200878 ctl2 = I915_READ(BLC_PWM_CTL2);
879 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000880 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200881 ctl2 &= ~BLM_PWM_ENABLE;
882 I915_WRITE(BLC_PWM_CTL2, ctl2);
883 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200884
Jani Nikulab35684b2013-11-14 12:13:41 +0200885 freq = panel->backlight.max;
886 if (panel->backlight.combination_mode)
887 freq /= 0xff;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200888
Jani Nikulab35684b2013-11-14 12:13:41 +0200889 ctl = freq << 16;
890 I915_WRITE(BLC_PWM_CTL, ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200891
Jani Nikulab35684b2013-11-14 12:13:41 +0200892 ctl2 = BLM_PIPE(pipe);
893 if (panel->backlight.combination_mode)
894 ctl2 |= BLM_COMBINATION_MODE;
895 if (panel->backlight.active_low_pwm)
896 ctl2 |= BLM_POLARITY_I965;
897 I915_WRITE(BLC_PWM_CTL2, ctl2);
898 POSTING_READ(BLC_PWM_CTL2);
899 I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
Jani Nikula2e7eeeb2014-06-09 18:24:34 +0300900
901 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200902}
903
904static void vlv_enable_backlight(struct intel_connector *connector)
905{
906 struct drm_device *dev = connector->base.dev;
907 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula3bd712e2013-11-08 16:48:59 +0200908 struct intel_panel *panel = &connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200909 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikulab35684b2013-11-14 12:13:41 +0200910 u32 ctl, ctl2;
Jani Nikula7bd688c2013-11-08 16:48:56 +0200911
Jani Nikulab35684b2013-11-14 12:13:41 +0200912 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
913 if (ctl2 & BLM_PWM_ENABLE) {
Scot Doyle813008c2014-08-19 02:07:13 +0000914 DRM_DEBUG_KMS("backlight already enabled\n");
Jani Nikulab35684b2013-11-14 12:13:41 +0200915 ctl2 &= ~BLM_PWM_ENABLE;
916 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
917 }
Jani Nikula7bd688c2013-11-08 16:48:56 +0200918
Jani Nikulab35684b2013-11-14 12:13:41 +0200919 ctl = panel->backlight.max << 16;
920 I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200921
Jani Nikulab35684b2013-11-14 12:13:41 +0200922 /* XXX: combine this into above write? */
Jani Nikula3bd712e2013-11-08 16:48:59 +0200923 intel_panel_actually_set_backlight(connector, panel->backlight.level);
Jani Nikulab35684b2013-11-14 12:13:41 +0200924
925 ctl2 = 0;
926 if (panel->backlight.active_low_pwm)
927 ctl2 |= BLM_POLARITY_I965;
928 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
929 POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
930 I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
Jani Nikula7bd688c2013-11-08 16:48:56 +0200931}
932
Jesse Barnes752aa882013-10-31 18:55:49 +0200933void intel_panel_enable_backlight(struct intel_connector *connector)
Chris Wilson47356eb2011-01-11 17:06:04 +0000934{
Jesse Barnes752aa882013-10-31 18:55:49 +0200935 struct drm_device *dev = connector->base.dev;
Chris Wilson47356eb2011-01-11 17:06:04 +0000936 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula58c68772013-11-08 16:48:54 +0200937 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +0200938 enum pipe pipe = intel_get_pipe_from_connector(connector);
Jani Nikula8ba2d182013-04-12 15:18:37 +0300939 unsigned long flags;
940
Ville Syrjälädc5a4362014-01-16 18:27:15 +0200941 if (!panel->backlight.present || pipe == INVALID_PIPE)
Jesse Barnes752aa882013-10-31 18:55:49 +0200942 return;
943
Damien Lespiau6f2bcce2013-10-16 12:29:54 +0100944 DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
Chris Wilson540b5d02013-10-13 12:56:31 +0100945
Jani Nikula58c68772013-11-08 16:48:54 +0200946 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000947
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200948 WARN_ON(panel->backlight.max == 0);
949
Jani Nikula58c68772013-11-08 16:48:54 +0200950 if (panel->backlight.level == 0) {
Jani Nikulaf91c15e2013-11-08 16:49:00 +0200951 panel->backlight.level = panel->backlight.max;
Jani Nikula58c68772013-11-08 16:48:54 +0200952 if (panel->backlight.device)
953 panel->backlight.device->props.brightness =
Jani Nikula6dda7302014-06-24 18:27:40 +0300954 scale_hw_to_user(connector,
955 panel->backlight.level,
956 panel->backlight.device->props.max_brightness);
Jani Nikulab6b3ba52013-03-12 11:44:15 +0200957 }
Chris Wilson47356eb2011-01-11 17:06:04 +0000958
Jani Nikula3bd712e2013-11-08 16:48:59 +0200959 dev_priv->display.enable_backlight(connector);
Jani Nikula58c68772013-11-08 16:48:54 +0200960 panel->backlight.enabled = true;
Jani Nikulaab656bb2014-08-13 12:10:12 +0300961 if (panel->backlight.device)
962 panel->backlight.device->props.power = FB_BLANK_UNBLANK;
Jani Nikula8ba2d182013-04-12 15:18:37 +0300963
Jani Nikula58c68772013-11-08 16:48:54 +0200964 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
Chris Wilson47356eb2011-01-11 17:06:04 +0000965}
966
Jani Nikula912e8b12013-09-18 17:19:45 +0300967#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
Jani Nikuladb31af1d2013-11-08 16:48:53 +0200968static int intel_backlight_device_update_status(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200969{
Jesse Barnes752aa882013-10-31 18:55:49 +0200970 struct intel_connector *connector = bl_get_data(bd);
Jani Nikulaab656bb2014-08-13 12:10:12 +0300971 struct intel_panel *panel = &connector->panel;
Jesse Barnes752aa882013-10-31 18:55:49 +0200972 struct drm_device *dev = connector->base.dev;
973
Rob Clark51fd3712013-11-19 12:10:12 -0500974 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Chris Wilson540b5d02013-10-13 12:56:31 +0100975 DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
976 bd->props.brightness, bd->props.max_brightness);
Jesse Barnes752aa882013-10-31 18:55:49 +0200977 intel_panel_set_backlight(connector, bd->props.brightness,
Jani Nikulad6540632013-04-12 15:18:36 +0300978 bd->props.max_brightness);
Jani Nikulaab656bb2014-08-13 12:10:12 +0300979
980 /*
981 * Allow flipping bl_power as a sub-state of enabled. Sadly the
982 * backlight class device does not make it easy to to differentiate
983 * between callbacks for brightness and bl_power, so our backlight_power
984 * callback needs to take this into account.
985 */
986 if (panel->backlight.enabled) {
987 if (panel->backlight_power) {
Jani Nikulae6755fb2014-08-12 17:11:42 +0300988 bool enable = bd->props.power == FB_BLANK_UNBLANK &&
989 bd->props.brightness != 0;
Jani Nikulaab656bb2014-08-13 12:10:12 +0300990 panel->backlight_power(connector, enable);
991 }
992 } else {
993 bd->props.power = FB_BLANK_POWERDOWN;
994 }
995
Rob Clark51fd3712013-11-19 12:10:12 -0500996 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +0200997 return 0;
998}
999
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001000static int intel_backlight_device_get_brightness(struct backlight_device *bd)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001001{
Jesse Barnes752aa882013-10-31 18:55:49 +02001002 struct intel_connector *connector = bl_get_data(bd);
1003 struct drm_device *dev = connector->base.dev;
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001004 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula6dda7302014-06-24 18:27:40 +03001005 u32 hw_level;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001006 int ret;
Jesse Barnes752aa882013-10-31 18:55:49 +02001007
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001008 intel_runtime_pm_get(dev_priv);
Rob Clark51fd3712013-11-19 12:10:12 -05001009 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Jani Nikula6dda7302014-06-24 18:27:40 +03001010
1011 hw_level = intel_panel_get_backlight(connector);
1012 ret = scale_hw_to_user(connector, hw_level, bd->props.max_brightness);
1013
Rob Clark51fd3712013-11-19 12:10:12 -05001014 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Paulo Zanonic8c8fb32013-11-27 18:21:54 -02001015 intel_runtime_pm_put(dev_priv);
Jesse Barnes752aa882013-10-31 18:55:49 +02001016
Jani Nikula7bd688c2013-11-08 16:48:56 +02001017 return ret;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001018}
1019
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001020static const struct backlight_ops intel_backlight_device_ops = {
1021 .update_status = intel_backlight_device_update_status,
1022 .get_brightness = intel_backlight_device_get_brightness,
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001023};
1024
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001025static int intel_backlight_device_register(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001026{
Jani Nikula58c68772013-11-08 16:48:54 +02001027 struct intel_panel *panel = &connector->panel;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001028 struct backlight_properties props;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001029
Jani Nikula58c68772013-11-08 16:48:54 +02001030 if (WARN_ON(panel->backlight.device))
Jani Nikuladc652f92013-04-12 15:18:38 +03001031 return -ENODEV;
1032
Jani Nikula6dda7302014-06-24 18:27:40 +03001033 WARN_ON(panel->backlight.max == 0);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001034
Corentin Charyaf437cf2012-05-22 10:29:46 +01001035 memset(&props, 0, sizeof(props));
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001036 props.type = BACKLIGHT_RAW;
Jani Nikula6dda7302014-06-24 18:27:40 +03001037
1038 /*
1039 * Note: Everything should work even if the backlight device max
1040 * presented to the userspace is arbitrarily chosen.
1041 */
Jani Nikula7bd688c2013-11-08 16:48:56 +02001042 props.max_brightness = panel->backlight.max;
Jani Nikula6dda7302014-06-24 18:27:40 +03001043 props.brightness = scale_hw_to_user(connector,
1044 panel->backlight.level,
1045 props.max_brightness);
Jani Nikula58c68772013-11-08 16:48:54 +02001046
Jani Nikulaab656bb2014-08-13 12:10:12 +03001047 if (panel->backlight.enabled)
1048 props.power = FB_BLANK_UNBLANK;
1049 else
1050 props.power = FB_BLANK_POWERDOWN;
1051
Jani Nikula58c68772013-11-08 16:48:54 +02001052 /*
1053 * Note: using the same name independent of the connector prevents
1054 * registration of multiple backlight devices in the driver.
1055 */
1056 panel->backlight.device =
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001057 backlight_device_register("intel_backlight",
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001058 connector->base.kdev,
1059 connector,
1060 &intel_backlight_device_ops, &props);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001061
Jani Nikula58c68772013-11-08 16:48:54 +02001062 if (IS_ERR(panel->backlight.device)) {
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001063 DRM_ERROR("Failed to register backlight: %ld\n",
Jani Nikula58c68772013-11-08 16:48:54 +02001064 PTR_ERR(panel->backlight.device));
1065 panel->backlight.device = NULL;
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001066 return -ENODEV;
1067 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001068 return 0;
1069}
1070
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001071static void intel_backlight_device_unregister(struct intel_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001072{
Jani Nikula58c68772013-11-08 16:48:54 +02001073 struct intel_panel *panel = &connector->panel;
1074
1075 if (panel->backlight.device) {
1076 backlight_device_unregister(panel->backlight.device);
1077 panel->backlight.device = NULL;
Jani Nikuladc652f92013-04-12 15:18:38 +03001078 }
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001079}
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001080#else /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1081static int intel_backlight_device_register(struct intel_connector *connector)
1082{
1083 return 0;
1084}
1085static void intel_backlight_device_unregister(struct intel_connector *connector)
1086{
1087}
1088#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
1089
Jani Nikulaf91c15e2013-11-08 16:49:00 +02001090/*
1091 * Note: The setup hooks can't assume pipe is set!
1092 *
1093 * XXX: Query mode clock or hardware clock and program PWM modulation frequency
1094 * appropriately when it's 0. Use VBT and/or sane defaults.
1095 */
Jani Nikula6dda7302014-06-24 18:27:40 +03001096static u32 get_backlight_min_vbt(struct intel_connector *connector)
1097{
1098 struct drm_device *dev = connector->base.dev;
1099 struct drm_i915_private *dev_priv = dev->dev_private;
1100 struct intel_panel *panel = &connector->panel;
Jani Nikulae1c412e2014-11-05 14:46:31 +02001101 int min;
Jani Nikula6dda7302014-06-24 18:27:40 +03001102
1103 WARN_ON(panel->backlight.max == 0);
1104
Jani Nikulae1c412e2014-11-05 14:46:31 +02001105 /*
1106 * XXX: If the vbt value is 255, it makes min equal to max, which leads
1107 * to problems. There are such machines out there. Either our
1108 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
1109 * against this by letting the minimum be at most (arbitrarily chosen)
1110 * 25% of the max.
1111 */
1112 min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
1113 if (min != dev_priv->vbt.backlight.min_brightness) {
1114 DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
1115 dev_priv->vbt.backlight.min_brightness, min);
1116 }
1117
Jani Nikula6dda7302014-06-24 18:27:40 +03001118 /* vbt value is a coefficient in range [0..255] */
Jani Nikulae1c412e2014-11-05 14:46:31 +02001119 return scale(min, 0, 255, 0, panel->backlight.max);
Jani Nikula6dda7302014-06-24 18:27:40 +03001120}
1121
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001122static int bdw_setup_backlight(struct intel_connector *connector)
1123{
1124 struct drm_device *dev = connector->base.dev;
1125 struct drm_i915_private *dev_priv = dev->dev_private;
1126 struct intel_panel *panel = &connector->panel;
1127 u32 pch_ctl1, pch_ctl2, val;
1128
1129 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1130 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1131
1132 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1133 panel->backlight.max = pch_ctl2 >> 16;
1134 if (!panel->backlight.max)
1135 return -ENODEV;
1136
Jani Nikula6dda7302014-06-24 18:27:40 +03001137 panel->backlight.min = get_backlight_min_vbt(connector);
1138
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001139 val = bdw_get_backlight(connector);
1140 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1141
1142 panel->backlight.enabled = (pch_ctl1 & BLM_PCH_PWM_ENABLE) &&
1143 panel->backlight.level != 0;
1144
1145 return 0;
1146}
1147
Jani Nikula7bd688c2013-11-08 16:48:56 +02001148static int pch_setup_backlight(struct intel_connector *connector)
1149{
Jani Nikula636baeb2013-11-08 16:49:02 +02001150 struct drm_device *dev = connector->base.dev;
1151 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001152 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001153 u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001154
Jani Nikula636baeb2013-11-08 16:49:02 +02001155 pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
1156 panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
1157
1158 pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
1159 panel->backlight.max = pch_ctl2 >> 16;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001160 if (!panel->backlight.max)
1161 return -ENODEV;
1162
Jani Nikula6dda7302014-06-24 18:27:40 +03001163 panel->backlight.min = get_backlight_min_vbt(connector);
1164
Jani Nikula7bd688c2013-11-08 16:48:56 +02001165 val = pch_get_backlight(connector);
1166 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1167
Jani Nikula636baeb2013-11-08 16:49:02 +02001168 cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
1169 panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1170 (pch_ctl1 & BLM_PCH_PWM_ENABLE) && panel->backlight.level != 0;
1171
Jani Nikula7bd688c2013-11-08 16:48:56 +02001172 return 0;
1173}
1174
1175static int i9xx_setup_backlight(struct intel_connector *connector)
1176{
Jani Nikula636baeb2013-11-08 16:49:02 +02001177 struct drm_device *dev = connector->base.dev;
1178 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001179 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001180 u32 ctl, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001181
Jani Nikula636baeb2013-11-08 16:49:02 +02001182 ctl = I915_READ(BLC_PWM_CTL);
1183
Jani Nikulab6ab66a2014-02-25 13:11:47 +02001184 if (IS_GEN2(dev) || IS_I915GM(dev) || IS_I945GM(dev))
Jani Nikula636baeb2013-11-08 16:49:02 +02001185 panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
1186
1187 if (IS_PINEVIEW(dev))
1188 panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
1189
1190 panel->backlight.max = ctl >> 17;
1191 if (panel->backlight.combination_mode)
1192 panel->backlight.max *= 0xff;
1193
Jani Nikula7bd688c2013-11-08 16:48:56 +02001194 if (!panel->backlight.max)
1195 return -ENODEV;
1196
Jani Nikula6dda7302014-06-24 18:27:40 +03001197 panel->backlight.min = get_backlight_min_vbt(connector);
1198
Jani Nikula7bd688c2013-11-08 16:48:56 +02001199 val = i9xx_get_backlight(connector);
1200 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1201
Jani Nikula636baeb2013-11-08 16:49:02 +02001202 panel->backlight.enabled = panel->backlight.level != 0;
1203
Jani Nikula7bd688c2013-11-08 16:48:56 +02001204 return 0;
1205}
1206
1207static int i965_setup_backlight(struct intel_connector *connector)
1208{
Jani Nikula636baeb2013-11-08 16:49:02 +02001209 struct drm_device *dev = connector->base.dev;
1210 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001211 struct intel_panel *panel = &connector->panel;
Jani Nikula636baeb2013-11-08 16:49:02 +02001212 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001213
Jani Nikula636baeb2013-11-08 16:49:02 +02001214 ctl2 = I915_READ(BLC_PWM_CTL2);
1215 panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
1216 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1217
1218 ctl = I915_READ(BLC_PWM_CTL);
1219 panel->backlight.max = ctl >> 16;
1220 if (panel->backlight.combination_mode)
1221 panel->backlight.max *= 0xff;
1222
Jani Nikula7bd688c2013-11-08 16:48:56 +02001223 if (!panel->backlight.max)
1224 return -ENODEV;
1225
Jani Nikula6dda7302014-06-24 18:27:40 +03001226 panel->backlight.min = get_backlight_min_vbt(connector);
1227
Jani Nikula7bd688c2013-11-08 16:48:56 +02001228 val = i9xx_get_backlight(connector);
1229 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1230
Jani Nikula636baeb2013-11-08 16:49:02 +02001231 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1232 panel->backlight.level != 0;
1233
Jani Nikula7bd688c2013-11-08 16:48:56 +02001234 return 0;
1235}
1236
1237static int vlv_setup_backlight(struct intel_connector *connector)
1238{
1239 struct drm_device *dev = connector->base.dev;
1240 struct drm_i915_private *dev_priv = dev->dev_private;
1241 struct intel_panel *panel = &connector->panel;
1242 enum pipe pipe;
Jani Nikula636baeb2013-11-08 16:49:02 +02001243 u32 ctl, ctl2, val;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001244
Damien Lespiau055e3932014-08-18 13:49:10 +01001245 for_each_pipe(dev_priv, pipe) {
Jani Nikula7bd688c2013-11-08 16:48:56 +02001246 u32 cur_val = I915_READ(VLV_BLC_PWM_CTL(pipe));
1247
1248 /* Skip if the modulation freq is already set */
1249 if (cur_val & ~BACKLIGHT_DUTY_CYCLE_MASK)
1250 continue;
1251
1252 cur_val &= BACKLIGHT_DUTY_CYCLE_MASK;
1253 I915_WRITE(VLV_BLC_PWM_CTL(pipe), (0xf42 << 16) |
1254 cur_val);
1255 }
1256
Jani Nikula636baeb2013-11-08 16:49:02 +02001257 ctl2 = I915_READ(VLV_BLC_PWM_CTL2(PIPE_A));
1258 panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
1259
1260 ctl = I915_READ(VLV_BLC_PWM_CTL(PIPE_A));
1261 panel->backlight.max = ctl >> 16;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001262 if (!panel->backlight.max)
1263 return -ENODEV;
1264
Jani Nikula6dda7302014-06-24 18:27:40 +03001265 panel->backlight.min = get_backlight_min_vbt(connector);
1266
Jani Nikula7bd688c2013-11-08 16:48:56 +02001267 val = _vlv_get_backlight(dev, PIPE_A);
1268 panel->backlight.level = intel_panel_compute_brightness(connector, val);
1269
Jani Nikula636baeb2013-11-08 16:49:02 +02001270 panel->backlight.enabled = (ctl2 & BLM_PWM_ENABLE) &&
1271 panel->backlight.level != 0;
1272
Jani Nikula7bd688c2013-11-08 16:48:56 +02001273 return 0;
1274}
1275
Jani Nikula0657b6b2012-10-19 14:51:46 +03001276int intel_panel_setup_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001277{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001278 struct drm_device *dev = connector->dev;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001279 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001280 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikula58c68772013-11-08 16:48:54 +02001281 struct intel_panel *panel = &intel_connector->panel;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001282 unsigned long flags;
1283 int ret;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001284
Jani Nikulac6759492014-04-09 11:31:37 +03001285 if (!dev_priv->vbt.backlight.present) {
Scot Doyle9c72cc62014-07-03 23:27:50 +00001286 if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1287 DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
1288 } else {
1289 DRM_DEBUG_KMS("no backlight present per VBT\n");
1290 return 0;
1291 }
Jani Nikulac6759492014-04-09 11:31:37 +03001292 }
1293
Jani Nikula7bd688c2013-11-08 16:48:56 +02001294 /* set level and max in panel struct */
1295 spin_lock_irqsave(&dev_priv->backlight_lock, flags);
1296 ret = dev_priv->display.setup_backlight(intel_connector);
1297 spin_unlock_irqrestore(&dev_priv->backlight_lock, flags);
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001298
Jani Nikula7bd688c2013-11-08 16:48:56 +02001299 if (ret) {
1300 DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001301 connector->name);
Jani Nikula7bd688c2013-11-08 16:48:56 +02001302 return ret;
1303 }
1304
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001305 intel_backlight_device_register(intel_connector);
1306
Jani Nikulac91c9f32013-11-08 16:48:55 +02001307 panel->backlight.present = true;
1308
Jani Nikulac445b3b2013-11-08 16:49:01 +02001309 DRM_DEBUG_KMS("backlight initialized, %s, brightness %u/%u, "
1310 "sysfs interface %sregistered\n",
1311 panel->backlight.enabled ? "enabled" : "disabled",
1312 panel->backlight.level, panel->backlight.max,
1313 panel->backlight.device ? "" : "not ");
1314
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001315 return 0;
1316}
1317
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001318void intel_panel_destroy_backlight(struct drm_connector *connector)
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001319{
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001320 struct intel_connector *intel_connector = to_intel_connector(connector);
Jani Nikulac91c9f32013-11-08 16:48:55 +02001321 struct intel_panel *panel = &intel_connector->panel;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001322
Jani Nikulac91c9f32013-11-08 16:48:55 +02001323 panel->backlight.present = false;
Jani Nikuladb31af1d2013-11-08 16:48:53 +02001324 intel_backlight_device_unregister(intel_connector);
Matthew Garrettaaa6fd22011-08-12 12:11:33 +02001325}
Jani Nikula1d508702012-10-19 14:51:49 +03001326
Jani Nikula7bd688c2013-11-08 16:48:56 +02001327/* Set up chip specific backlight functions */
1328void intel_panel_init_backlight_funcs(struct drm_device *dev)
1329{
1330 struct drm_i915_private *dev_priv = dev->dev_private;
1331
Daniel Vetter96ab4c72013-11-14 15:17:41 +01001332 if (IS_BROADWELL(dev)) {
1333 dev_priv->display.setup_backlight = bdw_setup_backlight;
1334 dev_priv->display.enable_backlight = bdw_enable_backlight;
1335 dev_priv->display.disable_backlight = pch_disable_backlight;
1336 dev_priv->display.set_backlight = bdw_set_backlight;
1337 dev_priv->display.get_backlight = bdw_get_backlight;
1338 } else if (HAS_PCH_SPLIT(dev)) {
Jani Nikula7bd688c2013-11-08 16:48:56 +02001339 dev_priv->display.setup_backlight = pch_setup_backlight;
1340 dev_priv->display.enable_backlight = pch_enable_backlight;
1341 dev_priv->display.disable_backlight = pch_disable_backlight;
1342 dev_priv->display.set_backlight = pch_set_backlight;
1343 dev_priv->display.get_backlight = pch_get_backlight;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001344 } else if (IS_VALLEYVIEW(dev)) {
1345 dev_priv->display.setup_backlight = vlv_setup_backlight;
1346 dev_priv->display.enable_backlight = vlv_enable_backlight;
1347 dev_priv->display.disable_backlight = vlv_disable_backlight;
1348 dev_priv->display.set_backlight = vlv_set_backlight;
1349 dev_priv->display.get_backlight = vlv_get_backlight;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001350 } else if (IS_GEN4(dev)) {
1351 dev_priv->display.setup_backlight = i965_setup_backlight;
1352 dev_priv->display.enable_backlight = i965_enable_backlight;
1353 dev_priv->display.disable_backlight = i965_disable_backlight;
1354 dev_priv->display.set_backlight = i9xx_set_backlight;
1355 dev_priv->display.get_backlight = i9xx_get_backlight;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001356 } else {
1357 dev_priv->display.setup_backlight = i9xx_setup_backlight;
Jani Nikula3bd712e2013-11-08 16:48:59 +02001358 dev_priv->display.enable_backlight = i9xx_enable_backlight;
1359 dev_priv->display.disable_backlight = i9xx_disable_backlight;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001360 dev_priv->display.set_backlight = i9xx_set_backlight;
1361 dev_priv->display.get_backlight = i9xx_get_backlight;
Jani Nikula7bd688c2013-11-08 16:48:56 +02001362 }
1363}
1364
Jani Nikuladd06f902012-10-19 14:51:50 +03001365int intel_panel_init(struct intel_panel *panel,
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301366 struct drm_display_mode *fixed_mode,
1367 struct drm_display_mode *downclock_mode)
Jani Nikula1d508702012-10-19 14:51:49 +03001368{
Jani Nikuladd06f902012-10-19 14:51:50 +03001369 panel->fixed_mode = fixed_mode;
Vandana Kannan4b6ed682014-02-11 14:26:36 +05301370 panel->downclock_mode = downclock_mode;
Jani Nikuladd06f902012-10-19 14:51:50 +03001371
Jani Nikula1d508702012-10-19 14:51:49 +03001372 return 0;
1373}
1374
1375void intel_panel_fini(struct intel_panel *panel)
1376{
Jani Nikuladd06f902012-10-19 14:51:50 +03001377 struct intel_connector *intel_connector =
1378 container_of(panel, struct intel_connector, panel);
1379
1380 if (panel->fixed_mode)
1381 drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
Vandana Kannanec9ed192013-12-10 13:37:36 +05301382
1383 if (panel->downclock_mode)
1384 drm_mode_destroy(intel_connector->base.dev,
1385 panel->downclock_mode);
Jani Nikula1d508702012-10-19 14:51:49 +03001386}