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