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

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

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

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

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

	drm_mode_set_crtcinfo(adjusted_mode, 0);
50 51
}

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

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

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

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

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

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

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

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

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

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

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

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

		return fixed_mode;
	}

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

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

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

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

	return fixed_mode;
147 148
}

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

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

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

	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;

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

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

	return fixed_mode;
}

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

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

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

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

227 228 229
	case DRM_MODE_SCALE_NONE:
		WARN_ON(adjusted_mode->crtc_hdisplay != crtc_state->pipe_src_w);
		WARN_ON(adjusted_mode->crtc_vdisplay != crtc_state->pipe_src_h);
230
		fallthrough;
231 232
	case DRM_MODE_SCALE_FULLSCREEN:
		x = y = 0;
233 234
		width = adjusted_mode->crtc_hdisplay;
		height = adjusted_mode->crtc_vdisplay;
235
		break;
236 237

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

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

	return 0;
247
}
248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;
456 457
}

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

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

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

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

	return target_val;
}

/* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
 * to [hw_min..hw_max]. */
492 493
static u32 clamp_user_to_hw(struct intel_connector *connector,
			    u32 user_level, u32 user_max)
494 495 496 497 498 499 500 501 502 503 504
{
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

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

	return hw_level;
}

/* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
505 506
static u32 scale_hw_to_user(struct intel_connector *connector,
			    u32 hw_level, u32 user_max)
507 508 509 510 511 512 513
{
	struct intel_panel *panel = &connector->panel;

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

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

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

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

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

	return val;
}

532 533 534 535 536 537 538 539 540 541
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);
}

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
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);
}

572
static u32 lpt_get_backlight(struct intel_connector *connector, enum pipe unused)
573
{
574
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
575

576
	return intel_de_read(dev_priv, BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
577
}
578

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

583
	return intel_de_read(dev_priv, BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
584
}
585

586
static u32 i9xx_get_backlight(struct intel_connector *connector, enum pipe unused)
587
{
588
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
589
	struct intel_panel *panel = &connector->panel;
590
	u32 val;
591

592
	val = intel_de_read(dev_priv, BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
593
	if (DISPLAY_VER(dev_priv) < 4)
594
		val >>= 1;
595

596
	if (panel->backlight.combination_mode) {
597
		u8 lbpc;
598

599
		pci_read_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, &lbpc);
600
		val *= lbpc;
601 602
	}

603 604 605
	return val;
}

606
static u32 vlv_get_backlight(struct intel_connector *connector, enum pipe pipe)
607
{
608 609
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);

610
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
611 612
		return 0;

613
	return intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
614 615
}

616
static u32 bxt_get_backlight(struct intel_connector *connector, enum pipe unused)
617
{
618
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
619
	struct intel_panel *panel = &connector->panel;
620

621 622
	return intel_de_read(dev_priv,
			     BXT_BLC_PWM_DUTY(panel->backlight.controller));
623 624
}

625
static u32 ext_pwm_get_backlight(struct intel_connector *connector, enum pipe unused)
626 627
{
	struct intel_panel *panel = &connector->panel;
628
	struct pwm_state state;
629

630 631
	pwm_get_state(panel->backlight.pwm, &state);
	return pwm_get_relative_duty_cycle(&state, 100);
632 633
}

634
static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
635
{
636
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
637
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
638

639 640
	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);
641 642
}

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

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

653
static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
654
{
655
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
656
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
657
	struct intel_panel *panel = &connector->panel;
658
	u32 tmp, mask;
659

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

662
	if (panel->backlight.combination_mode) {
663 664
		u8 lbpc;

665
		lbpc = level * 0xfe / panel->backlight.pwm_level_max + 1;
666
		level /= lbpc;
667
		pci_write_config_byte(to_pci_dev(dev_priv->drm.dev), LBPC, lbpc);
668 669
	}

670
	if (DISPLAY_VER(dev_priv) == 4) {
671 672
		mask = BACKLIGHT_DUTY_CYCLE_MASK;
	} else {
673
		level <<= 1;
674 675
		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
	}
676

677 678
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL) & ~mask;
	intel_de_write(dev_priv, BLC_PWM_CTL, tmp | level);
679 680
}

681
static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
682
{
683
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
684
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
685
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
686 687
	u32 tmp;

688 689
	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);
690 691
}

692
static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
693
{
694
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
695
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
696
	struct intel_panel *panel = &connector->panel;
697

698 699
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
700 701
}

702
static void ext_pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
703
{
704
	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
705

706 707
	pwm_set_relative_duty_cycle(&panel->backlight.pwm_state, level, 100);
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
708 709
}

710
static void
711
intel_panel_actually_set_backlight(const struct drm_connector_state *conn_state, u32 level)
712
{
713
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
714
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
715
	struct intel_panel *panel = &connector->panel;
716

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

719
	panel->backlight.funcs->set(conn_state, level);
720
}
721

722 723 724
/* set backlight brightness to level in range [0..max], assuming hw min is
 * respected.
 */
725
void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
726 727
				    u32 user_level, u32 user_max)
{
728
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
729
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
730 731 732
	struct intel_panel *panel = &connector->panel;
	u32 hw_level;

733
	/*
734
	 * Lack of crtc may occur during driver init because
735 736 737 738
	 * connection_mutex isn't held across the entire backlight
	 * setup + modeset readout, and the BIOS can issue the
	 * requests at any time.
	 */
739
	if (!panel->backlight.present || !conn_state->crtc)
740 741
		return;

742
	mutex_lock(&dev_priv->backlight_lock);
743

744
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
745 746 747

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

749
	if (panel->backlight.device)
750 751 752 753
		panel->backlight.device->props.brightness =
			scale_hw_to_user(connector,
					 panel->backlight.level,
					 panel->backlight.device->props.max_brightness);
754

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

758
	mutex_unlock(&dev_priv->backlight_lock);
759 760
}

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

767
	intel_panel_set_pwm_level(old_conn_state, level);
768

769 770 771 772 773 774 775 776
	/*
	 * 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.
	 */
777
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
778
	if (tmp & BLM_PWM_ENABLE) {
779 780
		drm_dbg_kms(&dev_priv->drm,
			    "cpu backlight was enabled, disabling\n");
781 782
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       tmp & ~BLM_PWM_ENABLE);
783 784
	}

785 786
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
787 788
}

789
static void pch_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
790
{
791
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
792
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
793 794
	u32 tmp;

795
	intel_panel_set_pwm_level(old_conn_state, val);
796

797 798
	tmp = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
799

800 801
	tmp = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
802 803
}

804
static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
805
{
806
	intel_panel_set_pwm_level(old_conn_state, val);
807 808
}

809
static void i965_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
810
{
811
	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
812 813
	u32 tmp;

814
	intel_panel_set_pwm_level(old_conn_state, val);
815

816 817
	tmp = intel_de_read(dev_priv, BLC_PWM_CTL2);
	intel_de_write(dev_priv, BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
818 819
}

820
static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
821
{
822
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
823
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
824
	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
825 826
	u32 tmp;

827
	intel_panel_set_pwm_level(old_conn_state, val);
828

829 830 831
	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);
832 833
}

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

841
	intel_panel_set_pwm_level(old_conn_state, val);
842

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

	if (panel->backlight.controller == 1) {
849
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
850
		val &= ~UTIL_PIN_ENABLE;
851
		intel_de_write(dev_priv, UTIL_PIN_CTL, val);
852
	}
853 854
}

855
static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state, u32 val)
856
{
857
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
858 859 860 861
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 tmp;

862
	intel_panel_set_pwm_level(old_conn_state, val);
863

864 865 866 867
	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);
868 869
}

870
static void ext_pwm_disable_backlight(const struct drm_connector_state *old_conn_state, u32 level)
871
{
872
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
873 874
	struct intel_panel *panel = &connector->panel;

875 876
	panel->backlight.pwm_state.enabled = false;
	pwm_apply_state(panel->backlight.pwm, &panel->backlight.pwm_state);
877 878
}

879
void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
880
{
881
	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
882
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
883
	struct intel_panel *panel = &connector->panel;
884

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

888
	/*
889
	 * Do not disable backlight on the vga_switcheroo path. When switching
890 891 892 893
	 * 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.
	 */
894
	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
895 896
		drm_dbg_kms(&dev_priv->drm,
			    "Skipping backlight disable on vga switch\n");
897 898 899
		return;
	}

900
	mutex_lock(&dev_priv->backlight_lock);
901

902 903
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
904
	panel->backlight.enabled = false;
905
	panel->backlight.funcs->disable(old_conn_state, 0);
906

907
	mutex_unlock(&dev_priv->backlight_lock);
908
}
909

910
static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
911
				 const struct drm_connector_state *conn_state, u32 level)
912
{
913
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
914
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
915
	struct intel_panel *panel = &connector->panel;
916
	u32 pch_ctl1, pch_ctl2, schicken;
917

918
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
919
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
920
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
921
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
922
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
923
	}
924

925
	if (HAS_PCH_LPT(dev_priv)) {
926
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN2);
927 928 929 930
		if (panel->backlight.alternate_pwm_increment)
			schicken |= LPT_PWM_GRANULARITY;
		else
			schicken &= ~LPT_PWM_GRANULARITY;
931
		intel_de_write(dev_priv, SOUTH_CHICKEN2, schicken);
932
	} else {
933
		schicken = intel_de_read(dev_priv, SOUTH_CHICKEN1);
934 935 936 937
		if (panel->backlight.alternate_pwm_increment)
			schicken |= SPT_PWM_GRANULARITY;
		else
			schicken &= ~SPT_PWM_GRANULARITY;
938
		intel_de_write(dev_priv, SOUTH_CHICKEN1, schicken);
939 940
	}

941
	pch_ctl2 = panel->backlight.pwm_level_max << 16;
942
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
943

944 945 946
	pch_ctl1 = 0;
	if (panel->backlight.active_low_pwm)
		pch_ctl1 |= BLM_PCH_POLARITY;
947

948 949 950
	/* After LPT, override is the default. */
	if (HAS_PCH_LPT(dev_priv))
		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
951

952 953 954 955
	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);
956 957

	/* This won't stick until the above enable. */
958
	intel_panel_set_pwm_level(conn_state, level);
959 960
}

961
static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
962
				 const struct drm_connector_state *conn_state, u32 level)
963
{
964
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
965
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
966
	struct intel_panel *panel = &connector->panel;
967
	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
968
	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
969

970
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
971
	if (cpu_ctl2 & BLM_PWM_ENABLE) {
972
		drm_dbg_kms(&dev_priv->drm, "cpu backlight already enabled\n");
973
		cpu_ctl2 &= ~BLM_PWM_ENABLE;
974
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2, cpu_ctl2);
975
	}
976

977
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
978
	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
979
		drm_dbg_kms(&dev_priv->drm, "pch backlight already enabled\n");
980
		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
981
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1, pch_ctl1);
982
	}
983 984

	if (cpu_transcoder == TRANSCODER_EDP)
985
		cpu_ctl2 = BLM_TRANSCODER_EDP;
986
	else
987
		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
988 989 990
	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);
991

992
	/* This won't stick until the above enable. */
993
	intel_panel_set_pwm_level(conn_state, level);
994

995
	pch_ctl2 = panel->backlight.pwm_level_max << 16;
996
	intel_de_write(dev_priv, BLC_PWM_PCH_CTL2, pch_ctl2);
997 998 999 1000

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

1002 1003 1004 1005
	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);
1006 1007
}

1008
static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
1009
				  const struct drm_connector_state *conn_state, u32 level)
1010
{
1011
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1012
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1013
	struct intel_panel *panel = &connector->panel;
1014 1015
	u32 ctl, freq;

1016
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1017
	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
1018
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1019
		intel_de_write(dev_priv, BLC_PWM_CTL, 0);
1020
	}
1021

1022
	freq = panel->backlight.pwm_level_max;
1023 1024 1025 1026
	if (panel->backlight.combination_mode)
		freq /= 0xff;

	ctl = freq << 17;
1027
	if (panel->backlight.combination_mode)
1028
		ctl |= BLM_LEGACY_MODE;
1029
	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
1030 1031
		ctl |= BLM_POLARITY_PNV;

1032 1033
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
	intel_de_posting_read(dev_priv, BLC_PWM_CTL);
1034 1035

	/* XXX: combine this into above write? */
1036
	intel_panel_set_pwm_level(conn_state, level);
1037 1038 1039 1040 1041 1042

	/*
	 * 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.
	 */
1043
	if (DISPLAY_VER(dev_priv) == 2)
1044
		intel_de_write(dev_priv, BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
1045
}
1046

1047
static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
1048
				  const struct drm_connector_state *conn_state, u32 level)
1049
{
1050
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1051
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1052
	struct intel_panel *panel = &connector->panel;
1053
	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
1054
	u32 ctl, ctl2, freq;
1055

1056
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1057
	if (ctl2 & BLM_PWM_ENABLE) {
1058
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1059
		ctl2 &= ~BLM_PWM_ENABLE;
1060
		intel_de_write(dev_priv, BLC_PWM_CTL2, ctl2);
1061
	}
1062

1063
	freq = panel->backlight.pwm_level_max;
1064 1065
	if (panel->backlight.combination_mode)
		freq /= 0xff;
1066

1067
	ctl = freq << 16;
1068
	intel_de_write(dev_priv, BLC_PWM_CTL, ctl);
1069

1070 1071 1072 1073 1074
	ctl2 = BLM_PIPE(pipe);
	if (panel->backlight.combination_mode)
		ctl2 |= BLM_COMBINATION_MODE;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1075 1076 1077
	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);
1078

1079
	intel_panel_set_pwm_level(conn_state, level);
1080 1081
}

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

1091
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1092
	if (ctl2 & BLM_PWM_ENABLE) {
1093
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1094
		ctl2 &= ~BLM_PWM_ENABLE;
1095
		intel_de_write(dev_priv, VLV_BLC_PWM_CTL2(pipe), ctl2);
1096
	}
1097

1098
	ctl = panel->backlight.pwm_level_max << 16;
1099
	intel_de_write(dev_priv, VLV_BLC_PWM_CTL(pipe), ctl);
1100

1101
	/* XXX: combine this into above write? */
1102
	intel_panel_set_pwm_level(conn_state, level);
1103

1104 1105 1106
	ctl2 = 0;
	if (panel->backlight.active_low_pwm)
		ctl2 |= BLM_POLARITY_I965;
1107 1108 1109 1110
	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);
1111 1112
}

1113
static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
1114
				 const struct drm_connector_state *conn_state, u32 level)
1115
{
1116
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1117
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1118
	struct intel_panel *panel = &connector->panel;
1119
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1120 1121
	u32 pwm_ctl, val;

1122
	/* Controller 1 uses the utility pin. */
1123
	if (panel->backlight.controller == 1) {
1124
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1125
		if (val & UTIL_PIN_ENABLE) {
1126 1127
			drm_dbg_kms(&dev_priv->drm,
				    "util pin already enabled\n");
1128
			val &= ~UTIL_PIN_ENABLE;
1129
			intel_de_write(dev_priv, UTIL_PIN_CTL, val);
1130
		}
1131

1132 1133 1134
		val = 0;
		if (panel->backlight.util_pin_active_low)
			val |= UTIL_PIN_POLARITY;
1135 1136
		intel_de_write(dev_priv, UTIL_PIN_CTL,
			       val | UTIL_PIN_PIPE(pipe) | UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
1137 1138
	}

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

1149 1150
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
1151
		       panel->backlight.pwm_level_max);
1152

1153
	intel_panel_set_pwm_level(conn_state, level);
1154 1155 1156 1157 1158

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

1159 1160 1161 1162 1163 1164
	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);
1165 1166
}

1167
static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
1168
				 const struct drm_connector_state *conn_state, u32 level)
1169
{
1170
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1171 1172 1173 1174
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl;

1175 1176
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1177
	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
1178
		drm_dbg_kms(&dev_priv->drm, "backlight already enabled\n");
1179
		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
1180 1181 1182
		intel_de_write(dev_priv,
			       BXT_BLC_PWM_CTL(panel->backlight.controller),
			       pwm_ctl);
1183 1184
	}

1185 1186
	intel_de_write(dev_priv,
		       BXT_BLC_PWM_FREQ(panel->backlight.controller),
1187
		       panel->backlight.pwm_level_max);
1188

1189
	intel_panel_set_pwm_level(conn_state, level);
1190 1191 1192 1193 1194

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

1195 1196 1197 1198 1199 1200
	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);
1201 1202
}

1203
static void ext_pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
1204
				     const struct drm_connector_state *conn_state, u32 level)
1205
{
1206
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1207 1208
	struct intel_panel *panel = &connector->panel;

1209 1210 1211
	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);
1212 1213
}

1214 1215
static void __intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
					   const struct drm_connector_state *conn_state)
1216
{
1217
	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1218
	struct intel_panel *panel = &connector->panel;
1219

1220 1221
	WARN_ON(panel->backlight.max == 0);

1222
	if (panel->backlight.level <= panel->backlight.min) {
1223
		panel->backlight.level = panel->backlight.max;
1224 1225
		if (panel->backlight.device)
			panel->backlight.device->props.brightness =
1226 1227 1228
				scale_hw_to_user(connector,
						 panel->backlight.level,
						 panel->backlight.device->props.max_brightness);
1229
	}
1230

1231
	panel->backlight.funcs->enable(crtc_state, conn_state, panel->backlight.level);
1232
	panel->backlight.enabled = true;
1233 1234
	if (panel->backlight.device)
		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
1235 1236 1237 1238 1239 1240 1241 1242
}

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;
1243
	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1244 1245 1246 1247

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

1248
	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(pipe));
1249 1250 1251 1252

	mutex_lock(&dev_priv->backlight_lock);

	__intel_panel_enable_backlight(crtc_state, conn_state);
1253

1254
	mutex_unlock(&dev_priv->backlight_lock);
1255 1256
}

1257
#if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
1258 1259 1260 1261 1262 1263 1264 1265
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);

1266
	if (panel->backlight.enabled)
1267
		val = panel->backlight.funcs->get(connector, intel_connector_get_pipe(connector));
1268 1269 1270

	mutex_unlock(&dev_priv->backlight_lock);

1271
	drm_dbg_kms(&dev_priv->drm, "get backlight PWM = %d\n", val);
1272 1273 1274
	return val;
}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
/* 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);
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
/* 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);

1299
	drm_WARN_ON(&dev_priv->drm, panel->backlight.max == 0);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

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

1310
static int intel_backlight_device_update_status(struct backlight_device *bd)
1311
{
1312
	struct intel_connector *connector = bl_get_data(bd);
1313
	struct intel_panel *panel = &connector->panel;
1314 1315
	struct drm_device *dev = connector->base.dev;

1316
	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1317 1318
	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
		      bd->props.brightness, bd->props.max_brightness);
1319
	intel_panel_set_backlight(connector->base.state, bd->props.brightness,
1320
				  bd->props.max_brightness);
1321 1322 1323 1324 1325 1326 1327 1328

	/*
	 * 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) {
1329
		if (panel->backlight.power) {
1330 1331
			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
				bd->props.brightness != 0;
1332
			panel->backlight.power(connector, enable);
1333 1334 1335 1336 1337
		}
	} else {
		bd->props.power = FB_BLANK_POWERDOWN;
	}

1338
	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1339 1340 1341
	return 0;
}

1342
static int intel_backlight_device_get_brightness(struct backlight_device *bd)
1343
{
1344 1345
	struct intel_connector *connector = bl_get_data(bd);
	struct drm_device *dev = connector->base.dev;
1346
	struct drm_i915_private *dev_priv = to_i915(dev);
1347
	intel_wakeref_t wakeref;
1348
	int ret = 0;
1349

1350
	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
1351
		u32 hw_level;
1352

1353
		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1354

1355 1356 1357 1358 1359 1360
		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);
	}
1361

1362
	return ret;
1363 1364
}

1365 1366 1367
static const struct backlight_ops intel_backlight_device_ops = {
	.update_status = intel_backlight_device_update_status,
	.get_brightness = intel_backlight_device_get_brightness,
1368 1369
};

1370
int intel_backlight_device_register(struct intel_connector *connector)
1371
{
1372
	struct drm_i915_private *i915 = to_i915(connector->base.dev);
1373
	struct intel_panel *panel = &connector->panel;
1374
	struct backlight_properties props;
1375 1376 1377
	struct backlight_device *bd;
	const char *name;
	int ret = 0;
1378

1379
	if (WARN_ON(panel->backlight.device))
1380 1381
		return -ENODEV;

1382 1383 1384
	if (!panel->backlight.present)
		return 0;

1385
	WARN_ON(panel->backlight.max == 0);
1386

1387
	memset(&props, 0, sizeof(props));
1388
	props.type = BACKLIGHT_RAW;
1389 1390 1391 1392 1393

	/*
	 * Note: Everything should work even if the backlight device max
	 * presented to the userspace is arbitrarily chosen.
	 */
1394
	props.max_brightness = panel->backlight.max;
1395 1396 1397
	props.brightness = scale_hw_to_user(connector,
					    panel->backlight.level,
					    props.max_brightness);
1398

1399 1400 1401 1402 1403
	if (panel->backlight.enabled)
		props.power = FB_BLANK_UNBLANK;
	else
		props.power = FB_BLANK_POWERDOWN;

1404 1405 1406 1407
	/*
	 * Note: using the same name independent of the connector prevents
	 * registration of multiple backlight devices in the driver.
	 */
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	name = kstrdup("intel_backlight", GFP_KERNEL);
	if (!name)
		return -ENOMEM;

	bd = backlight_device_register(name, connector->base.kdev, connector,
				       &intel_backlight_device_ops, &props);
	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;
1420
	}
1421

1422 1423
	panel->backlight.device = bd;

1424
	drm_dbg_kms(&i915->drm,
1425 1426
		    "[CONNECTOR:%d:%s] backlight device %s registered\n",
		    connector->base.base.id, connector->base.name, name);
1427

1428 1429 1430 1431
out:
	kfree(name);

	return ret;
1432 1433
}

1434
void intel_backlight_device_unregister(struct intel_connector *connector)
1435
{
1436 1437 1438 1439 1440
	struct intel_panel *panel = &connector->panel;

	if (panel->backlight.device) {
		backlight_device_unregister(panel->backlight.device);
		panel->backlight.device = NULL;
1441
	}
1442
}
1443 1444
#endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */

1445 1446 1447 1448 1449 1450 1451 1452
/*
 * 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);

1453 1454
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz);
1455 1456
}

1457 1458 1459 1460 1461
/*
 * BXT: PWM clock frequency = 19.2 MHz.
 */
static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1462
	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
1463 1464
}

1465
/*
1466 1467 1468 1469 1470 1471
 * 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)
{
1472
	struct intel_panel *panel = &connector->panel;
1473
	u32 mul;
1474

1475
	if (panel->backlight.alternate_pwm_increment)
1476 1477 1478 1479
		mul = 128;
	else
		mul = 16;

1480
	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
1481 1482 1483 1484 1485 1486 1487 1488 1489
}

/*
 * 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)
{
1490
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1491
	struct intel_panel *panel = &connector->panel;
1492 1493
	u32 mul, clock;

1494
	if (panel->backlight.alternate_pwm_increment)
1495 1496 1497 1498
		mul = 16;
	else
		mul = 128;

V
Ville Syrjälä 已提交
1499
	if (HAS_PCH_LPT_H(dev_priv))
1500 1501 1502 1503
		clock = MHz(135); /* LPT:H */
	else
		clock = MHz(24); /* LPT:LP */

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

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

1515 1516
	return DIV_ROUND_CLOSEST(KHz(RUNTIME_INFO(dev_priv)->rawclk_freq),
				 pwm_freq_hz * 128);
1517 1518 1519 1520 1521 1522
}

/*
 * 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.
1523
 *
1524 1525 1526 1527 1528
 * 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)
{
1529
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1530 1531
	int clock;

1532
	if (IS_PINEVIEW(dev_priv))
1533
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1534
	else
1535
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1536

1537
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
1538 1539 1540 1541
}

/*
 * Gen4: This value represents the period of the PWM stream in display core
1542 1543
 * clocks ([DevCTG] HRAW clocks) multiplied by 128.
 *
1544 1545 1546
 */
static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
{
1547
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1548 1549 1550
	int clock;

	if (IS_G4X(dev_priv))
1551
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1552
	else
1553
		clock = KHz(dev_priv->cdclk.hw.cdclk);
1554

1555
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
1556 1557 1558 1559 1560 1561 1562 1563 1564
}

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

1568
	if ((intel_de_read(dev_priv, CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
1569 1570
		if (IS_CHERRYVIEW(dev_priv))
			clock = KHz(19200);
1571
		else
1572 1573
			clock = MHz(25);
		mul = 16;
1574
	} else {
1575
		clock = KHz(RUNTIME_INFO(dev_priv)->rawclk_freq);
1576
		mul = 128;
1577
	}
1578

1579
	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
1580 1581
}

1582
static u16 get_vbt_pwm_freq(struct drm_i915_private *dev_priv)
1583 1584 1585
{
	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;

1586
	if (pwm_freq_hz) {
1587 1588 1589
		drm_dbg_kms(&dev_priv->drm,
			    "VBT defined backlight frequency %u Hz\n",
			    pwm_freq_hz);
1590 1591
	} else {
		pwm_freq_hz = 200;
1592 1593 1594
		drm_dbg_kms(&dev_priv->drm,
			    "default backlight frequency %u Hz\n",
			    pwm_freq_hz);
1595 1596
	}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	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;

1607
	if (!panel->backlight.pwm_funcs->hz_to_pwm) {
1608 1609 1610 1611 1612
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion not supported\n");
		return 0;
	}

1613
	pwm = panel->backlight.pwm_funcs->hz_to_pwm(connector, pwm_freq_hz);
1614
	if (!pwm) {
1615 1616
		drm_dbg_kms(&dev_priv->drm,
			    "backlight frequency conversion failed\n");
1617 1618 1619 1620 1621 1622 1623 1624
		return 0;
	}

	return pwm;
}

/*
 * Note: The setup hooks can't assume pipe is set!
1625
 */
1626 1627
static u32 get_backlight_min_vbt(struct intel_connector *connector)
{
1628
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1629
	struct intel_panel *panel = &connector->panel;
1630
	int min;
1631

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

1634 1635 1636 1637 1638 1639 1640 1641 1642
	/*
	 * 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) {
1643 1644 1645
		drm_dbg_kms(&dev_priv->drm,
			    "clamping VBT min backlight %d/255 to %d/255\n",
			    dev_priv->vbt.backlight.min_brightness, min);
1646 1647
	}

1648
	/* vbt value is a coefficient in range [0..255] */
1649
	return scale(min, 0, 255, 0, panel->backlight.pwm_level_max);
1650 1651
}

1652
static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
1653
{
1654
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1655
	struct intel_panel *panel = &connector->panel;
1656 1657
	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
	bool alt, cpu_mode;
1658 1659

	if (HAS_PCH_LPT(dev_priv))
1660
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
1661
	else
1662
		alt = intel_de_read(dev_priv, SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
1663
	panel->backlight.alternate_pwm_increment = alt;
1664

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

1668
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1669
	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1670

1671
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1672

1673 1674
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1675

1676
	if (!panel->backlight.pwm_level_max)
1677 1678
		return -ENODEV;

1679
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1680

1681
	panel->backlight.pwm_enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
1682

1683
	cpu_mode = panel->backlight.pwm_enabled && HAS_PCH_LPT(dev_priv) &&
1684 1685
		   !(pch_ctl1 & BLM_PCH_OVERRIDE_ENABLE) &&
		   (cpu_ctl2 & BLM_PWM_ENABLE);
1686

1687
	if (cpu_mode) {
1688 1689
		val = pch_get_backlight(connector, unused);

1690 1691
		drm_dbg_kms(&dev_priv->drm,
			    "CPU backlight register was enabled, switching to PCH override\n");
1692 1693

		/* Write converted CPU PWM value to PCH override register */
1694
		lpt_set_backlight(connector->base.state, val);
1695 1696
		intel_de_write(dev_priv, BLC_PWM_PCH_CTL1,
			       pch_ctl1 | BLM_PCH_OVERRIDE_ENABLE);
1697

1698 1699
		intel_de_write(dev_priv, BLC_PWM_CPU_CTL2,
			       cpu_ctl2 & ~BLM_PWM_ENABLE);
1700
	}
1701 1702 1703 1704

	return 0;
}

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

1711
	pch_ctl1 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL1);
1712 1713
	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;

1714
	pch_ctl2 = intel_de_read(dev_priv, BLC_PWM_PCH_CTL2);
1715
	panel->backlight.pwm_level_max = pch_ctl2 >> 16;
1716

1717 1718
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1719

1720
	if (!panel->backlight.pwm_level_max)
1721 1722
		return -ENODEV;

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

1725
	cpu_ctl2 = intel_de_read(dev_priv, BLC_PWM_CPU_CTL2);
1726
	panel->backlight.pwm_enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
1727
		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
1728

1729 1730 1731
	return 0;
}

1732
static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
1733
{
1734
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1735
	struct intel_panel *panel = &connector->panel;
1736 1737
	u32 ctl, val;

1738
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1739

1740
	if (DISPLAY_VER(dev_priv) == 2 || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
1741 1742
		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;

1743
	if (IS_PINEVIEW(dev_priv))
1744 1745
		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;

1746
	panel->backlight.pwm_level_max = ctl >> 17;
1747

1748 1749 1750
	if (!panel->backlight.pwm_level_max) {
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
		panel->backlight.pwm_level_max >>= 1;
1751
	}
1752

1753
	if (!panel->backlight.pwm_level_max)
1754 1755
		return -ENODEV;

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

1759
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1760

1761
	val = i9xx_get_backlight(connector, unused);
1762 1763
	val = intel_panel_invert_pwm_level(connector, val);
	val = clamp(val, panel->backlight.pwm_level_min, panel->backlight.pwm_level_max);
1764

1765
	panel->backlight.pwm_enabled = val != 0;
1766

1767 1768 1769
	return 0;
}

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

1776
	ctl2 = intel_de_read(dev_priv, BLC_PWM_CTL2);
1777 1778 1779
	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1780
	ctl = intel_de_read(dev_priv, BLC_PWM_CTL);
1781
	panel->backlight.pwm_level_max = ctl >> 16;
1782

1783 1784
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1785

1786
	if (!panel->backlight.pwm_level_max)
1787 1788
		return -ENODEV;

1789
	if (panel->backlight.combination_mode)
1790
		panel->backlight.pwm_level_max *= 0xff;
1791

1792
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1793

1794
	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1795

1796 1797 1798
	return 0;
}

1799
static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
1800
{
1801
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1802
	struct intel_panel *panel = &connector->panel;
1803
	u32 ctl, ctl2;
1804

1805
	if (drm_WARN_ON(&dev_priv->drm, pipe != PIPE_A && pipe != PIPE_B))
1806 1807
		return -ENODEV;

1808
	ctl2 = intel_de_read(dev_priv, VLV_BLC_PWM_CTL2(pipe));
1809 1810
	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;

1811
	ctl = intel_de_read(dev_priv, VLV_BLC_PWM_CTL(pipe));
1812
	panel->backlight.pwm_level_max = ctl >> 16;
1813

1814 1815
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1816

1817
	if (!panel->backlight.pwm_level_max)
1818 1819
		return -ENODEV;

1820
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1821

1822
	panel->backlight.pwm_enabled = ctl2 & BLM_PWM_ENABLE;
1823

1824 1825 1826
	return 0;
}

1827 1828 1829
static int
bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
{
1830
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1831 1832 1833
	struct intel_panel *panel = &connector->panel;
	u32 pwm_ctl, val;

1834
	panel->backlight.controller = dev_priv->vbt.backlight.controller;
1835

1836 1837
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1838

1839
	/* Controller 1 uses the utility pin. */
1840
	if (panel->backlight.controller == 1) {
1841
		val = intel_de_read(dev_priv, UTIL_PIN_CTL);
1842 1843 1844 1845 1846
		panel->backlight.util_pin_active_low =
					val & UTIL_PIN_POLARITY;
	}

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1847 1848
	panel->backlight.pwm_level_max =
		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1849

1850 1851
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1852

1853
	if (!panel->backlight.pwm_level_max)
1854 1855
		return -ENODEV;

1856
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1857

1858
	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1859 1860 1861 1862

	return 0;
}

1863 1864 1865 1866 1867
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;
1868
	u32 pwm_ctl;
1869 1870

	/*
1871 1872 1873
	 * 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.
1874 1875 1876
	 */
	panel->backlight.controller = 0;

1877 1878
	pwm_ctl = intel_de_read(dev_priv,
				BXT_BLC_PWM_CTL(panel->backlight.controller));
1879 1880

	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
1881 1882
	panel->backlight.pwm_level_max =
		intel_de_read(dev_priv, BXT_BLC_PWM_FREQ(panel->backlight.controller));
1883

1884 1885
	if (!panel->backlight.pwm_level_max)
		panel->backlight.pwm_level_max = get_backlight_max_vbt(connector);
1886

1887
	if (!panel->backlight.pwm_level_max)
1888 1889
		return -ENODEV;

1890
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1891

1892
	panel->backlight.pwm_enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
1893 1894 1895 1896

	return 0;
}

1897 1898
static int ext_pwm_setup_backlight(struct intel_connector *connector,
				   enum pipe pipe)
1899 1900
{
	struct drm_device *dev = connector->base.dev;
1901
	struct drm_i915_private *dev_priv = to_i915(dev);
1902
	struct intel_panel *panel = &connector->panel;
1903
	const char *desc;
1904
	u32 level;
1905

1906 1907 1908 1909 1910 1911 1912 1913 1914
	/* 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";
	}

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

1922 1923
	panel->backlight.pwm_level_max = 100; /* 100% */
	panel->backlight.pwm_level_min = get_backlight_min_vbt(connector);
1924

1925 1926 1927 1928 1929 1930
	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);
1931 1932
		level = intel_panel_invert_pwm_level(connector, level);
		panel->backlight.pwm_enabled = true;
1933 1934 1935 1936 1937 1938 1939 1940 1941

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

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

1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 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
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;
}

2000 2001
void intel_panel_update_backlight(struct intel_atomic_state *state,
				  struct intel_encoder *encoder,
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
				  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);
}

2019
int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
2020
{
2021
	struct drm_i915_private *dev_priv = to_i915(connector->dev);
2022
	struct intel_connector *intel_connector = to_intel_connector(connector);
2023
	struct intel_panel *panel = &intel_connector->panel;
2024
	int ret;
2025

2026
	if (!dev_priv->vbt.backlight.present) {
2027
		if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
2028 2029
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT, but present per quirk\n");
2030
		} else {
2031 2032
			drm_dbg_kms(&dev_priv->drm,
				    "no backlight present per VBT\n");
2033 2034
			return 0;
		}
2035 2036
	}

2037
	/* ensure intel_panel has been initialized first */
2038
	if (drm_WARN_ON(&dev_priv->drm, !panel->backlight.funcs))
2039 2040
		return -ENODEV;

2041
	/* set level and max in panel struct */
2042
	mutex_lock(&dev_priv->backlight_lock);
2043
	ret = panel->backlight.funcs->setup(intel_connector, pipe);
2044
	mutex_unlock(&dev_priv->backlight_lock);
2045 2046

	if (ret) {
2047 2048 2049
		drm_dbg_kms(&dev_priv->drm,
			    "failed to setup backlight for connector %s\n",
			    connector->name);
2050 2051
		return ret;
	}
2052

2053 2054
	panel->backlight.present = true;

2055 2056 2057 2058 2059
	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);
2060

2061 2062 2063
	return 0;
}

2064
static void intel_panel_destroy_backlight(struct intel_panel *panel)
2065
{
2066 2067 2068 2069
	/* dispose of the pwm */
	if (panel->backlight.pwm)
		pwm_put(panel->backlight.pwm);

2070
	panel->backlight.present = false;
2071
}
2072

2073
static const struct intel_panel_bl_funcs bxt_pwm_funcs = {
2074 2075 2076 2077 2078 2079 2080 2081
	.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,
};

2082
static const struct intel_panel_bl_funcs cnp_pwm_funcs = {
2083 2084 2085 2086 2087 2088 2089 2090
	.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,
};

2091
static const struct intel_panel_bl_funcs lpt_pwm_funcs = {
2092 2093 2094 2095 2096 2097 2098 2099
	.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,
};

2100
static const struct intel_panel_bl_funcs spt_pwm_funcs = {
2101 2102 2103 2104 2105 2106 2107 2108
	.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,
};

2109
static const struct intel_panel_bl_funcs pch_pwm_funcs = {
2110 2111 2112 2113 2114 2115 2116 2117
	.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,
};

2118 2119 2120 2121 2122 2123
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,
2124 2125
};

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

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

2144
static const struct intel_panel_bl_funcs i9xx_pwm_funcs = {
2145 2146 2147 2148 2149 2150 2151 2152
	.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,
};

2153 2154 2155 2156 2157 2158 2159 2160
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,
};

2161
/* Set up chip specific backlight functions */
2162 2163
static void
intel_panel_init_backlight_funcs(struct intel_panel *panel)
2164
{
2165
	struct intel_connector *connector =
2166
		container_of(panel, struct intel_connector, panel);
2167
	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
2168

2169 2170 2171 2172
	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
		return;

2173
	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
2174
		panel->backlight.pwm_funcs = &bxt_pwm_funcs;
2175
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
2176
		panel->backlight.pwm_funcs = &cnp_pwm_funcs;
2177
	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
2178
		if (HAS_PCH_LPT(dev_priv))
2179
			panel->backlight.pwm_funcs = &lpt_pwm_funcs;
2180
		else
2181
			panel->backlight.pwm_funcs = &spt_pwm_funcs;
2182
	} else if (HAS_PCH_SPLIT(dev_priv)) {
2183
		panel->backlight.pwm_funcs = &pch_pwm_funcs;
2184
	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
2185
		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
2186
			panel->backlight.pwm_funcs = &ext_pwm_funcs;
2187
		} else {
2188
			panel->backlight.pwm_funcs = &vlv_pwm_funcs;
2189
		}
2190
	} else if (DISPLAY_VER(dev_priv) == 4) {
2191
		panel->backlight.pwm_funcs = &i965_pwm_funcs;
2192
	} else {
2193
		panel->backlight.pwm_funcs = &i9xx_pwm_funcs;
2194
	}
2195 2196 2197 2198

	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
	    intel_dp_aux_init_backlight_funcs(connector) == 0)
		return;
2199 2200 2201

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

2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
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;
}

2215
int intel_panel_init(struct intel_panel *panel,
2216 2217
		     struct drm_display_mode *fixed_mode,
		     struct drm_display_mode *downclock_mode)
2218
{
2219 2220
	intel_panel_init_backlight_funcs(panel);

2221
	panel->fixed_mode = fixed_mode;
2222
	panel->downclock_mode = downclock_mode;
2223

2224 2225 2226 2227 2228
	return 0;
}

void intel_panel_fini(struct intel_panel *panel)
{
2229 2230 2231
	struct intel_connector *intel_connector =
		container_of(panel, struct intel_connector, panel);

2232 2233
	intel_panel_destroy_backlight(panel);

2234 2235
	if (panel->fixed_mode)
		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
2236 2237 2238 2239

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