intel_panel.c 70.0 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_de.h"
39
#include "intel_display_types.h"
40
#include "intel_dp_aux_backlight.h"
41
#include "intel_dsi_dcs_backlight.h"
42
#include "intel_panel.h"
43

44 45 46 47 48 49 50 51
bool intel_panel_use_ssc(struct drm_i915_private *i915)
{
	if (i915->params.panel_use_ssc >= 0)
		return i915->params.panel_use_ssc != 0;
	return i915->vbt.lvds_use_ssc
		&& !(i915->quirks & QUIRK_LVDS_SSC_DISABLE);
}

52
void
53
intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
54 55
		       struct drm_display_mode *adjusted_mode)
{
56
	drm_mode_copy(adjusted_mode, fixed_mode);
57 58

	drm_mode_set_crtcinfo(adjusted_mode, 0);
59 60
}

61 62 63 64 65 66 67 68 69 70
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 已提交
71
struct drm_display_mode *
72 73
intel_panel_edid_downclock_mode(struct intel_connector *connector,
				const struct drm_display_mode *fixed_mode)
J
Jani Nikula 已提交
74
{
75
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
76
	const struct drm_display_mode *scan, *best_mode = NULL;
77
	struct drm_display_mode *downclock_mode;
78
	int best_clock = fixed_mode->clock;
J
Jani Nikula 已提交
79

80
	list_for_each_entry(scan, &connector->base.probed_modes, head) {
J
Jani Nikula 已提交
81 82 83 84 85 86 87
		/*
		 * 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.
		 */
88 89 90 91 92 93 94 95
		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 已提交
96 97 98
		}
	}

99 100 101 102 103 104 105
	if (!best_mode)
		return NULL;

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

106 107 108
	drm_dbg_kms(&dev_priv->drm,
		    "[CONNECTOR:%d:%s] using downclock mode from EDID: ",
		    connector->base.base.id, connector->base.name);
109
	drm_mode_debug_printmodeline(downclock_mode);
110

111
	return downclock_mode;
J
Jani Nikula 已提交
112 113
}

114 115 116 117 118
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;
119 120 121 122
	struct drm_display_mode *fixed_mode;

	if (list_empty(&connector->base.probed_modes))
		return NULL;
123 124 125 126 127 128 129 130 131 132

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

133 134 135
		drm_dbg_kms(&dev_priv->drm,
			    "[CONNECTOR:%d:%s] using preferred mode from EDID: ",
			    connector->base.base.id, connector->base.name);
136 137 138 139 140
		drm_mode_debug_printmodeline(fixed_mode);

		return fixed_mode;
	}

141 142 143 144 145 146 147 148 149
	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;

150 151 152
	drm_dbg_kms(&dev_priv->drm,
		    "[CONNECTOR:%d:%s] using first mode from EDID: ",
		    connector->base.base.id, connector->base.name);
153 154 155
	drm_mode_debug_printmodeline(fixed_mode);

	return fixed_mode;
156 157
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
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;

175 176
	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s] using mode from VBT: ",
		    connector->base.base.id, connector->base.name);
177 178 179 180 181 182 183 184
	drm_mode_debug_printmodeline(fixed_mode);

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

	return fixed_mode;
}

185
/* adjusted_mode has been preset to be the panel's fixed mode */
186 187
int intel_pch_panel_fitting(struct intel_crtc_state *crtc_state,
			    const struct drm_connector_state *conn_state)
188
{
189 190
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->hw.adjusted_mode;
191
	int x, y, width, height;
192 193

	/* Native modes don't need fitting */
194 195 196
	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)
197
		return 0;
198

199
	switch (conn_state->scaling_mode) {
200
	case DRM_MODE_SCALE_CENTER:
201 202
		width = crtc_state->pipe_src_w;
		height = crtc_state->pipe_src_h;
203 204
		x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
		y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
205 206 207 208 209
		break;

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

236 237 238
	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);
239
		fallthrough;
240 241
	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
242 243
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
244
		break;
245 246

	default:
247
		MISSING_CASE(conn_state->scaling_mode);
248
		return -EINVAL;
249 250
	}

251
	drm_rect_init(&crtc_state->pch_pfit.dst,
252
		      x, y, width, height);
253
	crtc_state->pch_pfit.enabled = true;
254 255

	return 0;
256
}
257

258
static void
259
centre_horizontally(struct drm_display_mode *adjusted_mode,
260 261 262 263 264
		    int width)
{
	u32 border, sync_pos, blank_width, sync_width;

	/* keep the hsync and hblank widths constant */
265 266
	sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
	blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
267 268
	sync_pos = (blank_width - sync_width + 1) / 2;

269
	border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
270 271
	border += border & 1; /* make the border even */

272 273 274
	adjusted_mode->crtc_hdisplay = width;
	adjusted_mode->crtc_hblank_start = width + border;
	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
275

276 277
	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
	adjusted_mode->crtc_hsync_end = adjusted_mode->crtc_hsync_start + sync_width;
278 279 280
}

static void
281
centre_vertically(struct drm_display_mode *adjusted_mode,
282 283 284 285 286
		  int height)
{
	u32 border, sync_pos, blank_width, sync_width;

	/* keep the vsync and vblank widths constant */
287 288
	sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
	blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
289 290
	sync_pos = (blank_width - sync_width + 1) / 2;

291
	border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
292

293 294 295
	adjusted_mode->crtc_vdisplay = height;
	adjusted_mode->crtc_vblank_start = height + border;
	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
296

297 298
	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
299 300
}

301
static u32 panel_fitter_scaling(u32 source, u32 target)
302 303 304 305 306 307 308 309 310 311 312 313
{
	/*
	 * 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;
}

314
static void i965_scale_aspect(struct intel_crtc_state *crtc_state,
315 316
			      u32 *pfit_control)
{
317 318
	const struct drm_display_mode *adjusted_mode =
		&crtc_state->hw.adjusted_mode;
319
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
320 321
		crtc_state->pipe_src_h;
	u32 scaled_height = crtc_state->pipe_src_w *
322
		adjusted_mode->crtc_vdisplay;
323 324 325 326 327 328 329 330

	/* 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;
331
	else if (adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w)
332 333 334
		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
}

335
static void i9xx_scale_aspect(struct intel_crtc_state *crtc_state,
336 337 338
			      u32 *pfit_control, u32 *pfit_pgm_ratios,
			      u32 *border)
{
339
	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
340
	u32 scaled_width = adjusted_mode->crtc_hdisplay *
341 342
		crtc_state->pipe_src_h;
	u32 scaled_height = crtc_state->pipe_src_w *
343
		adjusted_mode->crtc_vdisplay;
344 345 346 347 348 349 350 351 352 353
	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 /
354
				    crtc_state->pipe_src_h);
355 356

		*border = LVDS_BORDER_ENABLE;
357 358
		if (crtc_state->pipe_src_h != adjusted_mode->crtc_vdisplay) {
			bits = panel_fitter_scaling(crtc_state->pipe_src_h,
359
						    adjusted_mode->crtc_vdisplay);
360 361 362 363 364 365 366 367 368 369

			*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 /
370
				  crtc_state->pipe_src_w);
371 372

		*border = LVDS_BORDER_ENABLE;
373 374
		if (crtc_state->pipe_src_w != adjusted_mode->crtc_hdisplay) {
			bits = panel_fitter_scaling(crtc_state->pipe_src_w,
375
						    adjusted_mode->crtc_hdisplay);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

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

392 393
int intel_gmch_panel_fitting(struct intel_crtc_state *crtc_state,
			     const struct drm_connector_state *conn_state)
394
{
395 396
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
397
	u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
398
	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
399 400

	/* Native modes don't need fitting */
401 402
	if (adjusted_mode->crtc_hdisplay == crtc_state->pipe_src_w &&
	    adjusted_mode->crtc_vdisplay == crtc_state->pipe_src_h)
403 404
		goto out;

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

	/* 965+ wants fuzzy fitting */
	/* FIXME: handle multiple panels by failing gracefully */
447
	if (DISPLAY_VER(dev_priv) >= 4)
448
		pfit_control |= PFIT_PIPE(crtc->pipe) | PFIT_FILTER_FUZZY;
449 450 451 452 453 454 455

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

456
	/* Make sure pre-965 set dither correctly for 18bpp panels. */
457
	if (DISPLAY_VER(dev_priv) < 4 && crtc_state->pipe_bpp == 18)
458 459
		pfit_control |= PANEL_8TO6_DITHER_ENABLE;

460 461 462
	crtc_state->gmch_pfit.control = pfit_control;
	crtc_state->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
	crtc_state->gmch_pfit.lvds_border_bits = border;
463 464

	return 0;
465 466
}

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

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

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

	/* avoid overflows */
491 492 493
	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);
494 495 496 497 498 499 500
	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]. */
501 502
static u32 clamp_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
503 504 505 506 507 508 509 510 511 512 513
{
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

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

	return hw_level;
}

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

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

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

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

530
	if (dev_priv->params.invert_brightness < 0)
531 532
		return val;

533
	if (dev_priv->params.invert_brightness > 0 ||
534
	    dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
535
		return panel->backlight.pwm_level_max - val + panel->backlight.pwm_level_min;
536
	}
537 538 539 540

	return val;
}

541 542 543 544 545 546 547 548 549 550
void intel_panel_set_pwm_level(const struct drm_connector_state *conn_state, u32 val)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;

	drm_dbg_kms(&i915->drm, "set backlight PWM = %d\n", val);
	panel->backlight.pwm_funcs->set(conn_state, val);
}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
u32 intel_panel_backlight_level_to_pwm(struct intel_connector *connector, u32 val)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;

	drm_WARN_ON_ONCE(&dev_priv->drm,
			 panel->backlight.max == 0 || panel->backlight.pwm_level_max == 0);

	val = scale(val, panel->backlight.min, panel->backlight.max,
		    panel->backlight.pwm_level_min, panel->backlight.pwm_level_max);

	return intel_panel_invert_pwm_level(connector, val);
}

u32 intel_panel_backlight_level_from_pwm(struct intel_connector *connector, u32 val)
{
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;

	drm_WARN_ON_ONCE(&dev_priv->drm,
			 panel->backlight.max == 0 || panel->backlight.pwm_level_max == 0);

	if (dev_priv->params.invert_brightness > 0 ||
	    (dev_priv->params.invert_brightness == 0 && dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS))
		val = panel->backlight.pwm_level_max - (val - panel->backlight.pwm_level_min);

	return scale(val, panel->backlight.pwm_level_min, panel->backlight.pwm_level_max,
		     panel->backlight.min, panel->backlight.max);
}

581
static u32 lpt_get_backlight(struct intel_connector *connector, enum pipe unused)
582
{
583
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
584

585
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
586
}
587

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

592
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
593
}
594

595
static u32 i9xx_get_backlight(struct intel_connector *connector, enum pipe unused)
596
{
597
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
598
	struct intel_panel *panel = &connector->panel;
599
	u32 val;
600

601
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
602
	if (DISPLAY_VER(dev_priv) < 4)
603
		val >>= 1;
604

605
	if (panel->backlight.combination_mode) {
606
		u8 lbpc;
607

608
		pci_read_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, &lbpc);
609
		val *= lbpc;
610 611
	}

612 613 614
	return val;
}

615
static u32 vlv_get_backlight(struct intel_connector *connector, enum pipe pipe)
616
{
617 618
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);

619
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
620 621
		return 0;

622
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
623 624
}

625
static u32 bxt_get_backlight(struct intel_connector *connector, enum pipe unused)
626
{
627
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
628
	struct intel_panel *panel = &connector->panel;
629

630 631
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
632 633
}

634
static u32 ext_pwm_get_backlight(struct intel_connector *connector, enum pipe unused)
635 636
{
	struct intel_panel *panel = &connector->panel;
637
	struct pwm_state state;
638

639 640
	pwm_get_state(panel->backlight.pwm, &state);
	return pwm_get_relative_duty_cycle(&state, 100);
641 642
}

643
static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
644
{
645
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
646
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
647

648 649
	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);
650 651
}

652
static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
653
{
654
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
655
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
656 657
	u32 tmp;

658 659
	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);
660 661
}

662
static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
663
{
664
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
665
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
666
	struct intel_panel *panel = &connector->panel;
667
	u32 tmp, mask;
668

669
	drm_WARN_ON(&dev_priv->drm, panel->backlight.pwm_level_max == 0);
670

671
	if (panel->backlight.combination_mode) {
672 673
		u8 lbpc;

674
		lbpc = level * 0xfe / panel->backlight.pwm_level_max + 1;
675
		level /= lbpc;
676
		pci_write_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, lbpc);
677 678
	}

679
	if (DISPLAY_VER(dev_priv) == 4) {
680 681
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
682
		level <<= 1;
683 684
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
685

686 687
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
688 689
}

690
static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
691
{
692
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
693
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
694
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
695 696
	u32 tmp;

697 698
	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);
699 700
}

701
static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
702
{
703
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
704
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
705
	struct intel_panel *panel = &connector->panel;
706

707 708
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
709 710
}

711
static void ext_pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
712
{
713
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
714

715 716
	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
717 718
}

719
static void
720
intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
721
{
722
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
723
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
724
	struct intel_panel *panel = &connector->panel;
725

726
	drm_dbg_kms(&i915->drm, "set backlight level = %d\n", level);
727

728
	panel->backlight.funcs->set(conn_state, level);
729
}
730

731 732 733
/* set backlight brightness to level in range [0..max], assuming hw min is
 * respected.
 */
734
void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
735 736
				    u32 user_level, u32 user_max)
{
737
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
738
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
739 740 741
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

742
	/*
743
	 * Lack of crtc may occur during driver init because
744 745 746 747
	 * connection_mutex isn't held across the entire backlight
	 * setup + modeset readout, and the BIOS can issue the
	 * requests at any time.
	 */
748
	if (!panel->backlight.present || !conn_state->crtc)
749 750
		return;

751
	mutex_lock(&dev_priv->backlight_lock);
752

753
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
754 755 756

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

758
	if (panel->backlight.device)
759 760 761 762
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
763

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

767
	mutex_unlock(&dev_priv->backlight_lock);
768 769
}

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

776
	intel_panel_set_pwm_level(old_conn_state, level);
777

778 779 780 781 782 783 784 785
	/*
	 * 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.
	 */
786
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
787
	if (tmp & BLM_PWM_ENABLE) {
788 789
		drm_dbg_kms(&dev_priv->drm,
			    "cpu backlight was enabled, disabling\n");
790 791
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       tmp & ~BLM_PWM_ENABLE);
792 793
	}

794 795
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
796 797
}

798
static void pch_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
799
{
800
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
801
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
802 803
	u32 tmp;

804
	intel_panel_set_pwm_level(old_conn_state, val);
805

806 807
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
808

809 810
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
811 812
}

813
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
814
{
815
	intel_panel_set_pwm_level(old_conn_state, val);
816 817
}

818
static void i965_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
819
{
820
	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
821 822
	u32 tmp;

823
	intel_panel_set_pwm_level(old_conn_state, val);
824

825 826
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
827 828
}

829
static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
830
{
831
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
832
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
833
	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
834 835
	u32 tmp;

836
	intel_panel_set_pwm_level(old_conn_state, val);
837

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

843
static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
844
{
845
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
846
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
847
	struct intel_panel *panel = &connector->panel;
848
	u32 tmp;
849

850
	intel_panel_set_pwm_level(old_conn_state, val);
851

852 853 854 855
	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);
856 857

	if (panel->backlight.controller == 1) {
858
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
859
		val &= ~UTIL_PIN_ENABLE;
860
		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
861
	}
862 863
}

864
static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
865
{
866
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
867 868 869 870
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 tmp;

871
	intel_panel_set_pwm_level(old_conn_state, val);
872

873 874 875 876
	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);
877 878
}

879
static void ext_pwm_disable_backlight(const struct drm_connector_state *old_conn_state, u32 level)
880
{
881
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
882 883
	struct intel_panel *panel = &connector->panel;

884 885
	panel->backlight.pwm_state.enabled = false;
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
886 887
}

888
void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
889
{
890
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
891
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
892
	struct intel_panel *panel = &connector->panel;
893

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

897
	/*
898
	 * Do not disable backlight on the vga_switcheroo path. When switching
899 900 901 902
	 * 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.
	 */
903
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
904 905
		drm_dbg_kms(&dev_priv->drm,
			    "Skipping backlight disable on vga switch\n");
906 907 908
		return;
	}

909
	mutex_lock(&dev_priv->backlight_lock);
910

911 912
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
913
	panel->backlight.enabled = false;
914
	panel->backlight.funcs->disable(old_conn_state, 0);
915

916
	mutex_unlock(&dev_priv->backlight_lock);
917
}
918

919
static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
920
				 const struct drm_connector_state *conn_state, u32 level)
921
{
922
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
923
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
924
	struct intel_panel *panel = &connector->panel;
925
	u32 pch_ctl1, pch_ctl2, schicken;
926

927
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
928
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
929
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
930
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
931
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
932
	}
933

934
	if (HAS_PCH_LPT(dev_priv)) {
935
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN2);
936 937 938 939
		if (panel->backlight.alternate_pwm_increment)
			schicken |= LPT_PWM_GRANULARITY;
		else
			schicken &= ~LPT_PWM_GRANULARITY;
940
		intel_de_write(dev_priv, SOUTH_CHICKEN2, schicken);
941
	} else {
942
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN1);
943 944 945 946
		if (panel->backlight.alternate_pwm_increment)
			schicken |= SPT_PWM_GRANULARITY;
		else
			schicken &= ~SPT_PWM_GRANULARITY;
947
		intel_de_write(dev_priv, SOUTH_CHICKEN1, schicken);
948 949
	}

950
	pch_ctl2 = panel->backlight.pwm_level_max << 16;
951
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
952

953 954 955
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
956

957 958 959
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
960

961 962 963 964
	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);
965 966

	/* This won't stick until the above enable. */
967
	intel_panel_set_pwm_level(conn_state, level);
968 969
}

970
static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
971
				 const struct drm_connector_state *conn_state, u32 level)
972
{
973
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
974
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
975
	struct intel_panel *panel = &connector->panel;
976
	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
977
	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
978

979
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
980
	if (cpu_ctl2 & BLM_PWM_ENABLE) {
981
		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
982
		cpu_ctl2 &= ~BLM_PWM_ENABLE;
983
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
984
	}
985

986
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
987
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
988
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
989
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
990
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
991
	}
992 993

	if (cpu_transcoder == TRANSCODER_EDP)
994
		cpu_ctl2 = BLM_TRANSCODER_EDP;
995
	else
996
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
997 998 999
	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);
1000

1001
	/* This won't stick until the above enable. */
1002
	intel_panel_set_pwm_level(conn_state, level);
1003

1004
	pch_ctl2 = panel->backlight.pwm_level_max << 16;
1005
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
1006 1007 1008 1009

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

1011 1012 1013 1014
	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);
1015 1016
}

1017
static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
1018
				  const struct drm_connector_state *conn_state, u32 level)
1019
{
1020
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1021
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1022
	struct intel_panel *panel = &connector->panel;
1023 1024
	u32 ctl, freq;

1025
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1026
	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
1027
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1028
		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
1029
	}
1030

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

	ctl = freq << 17;
1036
	if (panel->backlight.combination_mode)
1037
		ctl |= BLM_LEGACY_MODE;
1038
	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
1039 1040
		ctl |= BLM_POLARITY_PNV;

1041 1042
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1043 1044

	/* XXX: combine this into above write? */
1045
	intel_panel_set_pwm_level(conn_state, level);
1046 1047 1048 1049 1050 1051

	/*
	 * 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.
	 */
1052
	if (DISPLAY_VER(dev_priv) == 2)
1053
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1054
}
1055

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

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

1072
	freq = panel->backlight.pwm_level_max;
1073 1074
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1075

1076
	ctl = freq << 16;
1077
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1078

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

1088
	intel_panel_set_pwm_level(conn_state, level);
1089 1090
}

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

1100
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1101
	if (ctl2 & BLM_PWM_ENABLE) {
1102
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1103
		ctl2 &= ~BLM_PWM_ENABLE;
1104
		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
1105
	}
1106

1107
	ctl = panel->backlight.pwm_level_max << 16;
1108
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1109

1110
	/* XXX: combine this into above write? */
1111
	intel_panel_set_pwm_level(conn_state, level);
1112

1113 1114 1115
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1116 1117 1118 1119
	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);
1120 1121
}

1122
static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
1123
				 const struct drm_connector_state *conn_state, u32 level)
1124
{
1125
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1126
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1127
	struct intel_panel *panel = &connector->panel;
1128
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1129 1130
	u32 pwm_ctl, val;

1131
	/* Controller 1 uses the utility pin. */
1132
	if (panel->backlight.controller == 1) {
1133
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1134
		if (val & UTIL_PIN_ENABLE) {
1135 1136
			drm_dbg_kms(&dev_priv->drm,
				    "util pin already enabled\n");
1137
			val &= ~UTIL_PIN_ENABLE;
1138
			intel_de_write(dev_priv, UTIL_PIN_CTL, val);
1139
		}
1140

1141 1142 1143
		val = 0;
		if (panel->backlight.util_pin_active_low)
			val |= UTIL_PIN_POLARITY;
1144 1145
		intel_de_write(dev_priv, UTIL_PIN_CTL,
			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1146 1147
	}

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

1158 1159
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
1160
		       panel->backlight.pwm_level_max);
1161

1162
	intel_panel_set_pwm_level(conn_state, level);
1163 1164 1165 1166 1167

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

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

1176
static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
1177
				 const struct drm_connector_state *conn_state, u32 level)
1178
{
1179
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1180 1181 1182 1183
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl;

1184 1185
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1186
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1187
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1188
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1189 1190 1191
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1192 1193
	}

1194 1195
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
1196
		       panel->backlight.pwm_level_max);
1197

1198
	intel_panel_set_pwm_level(conn_state, level);
1199 1200 1201 1202 1203

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

1204 1205 1206 1207 1208 1209
	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);
1210 1211
}

1212
static void ext_pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1213
				     const struct drm_connector_state *conn_state, u32 level)
1214
{
1215
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1216 1217
	struct intel_panel *panel = &connector->panel;

1218 1219 1220
	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);
1221 1222
}

1223 1224
static void __intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
					   const struct drm_connector_state *conn_state)
1225
{
1226
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1227
	struct intel_panel *panel = &connector->panel;
1228

1229 1230
	WARN_ON(panel->backlight.max == 0);

1231
	if (panel->backlight.level <= panel->backlight.min) {
1232
		panel->backlight.level = panel->backlight.max;
1233 1234
		if (panel->backlight.device)
			panel->backlight.device->props.brightness =
1235 1236 1237
				scale_hw_to_user(connector,
						 panel->backlight.level,
						 panel->backlight.device->props.max_brightness);
1238
	}
1239

1240
	panel->backlight.funcs->enable(crtc_state, conn_state, panel->backlight.level);
1241
	panel->backlight.enabled = true;
1242 1243
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1244 1245 1246 1247 1248 1249 1250 1251
}

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;
1252
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1253 1254 1255 1256

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

1257
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1258 1259 1260 1261

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1262

1263
	mutex_unlock(&dev_priv->backlight_lock);
1264 1265
}

1266
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1267 1268 1269 1270 1271 1272 1273 1274
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);

1275
	if (panel->backlight.enabled)
1276
		val = panel->backlight.funcs->get(connector, intel_connector_get_pipe(connector));
1277 1278 1279

	mutex_unlock(&dev_priv->backlight_lock);

1280
	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
1281 1282 1283
	return val;
}

1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
/* 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);
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
/* 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);

1308
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

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

1319
static int intel_backlight_device_update_status(struct backlight_device *bd)
1320
{
1321
	struct intel_connector *connector = bl_get_data(bd);
1322
	struct intel_panel *panel = &connector->panel;
1323 1324
	struct drm_device *dev = connector->base.dev;

1325
	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1326 1327
	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
		      bd->props.brightness, bd->props.max_brightness);
1328
	intel_panel_set_backlight(connector->base.state, bd->props.brightness,
1329
				  bd->props.max_brightness);
1330 1331 1332 1333 1334 1335 1336 1337

	/*
	 * 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) {
1338
		if (panel->backlight.power) {
1339 1340
			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
				bd->props.brightness != 0;
1341
			panel->backlight.power(connector, enable);
1342 1343 1344 1345 1346
		}
	} else {
		bd->props.power = FB_BLANK_POWERDOWN;
	}

1347
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1348 1349 1350
	return 0;
}

1351
static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1352
{
1353 1354
	struct intel_connector *connector = bl_get_data(bd);
	struct drm_device *dev = connector->base.dev;
1355
	struct drm_i915_private *dev_priv = to_i915(dev);
1356
	intel_wakeref_t wakeref;
1357
	int ret = 0;
1358

1359
	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
1360
		u32 hw_level;
1361

1362
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1363

1364 1365 1366 1367 1368 1369
		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);
	}
1370

1371
	return ret;
1372 1373
}

1374 1375 1376
static const struct backlight_ops intel_backlight_device_ops = {
	.update_status = intel_backlight_device_update_status,
	.get_brightness = intel_backlight_device_get_brightness,
1377 1378
};

1379
int intel_backlight_device_register(struct intel_connector *connector)
1380
{
1381
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1382
	struct intel_panel *panel = &connector->panel;
1383
	struct backlight_properties props;
1384 1385 1386
	struct backlight_device *bd;
	const char *name;
	int ret = 0;
1387

1388
	if (WARN_ON(panel->backlight.device))
1389 1390
		return -ENODEV;

1391 1392 1393
	if (!panel->backlight.present)
		return 0;

1394
	WARN_ON(panel->backlight.max == 0);
1395

1396
	memset(&props, 0, sizeof(props));
1397
	props.type = BACKLIGHT_RAW;
1398 1399 1400 1401 1402

	/*
	 * Note: Everything should work even if the backlight device max
	 * presented to the userspace is arbitrarily chosen.
	 */
1403
	props.max_brightness = panel->backlight.max;
1404 1405 1406
	props.brightness = scale_hw_to_user(connector,
					    panel->backlight.level,
					    props.max_brightness);
1407

1408 1409 1410 1411 1412
	if (panel->backlight.enabled)
		props.power = FB_BLANK_UNBLANK;
	else
		props.power = FB_BLANK_POWERDOWN;

1413 1414 1415 1416 1417 1418
	name = kstrdup("intel_backlight", GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	bd = backlight_device_register(name, connector->base.kdev, connector,
				       &intel_backlight_device_ops, &props);
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

	/*
	 * Using the same name independent of the drm device or connector
	 * prevents registration of multiple backlight devices in the
	 * driver. However, we need to use the default name for backward
	 * compatibility. Use unique names for subsequent backlight devices as a
	 * fallback when the default name already exists.
	 */
	if (IS_ERR(bd) && PTR_ERR(bd) == -EEXIST) {
		kfree(name);
		name = kasprintf(GFP_KERNEL, "card%d-%s-backlight",
				 i915->drm.primary->index, connector->base.name);
		if (!name)
			return -ENOMEM;

		bd = backlight_device_register(name, connector->base.kdev, connector,
					       &intel_backlight_device_ops, &props);
	}

1438 1439 1440 1441 1442 1443
	if (IS_ERR(bd)) {
		drm_err(&i915->drm,
			"[CONNECTOR:%d:%s] backlight device %s register failed: %ld\n",
			connector->base.base.id, connector->base.name, name, PTR_ERR(bd));
		ret = PTR_ERR(bd);
		goto out;
1444
	}
1445

1446 1447
	panel->backlight.device = bd;

1448
	drm_dbg_kms(&i915->drm,
1449 1450
		    "[CONNECTOR:%d:%s] backlight device %s registered\n",
		    connector->base.base.id, connector->base.name, name);
1451

1452 1453 1454 1455
out:
	kfree(name);

	return ret;
1456 1457
}

1458
void intel_backlight_device_unregister(struct intel_connector *connector)
1459
{
1460 1461 1462 1463 1464
	struct intel_panel *panel = &connector->panel;

	if (panel->backlight.device) {
		backlight_device_unregister(panel->backlight.device);
		panel->backlight.device = NULL;
1465
	}
1466
}
1467 1468
#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */

1469 1470 1471 1472 1473 1474 1475 1476
/*
 * 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);

1477 1478
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz);
1479 1480
}

1481 1482 1483 1484 1485
/*
 * BXT: PWM clock frequency = 19.2 MHz.
 */
static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1486
	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1487 1488
}

1489
/*
1490 1491 1492 1493 1494 1495
 * 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)
{
1496
	struct intel_panel *panel = &connector->panel;
1497
	u32 mul;
1498

1499
	if (panel->backlight.alternate_pwm_increment)
1500 1501 1502 1503
		mul = 128;
	else
		mul = 16;

1504
	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1505 1506 1507 1508 1509 1510 1511 1512 1513
}

/*
 * 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)
{
1514
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1515
	struct intel_panel *panel = &connector->panel;
1516 1517
	u32 mul, clock;

1518
	if (panel->backlight.alternate_pwm_increment)
1519 1520 1521 1522
		mul = 16;
	else
		mul = 128;

V
Ville Syrjälä 已提交
1523
	if (HAS_PCH_LPT_H(dev_priv))
1524 1525 1526 1527
		clock = MHz(135); /* LPT:H */
	else
		clock = MHz(24); /* LPT:LP */

1528
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1529 1530 1531 1532 1533 1534 1535 1536
}

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

1539 1540
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz * 128);
1541 1542 1543 1544 1545 1546
}

/*
 * 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.
1547
 *
1548 1549 1550 1551 1552
 * 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)
{
1553
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1554 1555
	int clock;

1556
	if (IS_PINEVIEW(dev_priv))
1557
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1558
	else
1559
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1560

1561
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1562 1563 1564 1565
}

/*
 * Gen4: This value represents the period of the PWM stream in display core
1566 1567
 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
 *
1568 1569 1570
 */
static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1571
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1572 1573 1574
	int clock;

	if (IS_G4X(dev_priv))
1575
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1576
	else
1577
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1578

1579
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1580 1581 1582 1583 1584 1585 1586 1587 1588
}

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

1592
	if ((intel_de_read(dev_priv, CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1593 1594
		if (IS_CHERRYVIEW(dev_priv))
			clock = KHz(19200);
1595
		else
1596 1597
			clock = MHz(25);
		mul = 16;
1598
	} else {
1599
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1600
		mul = 128;
1601
	}
1602

1603
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1604 1605
}

1606
static u16 get_vbt_pwm_freq(struct drm_i915_private *dev_priv)
1607 1608 1609
{
	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;

1610
	if (pwm_freq_hz) {
1611 1612 1613
		drm_dbg_kms(&dev_priv->drm,
			    "VBT defined backlight frequency %u Hz\n",
			    pwm_freq_hz);
1614 1615
	} else {
		pwm_freq_hz = 200;
1616 1617 1618
		drm_dbg_kms(&dev_priv->drm,
			    "default backlight frequency %u Hz\n",
			    pwm_freq_hz);
1619 1620
	}

1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
	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;

1631
	if (!panel->backlight.pwm_funcs->hz_to_pwm) {
1632 1633 1634 1635 1636
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion not supported\n");
		return 0;
	}

1637
	pwm = panel->backlight.pwm_funcs->hz_to_pwm(connector, pwm_freq_hz);
1638
	if (!pwm) {
1639 1640
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion failed\n");
1641 1642 1643 1644 1645 1646 1647 1648
		return 0;
	}

	return pwm;
}

/*
 * Note: The setup hooks can't assume pipe is set!
1649
 */
1650 1651
static u32 get_backlight_min_vbt(struct intel_connector *connector)
{
1652
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1653
	struct intel_panel *panel = &connector->panel;
1654
	int min;
1655

1656
	drm_WARN_ON(&dev_priv->drm, panel->backlight.pwm_level_max == 0);
1657

1658 1659 1660 1661 1662 1663 1664 1665 1666
	/*
	 * 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) {
1667 1668 1669
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1670 1671
	}

1672
	/* vbt value is a coefficient in range [0..255] */
1673
	return scale(min, 0, 255, 0, panel->backlight.pwm_level_max);
1674 1675
}

1676
static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1677
{
1678
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1679
	struct intel_panel *panel = &connector->panel;
1680 1681
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
	bool alt, cpu_mode;
1682 1683

	if (HAS_PCH_LPT(dev_priv))
1684
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1685
	else
1686
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1687
	panel->backlight.alternate_pwm_increment = alt;
1688

1689
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1690 1691
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1692
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1693
	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1694

1695
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1696

1697 1698
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1699

1700
	if (!panel->backlight.pwm_level_max)
1701 1702
		return -ENODEV;

1703
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1704

1705
	panel->backlight.pwm_enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
1706

1707
	cpu_mode = panel->backlight.pwm_enabled && HAS_PCH_LPT(dev_priv) &&
1708 1709
		   !(pch_ctl1 & BLM_PCH_OVERRIDE_ENABLE) &&
		   (cpu_ctl2 & BLM_PWM_ENABLE);
1710

1711
	if (cpu_mode) {
1712 1713
		val = pch_get_backlight(connector, unused);

1714 1715
		drm_dbg_kms(&dev_priv->drm,
			    "CPU backlight register was enabled, switching to PCH override\n");
1716 1717

		/* Write converted CPU PWM value to PCH override register */
1718
		lpt_set_backlight(connector->base.state, val);
1719 1720
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
			       pch_ctl1 | BLM_PCH_OVERRIDE_ENABLE);
1721

1722 1723
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1724
	}
1725 1726 1727 1728

	return 0;
}

1729
static int pch_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
	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
1734

1735
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1736 1737
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1738
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1739
	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1740

1741 1742
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1743

1744
	if (!panel->backlight.pwm_level_max)
1745 1746
		return -ENODEV;

1747
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1748

1749
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1750
	panel->backlight.pwm_enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1751
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1752

1753 1754 1755
	return 0;
}

1756
static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1757
{
1758
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1759
	struct intel_panel *panel = &connector->panel;
1760 1761
	u32 ctl, val;

1762
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1763

1764
	if (DISPLAY_VER(dev_priv) == 2 || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1765 1766
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1767
	if (IS_PINEVIEW(dev_priv))
1768 1769
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

1770
	panel->backlight.pwm_level_max = ctl >> 17;
1771

1772 1773 1774
	if (!panel->backlight.pwm_level_max) {
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
		panel->backlight.pwm_level_max >>= 1;
1775
	}
1776

1777
	if (!panel->backlight.pwm_level_max)
1778 1779
		return -ENODEV;

1780
	if (panel->backlight.combination_mode)
1781
		panel->backlight.pwm_level_max *= 0xff;
1782

1783
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1784

1785
	val = i9xx_get_backlight(connector, unused);
1786 1787
	val = intel_panel_invert_pwm_level(connector, val);
	val = clamp(val, panel->backlight.pwm_level_min, panel->backlight.pwm_level_max);
1788

1789
	panel->backlight.pwm_enabled = val != 0;
1790

1791 1792 1793
	return 0;
}

1794
static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
1795
{
1796
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1797
	struct intel_panel *panel = &connector->panel;
1798
	u32 ctl, ctl2;
1799

1800
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1801 1802 1803
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1804
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1805
	panel->backlight.pwm_level_max = ctl >> 16;
1806

1807 1808
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1809

1810
	if (!panel->backlight.pwm_level_max)
1811 1812
		return -ENODEV;

1813
	if (panel->backlight.combination_mode)
1814
		panel->backlight.pwm_level_max *= 0xff;
1815

1816
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1817

1818
	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1819

1820 1821 1822
	return 0;
}

1823
static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1824
{
1825
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1826
	struct intel_panel *panel = &connector->panel;
1827
	u32 ctl, ctl2;
1828

1829
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1830 1831
		return -ENODEV;

1832
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1833 1834
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1835
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1836
	panel->backlight.pwm_level_max = ctl >> 16;
1837

1838 1839
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1840

1841
	if (!panel->backlight.pwm_level_max)
1842 1843
		return -ENODEV;

1844
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1845

1846
	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1847

1848 1849 1850
	return 0;
}

1851 1852 1853
static int
bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
1854
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1855 1856 1857
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

1858
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1859

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

1863
	/* Controller 1 uses the utility pin. */
1864
	if (panel->backlight.controller == 1) {
1865
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1866 1867 1868 1869 1870
		panel->backlight.util_pin_active_low =
					val & UTIL_PIN_POLARITY;
	}

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1871 1872
	panel->backlight.pwm_level_max =
		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1873

1874 1875
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1876

1877
	if (!panel->backlight.pwm_level_max)
1878 1879
		return -ENODEV;

1880
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1881

1882
	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1883 1884 1885 1886

	return 0;
}

1887 1888 1889 1890 1891
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;
1892
	u32 pwm_ctl;
1893 1894

	/*
1895 1896 1897
	 * 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.
1898 1899 1900
	 */
	panel->backlight.controller = 0;

1901 1902
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1903 1904

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1905 1906
	panel->backlight.pwm_level_max =
		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1907

1908 1909
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1910

1911
	if (!panel->backlight.pwm_level_max)
1912 1913
		return -ENODEV;

1914
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1915

1916
	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1917 1918 1919 1920

	return 0;
}

1921 1922
static int ext_pwm_setup_backlight(struct intel_connector *connector,
				   enum pipe pipe)
1923 1924
{
	struct drm_device *dev = connector->base.dev;
1925
	struct drm_i915_private *dev_priv = to_i915(dev);
1926
	struct intel_panel *panel = &connector->panel;
1927
	const char *desc;
1928
	u32 level;
1929

1930 1931 1932 1933 1934 1935 1936 1937 1938
	/* 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";
	}

1939
	if (IS_ERR(panel->backlight.pwm)) {
1940 1941
		drm_err(&dev_priv->drm, "Failed to get the %s PWM chip\n",
			desc);
1942 1943 1944 1945
		panel->backlight.pwm = NULL;
		return -ENODEV;
	}

1946 1947
	panel->backlight.pwm_level_max = 100; /* 100% */
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1948

1949 1950 1951 1952 1953 1954
	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);
1955 1956
		level = intel_panel_invert_pwm_level(connector, level);
		panel->backlight.pwm_enabled = true;
1957 1958 1959 1960 1961 1962 1963 1964 1965

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

1967 1968
	drm_info(&dev_priv->drm, "Using %s PWM for LCD backlight control\n",
		 desc);
1969 1970 1971
	return 0;
}

1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
static void intel_pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct intel_panel *panel = &connector->panel;

	panel->backlight.pwm_funcs->set(conn_state,
				       intel_panel_invert_pwm_level(connector, level));
}

static u32 intel_pwm_get_backlight(struct intel_connector *connector, enum pipe pipe)
{
	struct intel_panel *panel = &connector->panel;

	return intel_panel_invert_pwm_level(connector,
					    panel->backlight.pwm_funcs->get(connector, pipe));
}

static void intel_pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
				       const struct drm_connector_state *conn_state, u32 level)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct intel_panel *panel = &connector->panel;

	panel->backlight.pwm_funcs->enable(crtc_state, conn_state,
					   intel_panel_invert_pwm_level(connector, level));
}

static void intel_pwm_disable_backlight(const struct drm_connector_state *conn_state, u32 level)
{
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
	struct intel_panel *panel = &connector->panel;

	panel->backlight.pwm_funcs->disable(conn_state,
					    intel_panel_invert_pwm_level(connector, level));
}

static int intel_pwm_setup_backlight(struct intel_connector *connector, enum pipe pipe)
{
	struct intel_panel *panel = &connector->panel;
	int ret = panel->backlight.pwm_funcs->setup(connector, pipe);

	if (ret < 0)
		return ret;

	panel->backlight.min = panel->backlight.pwm_level_min;
	panel->backlight.max = panel->backlight.pwm_level_max;
	panel->backlight.level = intel_pwm_get_backlight(connector, pipe);
	panel->backlight.enabled = panel->backlight.pwm_enabled;

	return 0;
}

2024 2025
void intel_panel_update_backlight(struct intel_atomic_state *state,
				  struct intel_encoder *encoder,
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
				  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);
}

2043
int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
2044
{
2045
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2046
	struct intel_connector *intel_connector = to_intel_connector(connector);
2047
	struct intel_panel *panel = &intel_connector->panel;
2048
	int ret;
2049

2050
	if (!dev_priv->vbt.backlight.present) {
2051
		if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
2052 2053
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT, but present per quirk\n");
2054
		} else {
2055 2056
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT\n");
2057 2058
			return 0;
		}
2059 2060
	}

2061
	/* ensure intel_panel has been initialized first */
2062
	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.funcs))
2063 2064
		return -ENODEV;

2065
	/* set level and max in panel struct */
2066
	mutex_lock(&dev_priv->backlight_lock);
2067
	ret = panel->backlight.funcs->setup(intel_connector, pipe);
2068
	mutex_unlock(&dev_priv->backlight_lock);
2069 2070

	if (ret) {
2071 2072 2073
		drm_dbg_kms(&dev_priv->drm,
			    "failed to setup backlight for connector %s\n",
			    connector->name);
2074 2075
		return ret;
	}
2076

2077 2078
	panel->backlight.present = true;

2079 2080 2081 2082 2083
	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);
2084

2085 2086 2087
	return 0;
}

2088
static void intel_panel_destroy_backlight(struct intel_panel *panel)
2089
{
2090 2091 2092 2093
	/* dispose of the pwm */
	if (panel->backlight.pwm)
		pwm_put(panel->backlight.pwm);

2094
	panel->backlight.present = false;
2095
}
2096

2097
static const struct intel_panel_bl_funcs bxt_pwm_funcs = {
2098 2099 2100 2101 2102 2103 2104 2105
	.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,
};

2106
static const struct intel_panel_bl_funcs cnp_pwm_funcs = {
2107 2108 2109 2110 2111 2112 2113 2114
	.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,
};

2115
static const struct intel_panel_bl_funcs lpt_pwm_funcs = {
2116 2117 2118 2119 2120 2121 2122 2123
	.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,
};

2124
static const struct intel_panel_bl_funcs spt_pwm_funcs = {
2125 2126 2127 2128 2129 2130 2131 2132
	.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,
};

2133
static const struct intel_panel_bl_funcs pch_pwm_funcs = {
2134 2135 2136 2137 2138 2139 2140 2141
	.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,
};

2142 2143 2144 2145 2146 2147
static const struct intel_panel_bl_funcs ext_pwm_funcs = {
	.setup = ext_pwm_setup_backlight,
	.enable = ext_pwm_enable_backlight,
	.disable = ext_pwm_disable_backlight,
	.set = ext_pwm_set_backlight,
	.get = ext_pwm_get_backlight,
2148 2149
};

2150
static const struct intel_panel_bl_funcs vlv_pwm_funcs = {
2151 2152 2153 2154 2155 2156 2157 2158
	.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,
};

2159
static const struct intel_panel_bl_funcs i965_pwm_funcs = {
2160 2161 2162 2163 2164 2165 2166 2167
	.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,
};

2168
static const struct intel_panel_bl_funcs i9xx_pwm_funcs = {
2169 2170 2171 2172 2173 2174 2175 2176
	.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,
};

2177 2178 2179 2180 2181 2182 2183 2184
static const struct intel_panel_bl_funcs pwm_bl_funcs = {
	.setup = intel_pwm_setup_backlight,
	.enable = intel_pwm_enable_backlight,
	.disable = intel_pwm_disable_backlight,
	.set = intel_pwm_set_backlight,
	.get = intel_pwm_get_backlight,
};

2185
/* Set up chip specific backlight functions */
2186 2187
static void
intel_panel_init_backlight_funcs(struct intel_panel *panel)
2188
{
2189
	struct intel_connector *connector =
2190
		container_of(panel, struct intel_connector, panel);
2191
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
2192

2193 2194 2195 2196
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

2197
	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
2198
		panel->backlight.pwm_funcs = &bxt_pwm_funcs;
2199
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
2200
		panel->backlight.pwm_funcs = &cnp_pwm_funcs;
2201
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
2202
		if (HAS_PCH_LPT(dev_priv))
2203
			panel->backlight.pwm_funcs = &lpt_pwm_funcs;
2204
		else
2205
			panel->backlight.pwm_funcs = &spt_pwm_funcs;
2206
	} else if (HAS_PCH_SPLIT(dev_priv)) {
2207
		panel->backlight.pwm_funcs = &pch_pwm_funcs;
2208
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2209
		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
2210
			panel->backlight.pwm_funcs = &ext_pwm_funcs;
2211
		} else {
2212
			panel->backlight.pwm_funcs = &vlv_pwm_funcs;
2213
		}
2214
	} else if (DISPLAY_VER(dev_priv) == 4) {
2215
		panel->backlight.pwm_funcs = &i965_pwm_funcs;
2216
	} else {
2217
		panel->backlight.pwm_funcs = &i9xx_pwm_funcs;
2218
	}
2219 2220 2221 2222

	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;
2223 2224 2225

	/* We're using a standard PWM backlight interface */
	panel->backlight.funcs = &pwm_bl_funcs;
2226 2227
}

2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
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;
}

2239
int intel_panel_init(struct intel_panel *panel,
2240 2241
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2242
{
2243 2244
	intel_panel_init_backlight_funcs(panel);

2245
	panel->fixed_mode = fixed_mode;
2246
	panel->downclock_mode = downclock_mode;
2247

2248 2249 2250 2251 2252
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2253 2254 2255
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2256 2257
	intel_panel_destroy_backlight(panel);

2258 2259
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2260 2261 2262 2263

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