intel_panel.c 66.2 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

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

	drm_mode_set_crtcinfo(adjusted_mode, 0);
50 51
}

52 53 54 55 56 57 58 59 60 61
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 已提交
62
struct drm_display_mode *
63 64
intel_panel_edid_downclock_mode(struct intel_connector *connector,
				const struct drm_display_mode *fixed_mode)
J
Jani Nikula 已提交
65
{
66
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
67
	const struct drm_display_mode *scan, *best_mode = NULL;
68
	struct drm_display_mode *downclock_mode;
69
	int best_clock = fixed_mode->clock;
J
Jani Nikula 已提交
70

71
	list_for_each_entry(scan, &connector->base.probed_modes, head) {
J
Jani Nikula 已提交
72 73 74 75 76 77 78
		/*
		 * 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.
		 */
79 80 81 82 83 84 85 86
		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 已提交
87 88 89
		}
	}

90 91 92 93 94 95 96
	if (!best_mode)
		return NULL;

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

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

102
	return downclock_mode;
J
Jani Nikula 已提交
103 104
}

105 106 107 108 109
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;
110 111 112 113
	struct drm_display_mode *fixed_mode;

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

	/* 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;

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

		return fixed_mode;
	}

132 133 134 135 136 137 138 139 140
	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;

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

	return fixed_mode;
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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;

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

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

	return fixed_mode;
}

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

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

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

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

227 228 229
	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);
230
		fallthrough;
231 232
	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
233 234
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
235
		break;
236 237

	default:
238
		MISSING_CASE(conn_state->scaling_mode);
239
		return -EINVAL;
240 241
	}

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

	return 0;
247
}
248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

			*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 /
361
				  crtc_state->pipe_src_w);
362 363

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

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

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

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

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

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

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

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

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

	return 0;
456 457
}

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

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

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

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

	return target_val;
}

/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
 * to [hw_min..hw_max]. */
492 493
static u32 clamp_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
494 495 496 497 498 499 500 501 502 503 504
{
	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]. */
505 506
static u32 scale_hw_to_user(struct intel_connector *connector,
			    u32 hw_level, u32 user_max)
507 508 509 510 511 512 513
{
	struct intel_panel *panel = &connector->panel;

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

514 515
static u32 intel_panel_compute_brightness(struct intel_connector *connector,
					  u32 val)
516
{
517
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
518 519
	struct intel_panel *panel = &connector->panel;

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

522
	if (dev_priv->params.invert_brightness < 0)
523 524
		return val;

525
	if (dev_priv->params.invert_brightness > 0 ||
526
	    dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
527
		return panel->backlight.max - val + panel->backlight.min;
528
	}
529 530 531 532

	return val;
}

533
static u32 lpt_get_backlight(struct intel_connector *connector)
534
{
535
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
536

537
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
538
}
539

540
static u32 pch_get_backlight(struct intel_connector *connector)
541
{
542
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
543

544
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
545
}
546

547 548
static u32 i9xx_get_backlight(struct intel_connector *connector)
{
549
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
550
	struct intel_panel *panel = &connector->panel;
551
	u32 val;
552

553
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
554
	if (INTEL_GEN(dev_priv) < 4)
555
		val >>= 1;
556

557
	if (panel->backlight.combination_mode) {
558
		u8 lbpc;
559

560
		pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
561
		val *= lbpc;
562 563
	}

564 565 566
	return val;
}

567
static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
568
{
569
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
570 571
		return 0;

572
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
573 574 575 576
}

static u32 vlv_get_backlight(struct intel_connector *connector)
{
577
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
578
	enum pipe pipe = intel_connector_get_pipe(connector);
579

580
	return _vlv_get_backlight(dev_priv, pipe);
581 582
}

583 584
static u32 bxt_get_backlight(struct intel_connector *connector)
{
585
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
586
	struct intel_panel *panel = &connector->panel;
587

588 589
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
590 591
}

592 593 594
static u32 pwm_get_backlight(struct intel_connector *connector)
{
	struct intel_panel *panel = &connector->panel;
595
	struct pwm_state state;
596

597 598
	pwm_get_state(panel->backlight.pwm, &state);
	return pwm_get_relative_duty_cycle(&state, 100);
599 600
}

601
static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
602
{
603
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
604
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
605

606 607
	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);
608 609
}

610
static void pch_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
	u32 tmp;

616 617
	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);
618 619
}

620
static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
621
{
622
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
623
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
624
	struct intel_panel *panel = &connector->panel;
625
	u32 tmp, mask;
626

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

629
	if (panel->backlight.combination_mode) {
630 631
		u8 lbpc;

632
		lbpc = level * 0xfe / panel->backlight.max + 1;
633
		level /= lbpc;
634
		pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
635 636
	}

637
	if (IS_GEN(dev_priv, 4)) {
638 639
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
640
		level <<= 1;
641 642
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
643

644 645
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
646 647
}

648
static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
649
{
650
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
651
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
652
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
653 654
	u32 tmp;

655 656
	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);
657 658
}

659
static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
660
{
661
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
662
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
663
	struct intel_panel *panel = &connector->panel;
664

665 666
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
667 668
}

669
static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
670
{
671
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
672

673 674
	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
675 676
}

677
static void
678
intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
679
{
680
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
681
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
682
	struct intel_panel *panel = &connector->panel;
683

684
	drm_dbg_kms(&i915->drm, "set backlight PWM = %d\n", level);
685 686

	level = intel_panel_compute_brightness(connector, level);
687
	panel->backlight.funcs->set(conn_state, level);
688
}
689

690 691 692
/* set backlight brightness to level in range [0..max], assuming hw min is
 * respected.
 */
693
void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
694 695
				    u32 user_level, u32 user_max)
{
696
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
697
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
698 699 700
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

701
	/*
702
	 * Lack of crtc may occur during driver init because
703 704 705 706
	 * connection_mutex isn't held across the entire backlight
	 * setup + modeset readout, and the BIOS can issue the
	 * requests at any time.
	 */
707
	if (!panel->backlight.present || !conn_state->crtc)
708 709
		return;

710
	mutex_lock(&dev_priv->backlight_lock);
711

712
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
713 714 715

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

717
	if (panel->backlight.device)
718 719 720 721
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
722

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

726
	mutex_unlock(&dev_priv->backlight_lock);
727 728
}

729
static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
730
{
731
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
732
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
733 734
	u32 tmp;

735
	intel_panel_actually_set_backlight(old_conn_state, 0);
736

737 738 739 740 741 742 743 744
	/*
	 * 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.
	 */
745
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
746
	if (tmp & BLM_PWM_ENABLE) {
747 748
		drm_dbg_kms(&dev_priv->drm,
			    "cpu backlight was enabled, disabling\n");
749 750
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       tmp & ~BLM_PWM_ENABLE);
751 752
	}

753 754
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
755 756
}

757
static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
758
{
759
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
760
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
761 762
	u32 tmp;

763
	intel_panel_actually_set_backlight(old_conn_state, 0);
764

765 766
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
767

768 769
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
770 771
}

772
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
773
{
774
	intel_panel_actually_set_backlight(old_conn_state, 0);
775 776
}

777
static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
778
{
779
	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
780 781
	u32 tmp;

782
	intel_panel_actually_set_backlight(old_conn_state, 0);
783

784 785
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
786 787
}

788
static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
789
{
790
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
791
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
792
	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
793 794
	u32 tmp;

795
	intel_panel_actually_set_backlight(old_conn_state, 0);
796

797 798 799
	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);
800 801
}

802
static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
803
{
804
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
805
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
806 807
	struct intel_panel *panel = &connector->panel;
	u32 tmp, val;
808

809
	intel_panel_actually_set_backlight(old_conn_state, 0);
810

811 812 813 814
	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);
815 816

	if (panel->backlight.controller == 1) {
817
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
818
		val &= ~UTIL_PIN_ENABLE;
819
		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
820
	}
821 822
}

823
static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
824
{
825
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
826 827 828 829
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 tmp;

830
	intel_panel_actually_set_backlight(old_conn_state, 0);
831

832 833 834 835
	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);
836 837
}

838
static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
839
{
840
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
841 842
	struct intel_panel *panel = &connector->panel;

843 844
	panel->backlight.pwm_state.enabled = false;
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
845 846
}

847
void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
848
{
849
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
850
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
851
	struct intel_panel *panel = &connector->panel;
852

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

856
	/*
857
	 * Do not disable backlight on the vga_switcheroo path. When switching
858 859 860 861
	 * 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.
	 */
862
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
863 864
		drm_dbg_kms(&dev_priv->drm,
			    "Skipping backlight disable on vga switch\n");
865 866 867
		return;
	}

868
	mutex_lock(&dev_priv->backlight_lock);
869

870 871
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
872
	panel->backlight.enabled = false;
873
	panel->backlight.funcs->disable(old_conn_state);
874

875
	mutex_unlock(&dev_priv->backlight_lock);
876
}
877

878 879
static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
880
{
881
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
882
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
883
	struct intel_panel *panel = &connector->panel;
884
	u32 pch_ctl1, pch_ctl2, schicken;
885

886
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
887
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
888
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
889
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
890
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
891
	}
892

893
	if (HAS_PCH_LPT(dev_priv)) {
894
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN2);
895 896 897 898
		if (panel->backlight.alternate_pwm_increment)
			schicken |= LPT_PWM_GRANULARITY;
		else
			schicken &= ~LPT_PWM_GRANULARITY;
899
		intel_de_write(dev_priv, SOUTH_CHICKEN2, schicken);
900
	} else {
901
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN1);
902 903 904 905
		if (panel->backlight.alternate_pwm_increment)
			schicken |= SPT_PWM_GRANULARITY;
		else
			schicken &= ~SPT_PWM_GRANULARITY;
906
		intel_de_write(dev_priv, SOUTH_CHICKEN1, schicken);
907 908
	}

909
	pch_ctl2 = panel->backlight.max << 16;
910
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
911

912 913 914
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
915

916 917 918
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
919

920 921 922 923
	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);
924 925

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

929 930
static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
931
{
932
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
933
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
934
	struct intel_panel *panel = &connector->panel;
935
	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
936
	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
937

938
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
939
	if (cpu_ctl2 & BLM_PWM_ENABLE) {
940
		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
941
		cpu_ctl2 &= ~BLM_PWM_ENABLE;
942
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
943
	}
944

945
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
946
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
947
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
948
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
949
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
950
	}
951 952

	if (cpu_transcoder == TRANSCODER_EDP)
953
		cpu_ctl2 = BLM_TRANSCODER_EDP;
954
	else
955
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
956 957 958
	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);
959

960
	/* This won't stick until the above enable. */
961
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
962 963

	pch_ctl2 = panel->backlight.max << 16;
964
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
965 966 967 968

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

970 971 972 973
	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);
974 975
}

976 977
static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
978
{
979
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
980
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
981
	struct intel_panel *panel = &connector->panel;
982 983
	u32 ctl, freq;

984
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
985
	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
986
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
987
		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
988
	}
989

990 991 992 993 994
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;

	ctl = freq << 17;
995
	if (panel->backlight.combination_mode)
996
		ctl |= BLM_LEGACY_MODE;
997
	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
998 999
		ctl |= BLM_POLARITY_PNV;

1000 1001
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1002 1003

	/* XXX: combine this into above write? */
1004
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1005 1006 1007 1008 1009 1010

	/*
	 * 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.
	 */
1011
	if (IS_GEN(dev_priv, 2))
1012
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1013
}
1014

1015 1016
static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
				  const struct drm_connector_state *conn_state)
1017
{
1018
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1019
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1020
	struct intel_panel *panel = &connector->panel;
1021
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
1022
	u32 ctl, ctl2, freq;
1023

1024
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1025
	if (ctl2 & BLM_PWM_ENABLE) {
1026
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1027
		ctl2 &= ~BLM_PWM_ENABLE;
1028
		intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
1029
	}
1030

1031 1032 1033
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1034

1035
	ctl = freq << 16;
1036
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1037

1038 1039 1040 1041 1042
	ctl2 = BLM_PIPE(pipe);
	if (panel->backlight.combination_mode)
		ctl2 |= BLM_COMBINATION_MODE;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1043 1044 1045
	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);
1046

1047
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1048 1049
}

1050 1051
static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1052
{
1053
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1054
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1055
	struct intel_panel *panel = &connector->panel;
1056
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1057
	u32 ctl, ctl2;
1058

1059
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1060
	if (ctl2 & BLM_PWM_ENABLE) {
1061
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1062
		ctl2 &= ~BLM_PWM_ENABLE;
1063
		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
1064
	}
1065

1066
	ctl = panel->backlight.max << 16;
1067
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1068

1069
	/* XXX: combine this into above write? */
1070
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1071

1072 1073 1074
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1075 1076 1077 1078
	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);
1079 1080
}

1081 1082
static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1083
{
1084
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1085
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1086
	struct intel_panel *panel = &connector->panel;
1087
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1088 1089
	u32 pwm_ctl, val;

1090
	/* Controller 1 uses the utility pin. */
1091
	if (panel->backlight.controller == 1) {
1092
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1093
		if (val & UTIL_PIN_ENABLE) {
1094 1095
			drm_dbg_kms(&dev_priv->drm,
				    "util pin already enabled\n");
1096
			val &= ~UTIL_PIN_ENABLE;
1097
			intel_de_write(dev_priv, UTIL_PIN_CTL, val);
1098
		}
1099

1100 1101 1102
		val = 0;
		if (panel->backlight.util_pin_active_low)
			val |= UTIL_PIN_POLARITY;
1103 1104
		intel_de_write(dev_priv, UTIL_PIN_CTL,
			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1105 1106
	}

1107 1108
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1109
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1110
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1111
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1112 1113 1114
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1115 1116
	}

1117 1118 1119
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1120

1121
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1122 1123 1124 1125 1126

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

1127 1128 1129 1130 1131 1132
	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);
1133 1134
}

1135 1136
static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1137
{
1138
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1139 1140 1141 1142
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl;

1143 1144
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1145
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1146
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1147
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1148 1149 1150
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1151 1152
	}

1153 1154 1155
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1156

1157
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1158 1159 1160 1161 1162

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

1163 1164 1165 1166 1167 1168
	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);
1169 1170
}

1171 1172
static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1173
{
1174
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1175
	struct intel_panel *panel = &connector->panel;
1176
	int level = panel->backlight.level;
1177

1178 1179 1180 1181
	level = intel_panel_compute_brightness(connector, level);
	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
	panel->backlight.pwm_state.enabled = true;
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
1182 1183
}

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

1190 1191
	WARN_ON(panel->backlight.max == 0);

1192
	if (panel->backlight.level <= panel->backlight.min) {
1193
		panel->backlight.level = panel->backlight.max;
1194 1195
		if (panel->backlight.device)
			panel->backlight.device->props.brightness =
1196 1197 1198
				scale_hw_to_user(connector,
						 panel->backlight.level,
						 panel->backlight.device->props.max_brightness);
1199
	}
1200

1201
	panel->backlight.funcs->enable(crtc_state, conn_state);
1202
	panel->backlight.enabled = true;
1203 1204
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1205 1206 1207 1208 1209 1210 1211 1212
}

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;
1213
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1214 1215 1216 1217

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

1218
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1219 1220 1221 1222

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1223

1224
	mutex_unlock(&dev_priv->backlight_lock);
1225 1226
}

1227
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1228 1229 1230 1231 1232 1233 1234 1235 1236
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) {
1237
		val = panel->backlight.funcs->get(connector);
1238 1239 1240 1241 1242
		val = intel_panel_compute_brightness(connector, val);
	}

	mutex_unlock(&dev_priv->backlight_lock);

1243
	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
1244 1245 1246
	return val;
}

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
static u32 scale_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
{
	struct intel_panel *panel = &connector->panel;

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

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
/* 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);

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

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

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

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

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

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

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

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

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

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

1334
	return ret;
1335 1336
}

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

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

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

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

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

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

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

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

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

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

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

1394 1395 1396
	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1545
static u16 get_vbt_pwm_freq(struct drm_i915_private *dev_priv)
1546 1547 1548
{
	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;

1549
	if (pwm_freq_hz) {
1550 1551 1552
		drm_dbg_kms(&dev_priv->drm,
			    "VBT defined backlight frequency %u Hz\n",
			    pwm_freq_hz);
1553 1554
	} else {
		pwm_freq_hz = 200;
1555 1556 1557
		drm_dbg_kms(&dev_priv->drm,
			    "default backlight frequency %u Hz\n",
			    pwm_freq_hz);
1558 1559
	}

1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
	return pwm_freq_hz;
}

static u32 get_backlight_max_vbt(struct intel_connector *connector)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u16 pwm_freq_hz = get_vbt_pwm_freq(dev_priv);
	u32 pwm;

1570
	if (!panel->backlight.funcs->hz_to_pwm) {
1571 1572 1573 1574 1575
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion not supported\n");
		return 0;
	}

1576
	pwm = panel->backlight.funcs->hz_to_pwm(connector, pwm_freq_hz);
1577
	if (!pwm) {
1578 1579
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion failed\n");
1580 1581 1582 1583 1584 1585 1586 1587
		return 0;
	}

	return pwm;
}

/*
 * Note: The setup hooks can't assume pipe is set!
1588
 */
1589 1590
static u32 get_backlight_min_vbt(struct intel_connector *connector)
{
1591
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1592
	struct intel_panel *panel = &connector->panel;
1593
	int min;
1594

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

1597 1598 1599 1600 1601 1602 1603 1604 1605
	/*
	 * 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) {
1606 1607 1608
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1609 1610
	}

1611
	/* vbt value is a coefficient in range [0..255] */
1612
	return scale(min, 0, 255, 0, panel->backlight.max);
1613 1614
}

1615
static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1616
{
1617
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1618
	struct intel_panel *panel = &connector->panel;
1619 1620
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
	bool alt, cpu_mode;
1621 1622

	if (HAS_PCH_LPT(dev_priv))
1623
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1624
	else
1625
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1626
	panel->backlight.alternate_pwm_increment = alt;
1627

1628
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1629 1630
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1631
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1632
	panel->backlight.max = pch_ctl2 >> 16;
1633

1634
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1635

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

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

1642 1643
	panel->backlight.min = get_backlight_min_vbt(connector);

1644 1645 1646 1647 1648 1649 1650 1651 1652
	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);
1653 1654 1655
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1656

1657
	if (cpu_mode) {
1658 1659
		drm_dbg_kms(&dev_priv->drm,
			    "CPU backlight register was enabled, switching to PCH override\n");
1660 1661 1662

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

1666 1667
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1668
	}
1669 1670 1671 1672

	return 0;
}

1673
static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1674
{
1675
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1676
	struct intel_panel *panel = &connector->panel;
1677
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1678

1679
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1680 1681
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1682
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1683
	panel->backlight.max = pch_ctl2 >> 16;
1684 1685 1686 1687

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

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

1691 1692
	panel->backlight.min = get_backlight_min_vbt(connector);

1693
	val = pch_get_backlight(connector);
1694 1695 1696
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1697

1698
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1699
	panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1700
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1701

1702 1703 1704
	return 0;
}

1705
static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1706
{
1707
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1708
	struct intel_panel *panel = &connector->panel;
1709 1710
	u32 ctl, val;

1711
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1712

1713
	if (IS_GEN(dev_priv, 2) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1714 1715
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1716
	if (IS_PINEVIEW(dev_priv))
1717 1718 1719
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

	panel->backlight.max = ctl >> 17;
1720 1721 1722 1723 1724

	if (!panel->backlight.max) {
		panel->backlight.max = get_backlight_max_vbt(connector);
		panel->backlight.max >>= 1;
	}
1725 1726 1727 1728

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

1729 1730 1731
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1732 1733
	panel->backlight.min = get_backlight_min_vbt(connector);

1734
	val = i9xx_get_backlight(connector);
1735 1736 1737
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1738

1739
	panel->backlight.enabled = val != 0;
1740

1741 1742 1743
	return 0;
}

1744
static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1745
{
1746
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1747
	struct intel_panel *panel = &connector->panel;
1748 1749
	u32 ctl, ctl2, val;

1750
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1751 1752 1753
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1754
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1755
	panel->backlight.max = ctl >> 16;
1756 1757 1758

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);
1759 1760 1761 1762

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

1763 1764 1765
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1766 1767
	panel->backlight.min = get_backlight_min_vbt(connector);

1768
	val = i9xx_get_backlight(connector);
1769 1770 1771
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1772

1773
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1774

1775 1776 1777
	return 0;
}

1778
static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1779
{
1780
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1781
	struct intel_panel *panel = &connector->panel;
1782
	u32 ctl, ctl2, val;
1783

1784
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1785 1786
		return -ENODEV;

1787
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1788 1789
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1790
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1791
	panel->backlight.max = ctl >> 16;
1792 1793 1794 1795

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

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

1799 1800
	panel->backlight.min = get_backlight_min_vbt(connector);

1801
	val = _vlv_get_backlight(dev_priv, pipe);
1802 1803 1804
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1805

1806
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1807

1808 1809 1810
	return 0;
}

1811 1812 1813
static int
bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
1814
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1815 1816 1817
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

1818
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1819

1820 1821
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1822

1823
	/* Controller 1 uses the utility pin. */
1824
	if (panel->backlight.controller == 1) {
1825
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1826 1827 1828 1829 1830 1831
		panel->backlight.util_pin_active_low =
					val & UTIL_PIN_POLARITY;
	}

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1832 1833
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1834 1835 1836 1837

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

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

1841 1842
	panel->backlight.min = get_backlight_min_vbt(connector);

1843
	val = bxt_get_backlight(connector);
1844 1845 1846
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1847

1848
	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1849 1850 1851 1852

	return 0;
}

1853 1854 1855 1856 1857 1858 1859 1860
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;

	/*
1861 1862 1863
	 * 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.
1864 1865 1866
	 */
	panel->backlight.controller = 0;

1867 1868
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1869 1870 1871

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1872 1873
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1874 1875 1876 1877 1878 1879 1880

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

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

1881 1882
	panel->backlight.min = get_backlight_min_vbt(connector);

1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
	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;
}

1893 1894 1895 1896
static int pwm_setup_backlight(struct intel_connector *connector,
			       enum pipe pipe)
{
	struct drm_device *dev = connector->base.dev;
1897
	struct drm_i915_private *dev_priv = to_i915(dev);
1898
	struct intel_panel *panel = &connector->panel;
1899
	const char *desc;
1900
	u32 level;
1901

1902 1903 1904 1905 1906 1907 1908 1909 1910
	/* 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";
	}

1911
	if (IS_ERR(panel->backlight.pwm)) {
1912 1913
		drm_err(&dev_priv->drm, "Failed to get the %s PWM chip\n",
			desc);
1914 1915 1916 1917
		panel->backlight.pwm = NULL;
		return -ENODEV;
	}

1918
	panel->backlight.max = 100; /* 100% */
1919
	panel->backlight.min = get_backlight_min_vbt(connector);
1920

1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
	if (pwm_is_enabled(panel->backlight.pwm)) {
		/* PWM is already enabled, use existing settings */
		pwm_get_state(panel->backlight.pwm, &panel->backlight.pwm_state);

		level = pwm_get_relative_duty_cycle(&panel->backlight.pwm_state,
						    100);
		level = intel_panel_compute_brightness(connector, level);
		panel->backlight.level = clamp(level, panel->backlight.min,
					       panel->backlight.max);
		panel->backlight.enabled = true;

		drm_dbg_kms(&dev_priv->drm, "PWM already enabled at freq %ld, VBT freq %d, level %d\n",
			    NSEC_PER_SEC / (unsigned long)panel->backlight.pwm_state.period,
			    get_vbt_pwm_freq(dev_priv), level);
	} else {
		/* Set period from VBT frequency, leave other settings at 0. */
		panel->backlight.pwm_state.period =
			NSEC_PER_SEC / get_vbt_pwm_freq(dev_priv);
	}
1940

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

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

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

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

1983
	/* ensure intel_panel has been initialized first */
1984
	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.funcs))
1985 1986
		return -ENODEV;

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

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

1999 2000
	panel->backlight.present = true;

2001 2002 2003 2004 2005
	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);
2006

2007 2008 2009
	return 0;
}

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

2016
	panel->backlight.present = false;
2017
}
2018

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
static const struct intel_panel_bl_funcs bxt_funcs = {
	.setup = bxt_setup_backlight,
	.enable = bxt_enable_backlight,
	.disable = bxt_disable_backlight,
	.set = bxt_set_backlight,
	.get = bxt_get_backlight,
	.hz_to_pwm = bxt_hz_to_pwm,
};

static const struct intel_panel_bl_funcs cnp_funcs = {
	.setup = cnp_setup_backlight,
	.enable = cnp_enable_backlight,
	.disable = cnp_disable_backlight,
	.set = bxt_set_backlight,
	.get = bxt_get_backlight,
	.hz_to_pwm = cnp_hz_to_pwm,
};

static const struct intel_panel_bl_funcs lpt_funcs = {
	.setup = lpt_setup_backlight,
	.enable = lpt_enable_backlight,
	.disable = lpt_disable_backlight,
	.set = lpt_set_backlight,
	.get = lpt_get_backlight,
	.hz_to_pwm = lpt_hz_to_pwm,
};

static const struct intel_panel_bl_funcs spt_funcs = {
	.setup = lpt_setup_backlight,
	.enable = lpt_enable_backlight,
	.disable = lpt_disable_backlight,
	.set = lpt_set_backlight,
	.get = lpt_get_backlight,
	.hz_to_pwm = spt_hz_to_pwm,
};

static const struct intel_panel_bl_funcs pch_funcs = {
	.setup = pch_setup_backlight,
	.enable = pch_enable_backlight,
	.disable = pch_disable_backlight,
	.set = pch_set_backlight,
	.get = pch_get_backlight,
	.hz_to_pwm = pch_hz_to_pwm,
};

static const struct intel_panel_bl_funcs pwm_funcs = {
	.setup = pwm_setup_backlight,
	.enable = pwm_enable_backlight,
	.disable = pwm_disable_backlight,
	.set = pwm_set_backlight,
	.get = pwm_get_backlight,
};

static const struct intel_panel_bl_funcs vlv_funcs = {
	.setup = vlv_setup_backlight,
	.enable = vlv_enable_backlight,
	.disable = vlv_disable_backlight,
	.set = vlv_set_backlight,
	.get = vlv_get_backlight,
	.hz_to_pwm = vlv_hz_to_pwm,
};

static const struct intel_panel_bl_funcs i965_funcs = {
	.setup = i965_setup_backlight,
	.enable = i965_enable_backlight,
	.disable = i965_disable_backlight,
	.set = i9xx_set_backlight,
	.get = i9xx_get_backlight,
	.hz_to_pwm = i965_hz_to_pwm,
};

static const struct intel_panel_bl_funcs i9xx_funcs = {
	.setup = i9xx_setup_backlight,
	.enable = i9xx_enable_backlight,
	.disable = i9xx_disable_backlight,
	.set = i9xx_set_backlight,
	.get = i9xx_get_backlight,
	.hz_to_pwm = i9xx_hz_to_pwm,
};

2099
/* Set up chip specific backlight functions */
2100 2101
static void
intel_panel_init_backlight_funcs(struct intel_panel *panel)
2102
{
2103
	struct intel_connector *connector =
2104
		container_of(panel, struct intel_connector, panel);
2105
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
2106

2107 2108 2109 2110
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;

2111 2112 2113 2114
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

2115
	if (IS_GEN9_LP(dev_priv)) {
2116
		panel->backlight.funcs = &bxt_funcs;
2117
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
2118
		panel->backlight.funcs = &cnp_funcs;
2119
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
2120
		if (HAS_PCH_LPT(dev_priv))
2121
			panel->backlight.funcs = &lpt_funcs;
2122
		else
2123
			panel->backlight.funcs = &spt_funcs;
2124
	} else if (HAS_PCH_SPLIT(dev_priv)) {
2125
		panel->backlight.funcs = &pch_funcs;
2126
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2127
		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
2128
			panel->backlight.funcs = &pwm_funcs;
2129
		} else {
2130
			panel->backlight.funcs = &vlv_funcs;
2131
		}
2132
	} else if (IS_GEN(dev_priv, 4)) {
2133
		panel->backlight.funcs = &i965_funcs;
2134
	} else {
2135
		panel->backlight.funcs = &i9xx_funcs;
2136 2137 2138
	}
}

2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
enum drm_connector_status
intel_panel_detect(struct drm_connector *connector, bool force)
{
	struct drm_i915_private *i915 = to_i915(connector->dev);

	if (!INTEL_DISPLAY_ENABLED(i915))
		return connector_status_disconnected;

	return connector_status_connected;
}

2150
int intel_panel_init(struct intel_panel *panel,
2151 2152
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2153
{
2154 2155
	intel_panel_init_backlight_funcs(panel);

2156
	panel->fixed_mode = fixed_mode;
2157
	panel->downclock_mode = downclock_mode;
2158

2159 2160 2161 2162 2163
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2164 2165 2166
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2167 2168
	intel_panel_destroy_backlight(panel);

2169 2170
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2171 2172 2173 2174

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