intel_panel.c 65.4 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
/* adjusted_mode has been preset to be the panel's fixed mode */
179 180
int intel_pch_panel_fitting(struct intel_crtc_state *crtc_state,
			    const struct drm_connector_state *conn_state)
181
{
182 183
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->hw.adjusted_mode;
184
	int x, y, width, height;
185 186

	/* Native modes don't need fitting */
187 188 189
	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)
190
		return 0;
191

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

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

229 230 231 232
	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 */
233 234
	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
235 236
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
237
		break;
238 239

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

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

	return 0;
249
}
250

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

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

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

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

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

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

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

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

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

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

294
static u32 panel_fitter_scaling(u32 source, u32 target)
295 296 297 298 299 300 301 302 303 304 305 306
{
	/*
	 * 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;
}

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

	/* 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;
324
	else if (adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w)
325 326 327
		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
}

328
static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
329 330 331
			      u32 *pfit_control, u32 *pfit_pgm_ratios,
			      u32 *border)
{
332
	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
333
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
334 335
		crtc_state->pipe_src_h;
	u32 scaled_height = crtc_state->pipe_src_w *
336
		adjusted_mode->crtc_vdisplay;
337 338 339 340 341 342 343 344 345 346
	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 /
347
				    crtc_state->pipe_src_h);
348 349

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

			*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 /
363
				  crtc_state->pipe_src_w);
364 365

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

			*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);
	}
}

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

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

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

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

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

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

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

	return 0;
458 459
}

460 461 462
/**
 * scale - scale values from one range to another
 * @source_val: value in range [@source_min..@source_max]
463 464 465 466
 * @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
467 468 469 470
 *
 * Return @source_val in range [@source_min..@source_max] scaled to range
 * [@target_min..@target_max].
 */
471 472 473
static u32 scale(u32 source_val,
		 u32 source_min, u32 source_max,
		 u32 target_min, u32 target_max)
474
{
475
	u64 target_val;
476 477 478 479 480 481 482 483

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

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

	/* avoid overflows */
484 485 486
	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);
487 488 489 490 491 492
	target_val += target_min;

	return target_val;
}

/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
493 494
static u32 scale_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
495 496 497 498 499 500 501 502 503
{
	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]. */
504 505
static u32 clamp_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
506 507 508 509 510 511 512 513 514 515 516
{
	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]. */
517 518
static u32 scale_hw_to_user(struct intel_connector *connector,
			    u32 hw_level, u32 user_max)
519 520 521 522 523 524 525
{
	struct intel_panel *panel = &connector->panel;

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

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

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

534
	if (i915_modparams.invert_brightness < 0)
535 536
		return val;

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

	return val;
}

545
static u32 lpt_get_backlight(struct intel_connector *connector)
546
{
547
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
548

549
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
550
}
551

552
static u32 pch_get_backlight(struct intel_connector *connector)
553
{
554
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
555

556
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
557
}
558

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

565
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
566
	if (INTEL_GEN(dev_priv) < 4)
567
		val >>= 1;
568

569
	if (panel->backlight.combination_mode) {
570
		u8 lbpc;
571

572
		pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
573
		val *= lbpc;
574 575
	}

576 577 578
	return val;
}

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

584
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
585 586 587 588
}

static u32 vlv_get_backlight(struct intel_connector *connector)
{
589
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
590
	enum pipe pipe = intel_connector_get_pipe(connector);
591

592
	return _vlv_get_backlight(dev_priv, pipe);
593 594
}

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

600 601
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
602 603
}

604 605 606 607 608 609 610 611 612
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);
}

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

618 619
	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);
620 621
}

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

628 629
	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);
630 631
}

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

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

641
	if (panel->backlight.combination_mode) {
642 643
		u8 lbpc;

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

649
	if (IS_GEN(dev_priv, 4)) {
650 651
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
652
		level <<= 1;
653 654
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
655

656 657
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
658 659
}

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

667 668
	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);
669 670
}

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

677 678
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
679 680
}

681
static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
682
{
683
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
684 685 686 687 688
	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);
}

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

696
	drm_dbg_kms(&i915->drm, "set backlight PWM = %d\n", level);
697 698

	level = intel_panel_compute_brightness(connector, level);
699
	panel->backlight.set(conn_state, level);
700
}
701

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

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

722
	mutex_lock(&dev_priv->backlight_lock);
723

724
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
725 726 727

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

729
	if (panel->backlight.device)
730 731 732 733
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
734

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

738
	mutex_unlock(&dev_priv->backlight_lock);
739 740
}

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

747
	intel_panel_actually_set_backlight(old_conn_state, 0);
748

749 750 751 752 753 754 755 756
	/*
	 * 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.
	 */
757
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
758
	if (tmp & BLM_PWM_ENABLE) {
759 760
		drm_dbg_kms(&dev_priv->drm,
			    "cpu backlight was enabled, disabling\n");
761 762
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       tmp & ~BLM_PWM_ENABLE);
763 764
	}

765 766
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
767 768
}

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

775
	intel_panel_actually_set_backlight(old_conn_state, 0);
776

777 778
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
779

780 781
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
782 783
}

784
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
785
{
786
	intel_panel_actually_set_backlight(old_conn_state, 0);
787 788
}

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

794
	intel_panel_actually_set_backlight(old_conn_state, 0);
795

796 797
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
798 799
}

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

807
	intel_panel_actually_set_backlight(old_conn_state, 0);
808

809 810 811
	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);
812 813
}

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

821
	intel_panel_actually_set_backlight(old_conn_state, 0);
822

823 824 825 826
	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);
827 828

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

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

842
	intel_panel_actually_set_backlight(old_conn_state, 0);
843

844 845 846 847
	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);
848 849
}

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

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

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

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

870
	/*
871
	 * Do not disable backlight on the vga_switcheroo path. When switching
872 873 874 875
	 * 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.
	 */
876
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
877 878
		drm_dbg_kms(&dev_priv->drm,
			    "Skipping backlight disable on vga switch\n");
879 880 881
		return;
	}

882
	mutex_lock(&dev_priv->backlight_lock);
883

884 885
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
886
	panel->backlight.enabled = false;
887
	panel->backlight.disable(old_conn_state);
888

889
	mutex_unlock(&dev_priv->backlight_lock);
890
}
891

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

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

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

923
	pch_ctl2 = panel->backlight.max << 16;
924
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
925

926 927 928
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
929

930 931 932
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
933

934 935 936 937
	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);
938 939

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

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

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

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

	if (cpu_transcoder == TRANSCODER_EDP)
967
		cpu_ctl2 = BLM_TRANSCODER_EDP;
968
	else
969
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
970 971 972
	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);
973

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

	pch_ctl2 = panel->backlight.max << 16;
978
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
979 980 981 982

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

984 985 986 987
	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);
988 989
}

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

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

1004 1005 1006 1007 1008
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;

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

1014 1015
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1016 1017

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

	/*
	 * 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.
	 */
1025
	if (IS_GEN(dev_priv, 2))
1026
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1027
}
1028

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

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

1045 1046 1047
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1048

1049
	ctl = freq << 16;
1050
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1051

1052 1053 1054 1055 1056
	ctl2 = BLM_PIPE(pipe);
	if (panel->backlight.combination_mode)
		ctl2 |= BLM_COMBINATION_MODE;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1057 1058 1059
	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);
1060

1061
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1062 1063
}

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

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

1080
	ctl = panel->backlight.max << 16;
1081
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1082

1083
	/* XXX: combine this into above write? */
1084
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1085

1086 1087 1088
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1089 1090 1091 1092
	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);
1093 1094
}

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

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

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

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

1131 1132 1133
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1134

1135
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1136 1137 1138 1139 1140

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

1141 1142 1143 1144 1145 1146
	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);
1147 1148
}

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

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

1167 1168 1169
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1170

1171
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1172 1173 1174 1175 1176

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

1177 1178 1179 1180 1181 1182
	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);
1183 1184
}

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

	pwm_enable(panel->backlight.pwm);
1192
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1193 1194
}

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

1201 1202
	WARN_ON(panel->backlight.max == 0);

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

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

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;
1224
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1225 1226 1227 1228

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

1229
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1230 1231 1232 1233

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1234

1235
	mutex_unlock(&dev_priv->backlight_lock);
1236 1237
}

1238
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
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);

1254
	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	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);

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

	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);
}

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

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

	/*
	 * 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) {
1302
		if (panel->backlight.power) {
1303 1304
			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
				bd->props.brightness != 0;
1305
			panel->backlight.power(connector, enable);
1306 1307 1308 1309 1310
		}
	} else {
		bd->props.power = FB_BLANK_POWERDOWN;
	}

1311
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1312 1313 1314
	return 0;
}

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

1323
	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
1324
		u32 hw_level;
1325

1326
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1327

1328 1329 1330 1331 1332 1333
		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);
	}
1334

1335
	return ret;
1336 1337
}

1338 1339 1340
static const struct backlight_ops intel_backlight_device_ops = {
	.update_status = intel_backlight_device_update_status,
	.get_brightness = intel_backlight_device_get_brightness,
1341 1342
};

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

1349
	if (WARN_ON(panel->backlight.device))
1350 1351
		return -ENODEV;

1352 1353 1354
	if (!panel->backlight.present)
		return 0;

1355
	WARN_ON(panel->backlight.max == 0);
1356

1357
	memset(&props, 0, sizeof(props));
1358
	props.type = BACKLIGHT_RAW;
1359 1360 1361 1362 1363

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

1369 1370 1371 1372 1373
	if (panel->backlight.enabled)
		props.power = FB_BLANK_UNBLANK;
	else
		props.power = FB_BLANK_POWERDOWN;

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

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

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

1395 1396 1397
	return 0;
}

1398
void intel_backlight_device_unregister(struct intel_connector *connector)
1399
{
1400 1401 1402 1403 1404
	struct intel_panel *panel = &connector->panel;

	if (panel->backlight.device) {
		backlight_device_unregister(panel->backlight.device);
		panel->backlight.device = NULL;
1405
	}
1406
}
1407 1408
#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */

1409 1410 1411 1412 1413 1414 1415 1416
/*
 * 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);

1417 1418
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz);
1419 1420
}

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

1429
/*
1430 1431 1432 1433 1434 1435
 * 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)
{
1436
	struct intel_panel *panel = &connector->panel;
1437
	u32 mul;
1438

1439
	if (panel->backlight.alternate_pwm_increment)
1440 1441 1442 1443
		mul = 128;
	else
		mul = 16;

1444
	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1445 1446 1447 1448 1449 1450 1451 1452 1453
}

/*
 * 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)
{
1454
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1455
	struct intel_panel *panel = &connector->panel;
1456 1457
	u32 mul, clock;

1458
	if (panel->backlight.alternate_pwm_increment)
1459 1460 1461 1462
		mul = 16;
	else
		mul = 128;

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

1468
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1469 1470 1471 1472 1473 1474 1475 1476
}

/*
 * 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)
{
1477
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1478

1479 1480
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz * 128);
1481 1482 1483 1484 1485 1486
}

/*
 * 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.
1487
 *
1488 1489 1490 1491 1492
 * 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)
{
1493
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1494 1495
	int clock;

1496
	if (IS_PINEVIEW(dev_priv))
1497
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1498
	else
1499
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1500

1501
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1502 1503 1504 1505
}

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

	if (IS_G4X(dev_priv))
1515
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1516
	else
1517
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1518

1519
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1520 1521 1522 1523 1524 1525 1526 1527 1528
}

/*
 * 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)
{
1529 1530
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	int mul, clock;
1531

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

1543
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1544 1545 1546 1547
}

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

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

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

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

	return pwm;
}

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

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

1591 1592 1593 1594 1595 1596 1597 1598 1599
	/*
	 * 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) {
1600 1601 1602
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1603 1604
	}

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

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

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

1622
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1623 1624
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1625
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1626
	panel->backlight.max = pch_ctl2 >> 16;
1627

1628
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1629

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

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

1636 1637
	panel->backlight.min = get_backlight_min_vbt(connector);

1638 1639 1640 1641 1642 1643 1644 1645 1646
	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);
1647 1648 1649
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1650

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

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

1660 1661
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1662
	}
1663 1664 1665 1666

	return 0;
}

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

1673
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1674 1675
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1676
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1677
	panel->backlight.max = pch_ctl2 >> 16;
1678 1679 1680 1681

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

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

1685 1686
	panel->backlight.min = get_backlight_min_vbt(connector);

1687
	val = pch_get_backlight(connector);
1688 1689 1690
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1691

1692
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1693
	panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1694
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1695

1696 1697 1698
	return 0;
}

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

1705
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1706

1707
	if (IS_GEN(dev_priv, 2) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1708 1709
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1710
	if (IS_PINEVIEW(dev_priv))
1711 1712 1713
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

	panel->backlight.max = ctl >> 17;
1714 1715 1716 1717 1718

	if (!panel->backlight.max) {
		panel->backlight.max = get_backlight_max_vbt(connector);
		panel->backlight.max >>= 1;
	}
1719 1720 1721 1722

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

1723 1724 1725
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1726 1727
	panel->backlight.min = get_backlight_min_vbt(connector);

1728
	val = i9xx_get_backlight(connector);
1729 1730 1731
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1732

1733
	panel->backlight.enabled = val != 0;
1734

1735 1736 1737
	return 0;
}

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

1744
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1745 1746 1747
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1748
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1749
	panel->backlight.max = ctl >> 16;
1750 1751 1752

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);
1753 1754 1755 1756

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

1757 1758 1759
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1760 1761
	panel->backlight.min = get_backlight_min_vbt(connector);

1762
	val = i9xx_get_backlight(connector);
1763 1764 1765
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1766

1767
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1768

1769 1770 1771
	return 0;
}

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

1778
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1779 1780
		return -ENODEV;

1781
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1782 1783
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1784
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1785
	panel->backlight.max = ctl >> 16;
1786 1787 1788 1789

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

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

1793 1794
	panel->backlight.min = get_backlight_min_vbt(connector);

1795
	val = _vlv_get_backlight(dev_priv, pipe);
1796 1797 1798
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1799

1800
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1801

1802 1803 1804
	return 0;
}

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

1812
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1813

1814 1815
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1816

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

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1826 1827
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1828 1829 1830 1831

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

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

1835 1836
	panel->backlight.min = get_backlight_min_vbt(connector);

1837
	val = bxt_get_backlight(connector);
1838 1839 1840
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1841

1842
	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1843 1844 1845 1846

	return 0;
}

1847 1848 1849 1850 1851 1852 1853 1854
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;

	/*
1855 1856 1857
	 * 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.
1858 1859 1860
	 */
	panel->backlight.controller = 0;

1861 1862
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1863 1864 1865

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

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

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

1875 1876
	panel->backlight.min = get_backlight_min_vbt(connector);

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
	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;
}

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

1897 1898 1899 1900 1901 1902 1903 1904 1905
	/* 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";
	}

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

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

1919 1920 1921 1922 1923 1924
	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);
1925
	if (retval < 0) {
1926
		drm_err(&dev_priv->drm, "Failed to configure the pwm chip\n");
1927 1928 1929 1930 1931
		pwm_put(panel->backlight.pwm);
		panel->backlight.pwm = NULL;
		return retval;
	}

1932 1933 1934 1935
	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);
1936 1937
	panel->backlight.enabled = panel->backlight.level != 0;

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

1943 1944
void intel_panel_update_backlight(struct intel_atomic_state *state,
				  struct intel_encoder *encoder,
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
				  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);
}

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

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

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

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

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

1996 1997
	panel->backlight.present = true;

1998 1999 2000 2001 2002
	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);
2003

2004 2005 2006
	return 0;
}

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

2013
	panel->backlight.present = false;
2014
}
2015

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

2024 2025 2026 2027
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;

2028 2029 2030 2031
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

2032
	if (IS_GEN9_LP(dev_priv)) {
2033 2034 2035 2036 2037
		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;
2038
		panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
2039
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
2040 2041 2042 2043 2044 2045
		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;
2046
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
2047 2048 2049 2050 2051
		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;
2052
		if (HAS_PCH_LPT(dev_priv))
2053
			panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
2054
		else
2055
			panel->backlight.hz_to_pwm = spt_hz_to_pwm;
2056
	} else if (HAS_PCH_SPLIT(dev_priv)) {
2057 2058 2059 2060 2061 2062
		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;
2063
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2064
		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
2065 2066 2067 2068 2069
			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;
2070
		} else {
2071 2072 2073 2074 2075 2076
			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;
2077
		}
2078
	} else if (IS_GEN(dev_priv, 4)) {
2079 2080 2081 2082 2083 2084
		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;
2085
	} else {
2086 2087 2088 2089 2090 2091
		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;
2092 2093 2094
	}
}

2095
int intel_panel_init(struct intel_panel *panel,
2096 2097
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2098
{
2099 2100
	intel_panel_init_backlight_funcs(panel);

2101
	panel->fixed_mode = fixed_mode;
2102
	panel->downclock_mode = downclock_mode;
2103

2104 2105 2106 2107 2108
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2109 2110 2111
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2112 2113
	intel_panel_destroy_backlight(panel);

2114 2115
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2116 2117 2118 2119

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