intel_panel.c 65.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright © 2006-2010 Intel Corporation
 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *	Eric Anholt <eric@anholt.net>
 *      Dave Airlie <airlied@linux.ie>
 *      Jesse Barnes <jesse.barnes@intel.com>
 *      Chris Wilson <chris@chris-wilson.co.uk>
 */

31 32
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

33
#include <linux/kernel.h>
34
#include <linux/moduleparam.h>
35
#include <linux/pwm.h>
36 37

#include "intel_connector.h"
38
#include "intel_display_types.h"
39
#include "intel_dp_aux_backlight.h"
40
#include "intel_dsi_dcs_backlight.h"
41
#include "intel_panel.h"
42

43 44
#define CRC_PMIC_PWM_PERIOD_NS	21333

45
void
46
intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
47 48
		       struct drm_display_mode *adjusted_mode)
{
49
	drm_mode_copy(adjusted_mode, fixed_mode);
50 51

	drm_mode_set_crtcinfo(adjusted_mode, 0);
52 53
}

54 55 56 57 58 59 60 61 62 63
static bool is_downclock_mode(const struct drm_display_mode *downclock_mode,
			      const struct drm_display_mode *fixed_mode)
{
	return drm_mode_match(downclock_mode, fixed_mode,
			      DRM_MODE_MATCH_TIMINGS |
			      DRM_MODE_MATCH_FLAGS |
			      DRM_MODE_MATCH_3D_FLAGS) &&
		downclock_mode->clock < fixed_mode->clock;
}

J
Jani Nikula 已提交
64
struct drm_display_mode *
65 66
intel_panel_edid_downclock_mode(struct intel_connector *connector,
				const struct drm_display_mode *fixed_mode)
J
Jani Nikula 已提交
67
{
68
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
69
	const struct drm_display_mode *scan, *best_mode = NULL;
70
	struct drm_display_mode *downclock_mode;
71
	int best_clock = fixed_mode->clock;
J
Jani Nikula 已提交
72

73
	list_for_each_entry(scan, &connector->base.probed_modes, head) {
J
Jani Nikula 已提交
74 75 76 77 78 79 80
		/*
		 * If one mode has the same resolution with the fixed_panel
		 * mode while they have the different refresh rate, it means
		 * that the reduced downclock is found. In such
		 * case we can set the different FPx0/1 to dynamically select
		 * between low and high frequency.
		 */
81 82 83 84 85 86 87 88
		if (is_downclock_mode(scan, fixed_mode) &&
		    scan->clock < best_clock) {
			/*
			 * The downclock is already found. But we
			 * expect to find the lower downclock.
			 */
			best_clock = scan->clock;
			best_mode = scan;
J
Jani Nikula 已提交
89 90 91
		}
	}

92 93 94 95 96 97 98
	if (!best_mode)
		return NULL;

	downclock_mode = drm_mode_duplicate(&dev_priv->drm, best_mode);
	if (!downclock_mode)
		return NULL;

99 100 101
	drm_dbg_kms(&dev_priv->drm,
		    "[CONNECTOR:%d:%s] using downclock mode from EDID: ",
		    connector->base.base.id, connector->base.name);
102
	drm_mode_debug_printmodeline(downclock_mode);
103

104
	return downclock_mode;
J
Jani Nikula 已提交
105 106
}

107 108 109 110 111
struct drm_display_mode *
intel_panel_edid_fixed_mode(struct intel_connector *connector)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	const struct drm_display_mode *scan;
112 113 114 115
	struct drm_display_mode *fixed_mode;

	if (list_empty(&connector->base.probed_modes))
		return NULL;
116 117 118 119 120 121 122 123 124 125

	/* prefer fixed mode from EDID if available */
	list_for_each_entry(scan, &connector->base.probed_modes, head) {
		if ((scan->type & DRM_MODE_TYPE_PREFERRED) == 0)
			continue;

		fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
		if (!fixed_mode)
			return NULL;

126 127 128
		drm_dbg_kms(&dev_priv->drm,
			    "[CONNECTOR:%d:%s] using preferred mode from EDID: ",
			    connector->base.base.id, connector->base.name);
129 130 131 132 133
		drm_mode_debug_printmodeline(fixed_mode);

		return fixed_mode;
	}

134 135 136 137 138 139 140 141 142
	scan = list_first_entry(&connector->base.probed_modes,
				typeof(*scan), head);

	fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
	if (!fixed_mode)
		return NULL;

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

143 144 145
	drm_dbg_kms(&dev_priv->drm,
		    "[CONNECTOR:%d:%s] using first mode from EDID: ",
		    connector->base.base.id, connector->base.name);
146 147 148
	drm_mode_debug_printmodeline(fixed_mode);

	return fixed_mode;
149 150
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
struct drm_display_mode *
intel_panel_vbt_fixed_mode(struct intel_connector *connector)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct drm_display_info *info = &connector->base.display_info;
	struct drm_display_mode *fixed_mode;

	if (!dev_priv->vbt.lfp_lvds_vbt_mode)
		return NULL;

	fixed_mode = drm_mode_duplicate(&dev_priv->drm,
					dev_priv->vbt.lfp_lvds_vbt_mode);
	if (!fixed_mode)
		return NULL;

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

168 169
	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s] using mode from VBT: ",
		    connector->base.base.id, connector->base.name);
170 171 172 173 174 175 176 177
	drm_mode_debug_printmodeline(fixed_mode);

	info->width_mm = fixed_mode->width_mm;
	info->height_mm = fixed_mode->height_mm;

	return fixed_mode;
}

178 179
/* adjusted_mode has been preset to be the panel's fixed mode */
void
180
intel_pch_panel_fitting(struct intel_crtc_state *crtc_state,
181
			const struct drm_connector_state *conn_state)
182
{
183 184
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->hw.adjusted_mode;
185
	int x, y, width, height;
186 187

	/* Native modes don't need fitting */
188 189 190
	if (adjusted_mode->crtc_hdisplay == crtc_state->pipe_src_w &&
	    adjusted_mode->crtc_vdisplay == crtc_state->pipe_src_h &&
	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_YCBCR420)
191
		return;
192

193
	switch (conn_state->scaling_mode) {
194
	case DRM_MODE_SCALE_CENTER:
195 196
		width = crtc_state->pipe_src_w;
		height = crtc_state->pipe_src_h;
197 198
		x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
		y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
199 200 201 202 203
		break;

	case DRM_MODE_SCALE_ASPECT:
		/* Scale but preserve the aspect ratio */
		{
204
			u32 scaled_width = adjusted_mode->crtc_hdisplay
205 206
				* crtc_state->pipe_src_h;
			u32 scaled_height = crtc_state->pipe_src_w
207
				* adjusted_mode->crtc_vdisplay;
208
			if (scaled_width > scaled_height) { /* pillar */
209
				width = scaled_height / crtc_state->pipe_src_h;
210
				if (width & 1)
211
					width++;
212
				x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
213
				y = 0;
214
				height = adjusted_mode->crtc_vdisplay;
215
			} else if (scaled_width < scaled_height) { /* letter */
216
				height = scaled_width / crtc_state->pipe_src_w;
217 218
				if (height & 1)
				    height++;
219
				y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
220
				x = 0;
221
				width = adjusted_mode->crtc_hdisplay;
222 223
			} else {
				x = y = 0;
224 225
				width = adjusted_mode->crtc_hdisplay;
				height = adjusted_mode->crtc_vdisplay;
226 227 228 229
			}
		}
		break;

230 231 232 233
	case DRM_MODE_SCALE_NONE:
		WARN_ON(adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w);
		WARN_ON(adjusted_mode->crtc_vdisplay != crtc_state->pipe_src_h);
		/* fall through */
234 235
	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
236 237
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
238
		break;
239 240

	default:
241
		MISSING_CASE(conn_state->scaling_mode);
242
		return;
243 244
	}

245
	drm_rect_init(&crtc_state->pch_pfit.dst,
246
		      x, y, width, height);
247
	crtc_state->pch_pfit.enabled = true;
248
}
249

250
static void
251
centre_horizontally(struct drm_display_mode *adjusted_mode,
252 253 254 255 256
		    int width)
{
	u32 border, sync_pos, blank_width, sync_width;

	/* keep the hsync and hblank widths constant */
257 258
	sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
	blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
259 260
	sync_pos = (blank_width - sync_width + 1) / 2;

261
	border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
262 263
	border += border & 1; /* make the border even */

264 265 266
	adjusted_mode->crtc_hdisplay = width;
	adjusted_mode->crtc_hblank_start = width + border;
	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
267

268 269
	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
270 271 272
}

static void
273
centre_vertically(struct drm_display_mode *adjusted_mode,
274 275 276 277 278
		  int height)
{
	u32 border, sync_pos, blank_width, sync_width;

	/* keep the vsync and vblank widths constant */
279 280
	sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
	blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
281 282
	sync_pos = (blank_width - sync_width + 1) / 2;

283
	border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
284

285 286 287
	adjusted_mode->crtc_vdisplay = height;
	adjusted_mode->crtc_vblank_start = height + border;
	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
288

289 290
	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
291 292
}

293
static u32 panel_fitter_scaling(u32 source, u32 target)
294 295 296 297 298 299 300 301 302 303 304 305
{
	/*
	 * Floating point operation is not supported. So the FACTOR
	 * is defined, which can avoid the floating point computation
	 * when calculating the panel ratio.
	 */
#define ACCURACY 12
#define FACTOR (1 << ACCURACY)
	u32 ratio = source * FACTOR / target;
	return (FACTOR * ratio + FACTOR/2) / FACTOR;
}

306
static void i965_scale_aspect(struct intel_crtc_state *crtc_state,
307 308
			      u32 *pfit_control)
{
309 310
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->hw.adjusted_mode;
311
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
312 313
		crtc_state->pipe_src_h;
	u32 scaled_height = crtc_state->pipe_src_w *
314
		adjusted_mode->crtc_vdisplay;
315 316 317 318 319 320 321 322

	/* 965+ is easy, it does everything in hw */
	if (scaled_width > scaled_height)
		*pfit_control |= PFIT_ENABLE |
			PFIT_SCALING_PILLAR;
	else if (scaled_width < scaled_height)
		*pfit_control |= PFIT_ENABLE |
			PFIT_SCALING_LETTER;
323
	else if (adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w)
324 325 326
		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
}

327
static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
328 329 330
			      u32 *pfit_control, u32 *pfit_pgm_ratios,
			      u32 *border)
{
331
	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
332
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
333 334
		crtc_state->pipe_src_h;
	u32 scaled_height = crtc_state->pipe_src_w *
335
		adjusted_mode->crtc_vdisplay;
336 337 338 339 340 341 342 343 344 345
	u32 bits;

	/*
	 * For earlier chips we have to calculate the scaling
	 * ratio by hand and program it into the
	 * PFIT_PGM_RATIO register
	 */
	if (scaled_width > scaled_height) { /* pillar */
		centre_horizontally(adjusted_mode,
				    scaled_height /
346
				    crtc_state->pipe_src_h);
347 348

		*border = LVDS_BORDER_ENABLE;
349 350
		if (crtc_state->pipe_src_h != adjusted_mode->crtc_vdisplay) {
			bits = panel_fitter_scaling(crtc_state->pipe_src_h,
351
						    adjusted_mode->crtc_vdisplay);
352 353 354 355 356 357 358 359 360 361

			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
					     bits << PFIT_VERT_SCALE_SHIFT);
			*pfit_control |= (PFIT_ENABLE |
					  VERT_INTERP_BILINEAR |
					  HORIZ_INTERP_BILINEAR);
		}
	} else if (scaled_width < scaled_height) { /* letter */
		centre_vertically(adjusted_mode,
				  scaled_width /
362
				  crtc_state->pipe_src_w);
363 364

		*border = LVDS_BORDER_ENABLE;
365 366
		if (crtc_state->pipe_src_w != adjusted_mode->crtc_hdisplay) {
			bits = panel_fitter_scaling(crtc_state->pipe_src_w,
367
						    adjusted_mode->crtc_hdisplay);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
					     bits << PFIT_VERT_SCALE_SHIFT);
			*pfit_control |= (PFIT_ENABLE |
					  VERT_INTERP_BILINEAR |
					  HORIZ_INTERP_BILINEAR);
		}
	} else {
		/* Aspects match, Let hw scale both directions */
		*pfit_control |= (PFIT_ENABLE |
				  VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
				  VERT_INTERP_BILINEAR |
				  HORIZ_INTERP_BILINEAR);
	}
}

384
void intel_gmch_panel_fitting(struct intel_crtc_state *crtc_state,
385
			      const struct drm_connector_state *conn_state)
386
{
387 388
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
389
	u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
390
	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
391 392

	/* Native modes don't need fitting */
393 394
	if (adjusted_mode->crtc_hdisplay == crtc_state->pipe_src_w &&
	    adjusted_mode->crtc_vdisplay == crtc_state->pipe_src_h)
395 396
		goto out;

397
	switch (conn_state->scaling_mode) {
398 399 400 401 402
	case DRM_MODE_SCALE_CENTER:
		/*
		 * For centered modes, we have to calculate border widths &
		 * heights and modify the values programmed into the CRTC.
		 */
403 404
		centre_horizontally(adjusted_mode, crtc_state->pipe_src_w);
		centre_vertically(adjusted_mode, crtc_state->pipe_src_h);
405 406 407 408
		border = LVDS_BORDER_ENABLE;
		break;
	case DRM_MODE_SCALE_ASPECT:
		/* Scale but preserve the aspect ratio */
409
		if (INTEL_GEN(dev_priv) >= 4)
410
			i965_scale_aspect(crtc_state, &pfit_control);
411
		else
412
			i9xx_scale_aspect(crtc_state, &pfit_control,
413
					  &pfit_pgm_ratios, &border);
414 415 416 417 418 419
		break;
	case DRM_MODE_SCALE_FULLSCREEN:
		/*
		 * Full scaling, even if it changes the aspect ratio.
		 * Fortunately this is all done for us in hw.
		 */
420 421
		if (crtc_state->pipe_src_h != adjusted_mode->crtc_vdisplay ||
		    crtc_state->pipe_src_w != adjusted_mode->crtc_hdisplay) {
422
			pfit_control |= PFIT_ENABLE;
423
			if (INTEL_GEN(dev_priv) >= 4)
424 425 426 427 428 429 430 431
				pfit_control |= PFIT_SCALING_AUTO;
			else
				pfit_control |= (VERT_AUTO_SCALE |
						 VERT_INTERP_BILINEAR |
						 HORIZ_AUTO_SCALE |
						 HORIZ_INTERP_BILINEAR);
		}
		break;
432
	default:
433
		MISSING_CASE(conn_state->scaling_mode);
434
		return;
435 436 437 438
	}

	/* 965+ wants fuzzy fitting */
	/* FIXME: handle multiple panels by failing gracefully */
439
	if (INTEL_GEN(dev_priv) >= 4)
440
		pfit_control |= PFIT_PIPE(crtc->pipe) | PFIT_FILTER_FUZZY;
441 442 443 444 445 446 447

out:
	if ((pfit_control & PFIT_ENABLE) == 0) {
		pfit_control = 0;
		pfit_pgm_ratios = 0;
	}

448
	/* Make sure pre-965 set dither correctly for 18bpp panels. */
449
	if (INTEL_GEN(dev_priv) < 4 && crtc_state->pipe_bpp == 18)
450 451
		pfit_control |= PANEL_8TO6_DITHER_ENABLE;

452 453 454
	crtc_state->gmch_pfit.control = pfit_control;
	crtc_state->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
	crtc_state->gmch_pfit.lvds_border_bits = border;
455 456
}

457 458 459
/**
 * scale - scale values from one range to another
 * @source_val: value in range [@source_min..@source_max]
460 461 462 463
 * @source_min: minimum legal value for @source_val
 * @source_max: maximum legal value for @source_val
 * @target_min: corresponding target value for @source_min
 * @target_max: corresponding target value for @source_max
464 465 466 467
 *
 * Return @source_val in range [@source_min..@source_max] scaled to range
 * [@target_min..@target_max].
 */
468 469 470
static u32 scale(u32 source_val,
		 u32 source_min, u32 source_max,
		 u32 target_min, u32 target_max)
471
{
472
	u64 target_val;
473 474 475 476 477 478 479 480

	WARN_ON(source_min > source_max);
	WARN_ON(target_min > target_max);

	/* defensive */
	source_val = clamp(source_val, source_min, source_max);

	/* avoid overflows */
481 482 483
	target_val = mul_u32_u32(source_val - source_min,
				 target_max - target_min);
	target_val = DIV_ROUND_CLOSEST_ULL(target_val, source_max - source_min);
484 485 486 487 488 489
	target_val += target_min;

	return target_val;
}

/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
490 491
static u32 scale_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
492 493 494 495 496 497 498 499 500
{
	struct intel_panel *panel = &connector->panel;

	return scale(user_level, 0, user_max,
		     panel->backlight.min, panel->backlight.max);
}

/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
 * to [hw_min..hw_max]. */
501 502
static u32 clamp_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
503 504 505 506 507 508 509 510 511 512 513
{
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

	hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
	hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);

	return hw_level;
}

/* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
514 515
static u32 scale_hw_to_user(struct intel_connector *connector,
			    u32 hw_level, u32 user_max)
516 517 518 519 520 521 522
{
	struct intel_panel *panel = &connector->panel;

	return scale(hw_level, panel->backlight.min, panel->backlight.max,
		     0, user_max);
}

523 524
static u32 intel_panel_compute_brightness(struct intel_connector *connector,
					  u32 val)
525
{
526
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
527 528
	struct intel_panel *panel = &connector->panel;

529
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
530

531
	if (i915_modparams.invert_brightness < 0)
532 533
		return val;

534
	if (i915_modparams.invert_brightness > 0 ||
535
	    dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
536
		return panel->backlight.max - val + panel->backlight.min;
537
	}
538 539 540 541

	return val;
}

542
static u32 lpt_get_backlight(struct intel_connector *connector)
543
{
544
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
545

546
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
547
}
548

549
static u32 pch_get_backlight(struct intel_connector *connector)
550
{
551
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
552

553
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
554
}
555

556 557
static u32 i9xx_get_backlight(struct intel_connector *connector)
{
558
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
559
	struct intel_panel *panel = &connector->panel;
560
	u32 val;
561

562
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
563
	if (INTEL_GEN(dev_priv) < 4)
564
		val >>= 1;
565

566
	if (panel->backlight.combination_mode) {
567
		u8 lbpc;
568

569
		pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
570
		val *= lbpc;
571 572
	}

573 574 575
	return val;
}

576
static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
577
{
578
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
579 580
		return 0;

581
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
582 583 584 585
}

static u32 vlv_get_backlight(struct intel_connector *connector)
{
586
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
587
	enum pipe pipe = intel_connector_get_pipe(connector);
588

589
	return _vlv_get_backlight(dev_priv, pipe);
590 591
}

592 593
static u32 bxt_get_backlight(struct intel_connector *connector)
{
594
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
595
	struct intel_panel *panel = &connector->panel;
596

597 598
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
599 600
}

601 602 603 604 605 606 607 608 609
static u32 pwm_get_backlight(struct intel_connector *connector)
{
	struct intel_panel *panel = &connector->panel;
	int duty_ns;

	duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
	return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
}

610
static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
611
{
612
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
613
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
614

615 616
	u32 val = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, val | level);
617 618
}

619
static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
620
{
621
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
622
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
623 624
	u32 tmp;

625 626
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL, tmp | level);
627 628
}

629
static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
630
{
631
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
632
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
633
	struct intel_panel *panel = &connector->panel;
634
	u32 tmp, mask;
635

636
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
637

638
	if (panel->backlight.combination_mode) {
639 640
		u8 lbpc;

641
		lbpc = level * 0xfe / panel->backlight.max + 1;
642
		level /= lbpc;
643
		pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
644 645
	}

646
	if (IS_GEN(dev_priv, 4)) {
647 648
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
649
		level <<= 1;
650 651
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
652

653 654
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
655 656
}

657
static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
658
{
659
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
660
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
661
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
662 663
	u32 tmp;

664 665
	tmp = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), tmp | level);
666 667
}

668
static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
669
{
670
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
671
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
672
	struct intel_panel *panel = &connector->panel;
673

674 675
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
676 677
}

678
static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
679
{
680
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
681 682 683 684 685
	int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);

	pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
}

686
static void
687
intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
688
{
689
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
690
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
691
	struct intel_panel *panel = &connector->panel;
692

693
	drm_dbg_kms(&i915->drm, "set backlight PWM = %d\n", level);
694 695

	level = intel_panel_compute_brightness(connector, level);
696
	panel->backlight.set(conn_state, level);
697
}
698

699 700 701
/* set backlight brightness to level in range [0..max], assuming hw min is
 * respected.
 */
702
void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
703 704
				    u32 user_level, u32 user_max)
{
705
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
706
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
707 708 709
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

710
	/*
711
	 * Lack of crtc may occur during driver init because
712 713 714 715
	 * connection_mutex isn't held across the entire backlight
	 * setup + modeset readout, and the BIOS can issue the
	 * requests at any time.
	 */
716
	if (!panel->backlight.present || !conn_state->crtc)
717 718
		return;

719
	mutex_lock(&dev_priv->backlight_lock);
720

721
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
722 723 724

	hw_level = clamp_user_to_hw(connector, user_level, user_max);
	panel->backlight.level = hw_level;
725

726
	if (panel->backlight.device)
727 728 729 730
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
731

732
	if (panel->backlight.enabled)
733
		intel_panel_actually_set_backlight(conn_state, hw_level);
734

735
	mutex_unlock(&dev_priv->backlight_lock);
736 737
}

738
static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
739
{
740
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
741
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
742 743
	u32 tmp;

744
	intel_panel_actually_set_backlight(old_conn_state, 0);
745

746 747 748 749 750 751 752 753
	/*
	 * Although we don't support or enable CPU PWM with LPT/SPT based
	 * systems, it may have been enabled prior to loading the
	 * driver. Disable to avoid warnings on LCPLL disable.
	 *
	 * This needs rework if we need to add support for CPU PWM on PCH split
	 * platforms.
	 */
754
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
755
	if (tmp & BLM_PWM_ENABLE) {
756 757
		drm_dbg_kms(&dev_priv->drm,
			    "cpu backlight was enabled, disabling\n");
758 759
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       tmp & ~BLM_PWM_ENABLE);
760 761
	}

762 763
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
764 765
}

766
static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
767
{
768
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
769
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
770 771
	u32 tmp;

772
	intel_panel_actually_set_backlight(old_conn_state, 0);
773

774 775
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
776

777 778
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
779 780
}

781
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
782
{
783
	intel_panel_actually_set_backlight(old_conn_state, 0);
784 785
}

786
static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
787
{
788
	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
789 790
	u32 tmp;

791
	intel_panel_actually_set_backlight(old_conn_state, 0);
792

793 794
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
795 796
}

797
static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
798
{
799
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
800
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
801
	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
802 803
	u32 tmp;

804
	intel_panel_actually_set_backlight(old_conn_state, 0);
805

806 807 808
	tmp = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe),
		       tmp & ~BLM_PWM_ENABLE);
809 810
}

811
static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
812
{
813
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
814
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
815 816
	struct intel_panel *panel = &connector->panel;
	u32 tmp, val;
817

818
	intel_panel_actually_set_backlight(old_conn_state, 0);
819

820 821 822 823
	tmp = intel_de_read(dev_priv,
			    BXT_BLC_PWM_CTL(panel->backlight.controller));
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       tmp & ~BXT_BLC_PWM_ENABLE);
824 825

	if (panel->backlight.controller == 1) {
826
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
827
		val &= ~UTIL_PIN_ENABLE;
828
		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
829
	}
830 831
}

832
static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
833
{
834
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
835 836 837 838
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 tmp;

839
	intel_panel_actually_set_backlight(old_conn_state, 0);
840

841 842 843 844
	tmp = intel_de_read(dev_priv,
			    BXT_BLC_PWM_CTL(panel->backlight.controller));
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       tmp & ~BXT_BLC_PWM_ENABLE);
845 846
}

847
static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
848
{
849
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
850 851 852
	struct intel_panel *panel = &connector->panel;

	/* Disable the backlight */
853
	intel_panel_actually_set_backlight(old_conn_state, 0);
854 855 856 857
	usleep_range(2000, 3000);
	pwm_disable(panel->backlight.pwm);
}

858
void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
859
{
860
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
861
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
862
	struct intel_panel *panel = &connector->panel;
863

864
	if (!panel->backlight.present)
865 866
		return;

867
	/*
868
	 * Do not disable backlight on the vga_switcheroo path. When switching
869 870 871 872
	 * away from i915, the other client may depend on i915 to handle the
	 * backlight. This will leave the backlight on unnecessarily when
	 * another client is not activated.
	 */
873
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
874 875
		drm_dbg_kms(&dev_priv->drm,
			    "Skipping backlight disable on vga switch\n");
876 877 878
		return;
	}

879
	mutex_lock(&dev_priv->backlight_lock);
880

881 882
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
883
	panel->backlight.enabled = false;
884
	panel->backlight.disable(old_conn_state);
885

886
	mutex_unlock(&dev_priv->backlight_lock);
887
}
888

889 890
static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
891
{
892
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
893
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
894
	struct intel_panel *panel = &connector->panel;
895
	u32 pch_ctl1, pch_ctl2, schicken;
896

897
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
898
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
899
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
900
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
901
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
902
	}
903

904
	if (HAS_PCH_LPT(dev_priv)) {
905
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN2);
906 907 908 909
		if (panel->backlight.alternate_pwm_increment)
			schicken |= LPT_PWM_GRANULARITY;
		else
			schicken &= ~LPT_PWM_GRANULARITY;
910
		intel_de_write(dev_priv, SOUTH_CHICKEN2, schicken);
911
	} else {
912
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN1);
913 914 915 916
		if (panel->backlight.alternate_pwm_increment)
			schicken |= SPT_PWM_GRANULARITY;
		else
			schicken &= ~SPT_PWM_GRANULARITY;
917
		intel_de_write(dev_priv, SOUTH_CHICKEN1, schicken);
918 919
	}

920
	pch_ctl2 = panel->backlight.max << 16;
921
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
922

923 924 925
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
926

927 928 929
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
930

931 932 933 934
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
	intel_de_posting_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
		       pch_ctl1 | BLM_PCH_PWM_ENABLE);
935 936

	/* This won't stick until the above enable. */
937
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
938 939
}

940 941
static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
942
{
943
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
944
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
945
	struct intel_panel *panel = &connector->panel;
946
	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
947
	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
948

949
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
950
	if (cpu_ctl2 & BLM_PWM_ENABLE) {
951
		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
952
		cpu_ctl2 &= ~BLM_PWM_ENABLE;
953
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
954
	}
955

956
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
957
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
958
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
959
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
960
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
961
	}
962 963

	if (cpu_transcoder == TRANSCODER_EDP)
964
		cpu_ctl2 = BLM_TRANSCODER_EDP;
965
	else
966
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
967 968 969
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
	intel_de_posting_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
970

971
	/* This won't stick until the above enable. */
972
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
973 974

	pch_ctl2 = panel->backlight.max << 16;
975
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
976 977 978 979

	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
980

981 982 983 984
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
	intel_de_posting_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
		       pch_ctl1 | BLM_PCH_PWM_ENABLE);
985 986
}

987 988
static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
989
{
990
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
991
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
992
	struct intel_panel *panel = &connector->panel;
993 994
	u32 ctl, freq;

995
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
996
	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
997
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
998
		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
999
	}
1000

1001 1002 1003 1004 1005
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;

	ctl = freq << 17;
1006
	if (panel->backlight.combination_mode)
1007
		ctl |= BLM_LEGACY_MODE;
1008
	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
1009 1010
		ctl |= BLM_POLARITY_PNV;

1011 1012
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1013 1014

	/* XXX: combine this into above write? */
1015
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1016 1017 1018 1019 1020 1021

	/*
	 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
	 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
	 * that has backlight.
	 */
1022
	if (IS_GEN(dev_priv, 2))
1023
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1024
}
1025

1026 1027
static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
1028
{
1029
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1030
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1031
	struct intel_panel *panel = &connector->panel;
1032
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
1033
	u32 ctl, ctl2, freq;
1034

1035
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1036
	if (ctl2 & BLM_PWM_ENABLE) {
1037
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1038
		ctl2 &= ~BLM_PWM_ENABLE;
1039
		intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
1040
	}
1041

1042 1043 1044
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1045

1046
	ctl = freq << 16;
1047
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1048

1049 1050 1051 1052 1053
	ctl2 = BLM_PIPE(pipe);
	if (panel->backlight.combination_mode)
		ctl2 |= BLM_COMBINATION_MODE;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1054 1055 1056
	intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
1057

1058
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1059 1060
}

1061 1062
static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1063
{
1064
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1065
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1066
	struct intel_panel *panel = &connector->panel;
1067
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1068
	u32 ctl, ctl2;
1069

1070
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1071
	if (ctl2 & BLM_PWM_ENABLE) {
1072
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1073
		ctl2 &= ~BLM_PWM_ENABLE;
1074
		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
1075
	}
1076

1077
	ctl = panel->backlight.max << 16;
1078
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1079

1080
	/* XXX: combine this into above write? */
1081
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1082

1083 1084 1085
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1086 1087 1088 1089
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
	intel_de_posting_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe),
		       ctl2 | BLM_PWM_ENABLE);
1090 1091
}

1092 1093
static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1094
{
1095
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1096
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1097
	struct intel_panel *panel = &connector->panel;
1098
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1099 1100
	u32 pwm_ctl, val;

1101
	/* Controller 1 uses the utility pin. */
1102
	if (panel->backlight.controller == 1) {
1103
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1104
		if (val & UTIL_PIN_ENABLE) {
1105 1106
			drm_dbg_kms(&dev_priv->drm,
				    "util pin already enabled\n");
1107
			val &= ~UTIL_PIN_ENABLE;
1108
			intel_de_write(dev_priv, UTIL_PIN_CTL, val);
1109
		}
1110

1111 1112 1113
		val = 0;
		if (panel->backlight.util_pin_active_low)
			val |= UTIL_PIN_POLARITY;
1114 1115
		intel_de_write(dev_priv, UTIL_PIN_CTL,
			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1116 1117
	}

1118 1119
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1120
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1121
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1122
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1123 1124 1125
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1126 1127
	}

1128 1129 1130
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1131

1132
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1133 1134 1135 1136 1137

	pwm_ctl = 0;
	if (panel->backlight.active_low_pwm)
		pwm_ctl |= BXT_BLC_PWM_POLARITY;

1138 1139 1140 1141 1142 1143
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       pwm_ctl);
	intel_de_posting_read(dev_priv,
			      BXT_BLC_PWM_CTL(panel->backlight.controller));
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       pwm_ctl | BXT_BLC_PWM_ENABLE);
1144 1145
}

1146 1147
static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1148
{
1149
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1150 1151 1152 1153
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl;

1154 1155
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1156
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1157
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1158
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1159 1160 1161
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1162 1163
	}

1164 1165 1166
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1167

1168
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1169 1170 1171 1172 1173

	pwm_ctl = 0;
	if (panel->backlight.active_low_pwm)
		pwm_ctl |= BXT_BLC_PWM_POLARITY;

1174 1175 1176 1177 1178 1179
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       pwm_ctl);
	intel_de_posting_read(dev_priv,
			      BXT_BLC_PWM_CTL(panel->backlight.controller));
	intel_de_write(dev_priv, BXT_BLC_PWM_CTL(panel->backlight.controller),
		       pwm_ctl | BXT_BLC_PWM_ENABLE);
1180 1181
}

1182 1183
static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1184
{
1185
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1186 1187 1188
	struct intel_panel *panel = &connector->panel;

	pwm_enable(panel->backlight.pwm);
1189
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1190 1191
}

1192 1193
static void __intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
					   const struct drm_connector_state *conn_state)
1194
{
1195
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1196
	struct intel_panel *panel = &connector->panel;
1197

1198 1199
	WARN_ON(panel->backlight.max == 0);

1200
	if (panel->backlight.level <= panel->backlight.min) {
1201
		panel->backlight.level = panel->backlight.max;
1202 1203
		if (panel->backlight.device)
			panel->backlight.device->props.brightness =
1204 1205 1206
				scale_hw_to_user(connector,
						 panel->backlight.level,
						 panel->backlight.device->props.max_brightness);
1207
	}
1208

1209
	panel->backlight.enable(crtc_state, conn_state);
1210
	panel->backlight.enabled = true;
1211 1212
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1213 1214 1215 1216 1217 1218 1219 1220
}

void intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
1221
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1222 1223 1224 1225

	if (!panel->backlight.present)
		return;

1226
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1227 1228 1229 1230

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1231

1232
	mutex_unlock(&dev_priv->backlight_lock);
1233 1234
}

1235
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
static u32 intel_panel_get_backlight(struct intel_connector *connector)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 val = 0;

	mutex_lock(&dev_priv->backlight_lock);

	if (panel->backlight.enabled) {
		val = panel->backlight.get(connector);
		val = intel_panel_compute_brightness(connector, val);
	}

	mutex_unlock(&dev_priv->backlight_lock);

1251
	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	return val;
}

/* set backlight brightness to level in range [0..max], scaling wrt hw min */
static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
				      u32 user_level, u32 user_max)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

	if (!panel->backlight.present)
		return;

	mutex_lock(&dev_priv->backlight_lock);

1269
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

	hw_level = scale_user_to_hw(connector, user_level, user_max);
	panel->backlight.level = hw_level;

	if (panel->backlight.enabled)
		intel_panel_actually_set_backlight(conn_state, hw_level);

	mutex_unlock(&dev_priv->backlight_lock);
}

1280
static int intel_backlight_device_update_status(struct backlight_device *bd)
1281
{
1282
	struct intel_connector *connector = bl_get_data(bd);
1283
	struct intel_panel *panel = &connector->panel;
1284 1285
	struct drm_device *dev = connector->base.dev;

1286
	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1287 1288
	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
		      bd->props.brightness, bd->props.max_brightness);
1289
	intel_panel_set_backlight(connector->base.state, bd->props.brightness,
1290
				  bd->props.max_brightness);
1291 1292 1293 1294 1295 1296 1297 1298

	/*
	 * Allow flipping bl_power as a sub-state of enabled. Sadly the
	 * backlight class device does not make it easy to to differentiate
	 * between callbacks for brightness and bl_power, so our backlight_power
	 * callback needs to take this into account.
	 */
	if (panel->backlight.enabled) {
1299
		if (panel->backlight.power) {
1300 1301
			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
				bd->props.brightness != 0;
1302
			panel->backlight.power(connector, enable);
1303 1304 1305 1306 1307
		}
	} else {
		bd->props.power = FB_BLANK_POWERDOWN;
	}

1308
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1309 1310 1311
	return 0;
}

1312
static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1313
{
1314 1315
	struct intel_connector *connector = bl_get_data(bd);
	struct drm_device *dev = connector->base.dev;
1316
	struct drm_i915_private *dev_priv = to_i915(dev);
1317
	intel_wakeref_t wakeref;
1318
	int ret = 0;
1319

1320
	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
1321
		u32 hw_level;
1322

1323
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1324

1325 1326 1327 1328 1329 1330
		hw_level = intel_panel_get_backlight(connector);
		ret = scale_hw_to_user(connector,
				       hw_level, bd->props.max_brightness);

		drm_modeset_unlock(&dev->mode_config.connection_mutex);
	}
1331

1332
	return ret;
1333 1334
}

1335 1336 1337
static const struct backlight_ops intel_backlight_device_ops = {
	.update_status = intel_backlight_device_update_status,
	.get_brightness = intel_backlight_device_get_brightness,
1338 1339
};

1340
int intel_backlight_device_register(struct intel_connector *connector)
1341
{
1342
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1343
	struct intel_panel *panel = &connector->panel;
1344 1345
	struct backlight_properties props;

1346
	if (WARN_ON(panel->backlight.device))
1347 1348
		return -ENODEV;

1349 1350 1351
	if (!panel->backlight.present)
		return 0;

1352
	WARN_ON(panel->backlight.max == 0);
1353

1354
	memset(&props, 0, sizeof(props));
1355
	props.type = BACKLIGHT_RAW;
1356 1357 1358 1359 1360

	/*
	 * Note: Everything should work even if the backlight device max
	 * presented to the userspace is arbitrarily chosen.
	 */
1361
	props.max_brightness = panel->backlight.max;
1362 1363 1364
	props.brightness = scale_hw_to_user(connector,
					    panel->backlight.level,
					    props.max_brightness);
1365

1366 1367 1368 1369 1370
	if (panel->backlight.enabled)
		props.power = FB_BLANK_UNBLANK;
	else
		props.power = FB_BLANK_POWERDOWN;

1371 1372 1373 1374 1375
	/*
	 * Note: using the same name independent of the connector prevents
	 * registration of multiple backlight devices in the driver.
	 */
	panel->backlight.device =
1376
		backlight_device_register("intel_backlight",
1377 1378 1379
					  connector->base.kdev,
					  connector,
					  &intel_backlight_device_ops, &props);
1380

1381
	if (IS_ERR(panel->backlight.device)) {
1382 1383
		drm_err(&i915->drm, "Failed to register backlight: %ld\n",
			PTR_ERR(panel->backlight.device));
1384
		panel->backlight.device = NULL;
1385 1386
		return -ENODEV;
	}
1387

1388 1389 1390
	drm_dbg_kms(&i915->drm,
		    "Connector %s backlight sysfs interface registered\n",
		    connector->base.name);
1391

1392 1393 1394
	return 0;
}

1395
void intel_backlight_device_unregister(struct intel_connector *connector)
1396
{
1397 1398 1399 1400 1401
	struct intel_panel *panel = &connector->panel;

	if (panel->backlight.device) {
		backlight_device_unregister(panel->backlight.device);
		panel->backlight.device = NULL;
1402
	}
1403
}
1404 1405
#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */

1406 1407 1408 1409 1410 1411 1412 1413
/*
 * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
 *      PWM increment = 1
 */
static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);

1414 1415
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz);
1416 1417
}

1418 1419 1420 1421 1422
/*
 * BXT: PWM clock frequency = 19.2 MHz.
 */
static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1423
	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1424 1425
}

1426
/*
1427 1428 1429 1430 1431 1432
 * SPT: This value represents the period of the PWM stream in clock periods
 * multiplied by 16 (default increment) or 128 (alternate increment selected in
 * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
 */
static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1433
	struct intel_panel *panel = &connector->panel;
1434
	u32 mul;
1435

1436
	if (panel->backlight.alternate_pwm_increment)
1437 1438 1439 1440
		mul = 128;
	else
		mul = 16;

1441
	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1442 1443 1444 1445 1446 1447 1448 1449 1450
}

/*
 * LPT: This value represents the period of the PWM stream in clock periods
 * multiplied by 128 (default increment) or 16 (alternate increment, selected in
 * LPT SOUTH_CHICKEN2 register bit 5).
 */
static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1451
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1452
	struct intel_panel *panel = &connector->panel;
1453 1454
	u32 mul, clock;

1455
	if (panel->backlight.alternate_pwm_increment)
1456 1457 1458 1459
		mul = 16;
	else
		mul = 128;

V
Ville Syrjälä 已提交
1460
	if (HAS_PCH_LPT_H(dev_priv))
1461 1462 1463 1464
		clock = MHz(135); /* LPT:H */
	else
		clock = MHz(24); /* LPT:LP */

1465
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1466 1467 1468 1469 1470 1471 1472 1473
}

/*
 * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
 * display raw clocks multiplied by 128.
 */
static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1474
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1475

1476 1477
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz * 128);
1478 1479 1480 1481 1482 1483
}

/*
 * Gen2: This field determines the number of time base events (display core
 * clock frequency/32) in total for a complete cycle of modulated backlight
 * control.
1484
 *
1485 1486 1487 1488 1489
 * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
 * divided by 32.
 */
static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1490
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1491 1492
	int clock;

1493
	if (IS_PINEVIEW(dev_priv))
1494
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1495
	else
1496
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1497

1498
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1499 1500 1501 1502
}

/*
 * Gen4: This value represents the period of the PWM stream in display core
1503 1504
 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
 *
1505 1506 1507
 */
static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1508
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1509 1510 1511
	int clock;

	if (IS_G4X(dev_priv))
1512
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1513
	else
1514
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1515

1516
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1517 1518 1519 1520 1521 1522 1523 1524 1525
}

/*
 * VLV: This value represents the period of the PWM stream in display core
 * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
 * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
 */
static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1526 1527
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	int mul, clock;
1528

1529
	if ((intel_de_read(dev_priv, CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1530 1531
		if (IS_CHERRYVIEW(dev_priv))
			clock = KHz(19200);
1532
		else
1533 1534
			clock = MHz(25);
		mul = 16;
1535
	} else {
1536
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1537
		mul = 128;
1538
	}
1539

1540
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1541 1542 1543 1544
}

static u32 get_backlight_max_vbt(struct intel_connector *connector)
{
1545
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1546
	struct intel_panel *panel = &connector->panel;
1547 1548 1549
	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
	u32 pwm;

1550
	if (!panel->backlight.hz_to_pwm) {
1551 1552
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion not supported\n");
1553 1554 1555
		return 0;
	}

1556
	if (pwm_freq_hz) {
1557 1558 1559
		drm_dbg_kms(&dev_priv->drm,
			    "VBT defined backlight frequency %u Hz\n",
			    pwm_freq_hz);
1560 1561
	} else {
		pwm_freq_hz = 200;
1562 1563 1564
		drm_dbg_kms(&dev_priv->drm,
			    "default backlight frequency %u Hz\n",
			    pwm_freq_hz);
1565 1566
	}

1567
	pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
1568
	if (!pwm) {
1569 1570
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion failed\n");
1571 1572 1573 1574 1575 1576 1577 1578
		return 0;
	}

	return pwm;
}

/*
 * Note: The setup hooks can't assume pipe is set!
1579
 */
1580 1581
static u32 get_backlight_min_vbt(struct intel_connector *connector)
{
1582
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1583
	struct intel_panel *panel = &connector->panel;
1584
	int min;
1585

1586
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
1587

1588 1589 1590 1591 1592 1593 1594 1595 1596
	/*
	 * XXX: If the vbt value is 255, it makes min equal to max, which leads
	 * to problems. There are such machines out there. Either our
	 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
	 * against this by letting the minimum be at most (arbitrarily chosen)
	 * 25% of the max.
	 */
	min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
	if (min != dev_priv->vbt.backlight.min_brightness) {
1597 1598 1599
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1600 1601
	}

1602
	/* vbt value is a coefficient in range [0..255] */
1603
	return scale(min, 0, 255, 0, panel->backlight.max);
1604 1605
}

1606
static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1607
{
1608
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1609
	struct intel_panel *panel = &connector->panel;
1610 1611
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
	bool alt, cpu_mode;
1612 1613

	if (HAS_PCH_LPT(dev_priv))
1614
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1615
	else
1616
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1617
	panel->backlight.alternate_pwm_increment = alt;
1618

1619
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1620 1621
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1622
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1623
	panel->backlight.max = pch_ctl2 >> 16;
1624

1625
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1626

1627 1628 1629
	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);

1630 1631 1632
	if (!panel->backlight.max)
		return -ENODEV;

1633 1634
	panel->backlight.min = get_backlight_min_vbt(connector);

1635 1636 1637 1638 1639 1640 1641 1642 1643
	panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;

	cpu_mode = panel->backlight.enabled && HAS_PCH_LPT(dev_priv) &&
		   !(pch_ctl1 & BLM_PCH_OVERRIDE_ENABLE) &&
		   (cpu_ctl2 & BLM_PWM_ENABLE);
	if (cpu_mode)
		val = pch_get_backlight(connector);
	else
		val = lpt_get_backlight(connector);
1644 1645 1646
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1647

1648
	if (cpu_mode) {
1649 1650
		drm_dbg_kms(&dev_priv->drm,
			    "CPU backlight register was enabled, switching to PCH override\n");
1651 1652 1653

		/* Write converted CPU PWM value to PCH override register */
		lpt_set_backlight(connector->base.state, panel->backlight.level);
1654 1655
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
			       pch_ctl1 | BLM_PCH_OVERRIDE_ENABLE);
1656

1657 1658
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1659
	}
1660 1661 1662 1663

	return 0;
}

1664
static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1665
{
1666
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1667
	struct intel_panel *panel = &connector->panel;
1668
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1669

1670
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1671 1672
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1673
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1674
	panel->backlight.max = pch_ctl2 >> 16;
1675 1676 1677 1678

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);

1679 1680 1681
	if (!panel->backlight.max)
		return -ENODEV;

1682 1683
	panel->backlight.min = get_backlight_min_vbt(connector);

1684
	val = pch_get_backlight(connector);
1685 1686 1687
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1688

1689
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1690
	panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1691
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1692

1693 1694 1695
	return 0;
}

1696
static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1697
{
1698
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1699
	struct intel_panel *panel = &connector->panel;
1700 1701
	u32 ctl, val;

1702
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1703

1704
	if (IS_GEN(dev_priv, 2) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1705 1706
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1707
	if (IS_PINEVIEW(dev_priv))
1708 1709 1710
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

	panel->backlight.max = ctl >> 17;
1711 1712 1713 1714 1715

	if (!panel->backlight.max) {
		panel->backlight.max = get_backlight_max_vbt(connector);
		panel->backlight.max >>= 1;
	}
1716 1717 1718 1719

	if (!panel->backlight.max)
		return -ENODEV;

1720 1721 1722
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1723 1724
	panel->backlight.min = get_backlight_min_vbt(connector);

1725
	val = i9xx_get_backlight(connector);
1726 1727 1728
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1729

1730
	panel->backlight.enabled = val != 0;
1731

1732 1733 1734
	return 0;
}

1735
static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1736
{
1737
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1738
	struct intel_panel *panel = &connector->panel;
1739 1740
	u32 ctl, ctl2, val;

1741
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1742 1743 1744
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1745
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1746
	panel->backlight.max = ctl >> 16;
1747 1748 1749

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);
1750 1751 1752 1753

	if (!panel->backlight.max)
		return -ENODEV;

1754 1755 1756
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1757 1758
	panel->backlight.min = get_backlight_min_vbt(connector);

1759
	val = i9xx_get_backlight(connector);
1760 1761 1762
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1763

1764
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1765

1766 1767 1768
	return 0;
}

1769
static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1770
{
1771
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1772
	struct intel_panel *panel = &connector->panel;
1773
	u32 ctl, ctl2, val;
1774

1775
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1776 1777
		return -ENODEV;

1778
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1779 1780
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1781
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1782
	panel->backlight.max = ctl >> 16;
1783 1784 1785 1786

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);

1787 1788 1789
	if (!panel->backlight.max)
		return -ENODEV;

1790 1791
	panel->backlight.min = get_backlight_min_vbt(connector);

1792
	val = _vlv_get_backlight(dev_priv, pipe);
1793 1794 1795
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1796

1797
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1798

1799 1800 1801
	return 0;
}

1802 1803 1804
static int
bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
1805
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1806 1807 1808
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

1809
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1810

1811 1812
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1813

1814
	/* Controller 1 uses the utility pin. */
1815
	if (panel->backlight.controller == 1) {
1816
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1817 1818 1819 1820 1821 1822
		panel->backlight.util_pin_active_low =
					val & UTIL_PIN_POLARITY;
	}

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1823 1824
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1825 1826 1827 1828

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);

1829 1830 1831
	if (!panel->backlight.max)
		return -ENODEV;

1832 1833
	panel->backlight.min = get_backlight_min_vbt(connector);

1834
	val = bxt_get_backlight(connector);
1835 1836 1837
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1838

1839
	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1840 1841 1842 1843

	return 0;
}

1844 1845 1846 1847 1848 1849 1850 1851
static int
cnp_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

	/*
1852 1853 1854
	 * CNP has the BXT implementation of backlight, but with only one
	 * controller. TODO: ICP has multiple controllers but we only use
	 * controller 0 for now.
1855 1856 1857
	 */
	panel->backlight.controller = 0;

1858 1859
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1860 1861 1862

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1863 1864
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1865 1866 1867 1868 1869 1870 1871

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);

	if (!panel->backlight.max)
		return -ENODEV;

1872 1873
	panel->backlight.min = get_backlight_min_vbt(connector);

1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
	val = bxt_get_backlight(connector);
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);

	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;

	return 0;
}

1884 1885 1886 1887
static int pwm_setup_backlight(struct intel_connector *connector,
			       enum pipe pipe)
{
	struct drm_device *dev = connector->base.dev;
1888
	struct drm_i915_private *dev_priv = to_i915(dev);
1889
	struct intel_panel *panel = &connector->panel;
1890
	const char *desc;
1891
	u32 level, ns;
1892 1893
	int retval;

1894 1895 1896 1897 1898 1899 1900 1901 1902
	/* Get the right PWM chip for DSI backlight according to VBT */
	if (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC) {
		panel->backlight.pwm = pwm_get(dev->dev, "pwm_pmic_backlight");
		desc = "PMIC";
	} else {
		panel->backlight.pwm = pwm_get(dev->dev, "pwm_soc_backlight");
		desc = "SoC";
	}

1903
	if (IS_ERR(panel->backlight.pwm)) {
1904 1905
		drm_err(&dev_priv->drm, "Failed to get the %s PWM chip\n",
			desc);
1906 1907 1908 1909
		panel->backlight.pwm = NULL;
		return -ENODEV;
	}

1910 1911 1912 1913 1914 1915
	/*
	 * FIXME: pwm_apply_args() should be removed when switching to
	 * the atomic PWM API.
	 */
	pwm_apply_args(panel->backlight.pwm);

1916 1917 1918 1919 1920 1921
	panel->backlight.min = 0; /* 0% */
	panel->backlight.max = 100; /* 100% */
	level = intel_panel_compute_brightness(connector, 100);
	ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);

	retval = pwm_config(panel->backlight.pwm, ns, CRC_PMIC_PWM_PERIOD_NS);
1922
	if (retval < 0) {
1923
		drm_err(&dev_priv->drm, "Failed to configure the pwm chip\n");
1924 1925 1926 1927 1928
		pwm_put(panel->backlight.pwm);
		panel->backlight.pwm = NULL;
		return retval;
	}

1929 1930 1931 1932
	level = DIV_ROUND_UP(pwm_get_duty_cycle(panel->backlight.pwm) * 100,
			     CRC_PMIC_PWM_PERIOD_NS);
	panel->backlight.level =
		intel_panel_compute_brightness(connector, level);
1933 1934
	panel->backlight.enabled = panel->backlight.level != 0;

1935 1936
	drm_info(&dev_priv->drm, "Using %s PWM for LCD backlight control\n",
		 desc);
1937 1938 1939
	return 0;
}

1940 1941
void intel_panel_update_backlight(struct intel_atomic_state *state,
				  struct intel_encoder *encoder,
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
				  const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;

	if (!panel->backlight.present)
		return;

	mutex_lock(&dev_priv->backlight_lock);
	if (!panel->backlight.enabled)
		__intel_panel_enable_backlight(crtc_state, conn_state);

	mutex_unlock(&dev_priv->backlight_lock);
}

1959
int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
1960
{
1961
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
1962
	struct intel_connector *intel_connector = to_intel_connector(connector);
1963
	struct intel_panel *panel = &intel_connector->panel;
1964
	int ret;
1965

1966
	if (!dev_priv->vbt.backlight.present) {
1967
		if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1968 1969
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT, but present per quirk\n");
1970
		} else {
1971 1972
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT\n");
1973 1974
			return 0;
		}
1975 1976
	}

1977
	/* ensure intel_panel has been initialized first */
1978
	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.setup))
1979 1980
		return -ENODEV;

1981
	/* set level and max in panel struct */
1982
	mutex_lock(&dev_priv->backlight_lock);
1983
	ret = panel->backlight.setup(intel_connector, pipe);
1984
	mutex_unlock(&dev_priv->backlight_lock);
1985 1986

	if (ret) {
1987 1988 1989
		drm_dbg_kms(&dev_priv->drm,
			    "failed to setup backlight for connector %s\n",
			    connector->name);
1990 1991
		return ret;
	}
1992

1993 1994
	panel->backlight.present = true;

1995 1996 1997 1998 1999
	drm_dbg_kms(&dev_priv->drm,
		    "Connector %s backlight initialized, %s, brightness %u/%u\n",
		    connector->name,
		    enableddisabled(panel->backlight.enabled),
		    panel->backlight.level, panel->backlight.max);
2000

2001 2002 2003
	return 0;
}

2004
static void intel_panel_destroy_backlight(struct intel_panel *panel)
2005
{
2006 2007 2008 2009
	/* dispose of the pwm */
	if (panel->backlight.pwm)
		pwm_put(panel->backlight.pwm);

2010
	panel->backlight.present = false;
2011
}
2012

2013
/* Set up chip specific backlight functions */
2014 2015
static void
intel_panel_init_backlight_funcs(struct intel_panel *panel)
2016
{
2017
	struct intel_connector *connector =
2018
		container_of(panel, struct intel_connector, panel);
2019
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
2020

2021 2022 2023 2024
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;

2025 2026 2027 2028
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

2029
	if (IS_GEN9_LP(dev_priv)) {
2030 2031 2032 2033 2034
		panel->backlight.setup = bxt_setup_backlight;
		panel->backlight.enable = bxt_enable_backlight;
		panel->backlight.disable = bxt_disable_backlight;
		panel->backlight.set = bxt_set_backlight;
		panel->backlight.get = bxt_get_backlight;
2035
		panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
2036
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
2037 2038 2039 2040 2041 2042
		panel->backlight.setup = cnp_setup_backlight;
		panel->backlight.enable = cnp_enable_backlight;
		panel->backlight.disable = cnp_disable_backlight;
		panel->backlight.set = bxt_set_backlight;
		panel->backlight.get = bxt_get_backlight;
		panel->backlight.hz_to_pwm = cnp_hz_to_pwm;
2043
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
2044 2045 2046 2047 2048
		panel->backlight.setup = lpt_setup_backlight;
		panel->backlight.enable = lpt_enable_backlight;
		panel->backlight.disable = lpt_disable_backlight;
		panel->backlight.set = lpt_set_backlight;
		panel->backlight.get = lpt_get_backlight;
2049
		if (HAS_PCH_LPT(dev_priv))
2050
			panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
2051
		else
2052
			panel->backlight.hz_to_pwm = spt_hz_to_pwm;
2053
	} else if (HAS_PCH_SPLIT(dev_priv)) {
2054 2055 2056 2057 2058 2059
		panel->backlight.setup = pch_setup_backlight;
		panel->backlight.enable = pch_enable_backlight;
		panel->backlight.disable = pch_disable_backlight;
		panel->backlight.set = pch_set_backlight;
		panel->backlight.get = pch_get_backlight;
		panel->backlight.hz_to_pwm = pch_hz_to_pwm;
2060
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2061
		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
2062 2063 2064 2065 2066
			panel->backlight.setup = pwm_setup_backlight;
			panel->backlight.enable = pwm_enable_backlight;
			panel->backlight.disable = pwm_disable_backlight;
			panel->backlight.set = pwm_set_backlight;
			panel->backlight.get = pwm_get_backlight;
2067
		} else {
2068 2069 2070 2071 2072 2073
			panel->backlight.setup = vlv_setup_backlight;
			panel->backlight.enable = vlv_enable_backlight;
			panel->backlight.disable = vlv_disable_backlight;
			panel->backlight.set = vlv_set_backlight;
			panel->backlight.get = vlv_get_backlight;
			panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
2074
		}
2075
	} else if (IS_GEN(dev_priv, 4)) {
2076 2077 2078 2079 2080 2081
		panel->backlight.setup = i965_setup_backlight;
		panel->backlight.enable = i965_enable_backlight;
		panel->backlight.disable = i965_disable_backlight;
		panel->backlight.set = i9xx_set_backlight;
		panel->backlight.get = i9xx_get_backlight;
		panel->backlight.hz_to_pwm = i965_hz_to_pwm;
2082
	} else {
2083 2084 2085 2086 2087 2088
		panel->backlight.setup = i9xx_setup_backlight;
		panel->backlight.enable = i9xx_enable_backlight;
		panel->backlight.disable = i9xx_disable_backlight;
		panel->backlight.set = i9xx_set_backlight;
		panel->backlight.get = i9xx_get_backlight;
		panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
2089 2090 2091
	}
}

2092
int intel_panel_init(struct intel_panel *panel,
2093 2094
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2095
{
2096 2097
	intel_panel_init_backlight_funcs(panel);

2098
	panel->fixed_mode = fixed_mode;
2099
	panel->downclock_mode = downclock_mode;
2100

2101 2102 2103 2104 2105
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2106 2107 2108
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2109 2110
	intel_panel_destroy_backlight(panel);

2111 2112
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2113 2114 2115 2116

	if (panel->downclock_mode)
		drm_mode_destroy(intel_connector->base.dev,
				panel->downclock_mode);
2117
}