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

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

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

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

43 44
#define CRC_PMIC_PWM_PERIOD_NS	21333

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

	drm_mode_set_crtcinfo(adjusted_mode, 0);
52 53
}

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

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

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

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

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

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

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

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

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

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

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

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

		return fixed_mode;
	}

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

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

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

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

	return fixed_mode;
149 150
}

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

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

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

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

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

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

	return fixed_mode;
}

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

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

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

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

	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
232 233
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
234
		break;
235 236 237 238

	default:
		WARN(1, "bad panel fit mode: %d\n", fitting_mode);
		return;
239 240 241
	}

done:
242 243
	pipe_config->pch_pfit.pos = (x << 16) | y;
	pipe_config->pch_pfit.size = (width << 16) | height;
244
	pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
245
}
246

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

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

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

261 262 263
	adjusted_mode->crtc_hdisplay = width;
	adjusted_mode->crtc_hblank_start = width + border;
	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
264

265 266
	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
267 268 269
}

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

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

280
	border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
281

282 283 284
	adjusted_mode->crtc_vdisplay = height;
	adjusted_mode->crtc_vblank_start = height + border;
	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
285

286 287
	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

static inline u32 panel_fitter_scaling(u32 source, u32 target)
{
	/*
	 * 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;
}

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

	/* 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;
319
	else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
320 321 322
		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
}

323
static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
324 325 326
			      u32 *pfit_control, u32 *pfit_pgm_ratios,
			      u32 *border)
{
327
	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
328
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
329 330
		pipe_config->pipe_src_h;
	u32 scaled_height = pipe_config->pipe_src_w *
331
		adjusted_mode->crtc_vdisplay;
332 333 334 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 /
				    pipe_config->pipe_src_h);

		*border = LVDS_BORDER_ENABLE;
345
		if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
346
			bits = panel_fitter_scaling(pipe_config->pipe_src_h,
347
						    adjusted_mode->crtc_vdisplay);
348 349 350 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 /
				  pipe_config->pipe_src_w);

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

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

380
void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
381
			      struct intel_crtc_state *pipe_config,
382 383
			      int fitting_mode)
{
384
	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
385
	u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
386
	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
387 388

	/* Native modes don't need fitting */
389 390
	if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
	    adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
391 392 393 394 395 396 397 398
		goto out;

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

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

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

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

449 450
	pipe_config->gmch_pfit.control = pfit_control;
	pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
451
	pipe_config->gmch_pfit.lvds_border_bits = border;
452 453
}

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

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

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

	/* avoid overflows */
478 479 480
	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);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	target_val += target_min;

	return target_val;
}

/* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
static inline 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);
}

/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
 * to [hw_min..hw_max]. */
static inline u32 clamp_user_to_hw(struct intel_connector *connector,
				   u32 user_level, u32 user_max)
{
	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]. */
static inline u32 scale_hw_to_user(struct intel_connector *connector,
				   u32 hw_level, u32 user_max)
{
	struct intel_panel *panel = &connector->panel;

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

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

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

528
	if (i915_modparams.invert_brightness < 0)
529 530
		return val;

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

	return val;
}

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

543
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
544
}
545

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

550
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
551
}
552

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

559
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
560
	if (INTEL_GEN(dev_priv) < 4)
561
		val >>= 1;
562

563
	if (panel->backlight.combination_mode) {
564
		u8 lbpc;
565

566
		pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
567
		val *= lbpc;
568 569
	}

570 571 572
	return val;
}

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

578
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
579 580 581 582
}

static u32 vlv_get_backlight(struct intel_connector *connector)
{
583
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
584
	enum pipe pipe = intel_connector_get_pipe(connector);
585

586
	return _vlv_get_backlight(dev_priv, pipe);
587 588
}

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

594 595
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
596 597
}

598 599 600 601 602 603 604 605 606
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);
}

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

612 613
	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);
614 615
}

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

622 623
	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);
624 625
}

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

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

635
	if (panel->backlight.combination_mode) {
636 637
		u8 lbpc;

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

643
	if (IS_GEN(dev_priv, 4)) {
644 645
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
646
		level <<= 1;
647 648
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
649

650 651
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
652 653
}

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

661 662
	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);
663 664
}

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

671 672
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
673 674
}

675
static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
676
{
677
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
678 679 680 681 682
	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);
}

683
static void
684
intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
685
{
686
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
687
	struct intel_panel *panel = &connector->panel;
688 689 690 691

	DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);

	level = intel_panel_compute_brightness(connector, level);
692
	panel->backlight.set(conn_state, level);
693
}
694

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

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

715
	mutex_lock(&dev_priv->backlight_lock);
716

717
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
718 719 720

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

722
	if (panel->backlight.device)
723 724 725 726
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
727

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

731
	mutex_unlock(&dev_priv->backlight_lock);
732 733
}

734
static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
735
{
736
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
737
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
738 739
	u32 tmp;

740
	intel_panel_actually_set_backlight(old_conn_state, 0);
741

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

758 759
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
760 761
}

762
static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
763
{
764
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
765
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
766 767
	u32 tmp;

768
	intel_panel_actually_set_backlight(old_conn_state, 0);
769

770 771
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
772

773 774
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
775 776
}

777
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
778
{
779
	intel_panel_actually_set_backlight(old_conn_state, 0);
780 781
}

782
static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
783
{
784
	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
785 786
	u32 tmp;

787
	intel_panel_actually_set_backlight(old_conn_state, 0);
788

789 790
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
791 792
}

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

800
	intel_panel_actually_set_backlight(old_conn_state, 0);
801

802 803 804
	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);
805 806
}

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

814
	intel_panel_actually_set_backlight(old_conn_state, 0);
815

816 817 818 819
	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);
820 821

	if (panel->backlight.controller == 1) {
822
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
823
		val &= ~UTIL_PIN_ENABLE;
824
		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
825
	}
826 827
}

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

835
	intel_panel_actually_set_backlight(old_conn_state, 0);
836

837 838 839 840
	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);
841 842
}

843
static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
844
{
845
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
846 847 848
	struct intel_panel *panel = &connector->panel;

	/* Disable the backlight */
849
	intel_panel_actually_set_backlight(old_conn_state, 0);
850 851 852 853
	usleep_range(2000, 3000);
	pwm_disable(panel->backlight.pwm);
}

854
void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
855
{
856
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
857
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
858
	struct intel_panel *panel = &connector->panel;
859

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

863
	/*
864
	 * Do not disable backlight on the vga_switcheroo path. When switching
865 866 867 868
	 * 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.
	 */
869
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
870 871
		drm_dbg(&dev_priv->drm,
			"Skipping backlight disable on vga switch\n");
872 873 874
		return;
	}

875
	mutex_lock(&dev_priv->backlight_lock);
876

877 878
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
879
	panel->backlight.enabled = false;
880
	panel->backlight.disable(old_conn_state);
881

882
	mutex_unlock(&dev_priv->backlight_lock);
883
}
884

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

893
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
894
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
895
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
896
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
897
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
898
	}
899

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

916
	pch_ctl2 = panel->backlight.max << 16;
917
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
918

919 920 921
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
922

923 924 925
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
926

927 928 929 930
	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);
931 932

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

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

945
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
946
	if (cpu_ctl2 & BLM_PWM_ENABLE) {
947
		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
948
		cpu_ctl2 &= ~BLM_PWM_ENABLE;
949
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
950
	}
951

952
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
953
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
954
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
955
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
956
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
957
	}
958 959

	if (cpu_transcoder == TRANSCODER_EDP)
960
		cpu_ctl2 = BLM_TRANSCODER_EDP;
961
	else
962
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
963 964 965
	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);
966

967
	/* This won't stick until the above enable. */
968
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
969 970

	pch_ctl2 = panel->backlight.max << 16;
971
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
972 973 974 975

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

977 978 979 980
	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);
981 982
}

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

991
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
992
	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
993
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
994
		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
995
	}
996

997 998 999 1000 1001
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;

	ctl = freq << 17;
1002
	if (panel->backlight.combination_mode)
1003
		ctl |= BLM_LEGACY_MODE;
1004
	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
1005 1006
		ctl |= BLM_POLARITY_PNV;

1007 1008
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1009 1010

	/* XXX: combine this into above write? */
1011
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1012 1013 1014 1015 1016 1017

	/*
	 * 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.
	 */
1018
	if (IS_GEN(dev_priv, 2))
1019
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1020
}
1021

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

1031
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1032
	if (ctl2 & BLM_PWM_ENABLE) {
1033
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1034
		ctl2 &= ~BLM_PWM_ENABLE;
1035
		intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
1036
	}
1037

1038 1039 1040
	freq = panel->backlight.max;
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1041

1042
	ctl = freq << 16;
1043
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1044

1045 1046 1047 1048 1049
	ctl2 = BLM_PIPE(pipe);
	if (panel->backlight.combination_mode)
		ctl2 |= BLM_COMBINATION_MODE;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1050 1051 1052
	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);
1053

1054
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1055 1056
}

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

1066
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1067
	if (ctl2 & BLM_PWM_ENABLE) {
1068
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1069
		ctl2 &= ~BLM_PWM_ENABLE;
1070
		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
1071
	}
1072

1073
	ctl = panel->backlight.max << 16;
1074
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1075

1076
	/* XXX: combine this into above write? */
1077
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1078

1079 1080 1081
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1082 1083 1084 1085
	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);
1086 1087
}

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

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

1107 1108 1109
		val = 0;
		if (panel->backlight.util_pin_active_low)
			val |= UTIL_PIN_POLARITY;
1110 1111
		intel_de_write(dev_priv, UTIL_PIN_CTL,
			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1112 1113
	}

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

1124 1125 1126
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1127

1128
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1129 1130 1131 1132 1133

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

1134 1135 1136 1137 1138 1139
	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);
1140 1141
}

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

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

1160 1161 1162
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
		       panel->backlight.max);
1163

1164
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1165 1166 1167 1168 1169

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

1170 1171 1172 1173 1174 1175
	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);
1176 1177
}

1178 1179
static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
				 const struct drm_connector_state *conn_state)
1180
{
1181
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1182 1183 1184
	struct intel_panel *panel = &connector->panel;

	pwm_enable(panel->backlight.pwm);
1185
	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
1186 1187
}

1188 1189
static void __intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
					   const struct drm_connector_state *conn_state)
1190
{
1191
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1192
	struct intel_panel *panel = &connector->panel;
1193

1194 1195
	WARN_ON(panel->backlight.max == 0);

1196
	if (panel->backlight.level <= panel->backlight.min) {
1197
		panel->backlight.level = panel->backlight.max;
1198 1199
		if (panel->backlight.device)
			panel->backlight.device->props.brightness =
1200 1201 1202
				scale_hw_to_user(connector,
						 panel->backlight.level,
						 panel->backlight.device->props.max_brightness);
1203
	}
1204

1205
	panel->backlight.enable(crtc_state, conn_state);
1206
	panel->backlight.enabled = true;
1207 1208
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1209 1210 1211 1212 1213 1214 1215 1216
}

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;
1217
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1218 1219 1220 1221

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

1222
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1223 1224 1225 1226

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1227

1228
	mutex_unlock(&dev_priv->backlight_lock);
1229 1230
}

1231
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
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);

1247
	drm_dbg(&dev_priv->drm, "get backlight PWM = %d\n", val);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	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);

1265
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

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

1276
static int intel_backlight_device_update_status(struct backlight_device *bd)
1277
{
1278
	struct intel_connector *connector = bl_get_data(bd);
1279
	struct intel_panel *panel = &connector->panel;
1280 1281
	struct drm_device *dev = connector->base.dev;

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

	/*
	 * 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) {
1295
		if (panel->backlight.power) {
1296 1297
			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
				bd->props.brightness != 0;
1298
			panel->backlight.power(connector, enable);
1299 1300 1301 1302 1303
		}
	} else {
		bd->props.power = FB_BLANK_POWERDOWN;
	}

1304
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1305 1306 1307
	return 0;
}

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

1316
	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
1317
		u32 hw_level;
1318

1319
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1320

1321 1322 1323 1324 1325 1326
		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);
	}
1327

1328
	return ret;
1329 1330
}

1331 1332 1333
static const struct backlight_ops intel_backlight_device_ops = {
	.update_status = intel_backlight_device_update_status,
	.get_brightness = intel_backlight_device_get_brightness,
1334 1335
};

1336
int intel_backlight_device_register(struct intel_connector *connector)
1337
{
1338
	struct intel_panel *panel = &connector->panel;
1339 1340
	struct backlight_properties props;

1341
	if (WARN_ON(panel->backlight.device))
1342 1343
		return -ENODEV;

1344 1345 1346
	if (!panel->backlight.present)
		return 0;

1347
	WARN_ON(panel->backlight.max == 0);
1348

1349
	memset(&props, 0, sizeof(props));
1350
	props.type = BACKLIGHT_RAW;
1351 1352 1353 1354 1355

	/*
	 * Note: Everything should work even if the backlight device max
	 * presented to the userspace is arbitrarily chosen.
	 */
1356
	props.max_brightness = panel->backlight.max;
1357 1358 1359
	props.brightness = scale_hw_to_user(connector,
					    panel->backlight.level,
					    props.max_brightness);
1360

1361 1362 1363 1364 1365
	if (panel->backlight.enabled)
		props.power = FB_BLANK_UNBLANK;
	else
		props.power = FB_BLANK_POWERDOWN;

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

1376
	if (IS_ERR(panel->backlight.device)) {
1377
		DRM_ERROR("Failed to register backlight: %ld\n",
1378 1379
			  PTR_ERR(panel->backlight.device));
		panel->backlight.device = NULL;
1380 1381
		return -ENODEV;
	}
1382 1383 1384 1385

	DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
		      connector->base.name);

1386 1387 1388
	return 0;
}

1389
void intel_backlight_device_unregister(struct intel_connector *connector)
1390
{
1391 1392 1393 1394 1395
	struct intel_panel *panel = &connector->panel;

	if (panel->backlight.device) {
		backlight_device_unregister(panel->backlight.device);
		panel->backlight.device = NULL;
1396
	}
1397
}
1398 1399
#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */

1400 1401 1402 1403 1404 1405 1406 1407
/*
 * 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);

1408 1409
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz);
1410 1411
}

1412 1413 1414 1415 1416
/*
 * BXT: PWM clock frequency = 19.2 MHz.
 */
static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1417
	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1418 1419
}

1420
/*
1421 1422 1423 1424 1425 1426
 * 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)
{
1427
	struct intel_panel *panel = &connector->panel;
1428
	u32 mul;
1429

1430
	if (panel->backlight.alternate_pwm_increment)
1431 1432 1433 1434
		mul = 128;
	else
		mul = 16;

1435
	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1436 1437 1438 1439 1440 1441 1442 1443 1444
}

/*
 * 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)
{
1445
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1446
	struct intel_panel *panel = &connector->panel;
1447 1448
	u32 mul, clock;

1449
	if (panel->backlight.alternate_pwm_increment)
1450 1451 1452 1453
		mul = 16;
	else
		mul = 128;

V
Ville Syrjälä 已提交
1454
	if (HAS_PCH_LPT_H(dev_priv))
1455 1456 1457 1458
		clock = MHz(135); /* LPT:H */
	else
		clock = MHz(24); /* LPT:LP */

1459
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1460 1461 1462 1463 1464 1465 1466 1467
}

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

1470 1471
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz * 128);
1472 1473 1474 1475 1476 1477
}

/*
 * 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.
1478
 *
1479 1480 1481 1482 1483
 * 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)
{
1484
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1485 1486
	int clock;

1487
	if (IS_PINEVIEW(dev_priv))
1488
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1489
	else
1490
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1491

1492
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1493 1494 1495 1496
}

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

	if (IS_G4X(dev_priv))
1506
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1507
	else
1508
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1509

1510
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1511 1512 1513 1514 1515 1516 1517 1518 1519
}

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

1523
	if ((intel_de_read(dev_priv, CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1524 1525
		if (IS_CHERRYVIEW(dev_priv))
			clock = KHz(19200);
1526
		else
1527 1528
			clock = MHz(25);
		mul = 16;
1529
	} else {
1530
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1531
		mul = 128;
1532
	}
1533

1534
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1535 1536 1537 1538
}

static u32 get_backlight_max_vbt(struct intel_connector *connector)
{
1539
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1540
	struct intel_panel *panel = &connector->panel;
1541 1542 1543
	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
	u32 pwm;

1544
	if (!panel->backlight.hz_to_pwm) {
1545 1546
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion not supported\n");
1547 1548 1549
		return 0;
	}

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

1561
	pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
1562
	if (!pwm) {
1563 1564
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion failed\n");
1565 1566 1567 1568 1569 1570 1571 1572
		return 0;
	}

	return pwm;
}

/*
 * Note: The setup hooks can't assume pipe is set!
1573
 */
1574 1575
static u32 get_backlight_min_vbt(struct intel_connector *connector)
{
1576
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1577
	struct intel_panel *panel = &connector->panel;
1578
	int min;
1579

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

1582 1583 1584 1585 1586 1587 1588 1589 1590
	/*
	 * 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) {
1591 1592 1593
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1594 1595
	}

1596
	/* vbt value is a coefficient in range [0..255] */
1597
	return scale(min, 0, 255, 0, panel->backlight.max);
1598 1599
}

1600
static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1601
{
1602
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1603
	struct intel_panel *panel = &connector->panel;
1604 1605
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
	bool alt, cpu_mode;
1606 1607

	if (HAS_PCH_LPT(dev_priv))
1608
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1609
	else
1610
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1611
	panel->backlight.alternate_pwm_increment = alt;
1612

1613
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1614 1615
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1616
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1617
	panel->backlight.max = pch_ctl2 >> 16;
1618

1619
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1620

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

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

1627 1628
	panel->backlight.min = get_backlight_min_vbt(connector);

1629 1630 1631 1632 1633 1634 1635 1636 1637
	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);
1638 1639 1640
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1641

1642
	if (cpu_mode) {
1643 1644
		drm_dbg_kms(&dev_priv->drm,
			    "CPU backlight register was enabled, switching to PCH override\n");
1645 1646 1647

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

1651 1652
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1653
	}
1654 1655 1656 1657

	return 0;
}

1658
static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
1659
{
1660
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1661
	struct intel_panel *panel = &connector->panel;
1662
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
1663

1664
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1665 1666
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1667
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1668
	panel->backlight.max = pch_ctl2 >> 16;
1669 1670 1671 1672

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

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

1676 1677
	panel->backlight.min = get_backlight_min_vbt(connector);

1678
	val = pch_get_backlight(connector);
1679 1680 1681
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1682

1683
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1684
	panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1685
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1686

1687 1688 1689
	return 0;
}

1690
static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1691
{
1692
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1693
	struct intel_panel *panel = &connector->panel;
1694 1695
	u32 ctl, val;

1696
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1697

1698
	if (IS_GEN(dev_priv, 2) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1699 1700
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1701
	if (IS_PINEVIEW(dev_priv))
1702 1703 1704
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

	panel->backlight.max = ctl >> 17;
1705 1706 1707 1708 1709

	if (!panel->backlight.max) {
		panel->backlight.max = get_backlight_max_vbt(connector);
		panel->backlight.max >>= 1;
	}
1710 1711 1712 1713

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

1714 1715 1716
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1717 1718
	panel->backlight.min = get_backlight_min_vbt(connector);

1719
	val = i9xx_get_backlight(connector);
1720 1721 1722
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1723

1724
	panel->backlight.enabled = val != 0;
1725

1726 1727 1728
	return 0;
}

1729
static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1730
{
1731
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1732
	struct intel_panel *panel = &connector->panel;
1733 1734
	u32 ctl, ctl2, val;

1735
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1736 1737 1738
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1739
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1740
	panel->backlight.max = ctl >> 16;
1741 1742 1743

	if (!panel->backlight.max)
		panel->backlight.max = get_backlight_max_vbt(connector);
1744 1745 1746 1747

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

1748 1749 1750
	if (panel->backlight.combination_mode)
		panel->backlight.max *= 0xff;

1751 1752
	panel->backlight.min = get_backlight_min_vbt(connector);

1753
	val = i9xx_get_backlight(connector);
1754 1755 1756
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1757

1758
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1759

1760 1761 1762
	return 0;
}

1763
static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1764
{
1765
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1766
	struct intel_panel *panel = &connector->panel;
1767
	u32 ctl, ctl2, val;
1768

1769
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1770 1771
		return -ENODEV;

1772
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1773 1774
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1775
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1776
	panel->backlight.max = ctl >> 16;
1777 1778 1779 1780

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

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

1784 1785
	panel->backlight.min = get_backlight_min_vbt(connector);

1786
	val = _vlv_get_backlight(dev_priv, pipe);
1787 1788 1789
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1790

1791
	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
1792

1793 1794 1795
	return 0;
}

1796 1797 1798
static int
bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
1799
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1800 1801 1802
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

1803
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1804

1805 1806
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1807

1808
	/* Controller 1 uses the utility pin. */
1809
	if (panel->backlight.controller == 1) {
1810
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1811 1812 1813 1814 1815 1816
		panel->backlight.util_pin_active_low =
					val & UTIL_PIN_POLARITY;
	}

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1817 1818
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1819 1820 1821 1822

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

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

1826 1827
	panel->backlight.min = get_backlight_min_vbt(connector);

1828
	val = bxt_get_backlight(connector);
1829 1830 1831
	val = intel_panel_compute_brightness(connector, val);
	panel->backlight.level = clamp(val, panel->backlight.min,
				       panel->backlight.max);
1832

1833
	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1834 1835 1836 1837

	return 0;
}

1838 1839 1840 1841 1842 1843 1844 1845
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;

	/*
1846 1847 1848
	 * 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.
1849 1850 1851
	 */
	panel->backlight.controller = 0;

1852 1853
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1854 1855 1856

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
	panel->backlight.max =
1857 1858
		intel_de_read(dev_priv,
			      BXT_BLC_PWM_FREQ(panel->backlight.controller));
1859 1860 1861 1862 1863 1864 1865

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

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

1866 1867
	panel->backlight.min = get_backlight_min_vbt(connector);

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
	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;
}

1878 1879 1880 1881
static int pwm_setup_backlight(struct intel_connector *connector,
			       enum pipe pipe)
{
	struct drm_device *dev = connector->base.dev;
1882
	struct drm_i915_private *dev_priv = to_i915(dev);
1883
	struct intel_panel *panel = &connector->panel;
1884
	const char *desc;
1885
	u32 level, ns;
1886 1887
	int retval;

1888 1889 1890 1891 1892 1893 1894 1895 1896
	/* 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";
	}

1897
	if (IS_ERR(panel->backlight.pwm)) {
1898 1899
		drm_err(&dev_priv->drm, "Failed to get the %s PWM chip\n",
			desc);
1900 1901 1902 1903
		panel->backlight.pwm = NULL;
		return -ENODEV;
	}

1904 1905 1906 1907 1908 1909
	/*
	 * FIXME: pwm_apply_args() should be removed when switching to
	 * the atomic PWM API.
	 */
	pwm_apply_args(panel->backlight.pwm);

1910 1911 1912 1913 1914 1915
	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);
1916
	if (retval < 0) {
1917
		drm_err(&dev_priv->drm, "Failed to configure the pwm chip\n");
1918 1919 1920 1921 1922
		pwm_put(panel->backlight.pwm);
		panel->backlight.pwm = NULL;
		return retval;
	}

1923 1924 1925 1926
	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);
1927 1928
	panel->backlight.enabled = panel->backlight.level != 0;

1929 1930
	drm_info(&dev_priv->drm, "Using %s PWM for LCD backlight control\n",
		 desc);
1931 1932 1933
	return 0;
}

1934 1935
void intel_panel_update_backlight(struct intel_atomic_state *state,
				  struct intel_encoder *encoder,
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
				  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);
}

1953
int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
1954
{
1955
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
1956
	struct intel_connector *intel_connector = to_intel_connector(connector);
1957
	struct intel_panel *panel = &intel_connector->panel;
1958
	int ret;
1959

1960
	if (!dev_priv->vbt.backlight.present) {
1961
		if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
1962 1963
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT, but present per quirk\n");
1964
		} else {
1965 1966
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT\n");
1967 1968
			return 0;
		}
1969 1970
	}

1971
	/* ensure intel_panel has been initialized first */
1972
	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.setup))
1973 1974
		return -ENODEV;

1975
	/* set level and max in panel struct */
1976
	mutex_lock(&dev_priv->backlight_lock);
1977
	ret = panel->backlight.setup(intel_connector, pipe);
1978
	mutex_unlock(&dev_priv->backlight_lock);
1979 1980

	if (ret) {
1981 1982 1983
		drm_dbg_kms(&dev_priv->drm,
			    "failed to setup backlight for connector %s\n",
			    connector->name);
1984 1985
		return ret;
	}
1986

1987 1988
	panel->backlight.present = true;

1989 1990 1991 1992 1993
	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);
1994

1995 1996 1997
	return 0;
}

1998
static void intel_panel_destroy_backlight(struct intel_panel *panel)
1999
{
2000 2001 2002 2003
	/* dispose of the pwm */
	if (panel->backlight.pwm)
		pwm_put(panel->backlight.pwm);

2004
	panel->backlight.present = false;
2005
}
2006

2007
/* Set up chip specific backlight functions */
2008 2009
static void
intel_panel_init_backlight_funcs(struct intel_panel *panel)
2010
{
2011
	struct intel_connector *connector =
2012
		container_of(panel, struct intel_connector, panel);
2013
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
2014

2015 2016 2017 2018
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;

2019 2020 2021 2022
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

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

2086
int intel_panel_init(struct intel_panel *panel,
2087 2088
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2089
{
2090 2091
	intel_panel_init_backlight_funcs(panel);

2092
	panel->fixed_mode = fixed_mode;
2093
	panel->downclock_mode = downclock_mode;
2094

2095 2096 2097 2098 2099
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2100 2101 2102
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2103 2104
	intel_panel_destroy_backlight(panel);

2105 2106
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2107 2108 2109 2110

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