Home | History | Annotate | Line # | Download | only in display
intel_panel.c revision 1.2
      1 /*	$NetBSD: intel_panel.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2006-2010 Intel Corporation
      5  * Copyright (c) 2006 Dave Airlie <airlied (at) linux.ie>
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the next
     15  * paragraph) shall be included in all copies or substantial portions of the
     16  * Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     24  * DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors:
     27  *	Eric Anholt <eric (at) anholt.net>
     28  *      Dave Airlie <airlied (at) linux.ie>
     29  *      Jesse Barnes <jesse.barnes (at) intel.com>
     30  *      Chris Wilson <chris (at) chris-wilson.co.uk>
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: intel_panel.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $");
     35 
     36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
     37 
     38 #include <linux/kernel.h>
     39 #include <linux/moduleparam.h>
     40 #include <linux/pwm.h>
     41 
     42 #include "intel_connector.h"
     43 #include "intel_display_types.h"
     44 #include "intel_dp_aux_backlight.h"
     45 #include "intel_dsi_dcs_backlight.h"
     46 #include "intel_panel.h"
     47 
     48 #define CRC_PMIC_PWM_PERIOD_NS	21333
     49 
     50 void
     51 intel_fixed_panel_mode(const struct drm_display_mode *fixed_mode,
     52 		       struct drm_display_mode *adjusted_mode)
     53 {
     54 	drm_mode_copy(adjusted_mode, fixed_mode);
     55 
     56 	drm_mode_set_crtcinfo(adjusted_mode, 0);
     57 }
     58 
     59 static bool is_downclock_mode(const struct drm_display_mode *downclock_mode,
     60 			      const struct drm_display_mode *fixed_mode)
     61 {
     62 	return drm_mode_match(downclock_mode, fixed_mode,
     63 			      DRM_MODE_MATCH_TIMINGS |
     64 			      DRM_MODE_MATCH_FLAGS |
     65 			      DRM_MODE_MATCH_3D_FLAGS) &&
     66 		downclock_mode->clock < fixed_mode->clock;
     67 }
     68 
     69 struct drm_display_mode *
     70 intel_panel_edid_downclock_mode(struct intel_connector *connector,
     71 				const struct drm_display_mode *fixed_mode)
     72 {
     73 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
     74 	const struct drm_display_mode *scan, *best_mode = NULL;
     75 	struct drm_display_mode *downclock_mode;
     76 	int best_clock = fixed_mode->clock;
     77 
     78 	list_for_each_entry(scan, &connector->base.probed_modes, head) {
     79 		/*
     80 		 * If one mode has the same resolution with the fixed_panel
     81 		 * mode while they have the different refresh rate, it means
     82 		 * that the reduced downclock is found. In such
     83 		 * case we can set the different FPx0/1 to dynamically select
     84 		 * between low and high frequency.
     85 		 */
     86 		if (is_downclock_mode(scan, fixed_mode) &&
     87 		    scan->clock < best_clock) {
     88 			/*
     89 			 * The downclock is already found. But we
     90 			 * expect to find the lower downclock.
     91 			 */
     92 			best_clock = scan->clock;
     93 			best_mode = scan;
     94 		}
     95 	}
     96 
     97 	if (!best_mode)
     98 		return NULL;
     99 
    100 	downclock_mode = drm_mode_duplicate(&dev_priv->drm, best_mode);
    101 	if (!downclock_mode)
    102 		return NULL;
    103 
    104 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using downclock mode from EDID: ",
    105 		      connector->base.base.id, connector->base.name);
    106 	drm_mode_debug_printmodeline(downclock_mode);
    107 
    108 	return downclock_mode;
    109 }
    110 
    111 struct drm_display_mode *
    112 intel_panel_edid_fixed_mode(struct intel_connector *connector)
    113 {
    114 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    115 	const struct drm_display_mode *scan;
    116 	struct drm_display_mode *fixed_mode;
    117 
    118 	if (list_empty(&connector->base.probed_modes))
    119 		return NULL;
    120 
    121 	/* prefer fixed mode from EDID if available */
    122 	list_for_each_entry(scan, &connector->base.probed_modes, head) {
    123 		if ((scan->type & DRM_MODE_TYPE_PREFERRED) == 0)
    124 			continue;
    125 
    126 		fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
    127 		if (!fixed_mode)
    128 			return NULL;
    129 
    130 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using preferred mode from EDID: ",
    131 			      connector->base.base.id, connector->base.name);
    132 		drm_mode_debug_printmodeline(fixed_mode);
    133 
    134 		return fixed_mode;
    135 	}
    136 
    137 	scan = list_first_entry(&connector->base.probed_modes,
    138 				typeof(*scan), head);
    139 
    140 	fixed_mode = drm_mode_duplicate(&dev_priv->drm, scan);
    141 	if (!fixed_mode)
    142 		return NULL;
    143 
    144 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
    145 
    146 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using first mode from EDID: ",
    147 		      connector->base.base.id, connector->base.name);
    148 	drm_mode_debug_printmodeline(fixed_mode);
    149 
    150 	return fixed_mode;
    151 }
    152 
    153 struct drm_display_mode *
    154 intel_panel_vbt_fixed_mode(struct intel_connector *connector)
    155 {
    156 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    157 	struct drm_display_info *info = &connector->base.display_info;
    158 	struct drm_display_mode *fixed_mode;
    159 
    160 	if (!dev_priv->vbt.lfp_lvds_vbt_mode)
    161 		return NULL;
    162 
    163 	fixed_mode = drm_mode_duplicate(&dev_priv->drm,
    164 					dev_priv->vbt.lfp_lvds_vbt_mode);
    165 	if (!fixed_mode)
    166 		return NULL;
    167 
    168 	fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
    169 
    170 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] using mode from VBT: ",
    171 		      connector->base.base.id, connector->base.name);
    172 	drm_mode_debug_printmodeline(fixed_mode);
    173 
    174 	info->width_mm = fixed_mode->width_mm;
    175 	info->height_mm = fixed_mode->height_mm;
    176 
    177 	return fixed_mode;
    178 }
    179 
    180 /* adjusted_mode has been preset to be the panel's fixed mode */
    181 void
    182 intel_pch_panel_fitting(struct intel_crtc *intel_crtc,
    183 			struct intel_crtc_state *pipe_config,
    184 			int fitting_mode)
    185 {
    186 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
    187 	int x = 0, y = 0, width = 0, height = 0;
    188 
    189 	/* Native modes don't need fitting */
    190 	if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
    191 	    adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h &&
    192 	    pipe_config->output_format != INTEL_OUTPUT_FORMAT_YCBCR420)
    193 		goto done;
    194 
    195 	switch (fitting_mode) {
    196 	case DRM_MODE_SCALE_CENTER:
    197 		width = pipe_config->pipe_src_w;
    198 		height = pipe_config->pipe_src_h;
    199 		x = (adjusted_mode->crtc_hdisplay - width + 1)/2;
    200 		y = (adjusted_mode->crtc_vdisplay - height + 1)/2;
    201 		break;
    202 
    203 	case DRM_MODE_SCALE_ASPECT:
    204 		/* Scale but preserve the aspect ratio */
    205 		{
    206 			u32 scaled_width = adjusted_mode->crtc_hdisplay
    207 				* pipe_config->pipe_src_h;
    208 			u32 scaled_height = pipe_config->pipe_src_w
    209 				* adjusted_mode->crtc_vdisplay;
    210 			if (scaled_width > scaled_height) { /* pillar */
    211 				width = scaled_height / pipe_config->pipe_src_h;
    212 				if (width & 1)
    213 					width++;
    214 				x = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
    215 				y = 0;
    216 				height = adjusted_mode->crtc_vdisplay;
    217 			} else if (scaled_width < scaled_height) { /* letter */
    218 				height = scaled_width / pipe_config->pipe_src_w;
    219 				if (height & 1)
    220 				    height++;
    221 				y = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
    222 				x = 0;
    223 				width = adjusted_mode->crtc_hdisplay;
    224 			} else {
    225 				x = y = 0;
    226 				width = adjusted_mode->crtc_hdisplay;
    227 				height = adjusted_mode->crtc_vdisplay;
    228 			}
    229 		}
    230 		break;
    231 
    232 	case DRM_MODE_SCALE_FULLSCREEN:
    233 		x = y = 0;
    234 		width = adjusted_mode->crtc_hdisplay;
    235 		height = adjusted_mode->crtc_vdisplay;
    236 		break;
    237 
    238 	default:
    239 		WARN(1, "bad panel fit mode: %d\n", fitting_mode);
    240 		return;
    241 	}
    242 
    243 done:
    244 	pipe_config->pch_pfit.pos = (x << 16) | y;
    245 	pipe_config->pch_pfit.size = (width << 16) | height;
    246 	pipe_config->pch_pfit.enabled = pipe_config->pch_pfit.size != 0;
    247 }
    248 
    249 static void
    250 centre_horizontally(struct drm_display_mode *adjusted_mode,
    251 		    int width)
    252 {
    253 	u32 border, sync_pos, blank_width, sync_width;
    254 
    255 	/* keep the hsync and hblank widths constant */
    256 	sync_width = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
    257 	blank_width = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
    258 	sync_pos = (blank_width - sync_width + 1) / 2;
    259 
    260 	border = (adjusted_mode->crtc_hdisplay - width + 1) / 2;
    261 	border += border & 1; /* make the border even */
    262 
    263 	adjusted_mode->crtc_hdisplay = width;
    264 	adjusted_mode->crtc_hblank_start = width + border;
    265 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_hblank_start + blank_width;
    266 
    267 	adjusted_mode->crtc_hsync_start = adjusted_mode->crtc_hblank_start + sync_pos;
    268 	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 		  int height)
    274 {
    275 	u32 border, sync_pos, blank_width, sync_width;
    276 
    277 	/* keep the vsync and vblank widths constant */
    278 	sync_width = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
    279 	blank_width = adjusted_mode->crtc_vblank_end - adjusted_mode->crtc_vblank_start;
    280 	sync_pos = (blank_width - sync_width + 1) / 2;
    281 
    282 	border = (adjusted_mode->crtc_vdisplay - height + 1) / 2;
    283 
    284 	adjusted_mode->crtc_vdisplay = height;
    285 	adjusted_mode->crtc_vblank_start = height + border;
    286 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vblank_start + blank_width;
    287 
    288 	adjusted_mode->crtc_vsync_start = adjusted_mode->crtc_vblank_start + sync_pos;
    289 	adjusted_mode->crtc_vsync_end = adjusted_mode->crtc_vsync_start + sync_width;
    290 }
    291 
    292 static inline u32 panel_fitter_scaling(u32 source, u32 target)
    293 {
    294 	/*
    295 	 * Floating point operation is not supported. So the FACTOR
    296 	 * is defined, which can avoid the floating point computation
    297 	 * when calculating the panel ratio.
    298 	 */
    299 #define ACCURACY 12
    300 #define FACTOR (1 << ACCURACY)
    301 	u32 ratio = source * FACTOR / target;
    302 	return (FACTOR * ratio + FACTOR/2) / FACTOR;
    303 }
    304 
    305 static void i965_scale_aspect(struct intel_crtc_state *pipe_config,
    306 			      u32 *pfit_control)
    307 {
    308 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
    309 	u32 scaled_width = adjusted_mode->crtc_hdisplay *
    310 		pipe_config->pipe_src_h;
    311 	u32 scaled_height = pipe_config->pipe_src_w *
    312 		adjusted_mode->crtc_vdisplay;
    313 
    314 	/* 965+ is easy, it does everything in hw */
    315 	if (scaled_width > scaled_height)
    316 		*pfit_control |= PFIT_ENABLE |
    317 			PFIT_SCALING_PILLAR;
    318 	else if (scaled_width < scaled_height)
    319 		*pfit_control |= PFIT_ENABLE |
    320 			PFIT_SCALING_LETTER;
    321 	else if (adjusted_mode->crtc_hdisplay != pipe_config->pipe_src_w)
    322 		*pfit_control |= PFIT_ENABLE | PFIT_SCALING_AUTO;
    323 }
    324 
    325 static void i9xx_scale_aspect(struct intel_crtc_state *pipe_config,
    326 			      u32 *pfit_control, u32 *pfit_pgm_ratios,
    327 			      u32 *border)
    328 {
    329 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
    330 	u32 scaled_width = adjusted_mode->crtc_hdisplay *
    331 		pipe_config->pipe_src_h;
    332 	u32 scaled_height = pipe_config->pipe_src_w *
    333 		adjusted_mode->crtc_vdisplay;
    334 	u32 bits;
    335 
    336 	/*
    337 	 * For earlier chips we have to calculate the scaling
    338 	 * ratio by hand and program it into the
    339 	 * PFIT_PGM_RATIO register
    340 	 */
    341 	if (scaled_width > scaled_height) { /* pillar */
    342 		centre_horizontally(adjusted_mode,
    343 				    scaled_height /
    344 				    pipe_config->pipe_src_h);
    345 
    346 		*border = LVDS_BORDER_ENABLE;
    347 		if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay) {
    348 			bits = panel_fitter_scaling(pipe_config->pipe_src_h,
    349 						    adjusted_mode->crtc_vdisplay);
    350 
    351 			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
    352 					     bits << PFIT_VERT_SCALE_SHIFT);
    353 			*pfit_control |= (PFIT_ENABLE |
    354 					  VERT_INTERP_BILINEAR |
    355 					  HORIZ_INTERP_BILINEAR);
    356 		}
    357 	} else if (scaled_width < scaled_height) { /* letter */
    358 		centre_vertically(adjusted_mode,
    359 				  scaled_width /
    360 				  pipe_config->pipe_src_w);
    361 
    362 		*border = LVDS_BORDER_ENABLE;
    363 		if (pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
    364 			bits = panel_fitter_scaling(pipe_config->pipe_src_w,
    365 						    adjusted_mode->crtc_hdisplay);
    366 
    367 			*pfit_pgm_ratios |= (bits << PFIT_HORIZ_SCALE_SHIFT |
    368 					     bits << PFIT_VERT_SCALE_SHIFT);
    369 			*pfit_control |= (PFIT_ENABLE |
    370 					  VERT_INTERP_BILINEAR |
    371 					  HORIZ_INTERP_BILINEAR);
    372 		}
    373 	} else {
    374 		/* Aspects match, Let hw scale both directions */
    375 		*pfit_control |= (PFIT_ENABLE |
    376 				  VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
    377 				  VERT_INTERP_BILINEAR |
    378 				  HORIZ_INTERP_BILINEAR);
    379 	}
    380 }
    381 
    382 void intel_gmch_panel_fitting(struct intel_crtc *intel_crtc,
    383 			      struct intel_crtc_state *pipe_config,
    384 			      int fitting_mode)
    385 {
    386 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
    387 	u32 pfit_control = 0, pfit_pgm_ratios = 0, border = 0;
    388 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
    389 
    390 	/* Native modes don't need fitting */
    391 	if (adjusted_mode->crtc_hdisplay == pipe_config->pipe_src_w &&
    392 	    adjusted_mode->crtc_vdisplay == pipe_config->pipe_src_h)
    393 		goto out;
    394 
    395 	switch (fitting_mode) {
    396 	case DRM_MODE_SCALE_CENTER:
    397 		/*
    398 		 * For centered modes, we have to calculate border widths &
    399 		 * heights and modify the values programmed into the CRTC.
    400 		 */
    401 		centre_horizontally(adjusted_mode, pipe_config->pipe_src_w);
    402 		centre_vertically(adjusted_mode, pipe_config->pipe_src_h);
    403 		border = LVDS_BORDER_ENABLE;
    404 		break;
    405 	case DRM_MODE_SCALE_ASPECT:
    406 		/* Scale but preserve the aspect ratio */
    407 		if (INTEL_GEN(dev_priv) >= 4)
    408 			i965_scale_aspect(pipe_config, &pfit_control);
    409 		else
    410 			i9xx_scale_aspect(pipe_config, &pfit_control,
    411 					  &pfit_pgm_ratios, &border);
    412 		break;
    413 	case DRM_MODE_SCALE_FULLSCREEN:
    414 		/*
    415 		 * Full scaling, even if it changes the aspect ratio.
    416 		 * Fortunately this is all done for us in hw.
    417 		 */
    418 		if (pipe_config->pipe_src_h != adjusted_mode->crtc_vdisplay ||
    419 		    pipe_config->pipe_src_w != adjusted_mode->crtc_hdisplay) {
    420 			pfit_control |= PFIT_ENABLE;
    421 			if (INTEL_GEN(dev_priv) >= 4)
    422 				pfit_control |= PFIT_SCALING_AUTO;
    423 			else
    424 				pfit_control |= (VERT_AUTO_SCALE |
    425 						 VERT_INTERP_BILINEAR |
    426 						 HORIZ_AUTO_SCALE |
    427 						 HORIZ_INTERP_BILINEAR);
    428 		}
    429 		break;
    430 	default:
    431 		WARN(1, "bad panel fit mode: %d\n", fitting_mode);
    432 		return;
    433 	}
    434 
    435 	/* 965+ wants fuzzy fitting */
    436 	/* FIXME: handle multiple panels by failing gracefully */
    437 	if (INTEL_GEN(dev_priv) >= 4)
    438 		pfit_control |= ((intel_crtc->pipe << PFIT_PIPE_SHIFT) |
    439 				 PFIT_FILTER_FUZZY);
    440 
    441 out:
    442 	if ((pfit_control & PFIT_ENABLE) == 0) {
    443 		pfit_control = 0;
    444 		pfit_pgm_ratios = 0;
    445 	}
    446 
    447 	/* Make sure pre-965 set dither correctly for 18bpp panels. */
    448 	if (INTEL_GEN(dev_priv) < 4 && pipe_config->pipe_bpp == 18)
    449 		pfit_control |= PANEL_8TO6_DITHER_ENABLE;
    450 
    451 	pipe_config->gmch_pfit.control = pfit_control;
    452 	pipe_config->gmch_pfit.pgm_ratios = pfit_pgm_ratios;
    453 	pipe_config->gmch_pfit.lvds_border_bits = border;
    454 }
    455 
    456 /**
    457  * scale - scale values from one range to another
    458  * @source_val: value in range [@source_min..@source_max]
    459  * @source_min: minimum legal value for @source_val
    460  * @source_max: maximum legal value for @source_val
    461  * @target_min: corresponding target value for @source_min
    462  * @target_max: corresponding target value for @source_max
    463  *
    464  * Return @source_val in range [@source_min..@source_max] scaled to range
    465  * [@target_min..@target_max].
    466  */
    467 static u32 scale(u32 source_val,
    468 		 u32 source_min, u32 source_max,
    469 		 u32 target_min, u32 target_max)
    470 {
    471 	u64 target_val;
    472 
    473 	WARN_ON(source_min > source_max);
    474 	WARN_ON(target_min > target_max);
    475 
    476 	/* defensive */
    477 	source_val = clamp(source_val, source_min, source_max);
    478 
    479 	/* avoid overflows */
    480 	target_val = mul_u32_u32(source_val - source_min,
    481 				 target_max - target_min);
    482 	target_val = DIV_ROUND_CLOSEST_ULL(target_val, source_max - source_min);
    483 	target_val += target_min;
    484 
    485 	return target_val;
    486 }
    487 
    488 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
    489 /* Scale user_level in range [0..user_max] to [hw_min..hw_max]. */
    490 static inline u32 scale_user_to_hw(struct intel_connector *connector,
    491 				   u32 user_level, u32 user_max)
    492 {
    493 	struct intel_panel *panel = &connector->panel;
    494 
    495 	return scale(user_level, 0, user_max,
    496 		     panel->backlight.min, panel->backlight.max);
    497 }
    498 #endif
    499 
    500 /* Scale user_level in range [0..user_max] to [0..hw_max], clamping the result
    501  * to [hw_min..hw_max]. */
    502 static inline u32 clamp_user_to_hw(struct intel_connector *connector,
    503 				   u32 user_level, u32 user_max)
    504 {
    505 	struct intel_panel *panel = &connector->panel;
    506 	u32 hw_level;
    507 
    508 	hw_level = scale(user_level, 0, user_max, 0, panel->backlight.max);
    509 	hw_level = clamp(hw_level, panel->backlight.min, panel->backlight.max);
    510 
    511 	return hw_level;
    512 }
    513 
    514 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
    515 /* Scale hw_level in range [hw_min..hw_max] to [0..user_max]. */
    516 static inline u32 scale_hw_to_user(struct intel_connector *connector,
    517 				   u32 hw_level, u32 user_max)
    518 {
    519 	struct intel_panel *panel = &connector->panel;
    520 
    521 	return scale(hw_level, panel->backlight.min, panel->backlight.max,
    522 		     0, user_max);
    523 }
    524 #endif
    525 
    526 static u32 intel_panel_compute_brightness(struct intel_connector *connector,
    527 					  u32 val)
    528 {
    529 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    530 	struct intel_panel *panel = &connector->panel;
    531 
    532 	WARN_ON(panel->backlight.max == 0);
    533 
    534 	if (i915_modparams.invert_brightness < 0)
    535 		return val;
    536 
    537 	if (i915_modparams.invert_brightness > 0 ||
    538 	    dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS) {
    539 		return panel->backlight.max - val + panel->backlight.min;
    540 	}
    541 
    542 	return val;
    543 }
    544 
    545 static u32 lpt_get_backlight(struct intel_connector *connector)
    546 {
    547 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    548 
    549 	return I915_READ(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK;
    550 }
    551 
    552 static u32 pch_get_backlight(struct intel_connector *connector)
    553 {
    554 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    555 
    556 	return I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
    557 }
    558 
    559 static u32 i9xx_get_backlight(struct intel_connector *connector)
    560 {
    561 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    562 	struct intel_panel *panel = &connector->panel;
    563 	u32 val;
    564 
    565 	val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
    566 	if (INTEL_GEN(dev_priv) < 4)
    567 		val >>= 1;
    568 
    569 	if (panel->backlight.combination_mode) {
    570 		u8 lbpc;
    571 
    572 		pci_read_config_byte(dev_priv->drm.pdev, LBPC, &lbpc);
    573 		val *= lbpc;
    574 	}
    575 
    576 	return val;
    577 }
    578 
    579 static u32 _vlv_get_backlight(struct drm_i915_private *dev_priv, enum pipe pipe)
    580 {
    581 	if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
    582 		return 0;
    583 
    584 	return I915_READ(VLV_BLC_PWM_CTL(pipe)) & BACKLIGHT_DUTY_CYCLE_MASK;
    585 }
    586 
    587 static u32 vlv_get_backlight(struct intel_connector *connector)
    588 {
    589 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    590 	enum pipe pipe = intel_connector_get_pipe(connector);
    591 
    592 	return _vlv_get_backlight(dev_priv, pipe);
    593 }
    594 
    595 static u32 bxt_get_backlight(struct intel_connector *connector)
    596 {
    597 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    598 	struct intel_panel *panel = &connector->panel;
    599 
    600 	return I915_READ(BXT_BLC_PWM_DUTY(panel->backlight.controller));
    601 }
    602 
    603 #ifndef __NetBSD__		/* XXX mipi */
    604 static u32 pwm_get_backlight(struct intel_connector *connector)
    605 {
    606 	struct intel_panel *panel = &connector->panel;
    607 	int duty_ns;
    608 
    609 	duty_ns = pwm_get_duty_cycle(panel->backlight.pwm);
    610 	return DIV_ROUND_UP(duty_ns * 100, CRC_PMIC_PWM_PERIOD_NS);
    611 }
    612 #endif
    613 
    614 static void lpt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    615 {
    616 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    617 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    618 
    619 	u32 val = I915_READ(BLC_PWM_PCH_CTL2) & ~BACKLIGHT_DUTY_CYCLE_MASK;
    620 	I915_WRITE(BLC_PWM_PCH_CTL2, val | level);
    621 }
    622 
    623 static void pch_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    624 {
    625 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    626 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    627 	u32 tmp;
    628 
    629 	tmp = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
    630 	I915_WRITE(BLC_PWM_CPU_CTL, tmp | level);
    631 }
    632 
    633 static void i9xx_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    634 {
    635 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    636 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    637 	struct intel_panel *panel = &connector->panel;
    638 	u32 tmp, mask;
    639 
    640 	WARN_ON(panel->backlight.max == 0);
    641 
    642 	if (panel->backlight.combination_mode) {
    643 		u8 lbpc;
    644 
    645 		lbpc = level * 0xfe / panel->backlight.max + 1;
    646 		level /= lbpc;
    647 		pci_write_config_byte(dev_priv->drm.pdev, LBPC, lbpc);
    648 	}
    649 
    650 	if (IS_GEN(dev_priv, 4)) {
    651 		mask = BACKLIGHT_DUTY_CYCLE_MASK;
    652 	} else {
    653 		level <<= 1;
    654 		mask = BACKLIGHT_DUTY_CYCLE_MASK_PNV;
    655 	}
    656 
    657 	tmp = I915_READ(BLC_PWM_CTL) & ~mask;
    658 	I915_WRITE(BLC_PWM_CTL, tmp | level);
    659 }
    660 
    661 static void vlv_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    662 {
    663 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    664 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    665 	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
    666 	u32 tmp;
    667 
    668 	tmp = I915_READ(VLV_BLC_PWM_CTL(pipe)) & ~BACKLIGHT_DUTY_CYCLE_MASK;
    669 	I915_WRITE(VLV_BLC_PWM_CTL(pipe), tmp | level);
    670 }
    671 
    672 static void bxt_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    673 {
    674 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    675 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    676 	struct intel_panel *panel = &connector->panel;
    677 
    678 	I915_WRITE(BXT_BLC_PWM_DUTY(panel->backlight.controller), level);
    679 }
    680 
    681 #ifndef __NetBSD__		/* XXX mipi */
    682 static void pwm_set_backlight(const struct drm_connector_state *conn_state, u32 level)
    683 {
    684 	struct intel_panel *panel = &to_intel_connector(conn_state->connector)->panel;
    685 	int duty_ns = DIV_ROUND_UP(level * CRC_PMIC_PWM_PERIOD_NS, 100);
    686 
    687 	pwm_config(panel->backlight.pwm, duty_ns, CRC_PMIC_PWM_PERIOD_NS);
    688 }
    689 #endif
    690 
    691 static void
    692 intel_panel_actually_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 intel_panel *panel = &connector->panel;
    696 
    697 	DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
    698 
    699 	level = intel_panel_compute_brightness(connector, level);
    700 	panel->backlight.set(conn_state, level);
    701 }
    702 #endif
    703 
    704 /* set backlight brightness to level in range [0..max], assuming hw min is
    705  * respected.
    706  */
    707 void intel_panel_set_backlight_acpi(const struct drm_connector_state *conn_state,
    708 				    u32 user_level, u32 user_max)
    709 {
    710 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    711 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    712 	struct intel_panel *panel = &connector->panel;
    713 	u32 hw_level;
    714 
    715 	/*
    716 	 * Lack of crtc may occur during driver init because
    717 	 * connection_mutex isn't held across the entire backlight
    718 	 * setup + modeset readout, and the BIOS can issue the
    719 	 * requests at any time.
    720 	 */
    721 	if (!panel->backlight.present || !conn_state->crtc)
    722 		return;
    723 
    724 	mutex_lock(&dev_priv->backlight_lock);
    725 
    726 	WARN_ON(panel->backlight.max == 0);
    727 
    728 	hw_level = clamp_user_to_hw(connector, user_level, user_max);
    729 	panel->backlight.level = hw_level;
    730 
    731 #ifndef __NetBSD__		/* XXX backlight */
    732 	if (panel->backlight.device)
    733 		panel->backlight.device->props.brightness =
    734 			scale_hw_to_user(connector,
    735 					 panel->backlight.level,
    736 					 panel->backlight.device->props.max_brightness);
    737 #endif
    738 
    739 	if (panel->backlight.enabled)
    740 		intel_panel_actually_set_backlight(conn_state, hw_level);
    741 
    742 	mutex_unlock(&dev_priv->backlight_lock);
    743 }
    744 
    745 static void lpt_disable_backlight(const struct drm_connector_state *old_conn_state)
    746 {
    747 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    748 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    749 	u32 tmp;
    750 
    751 	intel_panel_actually_set_backlight(old_conn_state, 0);
    752 
    753 	/*
    754 	 * Although we don't support or enable CPU PWM with LPT/SPT based
    755 	 * systems, it may have been enabled prior to loading the
    756 	 * driver. Disable to avoid warnings on LCPLL disable.
    757 	 *
    758 	 * This needs rework if we need to add support for CPU PWM on PCH split
    759 	 * platforms.
    760 	 */
    761 	tmp = I915_READ(BLC_PWM_CPU_CTL2);
    762 	if (tmp & BLM_PWM_ENABLE) {
    763 		DRM_DEBUG_KMS("cpu backlight was enabled, disabling\n");
    764 		I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
    765 	}
    766 
    767 	tmp = I915_READ(BLC_PWM_PCH_CTL1);
    768 	I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
    769 }
    770 
    771 static void pch_disable_backlight(const struct drm_connector_state *old_conn_state)
    772 {
    773 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    774 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    775 	u32 tmp;
    776 
    777 	intel_panel_actually_set_backlight(old_conn_state, 0);
    778 
    779 	tmp = I915_READ(BLC_PWM_CPU_CTL2);
    780 	I915_WRITE(BLC_PWM_CPU_CTL2, tmp & ~BLM_PWM_ENABLE);
    781 
    782 	tmp = I915_READ(BLC_PWM_PCH_CTL1);
    783 	I915_WRITE(BLC_PWM_PCH_CTL1, tmp & ~BLM_PCH_PWM_ENABLE);
    784 }
    785 
    786 static void i9xx_disable_backlight(const struct drm_connector_state *old_conn_state)
    787 {
    788 	intel_panel_actually_set_backlight(old_conn_state, 0);
    789 }
    790 
    791 static void i965_disable_backlight(const struct drm_connector_state *old_conn_state)
    792 {
    793 	struct drm_i915_private *dev_priv = to_i915(old_conn_state->connector->dev);
    794 	u32 tmp;
    795 
    796 	intel_panel_actually_set_backlight(old_conn_state, 0);
    797 
    798 	tmp = I915_READ(BLC_PWM_CTL2);
    799 	I915_WRITE(BLC_PWM_CTL2, tmp & ~BLM_PWM_ENABLE);
    800 }
    801 
    802 static void vlv_disable_backlight(const struct drm_connector_state *old_conn_state)
    803 {
    804 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    805 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    806 	enum pipe pipe = to_intel_crtc(old_conn_state->crtc)->pipe;
    807 	u32 tmp;
    808 
    809 	intel_panel_actually_set_backlight(old_conn_state, 0);
    810 
    811 	tmp = I915_READ(VLV_BLC_PWM_CTL2(pipe));
    812 	I915_WRITE(VLV_BLC_PWM_CTL2(pipe), tmp & ~BLM_PWM_ENABLE);
    813 }
    814 
    815 static void bxt_disable_backlight(const struct drm_connector_state *old_conn_state)
    816 {
    817 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    818 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    819 	struct intel_panel *panel = &connector->panel;
    820 	u32 tmp, val;
    821 
    822 	intel_panel_actually_set_backlight(old_conn_state, 0);
    823 
    824 	tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
    825 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
    826 			tmp & ~BXT_BLC_PWM_ENABLE);
    827 
    828 	if (panel->backlight.controller == 1) {
    829 		val = I915_READ(UTIL_PIN_CTL);
    830 		val &= ~UTIL_PIN_ENABLE;
    831 		I915_WRITE(UTIL_PIN_CTL, val);
    832 	}
    833 }
    834 
    835 #ifndef __NetBSD__		/* XXX mipi */
    836 static void cnp_disable_backlight(const struct drm_connector_state *old_conn_state)
    837 {
    838 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    839 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    840 	struct intel_panel *panel = &connector->panel;
    841 	u32 tmp;
    842 
    843 	intel_panel_actually_set_backlight(old_conn_state, 0);
    844 
    845 	tmp = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
    846 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
    847 		   tmp & ~BXT_BLC_PWM_ENABLE);
    848 }
    849 
    850 static void pwm_disable_backlight(const struct drm_connector_state *old_conn_state)
    851 {
    852 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    853 	struct intel_panel *panel = &connector->panel;
    854 
    855 	/* Disable the backlight */
    856 	intel_panel_actually_set_backlight(old_conn_state, 0);
    857 	usleep_range(2000, 3000);
    858 	pwm_disable(panel->backlight.pwm);
    859 }
    860 #endif
    861 
    862 void intel_panel_disable_backlight(const struct drm_connector_state *old_conn_state)
    863 {
    864 	struct intel_connector *connector = to_intel_connector(old_conn_state->connector);
    865 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    866 	struct intel_panel *panel = &connector->panel;
    867 
    868 	if (!panel->backlight.present)
    869 		return;
    870 
    871 	/*
    872 	 * Do not disable backlight on the vga_switcheroo path. When switching
    873 	 * away from i915, the other client may depend on i915 to handle the
    874 	 * backlight. This will leave the backlight on unnecessarily when
    875 	 * another client is not activated.
    876 	 */
    877 	if (dev_priv->drm.switch_power_state == DRM_SWITCH_POWER_CHANGING) {
    878 		DRM_DEBUG_DRIVER("Skipping backlight disable on vga switch\n");
    879 		return;
    880 	}
    881 
    882 	mutex_lock(&dev_priv->backlight_lock);
    883 
    884 #ifndef __NetBSD__		/* XXX backlight */
    885 	if (panel->backlight.device)
    886 		panel->backlight.device->props.power = FB_BLANK_POWERDOWN;
    887 #endif
    888 	panel->backlight.enabled = false;
    889 	panel->backlight.disable(old_conn_state);
    890 
    891 	mutex_unlock(&dev_priv->backlight_lock);
    892 }
    893 
    894 static void lpt_enable_backlight(const struct intel_crtc_state *crtc_state,
    895 				 const struct drm_connector_state *conn_state)
    896 {
    897 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    898 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    899 	struct intel_panel *panel = &connector->panel;
    900 	u32 pch_ctl1, pch_ctl2, schicken;
    901 
    902 	pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
    903 	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
    904 		DRM_DEBUG_KMS("pch backlight already enabled\n");
    905 		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
    906 		I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
    907 	}
    908 
    909 	if (HAS_PCH_LPT(dev_priv)) {
    910 		schicken = I915_READ(SOUTH_CHICKEN2);
    911 		if (panel->backlight.alternate_pwm_increment)
    912 			schicken |= LPT_PWM_GRANULARITY;
    913 		else
    914 			schicken &= ~LPT_PWM_GRANULARITY;
    915 		I915_WRITE(SOUTH_CHICKEN2, schicken);
    916 	} else {
    917 		schicken = I915_READ(SOUTH_CHICKEN1);
    918 		if (panel->backlight.alternate_pwm_increment)
    919 			schicken |= SPT_PWM_GRANULARITY;
    920 		else
    921 			schicken &= ~SPT_PWM_GRANULARITY;
    922 		I915_WRITE(SOUTH_CHICKEN1, schicken);
    923 	}
    924 
    925 	pch_ctl2 = panel->backlight.max << 16;
    926 	I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
    927 
    928 	pch_ctl1 = 0;
    929 	if (panel->backlight.active_low_pwm)
    930 		pch_ctl1 |= BLM_PCH_POLARITY;
    931 
    932 	/* After LPT, override is the default. */
    933 	if (HAS_PCH_LPT(dev_priv))
    934 		pch_ctl1 |= BLM_PCH_OVERRIDE_ENABLE;
    935 
    936 	I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
    937 	POSTING_READ(BLC_PWM_PCH_CTL1);
    938 	I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
    939 
    940 	/* This won't stick until the above enable. */
    941 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
    942 }
    943 
    944 static void pch_enable_backlight(const struct intel_crtc_state *crtc_state,
    945 				 const struct drm_connector_state *conn_state)
    946 {
    947 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    948 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    949 	struct intel_panel *panel = &connector->panel;
    950 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
    951 	u32 cpu_ctl2, pch_ctl1, pch_ctl2;
    952 
    953 	cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
    954 	if (cpu_ctl2 & BLM_PWM_ENABLE) {
    955 		DRM_DEBUG_KMS("cpu backlight already enabled\n");
    956 		cpu_ctl2 &= ~BLM_PWM_ENABLE;
    957 		I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
    958 	}
    959 
    960 	pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
    961 	if (pch_ctl1 & BLM_PCH_PWM_ENABLE) {
    962 		DRM_DEBUG_KMS("pch backlight already enabled\n");
    963 		pch_ctl1 &= ~BLM_PCH_PWM_ENABLE;
    964 		I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
    965 	}
    966 
    967 	if (cpu_transcoder == TRANSCODER_EDP)
    968 		cpu_ctl2 = BLM_TRANSCODER_EDP;
    969 	else
    970 		cpu_ctl2 = BLM_PIPE(cpu_transcoder);
    971 	I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2);
    972 	POSTING_READ(BLC_PWM_CPU_CTL2);
    973 	I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 | BLM_PWM_ENABLE);
    974 
    975 	/* This won't stick until the above enable. */
    976 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
    977 
    978 	pch_ctl2 = panel->backlight.max << 16;
    979 	I915_WRITE(BLC_PWM_PCH_CTL2, pch_ctl2);
    980 
    981 	pch_ctl1 = 0;
    982 	if (panel->backlight.active_low_pwm)
    983 		pch_ctl1 |= BLM_PCH_POLARITY;
    984 
    985 	I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1);
    986 	POSTING_READ(BLC_PWM_PCH_CTL1);
    987 	I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_PWM_ENABLE);
    988 }
    989 
    990 static void i9xx_enable_backlight(const struct intel_crtc_state *crtc_state,
    991 				  const struct drm_connector_state *conn_state)
    992 {
    993 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
    994 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    995 	struct intel_panel *panel = &connector->panel;
    996 	u32 ctl, freq;
    997 
    998 	ctl = I915_READ(BLC_PWM_CTL);
    999 	if (ctl & BACKLIGHT_DUTY_CYCLE_MASK_PNV) {
   1000 		DRM_DEBUG_KMS("backlight already enabled\n");
   1001 		I915_WRITE(BLC_PWM_CTL, 0);
   1002 	}
   1003 
   1004 	freq = panel->backlight.max;
   1005 	if (panel->backlight.combination_mode)
   1006 		freq /= 0xff;
   1007 
   1008 	ctl = freq << 17;
   1009 	if (panel->backlight.combination_mode)
   1010 		ctl |= BLM_LEGACY_MODE;
   1011 	if (IS_PINEVIEW(dev_priv) && panel->backlight.active_low_pwm)
   1012 		ctl |= BLM_POLARITY_PNV;
   1013 
   1014 	I915_WRITE(BLC_PWM_CTL, ctl);
   1015 	POSTING_READ(BLC_PWM_CTL);
   1016 
   1017 	/* XXX: combine this into above write? */
   1018 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1019 
   1020 	/*
   1021 	 * Needed to enable backlight on some 855gm models. BLC_HIST_CTL is
   1022 	 * 855gm only, but checking for gen2 is safe, as 855gm is the only gen2
   1023 	 * that has backlight.
   1024 	 */
   1025 	if (IS_GEN(dev_priv, 2))
   1026 		I915_WRITE(BLC_HIST_CTL, BLM_HISTOGRAM_ENABLE);
   1027 }
   1028 
   1029 static void i965_enable_backlight(const struct intel_crtc_state *crtc_state,
   1030 				  const struct drm_connector_state *conn_state)
   1031 {
   1032 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1033 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1034 	struct intel_panel *panel = &connector->panel;
   1035 	enum pipe pipe = to_intel_crtc(conn_state->crtc)->pipe;
   1036 	u32 ctl, ctl2, freq;
   1037 
   1038 	ctl2 = I915_READ(BLC_PWM_CTL2);
   1039 	if (ctl2 & BLM_PWM_ENABLE) {
   1040 		DRM_DEBUG_KMS("backlight already enabled\n");
   1041 		ctl2 &= ~BLM_PWM_ENABLE;
   1042 		I915_WRITE(BLC_PWM_CTL2, ctl2);
   1043 	}
   1044 
   1045 	freq = panel->backlight.max;
   1046 	if (panel->backlight.combination_mode)
   1047 		freq /= 0xff;
   1048 
   1049 	ctl = freq << 16;
   1050 	I915_WRITE(BLC_PWM_CTL, ctl);
   1051 
   1052 	ctl2 = BLM_PIPE(pipe);
   1053 	if (panel->backlight.combination_mode)
   1054 		ctl2 |= BLM_COMBINATION_MODE;
   1055 	if (panel->backlight.active_low_pwm)
   1056 		ctl2 |= BLM_POLARITY_I965;
   1057 	I915_WRITE(BLC_PWM_CTL2, ctl2);
   1058 	POSTING_READ(BLC_PWM_CTL2);
   1059 	I915_WRITE(BLC_PWM_CTL2, ctl2 | BLM_PWM_ENABLE);
   1060 
   1061 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1062 }
   1063 
   1064 static void vlv_enable_backlight(const struct intel_crtc_state *crtc_state,
   1065 				 const struct drm_connector_state *conn_state)
   1066 {
   1067 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1068 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1069 	struct intel_panel *panel = &connector->panel;
   1070 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
   1071 	u32 ctl, ctl2;
   1072 
   1073 	ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
   1074 	if (ctl2 & BLM_PWM_ENABLE) {
   1075 		DRM_DEBUG_KMS("backlight already enabled\n");
   1076 		ctl2 &= ~BLM_PWM_ENABLE;
   1077 		I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
   1078 	}
   1079 
   1080 	ctl = panel->backlight.max << 16;
   1081 	I915_WRITE(VLV_BLC_PWM_CTL(pipe), ctl);
   1082 
   1083 	/* XXX: combine this into above write? */
   1084 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1085 
   1086 	ctl2 = 0;
   1087 	if (panel->backlight.active_low_pwm)
   1088 		ctl2 |= BLM_POLARITY_I965;
   1089 	I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2);
   1090 	POSTING_READ(VLV_BLC_PWM_CTL2(pipe));
   1091 	I915_WRITE(VLV_BLC_PWM_CTL2(pipe), ctl2 | BLM_PWM_ENABLE);
   1092 }
   1093 
   1094 static void bxt_enable_backlight(const struct intel_crtc_state *crtc_state,
   1095 				 const struct drm_connector_state *conn_state)
   1096 {
   1097 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1098 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1099 	struct intel_panel *panel = &connector->panel;
   1100 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
   1101 	u32 pwm_ctl, val;
   1102 
   1103 	/* Controller 1 uses the utility pin. */
   1104 	if (panel->backlight.controller == 1) {
   1105 		val = I915_READ(UTIL_PIN_CTL);
   1106 		if (val & UTIL_PIN_ENABLE) {
   1107 			DRM_DEBUG_KMS("util pin already enabled\n");
   1108 			val &= ~UTIL_PIN_ENABLE;
   1109 			I915_WRITE(UTIL_PIN_CTL, val);
   1110 		}
   1111 
   1112 		val = 0;
   1113 		if (panel->backlight.util_pin_active_low)
   1114 			val |= UTIL_PIN_POLARITY;
   1115 		I915_WRITE(UTIL_PIN_CTL, val | UTIL_PIN_PIPE(pipe) |
   1116 				UTIL_PIN_MODE_PWM | UTIL_PIN_ENABLE);
   1117 	}
   1118 
   1119 	pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1120 	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
   1121 		DRM_DEBUG_KMS("backlight already enabled\n");
   1122 		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
   1123 		I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
   1124 				pwm_ctl);
   1125 	}
   1126 
   1127 	I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
   1128 			panel->backlight.max);
   1129 
   1130 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1131 
   1132 	pwm_ctl = 0;
   1133 	if (panel->backlight.active_low_pwm)
   1134 		pwm_ctl |= BXT_BLC_PWM_POLARITY;
   1135 
   1136 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
   1137 	POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1138 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
   1139 			pwm_ctl | BXT_BLC_PWM_ENABLE);
   1140 }
   1141 
   1142 #ifndef __NetBSD__		/* XXX mipi */
   1143 static void cnp_enable_backlight(const struct intel_crtc_state *crtc_state,
   1144 				 const struct drm_connector_state *conn_state)
   1145 {
   1146 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1147 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1148 	struct intel_panel *panel = &connector->panel;
   1149 	u32 pwm_ctl;
   1150 
   1151 	pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1152 	if (pwm_ctl & BXT_BLC_PWM_ENABLE) {
   1153 		DRM_DEBUG_KMS("backlight already enabled\n");
   1154 		pwm_ctl &= ~BXT_BLC_PWM_ENABLE;
   1155 		I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
   1156 			   pwm_ctl);
   1157 	}
   1158 
   1159 	I915_WRITE(BXT_BLC_PWM_FREQ(panel->backlight.controller),
   1160 		   panel->backlight.max);
   1161 
   1162 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1163 
   1164 	pwm_ctl = 0;
   1165 	if (panel->backlight.active_low_pwm)
   1166 		pwm_ctl |= BXT_BLC_PWM_POLARITY;
   1167 
   1168 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller), pwm_ctl);
   1169 	POSTING_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1170 	I915_WRITE(BXT_BLC_PWM_CTL(panel->backlight.controller),
   1171 		   pwm_ctl | BXT_BLC_PWM_ENABLE);
   1172 }
   1173 
   1174 static void pwm_enable_backlight(const struct intel_crtc_state *crtc_state,
   1175 				 const struct drm_connector_state *conn_state)
   1176 {
   1177 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1178 	struct intel_panel *panel = &connector->panel;
   1179 
   1180 	pwm_enable(panel->backlight.pwm);
   1181 	intel_panel_actually_set_backlight(conn_state, panel->backlight.level);
   1182 }
   1183 #endif
   1184 
   1185 static void __intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
   1186 					   const struct drm_connector_state *conn_state)
   1187 {
   1188 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1189 	struct intel_panel *panel = &connector->panel;
   1190 
   1191 	WARN_ON(panel->backlight.max == 0);
   1192 
   1193 	if (panel->backlight.level <= panel->backlight.min) {
   1194 		panel->backlight.level = panel->backlight.max;
   1195 #ifndef __NetBSD__		/* XXX backlight */
   1196 		if (panel->backlight.device)
   1197 			panel->backlight.device->props.brightness =
   1198 				scale_hw_to_user(connector,
   1199 						 panel->backlight.level,
   1200 						 panel->backlight.device->props.max_brightness);
   1201 #endif
   1202 	}
   1203 
   1204 	panel->backlight.enable(crtc_state, conn_state);
   1205 	panel->backlight.enabled = true;
   1206 #ifndef __NetBSD__		/* XXX backlight */
   1207 	if (panel->backlight.device)
   1208 		panel->backlight.device->props.power = FB_BLANK_UNBLANK;
   1209 #endif
   1210 }
   1211 
   1212 void intel_panel_enable_backlight(const struct intel_crtc_state *crtc_state,
   1213 				  const struct drm_connector_state *conn_state)
   1214 {
   1215 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1216 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1217 	struct intel_panel *panel = &connector->panel;
   1218 	enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
   1219 
   1220 	if (!panel->backlight.present)
   1221 		return;
   1222 
   1223 	DRM_DEBUG_KMS("pipe %c\n", pipe_name(pipe));
   1224 
   1225 	mutex_lock(&dev_priv->backlight_lock);
   1226 
   1227 	__intel_panel_enable_backlight(crtc_state, conn_state);
   1228 
   1229 	mutex_unlock(&dev_priv->backlight_lock);
   1230 }
   1231 
   1232 #if IS_ENABLED(CONFIG_BACKLIGHT_CLASS_DEVICE)
   1233 static u32 intel_panel_get_backlight(struct intel_connector *connector)
   1234 {
   1235 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1236 	struct intel_panel *panel = &connector->panel;
   1237 	u32 val = 0;
   1238 
   1239 	mutex_lock(&dev_priv->backlight_lock);
   1240 
   1241 	if (panel->backlight.enabled) {
   1242 		val = panel->backlight.get(connector);
   1243 		val = intel_panel_compute_brightness(connector, val);
   1244 	}
   1245 
   1246 	mutex_unlock(&dev_priv->backlight_lock);
   1247 
   1248 	DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
   1249 	return val;
   1250 }
   1251 
   1252 /* set backlight brightness to level in range [0..max], scaling wrt hw min */
   1253 static void intel_panel_set_backlight(const struct drm_connector_state *conn_state,
   1254 				      u32 user_level, u32 user_max)
   1255 {
   1256 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1257 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1258 	struct intel_panel *panel = &connector->panel;
   1259 	u32 hw_level;
   1260 
   1261 	if (!panel->backlight.present)
   1262 		return;
   1263 
   1264 	mutex_lock(&dev_priv->backlight_lock);
   1265 
   1266 	WARN_ON(panel->backlight.max == 0);
   1267 
   1268 	hw_level = scale_user_to_hw(connector, user_level, user_max);
   1269 	panel->backlight.level = hw_level;
   1270 
   1271 	if (panel->backlight.enabled)
   1272 		intel_panel_actually_set_backlight(conn_state, hw_level);
   1273 
   1274 	mutex_unlock(&dev_priv->backlight_lock);
   1275 }
   1276 
   1277 static int intel_backlight_device_update_status(struct backlight_device *bd)
   1278 {
   1279 	struct intel_connector *connector = bl_get_data(bd);
   1280 	struct intel_panel *panel = &connector->panel;
   1281 	struct drm_device *dev = connector->base.dev;
   1282 
   1283 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
   1284 	DRM_DEBUG_KMS("updating intel_backlight, brightness=%d/%d\n",
   1285 		      bd->props.brightness, bd->props.max_brightness);
   1286 	intel_panel_set_backlight(connector->base.state, bd->props.brightness,
   1287 				  bd->props.max_brightness);
   1288 
   1289 	/*
   1290 	 * Allow flipping bl_power as a sub-state of enabled. Sadly the
   1291 	 * backlight class device does not make it easy to to differentiate
   1292 	 * between callbacks for brightness and bl_power, so our backlight_power
   1293 	 * callback needs to take this into account.
   1294 	 */
   1295 	if (panel->backlight.enabled) {
   1296 		if (panel->backlight.power) {
   1297 			bool enable = bd->props.power == FB_BLANK_UNBLANK &&
   1298 				bd->props.brightness != 0;
   1299 			panel->backlight.power(connector, enable);
   1300 		}
   1301 	} else {
   1302 		bd->props.power = FB_BLANK_POWERDOWN;
   1303 	}
   1304 
   1305 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
   1306 	return 0;
   1307 }
   1308 
   1309 static int intel_backlight_device_get_brightness(struct backlight_device *bd)
   1310 {
   1311 	struct intel_connector *connector = bl_get_data(bd);
   1312 	struct drm_device *dev = connector->base.dev;
   1313 	struct drm_i915_private *dev_priv = to_i915(dev);
   1314 	intel_wakeref_t wakeref;
   1315 	int ret = 0;
   1316 
   1317 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref) {
   1318 		u32 hw_level;
   1319 
   1320 		drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
   1321 
   1322 		hw_level = intel_panel_get_backlight(connector);
   1323 		ret = scale_hw_to_user(connector,
   1324 				       hw_level, bd->props.max_brightness);
   1325 
   1326 		drm_modeset_unlock(&dev->mode_config.connection_mutex);
   1327 	}
   1328 
   1329 	return ret;
   1330 }
   1331 
   1332 static const struct backlight_ops intel_backlight_device_ops = {
   1333 	.update_status = intel_backlight_device_update_status,
   1334 	.get_brightness = intel_backlight_device_get_brightness,
   1335 };
   1336 
   1337 int intel_backlight_device_register(struct intel_connector *connector)
   1338 {
   1339 	struct intel_panel *panel = &connector->panel;
   1340 	struct backlight_properties props;
   1341 
   1342 	if (WARN_ON(panel->backlight.device))
   1343 		return -ENODEV;
   1344 
   1345 	if (!panel->backlight.present)
   1346 		return 0;
   1347 
   1348 	WARN_ON(panel->backlight.max == 0);
   1349 
   1350 	memset(&props, 0, sizeof(props));
   1351 	props.type = BACKLIGHT_RAW;
   1352 
   1353 	/*
   1354 	 * Note: Everything should work even if the backlight device max
   1355 	 * presented to the userspace is arbitrarily chosen.
   1356 	 */
   1357 	props.max_brightness = panel->backlight.max;
   1358 	props.brightness = scale_hw_to_user(connector,
   1359 					    panel->backlight.level,
   1360 					    props.max_brightness);
   1361 
   1362 	if (panel->backlight.enabled)
   1363 		props.power = FB_BLANK_UNBLANK;
   1364 	else
   1365 		props.power = FB_BLANK_POWERDOWN;
   1366 
   1367 	/*
   1368 	 * Note: using the same name independent of the connector prevents
   1369 	 * registration of multiple backlight devices in the driver.
   1370 	 */
   1371 	panel->backlight.device =
   1372 		backlight_device_register("intel_backlight",
   1373 					  connector->base.kdev,
   1374 					  connector,
   1375 					  &intel_backlight_device_ops, &props);
   1376 
   1377 	if (IS_ERR(panel->backlight.device)) {
   1378 		DRM_ERROR("Failed to register backlight: %ld\n",
   1379 			  PTR_ERR(panel->backlight.device));
   1380 		panel->backlight.device = NULL;
   1381 		return -ENODEV;
   1382 	}
   1383 
   1384 	DRM_DEBUG_KMS("Connector %s backlight sysfs interface registered\n",
   1385 		      connector->base.name);
   1386 
   1387 	return 0;
   1388 }
   1389 
   1390 void intel_backlight_device_unregister(struct intel_connector *connector)
   1391 {
   1392 	struct intel_panel *panel = &connector->panel;
   1393 
   1394 	if (panel->backlight.device) {
   1395 		backlight_device_unregister(panel->backlight.device);
   1396 		panel->backlight.device = NULL;
   1397 	}
   1398 }
   1399 #endif /* CONFIG_BACKLIGHT_CLASS_DEVICE */
   1400 
   1401 /*
   1402  * CNP: PWM clock frequency is 19.2 MHz or 24 MHz.
   1403  *      PWM increment = 1
   1404  */
   1405 static u32 cnp_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1406 {
   1407 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1408 
   1409 	return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz);
   1410 }
   1411 
   1412 /*
   1413  * BXT: PWM clock frequency = 19.2 MHz.
   1414  */
   1415 static u32 bxt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1416 {
   1417 	return DIV_ROUND_CLOSEST(KHz(19200), pwm_freq_hz);
   1418 }
   1419 
   1420 /*
   1421  * SPT: This value represents the period of the PWM stream in clock periods
   1422  * multiplied by 16 (default increment) or 128 (alternate increment selected in
   1423  * SCHICKEN_1 bit 0). PWM clock is 24 MHz.
   1424  */
   1425 static u32 spt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1426 {
   1427 	struct intel_panel *panel = &connector->panel;
   1428 	u32 mul;
   1429 
   1430 	if (panel->backlight.alternate_pwm_increment)
   1431 		mul = 128;
   1432 	else
   1433 		mul = 16;
   1434 
   1435 	return DIV_ROUND_CLOSEST(MHz(24), pwm_freq_hz * mul);
   1436 }
   1437 
   1438 /*
   1439  * LPT: This value represents the period of the PWM stream in clock periods
   1440  * multiplied by 128 (default increment) or 16 (alternate increment, selected in
   1441  * LPT SOUTH_CHICKEN2 register bit 5).
   1442  */
   1443 static u32 lpt_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1444 {
   1445 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1446 	struct intel_panel *panel = &connector->panel;
   1447 	u32 mul, clock;
   1448 
   1449 	if (panel->backlight.alternate_pwm_increment)
   1450 		mul = 16;
   1451 	else
   1452 		mul = 128;
   1453 
   1454 	if (HAS_PCH_LPT_H(dev_priv))
   1455 		clock = MHz(135); /* LPT:H */
   1456 	else
   1457 		clock = MHz(24); /* LPT:LP */
   1458 
   1459 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
   1460 }
   1461 
   1462 /*
   1463  * ILK/SNB/IVB: This value represents the period of the PWM stream in PCH
   1464  * display raw clocks multiplied by 128.
   1465  */
   1466 static u32 pch_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1467 {
   1468 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1469 
   1470 	return DIV_ROUND_CLOSEST(KHz(dev_priv->rawclk_freq), pwm_freq_hz * 128);
   1471 }
   1472 
   1473 /*
   1474  * Gen2: This field determines the number of time base events (display core
   1475  * clock frequency/32) in total for a complete cycle of modulated backlight
   1476  * control.
   1477  *
   1478  * Gen3: A time base event equals the display core clock ([DevPNV] HRAW clock)
   1479  * divided by 32.
   1480  */
   1481 static u32 i9xx_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1482 {
   1483 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1484 	int clock;
   1485 
   1486 	if (IS_PINEVIEW(dev_priv))
   1487 		clock = KHz(dev_priv->rawclk_freq);
   1488 	else
   1489 		clock = KHz(dev_priv->cdclk.hw.cdclk);
   1490 
   1491 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 32);
   1492 }
   1493 
   1494 /*
   1495  * Gen4: This value represents the period of the PWM stream in display core
   1496  * clocks ([DevCTG] HRAW clocks) multiplied by 128.
   1497  *
   1498  */
   1499 static u32 i965_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1500 {
   1501 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1502 	int clock;
   1503 
   1504 	if (IS_G4X(dev_priv))
   1505 		clock = KHz(dev_priv->rawclk_freq);
   1506 	else
   1507 		clock = KHz(dev_priv->cdclk.hw.cdclk);
   1508 
   1509 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * 128);
   1510 }
   1511 
   1512 /*
   1513  * VLV: This value represents the period of the PWM stream in display core
   1514  * clocks ([DevCTG] 200MHz HRAW clocks) multiplied by 128 or 25MHz S0IX clocks
   1515  * multiplied by 16. CHV uses a 19.2MHz S0IX clock.
   1516  */
   1517 static u32 vlv_hz_to_pwm(struct intel_connector *connector, u32 pwm_freq_hz)
   1518 {
   1519 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1520 	int mul, clock;
   1521 
   1522 	if ((I915_READ(CBR1_VLV) & CBR_PWM_CLOCK_MUX_SELECT) == 0) {
   1523 		if (IS_CHERRYVIEW(dev_priv))
   1524 			clock = KHz(19200);
   1525 		else
   1526 			clock = MHz(25);
   1527 		mul = 16;
   1528 	} else {
   1529 		clock = KHz(dev_priv->rawclk_freq);
   1530 		mul = 128;
   1531 	}
   1532 
   1533 	return DIV_ROUND_CLOSEST(clock, pwm_freq_hz * mul);
   1534 }
   1535 
   1536 static u32 get_backlight_max_vbt(struct intel_connector *connector)
   1537 {
   1538 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1539 	struct intel_panel *panel = &connector->panel;
   1540 	u16 pwm_freq_hz = dev_priv->vbt.backlight.pwm_freq_hz;
   1541 	u32 pwm;
   1542 
   1543 	if (!panel->backlight.hz_to_pwm) {
   1544 		DRM_DEBUG_KMS("backlight frequency conversion not supported\n");
   1545 		return 0;
   1546 	}
   1547 
   1548 	if (pwm_freq_hz) {
   1549 		DRM_DEBUG_KMS("VBT defined backlight frequency %u Hz\n",
   1550 			      pwm_freq_hz);
   1551 	} else {
   1552 		pwm_freq_hz = 200;
   1553 		DRM_DEBUG_KMS("default backlight frequency %u Hz\n",
   1554 			      pwm_freq_hz);
   1555 	}
   1556 
   1557 	pwm = panel->backlight.hz_to_pwm(connector, pwm_freq_hz);
   1558 	if (!pwm) {
   1559 		DRM_DEBUG_KMS("backlight frequency conversion failed\n");
   1560 		return 0;
   1561 	}
   1562 
   1563 	return pwm;
   1564 }
   1565 
   1566 /*
   1567  * Note: The setup hooks can't assume pipe is set!
   1568  */
   1569 static u32 get_backlight_min_vbt(struct intel_connector *connector)
   1570 {
   1571 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1572 	struct intel_panel *panel = &connector->panel;
   1573 	int min;
   1574 
   1575 	WARN_ON(panel->backlight.max == 0);
   1576 
   1577 	/*
   1578 	 * XXX: If the vbt value is 255, it makes min equal to max, which leads
   1579 	 * to problems. There are such machines out there. Either our
   1580 	 * interpretation is wrong or the vbt has bogus data. Or both. Safeguard
   1581 	 * against this by letting the minimum be at most (arbitrarily chosen)
   1582 	 * 25% of the max.
   1583 	 */
   1584 	min = clamp_t(int, dev_priv->vbt.backlight.min_brightness, 0, 64);
   1585 	if (min != dev_priv->vbt.backlight.min_brightness) {
   1586 		DRM_DEBUG_KMS("clamping VBT min backlight %d/255 to %d/255\n",
   1587 			      dev_priv->vbt.backlight.min_brightness, min);
   1588 	}
   1589 
   1590 	/* vbt value is a coefficient in range [0..255] */
   1591 	return scale(min, 0, 255, 0, panel->backlight.max);
   1592 }
   1593 
   1594 static int lpt_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1595 {
   1596 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1597 	struct intel_panel *panel = &connector->panel;
   1598 	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
   1599 	bool alt, cpu_mode;
   1600 
   1601 	if (HAS_PCH_LPT(dev_priv))
   1602 		alt = I915_READ(SOUTH_CHICKEN2) & LPT_PWM_GRANULARITY;
   1603 	else
   1604 		alt = I915_READ(SOUTH_CHICKEN1) & SPT_PWM_GRANULARITY;
   1605 	panel->backlight.alternate_pwm_increment = alt;
   1606 
   1607 	pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
   1608 	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
   1609 
   1610 	pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
   1611 	panel->backlight.max = pch_ctl2 >> 16;
   1612 
   1613 	cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
   1614 
   1615 	if (!panel->backlight.max)
   1616 		panel->backlight.max = get_backlight_max_vbt(connector);
   1617 
   1618 	if (!panel->backlight.max)
   1619 		return -ENODEV;
   1620 
   1621 	panel->backlight.min = get_backlight_min_vbt(connector);
   1622 
   1623 	panel->backlight.enabled = pch_ctl1 & BLM_PCH_PWM_ENABLE;
   1624 
   1625 	cpu_mode = panel->backlight.enabled && HAS_PCH_LPT(dev_priv) &&
   1626 		   !(pch_ctl1 & BLM_PCH_OVERRIDE_ENABLE) &&
   1627 		   (cpu_ctl2 & BLM_PWM_ENABLE);
   1628 	if (cpu_mode)
   1629 		val = pch_get_backlight(connector);
   1630 	else
   1631 		val = lpt_get_backlight(connector);
   1632 	val = intel_panel_compute_brightness(connector, val);
   1633 	panel->backlight.level = clamp(val, panel->backlight.min,
   1634 				       panel->backlight.max);
   1635 
   1636 	if (cpu_mode) {
   1637 		DRM_DEBUG_KMS("CPU backlight register was enabled, switching to PCH override\n");
   1638 
   1639 		/* Write converted CPU PWM value to PCH override register */
   1640 		lpt_set_backlight(connector->base.state, panel->backlight.level);
   1641 		I915_WRITE(BLC_PWM_PCH_CTL1, pch_ctl1 | BLM_PCH_OVERRIDE_ENABLE);
   1642 
   1643 		I915_WRITE(BLC_PWM_CPU_CTL2, cpu_ctl2 & ~BLM_PWM_ENABLE);
   1644 	}
   1645 
   1646 	return 0;
   1647 }
   1648 
   1649 static int pch_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1650 {
   1651 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1652 	struct intel_panel *panel = &connector->panel;
   1653 	u32 cpu_ctl2, pch_ctl1, pch_ctl2, val;
   1654 
   1655 	pch_ctl1 = I915_READ(BLC_PWM_PCH_CTL1);
   1656 	panel->backlight.active_low_pwm = pch_ctl1 & BLM_PCH_POLARITY;
   1657 
   1658 	pch_ctl2 = I915_READ(BLC_PWM_PCH_CTL2);
   1659 	panel->backlight.max = pch_ctl2 >> 16;
   1660 
   1661 	if (!panel->backlight.max)
   1662 		panel->backlight.max = get_backlight_max_vbt(connector);
   1663 
   1664 	if (!panel->backlight.max)
   1665 		return -ENODEV;
   1666 
   1667 	panel->backlight.min = get_backlight_min_vbt(connector);
   1668 
   1669 	val = pch_get_backlight(connector);
   1670 	val = intel_panel_compute_brightness(connector, val);
   1671 	panel->backlight.level = clamp(val, panel->backlight.min,
   1672 				       panel->backlight.max);
   1673 
   1674 	cpu_ctl2 = I915_READ(BLC_PWM_CPU_CTL2);
   1675 	panel->backlight.enabled = (cpu_ctl2 & BLM_PWM_ENABLE) &&
   1676 		(pch_ctl1 & BLM_PCH_PWM_ENABLE);
   1677 
   1678 	return 0;
   1679 }
   1680 
   1681 static int i9xx_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1682 {
   1683 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1684 	struct intel_panel *panel = &connector->panel;
   1685 	u32 ctl, val;
   1686 
   1687 	ctl = I915_READ(BLC_PWM_CTL);
   1688 
   1689 	if (IS_GEN(dev_priv, 2) || IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
   1690 		panel->backlight.combination_mode = ctl & BLM_LEGACY_MODE;
   1691 
   1692 	if (IS_PINEVIEW(dev_priv))
   1693 		panel->backlight.active_low_pwm = ctl & BLM_POLARITY_PNV;
   1694 
   1695 	panel->backlight.max = ctl >> 17;
   1696 
   1697 	if (!panel->backlight.max) {
   1698 		panel->backlight.max = get_backlight_max_vbt(connector);
   1699 		panel->backlight.max >>= 1;
   1700 	}
   1701 
   1702 	if (!panel->backlight.max)
   1703 		return -ENODEV;
   1704 
   1705 	if (panel->backlight.combination_mode)
   1706 		panel->backlight.max *= 0xff;
   1707 
   1708 	panel->backlight.min = get_backlight_min_vbt(connector);
   1709 
   1710 	val = i9xx_get_backlight(connector);
   1711 	val = intel_panel_compute_brightness(connector, val);
   1712 	panel->backlight.level = clamp(val, panel->backlight.min,
   1713 				       panel->backlight.max);
   1714 
   1715 	panel->backlight.enabled = val != 0;
   1716 
   1717 	return 0;
   1718 }
   1719 
   1720 static int i965_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1721 {
   1722 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1723 	struct intel_panel *panel = &connector->panel;
   1724 	u32 ctl, ctl2, val;
   1725 
   1726 	ctl2 = I915_READ(BLC_PWM_CTL2);
   1727 	panel->backlight.combination_mode = ctl2 & BLM_COMBINATION_MODE;
   1728 	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
   1729 
   1730 	ctl = I915_READ(BLC_PWM_CTL);
   1731 	panel->backlight.max = ctl >> 16;
   1732 
   1733 	if (!panel->backlight.max)
   1734 		panel->backlight.max = get_backlight_max_vbt(connector);
   1735 
   1736 	if (!panel->backlight.max)
   1737 		return -ENODEV;
   1738 
   1739 	if (panel->backlight.combination_mode)
   1740 		panel->backlight.max *= 0xff;
   1741 
   1742 	panel->backlight.min = get_backlight_min_vbt(connector);
   1743 
   1744 	val = i9xx_get_backlight(connector);
   1745 	val = intel_panel_compute_brightness(connector, val);
   1746 	panel->backlight.level = clamp(val, panel->backlight.min,
   1747 				       panel->backlight.max);
   1748 
   1749 	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
   1750 
   1751 	return 0;
   1752 }
   1753 
   1754 static int vlv_setup_backlight(struct intel_connector *connector, enum pipe pipe)
   1755 {
   1756 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1757 	struct intel_panel *panel = &connector->panel;
   1758 	u32 ctl, ctl2, val;
   1759 
   1760 	if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
   1761 		return -ENODEV;
   1762 
   1763 	ctl2 = I915_READ(VLV_BLC_PWM_CTL2(pipe));
   1764 	panel->backlight.active_low_pwm = ctl2 & BLM_POLARITY_I965;
   1765 
   1766 	ctl = I915_READ(VLV_BLC_PWM_CTL(pipe));
   1767 	panel->backlight.max = ctl >> 16;
   1768 
   1769 	if (!panel->backlight.max)
   1770 		panel->backlight.max = get_backlight_max_vbt(connector);
   1771 
   1772 	if (!panel->backlight.max)
   1773 		return -ENODEV;
   1774 
   1775 	panel->backlight.min = get_backlight_min_vbt(connector);
   1776 
   1777 	val = _vlv_get_backlight(dev_priv, pipe);
   1778 	val = intel_panel_compute_brightness(connector, val);
   1779 	panel->backlight.level = clamp(val, panel->backlight.min,
   1780 				       panel->backlight.max);
   1781 
   1782 	panel->backlight.enabled = ctl2 & BLM_PWM_ENABLE;
   1783 
   1784 	return 0;
   1785 }
   1786 
   1787 static int
   1788 bxt_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1789 {
   1790 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1791 	struct intel_panel *panel = &connector->panel;
   1792 	u32 pwm_ctl, val;
   1793 
   1794 	panel->backlight.controller = dev_priv->vbt.backlight.controller;
   1795 
   1796 	pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1797 
   1798 	/* Controller 1 uses the utility pin. */
   1799 	if (panel->backlight.controller == 1) {
   1800 		val = I915_READ(UTIL_PIN_CTL);
   1801 		panel->backlight.util_pin_active_low =
   1802 					val & UTIL_PIN_POLARITY;
   1803 	}
   1804 
   1805 	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
   1806 	panel->backlight.max =
   1807 		I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
   1808 
   1809 	if (!panel->backlight.max)
   1810 		panel->backlight.max = get_backlight_max_vbt(connector);
   1811 
   1812 	if (!panel->backlight.max)
   1813 		return -ENODEV;
   1814 
   1815 	panel->backlight.min = get_backlight_min_vbt(connector);
   1816 
   1817 	val = bxt_get_backlight(connector);
   1818 	val = intel_panel_compute_brightness(connector, val);
   1819 	panel->backlight.level = clamp(val, panel->backlight.min,
   1820 				       panel->backlight.max);
   1821 
   1822 	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
   1823 
   1824 	return 0;
   1825 }
   1826 
   1827 static int
   1828 cnp_setup_backlight(struct intel_connector *connector, enum pipe unused)
   1829 {
   1830 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1831 	struct intel_panel *panel = &connector->panel;
   1832 	u32 pwm_ctl, val;
   1833 
   1834 	/*
   1835 	 * CNP has the BXT implementation of backlight, but with only one
   1836 	 * controller. TODO: ICP has multiple controllers but we only use
   1837 	 * controller 0 for now.
   1838 	 */
   1839 	panel->backlight.controller = 0;
   1840 
   1841 	pwm_ctl = I915_READ(BXT_BLC_PWM_CTL(panel->backlight.controller));
   1842 
   1843 	panel->backlight.active_low_pwm = pwm_ctl & BXT_BLC_PWM_POLARITY;
   1844 	panel->backlight.max =
   1845 		I915_READ(BXT_BLC_PWM_FREQ(panel->backlight.controller));
   1846 
   1847 	if (!panel->backlight.max)
   1848 		panel->backlight.max = get_backlight_max_vbt(connector);
   1849 
   1850 	if (!panel->backlight.max)
   1851 		return -ENODEV;
   1852 
   1853 	panel->backlight.min = get_backlight_min_vbt(connector);
   1854 
   1855 	val = bxt_get_backlight(connector);
   1856 	val = intel_panel_compute_brightness(connector, val);
   1857 	panel->backlight.level = clamp(val, panel->backlight.min,
   1858 				       panel->backlight.max);
   1859 
   1860 	panel->backlight.enabled = pwm_ctl & BXT_BLC_PWM_ENABLE;
   1861 
   1862 	return 0;
   1863 }
   1864 
   1865 #ifndef __NetBSD__		/* XXX mipi */
   1866 static int pwm_setup_backlight(struct intel_connector *connector,
   1867 			       enum pipe pipe)
   1868 {
   1869 	struct drm_device *dev = connector->base.dev;
   1870 	struct drm_i915_private *dev_priv = to_i915(dev);
   1871 	struct intel_panel *panel = &connector->panel;
   1872 	const char *desc;
   1873 	int retval;
   1874 
   1875 	/* Get the right PWM chip for DSI backlight according to VBT */
   1876 	if (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC) {
   1877 		panel->backlight.pwm = pwm_get(dev->dev, "pwm_pmic_backlight");
   1878 		desc = "PMIC";
   1879 	} else {
   1880 		panel->backlight.pwm = pwm_get(dev->dev, "pwm_soc_backlight");
   1881 		desc = "SoC";
   1882 	}
   1883 
   1884 	if (IS_ERR(panel->backlight.pwm)) {
   1885 		DRM_ERROR("Failed to get the %s PWM chip\n", desc);
   1886 		panel->backlight.pwm = NULL;
   1887 		return -ENODEV;
   1888 	}
   1889 
   1890 	/*
   1891 	 * FIXME: pwm_apply_args() should be removed when switching to
   1892 	 * the atomic PWM API.
   1893 	 */
   1894 	pwm_apply_args(panel->backlight.pwm);
   1895 
   1896 	retval = pwm_config(panel->backlight.pwm, CRC_PMIC_PWM_PERIOD_NS,
   1897 			    CRC_PMIC_PWM_PERIOD_NS);
   1898 	if (retval < 0) {
   1899 		DRM_ERROR("Failed to configure the pwm chip\n");
   1900 		pwm_put(panel->backlight.pwm);
   1901 		panel->backlight.pwm = NULL;
   1902 		return retval;
   1903 	}
   1904 
   1905 	panel->backlight.min = 0; /* 0% */
   1906 	panel->backlight.max = 100; /* 100% */
   1907 	panel->backlight.level = DIV_ROUND_UP(
   1908 				 pwm_get_duty_cycle(panel->backlight.pwm) * 100,
   1909 				 CRC_PMIC_PWM_PERIOD_NS);
   1910 	panel->backlight.enabled = panel->backlight.level != 0;
   1911 
   1912 	DRM_INFO("Using %s PWM for LCD backlight control\n", desc);
   1913 	return 0;
   1914 }
   1915 #endif
   1916 
   1917 void intel_panel_update_backlight(struct intel_encoder *encoder,
   1918 				  const struct intel_crtc_state *crtc_state,
   1919 				  const struct drm_connector_state *conn_state)
   1920 {
   1921 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
   1922 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1923 	struct intel_panel *panel = &connector->panel;
   1924 
   1925 	if (!panel->backlight.present)
   1926 		return;
   1927 
   1928 	mutex_lock(&dev_priv->backlight_lock);
   1929 	if (!panel->backlight.enabled)
   1930 		__intel_panel_enable_backlight(crtc_state, conn_state);
   1931 
   1932 	mutex_unlock(&dev_priv->backlight_lock);
   1933 }
   1934 
   1935 int intel_panel_setup_backlight(struct drm_connector *connector, enum pipe pipe)
   1936 {
   1937 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
   1938 	struct intel_connector *intel_connector = to_intel_connector(connector);
   1939 	struct intel_panel *panel = &intel_connector->panel;
   1940 	int ret;
   1941 
   1942 	if (!dev_priv->vbt.backlight.present) {
   1943 		if (dev_priv->quirks & QUIRK_BACKLIGHT_PRESENT) {
   1944 			DRM_DEBUG_KMS("no backlight present per VBT, but present per quirk\n");
   1945 		} else {
   1946 			DRM_DEBUG_KMS("no backlight present per VBT\n");
   1947 			return 0;
   1948 		}
   1949 	}
   1950 
   1951 	/* ensure intel_panel has been initialized first */
   1952 	if (WARN_ON(!panel->backlight.setup))
   1953 		return -ENODEV;
   1954 
   1955 	/* set level and max in panel struct */
   1956 	mutex_lock(&dev_priv->backlight_lock);
   1957 	ret = panel->backlight.setup(intel_connector, pipe);
   1958 	mutex_unlock(&dev_priv->backlight_lock);
   1959 
   1960 	if (ret) {
   1961 		DRM_DEBUG_KMS("failed to setup backlight for connector %s\n",
   1962 			      connector->name);
   1963 		return ret;
   1964 	}
   1965 
   1966 	panel->backlight.present = true;
   1967 
   1968 	DRM_DEBUG_KMS("Connector %s backlight initialized, %s, brightness %u/%u\n",
   1969 		      connector->name,
   1970 		      enableddisabled(panel->backlight.enabled),
   1971 		      panel->backlight.level, panel->backlight.max);
   1972 
   1973 	return 0;
   1974 }
   1975 
   1976 static void intel_panel_destroy_backlight(struct intel_panel *panel)
   1977 {
   1978 #ifndef __NetBSD__		/* XXX mipi */
   1979 	/* dispose of the pwm */
   1980 	if (panel->backlight.pwm)
   1981 		pwm_put(panel->backlight.pwm);
   1982 #endif
   1983 
   1984 	panel->backlight.present = false;
   1985 }
   1986 
   1987 /* Set up chip specific backlight functions */
   1988 static void
   1989 intel_panel_init_backlight_funcs(struct intel_panel *panel)
   1990 {
   1991 	struct intel_connector *connector =
   1992 		container_of(panel, struct intel_connector, panel);
   1993 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1994 
   1995 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP &&
   1996 	    intel_dp_aux_init_backlight_funcs(connector) == 0)
   1997 		return;
   1998 
   1999 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI &&
   2000 	    intel_dsi_dcs_init_backlight_funcs(connector) == 0)
   2001 		return;
   2002 
   2003 	if (IS_GEN9_LP(dev_priv)) {
   2004 		panel->backlight.setup = bxt_setup_backlight;
   2005 		panel->backlight.enable = bxt_enable_backlight;
   2006 		panel->backlight.disable = bxt_disable_backlight;
   2007 		panel->backlight.set = bxt_set_backlight;
   2008 		panel->backlight.get = bxt_get_backlight;
   2009 		panel->backlight.hz_to_pwm = bxt_hz_to_pwm;
   2010 	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP) {
   2011 		panel->backlight.setup = cnp_setup_backlight;
   2012 		panel->backlight.enable = cnp_enable_backlight;
   2013 		panel->backlight.disable = cnp_disable_backlight;
   2014 		panel->backlight.set = bxt_set_backlight;
   2015 		panel->backlight.get = bxt_get_backlight;
   2016 		panel->backlight.hz_to_pwm = cnp_hz_to_pwm;
   2017 	} else if (INTEL_PCH_TYPE(dev_priv) >= PCH_LPT) {
   2018 		panel->backlight.setup = lpt_setup_backlight;
   2019 		panel->backlight.enable = lpt_enable_backlight;
   2020 		panel->backlight.disable = lpt_disable_backlight;
   2021 		panel->backlight.set = lpt_set_backlight;
   2022 		panel->backlight.get = lpt_get_backlight;
   2023 		if (HAS_PCH_LPT(dev_priv))
   2024 			panel->backlight.hz_to_pwm = lpt_hz_to_pwm;
   2025 		else
   2026 			panel->backlight.hz_to_pwm = spt_hz_to_pwm;
   2027 	} else if (HAS_PCH_SPLIT(dev_priv)) {
   2028 		panel->backlight.setup = pch_setup_backlight;
   2029 		panel->backlight.enable = pch_enable_backlight;
   2030 		panel->backlight.disable = pch_disable_backlight;
   2031 		panel->backlight.set = pch_set_backlight;
   2032 		panel->backlight.get = pch_get_backlight;
   2033 		panel->backlight.hz_to_pwm = pch_hz_to_pwm;
   2034 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
   2035 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_DSI) {
   2036 #ifdef __NetBSD__
   2037 			panic("no muppets supported, sorry");
   2038 #else
   2039 			panel->backlight.setup = pwm_setup_backlight;
   2040 			panel->backlight.enable = pwm_enable_backlight;
   2041 			panel->backlight.disable = pwm_disable_backlight;
   2042 			panel->backlight.set = pwm_set_backlight;
   2043 			panel->backlight.get = pwm_get_backlight;
   2044 #endif
   2045 		} else {
   2046 			panel->backlight.setup = vlv_setup_backlight;
   2047 			panel->backlight.enable = vlv_enable_backlight;
   2048 			panel->backlight.disable = vlv_disable_backlight;
   2049 			panel->backlight.set = vlv_set_backlight;
   2050 			panel->backlight.get = vlv_get_backlight;
   2051 			panel->backlight.hz_to_pwm = vlv_hz_to_pwm;
   2052 		}
   2053 	} else if (IS_GEN(dev_priv, 4)) {
   2054 		panel->backlight.setup = i965_setup_backlight;
   2055 		panel->backlight.enable = i965_enable_backlight;
   2056 		panel->backlight.disable = i965_disable_backlight;
   2057 		panel->backlight.set = i9xx_set_backlight;
   2058 		panel->backlight.get = i9xx_get_backlight;
   2059 		panel->backlight.hz_to_pwm = i965_hz_to_pwm;
   2060 	} else {
   2061 		panel->backlight.setup = i9xx_setup_backlight;
   2062 		panel->backlight.enable = i9xx_enable_backlight;
   2063 		panel->backlight.disable = i9xx_disable_backlight;
   2064 		panel->backlight.set = i9xx_set_backlight;
   2065 		panel->backlight.get = i9xx_get_backlight;
   2066 		panel->backlight.hz_to_pwm = i9xx_hz_to_pwm;
   2067 	}
   2068 }
   2069 
   2070 int intel_panel_init(struct intel_panel *panel,
   2071 		     struct drm_display_mode *fixed_mode,
   2072 		     struct drm_display_mode *downclock_mode)
   2073 {
   2074 	intel_panel_init_backlight_funcs(panel);
   2075 
   2076 	panel->fixed_mode = fixed_mode;
   2077 	panel->downclock_mode = downclock_mode;
   2078 
   2079 	return 0;
   2080 }
   2081 
   2082 void intel_panel_fini(struct intel_panel *panel)
   2083 {
   2084 	struct intel_connector *intel_connector =
   2085 		container_of(panel, struct intel_connector, panel);
   2086 
   2087 	intel_panel_destroy_backlight(panel);
   2088 
   2089 	if (panel->fixed_mode)
   2090 		drm_mode_destroy(intel_connector->base.dev, panel->fixed_mode);
   2091 
   2092 	if (panel->downclock_mode)
   2093 		drm_mode_destroy(intel_connector->base.dev,
   2094 				panel->downclock_mode);
   2095 }
   2096