Skip to contentSkip to conversation

drm/i915: Adaptive backlight support

PATCH In review Stéphane Marchesin 3 participants 5 messages Jun 20, 2012 – Jun 21, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/i915: Adaptive backlight support

This is an initial implementation of i915 adaptive backlight support. The intended use for the adaptive backlight is to generate interrupts whenever the luminance of the screen changes by some thresholds. The main caveat with that implementation is that those additional interrupts will wake up the CPU and consume more power. Instead, we hook into the vblank handler and handle it from there. This makes the implementation a little less intuitive but a lot more efficient. We also need to compute the gamma correction from the interrupt handler so we do this with a (new) fixed point module.

Change-Id I9b9631cacc7d90e2801a542a3789118521bc25f0
---
 drivers/gpu/drm/i915/Makefile                   |    1 +
 drivers/gpu/drm/i915/i915_dma.c                 |   16 ++
 drivers/gpu/drm/i915/i915_drv.h                 |   10 +
 drivers/gpu/drm/i915/i915_irq.c                 |    8 +-
 drivers/gpu/drm/i915/i915_reg.h                 |   22 ++
 drivers/gpu/drm/i915/intel_adaptive_backlight.c |  266 +++++++++++++++++++++++
 drivers/gpu/drm/i915/intel_fixedpoint.h         |  140 ++++++++++++
 drivers/gpu/drm/i915/intel_panel.c              |    6 +
 include/drm/i915_drm.h                          |    2 +
 9 files changed, 469 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/intel_adaptive_backlight.c
 create mode 100644 drivers/gpu/drm/i915/intel_fixedpoint.h
drivers/gpu/drm/i915/Makefile +1 -0
index ce7fc77..5c125c3 100644
@@ -13,6 +13,7 @@ i915-y := i915_drv.o i915_dma.o i915_irq.o \
 	  i915_gem_gtt.o \
 	  i915_gem_tiling.o \
 	  i915_trace_points.o \
+	  intel_adaptive_backlight.o \
 	  intel_display.o \
 	  intel_crt.o \
 	  intel_lvds.o \
drivers/gpu/drm/i915/i915_dma.c +16 -0
index ba60f3c..c2626d6 100644
@@ -828,6 +828,22 @@ static int i915_setparam(struct drm_device *dev, void *data,
 		/* Userspace can use first N regs */
 		dev_priv->fence_reg_start = param->value;
 		break;
+	case I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_enabled = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
+	case I915_SETPARAM_PANEL_GAMMA:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_panel_gamma = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
 	default:
 		DRM_DEBUG_DRIVER("unknown parameter %d\n",
 					param->param);
drivers/gpu/drm/i915/i915_drv.h +10 -0
index d89f585..f778f93 100644
@@ -401,6 +401,13 @@ typedef struct drm_i915_private {
 	struct drm_display_mode *lfp_lvds_vbt_mode; /* if any */
 	struct drm_display_mode *sdvo_lvds_vbt_mode; /* if any */
 
+	/* Adaptive backlight */
+	bool adaptive_backlight_enabled;
+	int backlight_correction_level;
+	int backlight_correction_count;
+	int backlight_correction_direction;
+	int adaptive_backlight_panel_gamma;
+
 	/* Feature bits from the VBIOS */
 	unsigned int int_tv_support:1;
 	unsigned int lvds_dither:1;
@@ -1358,6 +1365,9 @@ extern int i915_restore_state(struct drm_device *dev);
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
 
+/* intel_adaptive_backlight.c */
+extern void intel_adaptive_backlight(struct drm_device *dev, int pipe);
+
 /* intel_i2c.c */
 extern int intel_setup_gmbus(struct drm_device *dev);
 extern void intel_teardown_gmbus(struct drm_device *dev);
drivers/gpu/drm/i915/i915_irq.c +6 -2
index afd4e03..948da6b 100644
@@ -619,11 +619,15 @@ static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
 		intel_finish_page_flip_plane(dev, 1);
 	}
 
-	if (de_iir & DE_PIPEA_VBLANK)
+	if (de_iir & DE_PIPEA_VBLANK) {
 		drm_handle_vblank(dev, 0);
+		intel_adaptive_backlight(dev, 0);
+	}
 
-	if (de_iir & DE_PIPEB_VBLANK)
+	if (de_iir & DE_PIPEB_VBLANK) {
 		drm_handle_vblank(dev, 1);
+		intel_adaptive_backlight(dev, 1);
+	}
 
 	/* check event from PCH */
 	if (de_iir & DE_PCH_EVENT) {
drivers/gpu/drm/i915/i915_reg.h +22 -0
index 552264c..2db874d 100644
@@ -3587,6 +3587,28 @@
 #define  PWM_PIPE_B		(1 << 29)
 #define BLC_PWM_CPU_CTL		0x48254
 
+#define BLM_HIST_CTL			0x48260
+#define  ENH_HIST_ENABLE		(1<<31)
+#define  ENH_MODIF_TBL_ENABLE		(1<<30)
+#define  ENH_PIPE_A_SELECT		(0<<29)
+#define  ENH_PIPE_B_SELECT		(1<<29)
+#define  ENH_PIPE(pipe) _PIPE(pipe, ENH_PIPE_A_SELECT, ENH_PIPE_B_SELECT)
+#define  HIST_MODE_YUV			(0<<24)
+#define  HIST_MODE_HSV			(1<<24)
+#define  ENH_MODE_DIRECT		(0<<13)
+#define  ENH_MODE_ADDITIVE		(1<<13)
+#define  ENH_MODE_MULTIPLICATIVE	(2<<13)
+#define  BIN_REGISTER_SET		(1<<11)
+#define  ENH_NUM_BINS			32
+
+#define BLM_HIST_ENH			0x48264
+
+#define BLM_HIST_GUARD_BAND		0x48268
+#define  BLM_HIST_INTR_ENABLE		(1<<31)
+#define  BLM_HIST_EVENT_STATUS		(1<<30)
+#define  BLM_HIST_INTR_DELAY_MASK	(0xFF<<22)
+#define  BLM_HIST_INTR_DELAY_SHIFT	22
+
 #define BLC_PWM_PCH_CTL1	0xc8250
 #define  PWM_PCH_ENABLE		(1 << 31)
 #define  PWM_POLARITY_ACTIVE_LOW	(1 << 29)
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +266 -0

266 lines not on this page · raw message

drivers/gpu/drm/i915/intel_fixedpoint.h new file +140 -0

140 lines not on this page · raw message

drivers/gpu/drm/i915/intel_panel.c +6 -0
index 3f9249b..ae95163 100644
@@ -251,6 +251,8 @@ static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level
 	struct drm_i915_private *dev_priv = dev->dev_private;
 	u32 tmp;
 
+	level = level * dev_priv->backlight_correction_level >> 8;
+
 	DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
 
 	if (HAS_PCH_SPLIT(dev))
@@ -306,6 +308,10 @@ static void intel_panel_init_backlight(struct drm_device *dev)
 
 	dev_priv->backlight_level = intel_panel_get_backlight(dev);
 	dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
+	dev_priv->adaptive_backlight_enabled = 0;
+	/* 2.2 as 16.16 fixed point */
+	dev_priv->adaptive_backlight_panel_gamma = 144179;
+	dev_priv->backlight_correction_level = 256;
 }
 
 enum drm_connector_status
include/drm/i915_drm.h +2 -0
index da929bb..70c9cfc 100644
@@ -309,6 +309,8 @@ typedef struct drm_i915_getparam {
 #define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
 #define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
 #define I915_SETPARAM_NUM_USED_FENCES                     4
+#define I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE           5
+#define I915_SETPARAM_PANEL_GAMMA                         6
 
 typedef struct drm_i915_setparam {
 	int param;
Signature · 6 lines
1.7.5.3.367.ga9930

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
maintainer
#1 lore raw
Quoted 9 lines · Stéphane Marchesin

This is an initial implementation of i915 adaptive backlight support. The intended use for the adaptive backlight is to generate interrupts whenever the luminance of the screen changes by some thresholds. The main caveat with that implementation is that those additional interrupts will wake up the CPU and consume more power. Instead, we hook into the vblank handler and handle it from there. This makes the implementation a little less intuitive but a lot more efficient. We also need to compute the gamma correction from the interrupt handler so we do this with a (new) fixed point module.

Hi Stéphane, interesting work.

My understanding is that content adaptive backlight control is basically a function that tries to reduce backlight brightness to save power while adjusting gamma to ensure there is no user detectable change in image. Like this:

(reduced_brightness, gamma) = f(brightness, luminance)

In my view, the histogram information, interrupt on its meaningful change, brightness and gamma setting are clearly mechanisms the driver should provide. I'm not at all sure if the function that defines the policy for brightness and gamma changes should be part of the driver.

In fact, looking at the histogram_find_correction_level() and adaptive_backlight_compute_correction() functions below, they are filled with policy conditions. Usually it's preferred to have the policy in user space. The fixed point troubles would all go away as well in user space. And there's the added benefit of being able to handle the user's desired brightness and possible ambient light sensor based adjustment to brightness in a single point of truth.

I guess the question is how to define sensible interfaces to the mechanisms in a way that the adaptive backlight function is feasible to implement in user space, and the power saving goal could still be reached. I would at least like this to be looked into first.

BR,
Jani.

Quoted 601 lines
Change-Id I9b9631cacc7d90e2801a542a3789118521bc25f0
---
 drivers/gpu/drm/i915/Makefile                   |    1 +
 drivers/gpu/drm/i915/i915_dma.c                 |   16 ++
 drivers/gpu/drm/i915/i915_drv.h                 |   10 +
 drivers/gpu/drm/i915/i915_irq.c                 |    8 +-
 drivers/gpu/drm/i915/i915_reg.h                 |   22 ++
 drivers/gpu/drm/i915/intel_adaptive_backlight.c |  266 +++++++++++++++++++++++
 drivers/gpu/drm/i915/intel_fixedpoint.h         |  140 ++++++++++++
 drivers/gpu/drm/i915/intel_panel.c              |    6 +
 include/drm/i915_drm.h                          |    2 +
 9 files changed, 469 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/intel_adaptive_backlight.c
 create mode 100644 drivers/gpu/drm/i915/intel_fixedpoint.h
drivers/gpu/drm/i915/Makefile +1 -0
index ce7fc77..5c125c3 100644
@@ -13,6 +13,7 @@ i915-y := i915_drv.o i915_dma.o i915_irq.o \
 	  i915_gem_gtt.o \
 	  i915_gem_tiling.o \
 	  i915_trace_points.o \
+	  intel_adaptive_backlight.o \
 	  intel_display.o \
 	  intel_crt.o \
 	  intel_lvds.o \
drivers/gpu/drm/i915/i915_dma.c +16 -0
index ba60f3c..c2626d6 100644
@@ -828,6 +828,22 @@ static int i915_setparam(struct drm_device *dev, void *data,
 		/* Userspace can use first N regs */
 		dev_priv->fence_reg_start = param->value;
 		break;
+	case I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_enabled = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
+	case I915_SETPARAM_PANEL_GAMMA:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_panel_gamma = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
 	default:
 		DRM_DEBUG_DRIVER("unknown parameter %d\n",
 					param->param);
drivers/gpu/drm/i915/i915_drv.h +10 -0
index d89f585..f778f93 100644
@@ -401,6 +401,13 @@ typedef struct drm_i915_private {
 	struct drm_display_mode *lfp_lvds_vbt_mode; /* if any */
 	struct drm_display_mode *sdvo_lvds_vbt_mode; /* if any */
 
+	/* Adaptive backlight */
+	bool adaptive_backlight_enabled;
+	int backlight_correction_level;
+	int backlight_correction_count;
+	int backlight_correction_direction;
+	int adaptive_backlight_panel_gamma;
+
 	/* Feature bits from the VBIOS */
 	unsigned int int_tv_support:1;
 	unsigned int lvds_dither:1;
@@ -1358,6 +1365,9 @@ extern int i915_restore_state(struct drm_device *dev);
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
 
+/* intel_adaptive_backlight.c */
+extern void intel_adaptive_backlight(struct drm_device *dev, int pipe);
+
 /* intel_i2c.c */
 extern int intel_setup_gmbus(struct drm_device *dev);
 extern void intel_teardown_gmbus(struct drm_device *dev);
drivers/gpu/drm/i915/i915_irq.c +6 -2
index afd4e03..948da6b 100644
@@ -619,11 +619,15 @@ static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
 		intel_finish_page_flip_plane(dev, 1);
 	}
 
-	if (de_iir & DE_PIPEA_VBLANK)
+	if (de_iir & DE_PIPEA_VBLANK) {
 		drm_handle_vblank(dev, 0);
+		intel_adaptive_backlight(dev, 0);
+	}
 
-	if (de_iir & DE_PIPEB_VBLANK)
+	if (de_iir & DE_PIPEB_VBLANK) {
 		drm_handle_vblank(dev, 1);
+		intel_adaptive_backlight(dev, 1);
+	}
 
 	/* check event from PCH */
 	if (de_iir & DE_PCH_EVENT) {
drivers/gpu/drm/i915/i915_reg.h +22 -0
index 552264c..2db874d 100644
@@ -3587,6 +3587,28 @@
 #define  PWM_PIPE_B		(1 << 29)
 #define BLC_PWM_CPU_CTL		0x48254
 
+#define BLM_HIST_CTL			0x48260
+#define  ENH_HIST_ENABLE		(1<<31)
+#define  ENH_MODIF_TBL_ENABLE		(1<<30)
+#define  ENH_PIPE_A_SELECT		(0<<29)
+#define  ENH_PIPE_B_SELECT		(1<<29)
+#define  ENH_PIPE(pipe) _PIPE(pipe, ENH_PIPE_A_SELECT, ENH_PIPE_B_SELECT)
+#define  HIST_MODE_YUV			(0<<24)
+#define  HIST_MODE_HSV			(1<<24)
+#define  ENH_MODE_DIRECT		(0<<13)
+#define  ENH_MODE_ADDITIVE		(1<<13)
+#define  ENH_MODE_MULTIPLICATIVE	(2<<13)
+#define  BIN_REGISTER_SET		(1<<11)
+#define  ENH_NUM_BINS			32
+
+#define BLM_HIST_ENH			0x48264
+
+#define BLM_HIST_GUARD_BAND		0x48268
+#define  BLM_HIST_INTR_ENABLE		(1<<31)
+#define  BLM_HIST_EVENT_STATUS		(1<<30)
+#define  BLM_HIST_INTR_DELAY_MASK	(0xFF<<22)
+#define  BLM_HIST_INTR_DELAY_SHIFT	22
+
 #define BLC_PWM_PCH_CTL1	0xc8250
 #define  PWM_PCH_ENABLE		(1 << 31)
 #define  PWM_POLARITY_ACTIVE_LOW	(1 << 29)
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +266 -0
new file mode 100644
index 0000000..4234962
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+
+#include "drmP.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "i915_drv.h"
+#include "i915_reg.h"
+#include "intel_drv.h"
+#include "intel_fixedpoint.h"
+
+/*
+ * This function takes a histogram of buckets as input and determines an
+ * acceptable target backlight level.
+ */
+static int histogram_find_correction_level(int *levels)
+{
+	int i, sum = 0;
+	int ratio, distortion, prev_distortion = 0, off, final_ratio, target;
+
+	for (i = 0; i < ENH_NUM_BINS; i++)
+		sum += levels[i];
+
+	/* Allow 0.33/256 distortion per pixel, on average */
+	target = sum / 3;
+
+	/* Special case where we only have less than 100 pixels
+	 * outside of the darkest bin.
+	 */
+	if (sum - levels[0] <= 100)
+		return 70;
+
+	for (ratio = ENH_NUM_BINS - 1; ratio >= 0 ; ratio--) {
+		distortion = 0;
+		for (i = ratio; i < ENH_NUM_BINS; i++) {
+			int pixel_distortion = (i - ratio)*8;
+			int num_pixels = levels[i];
+			distortion += num_pixels * pixel_distortion;
+		}
+		if (distortion > target)
+			break;
+		else
+			prev_distortion = distortion;
+	}
+
+	ratio++;
+
+	/* If we're not exactly at the border between two buckets, extrapolate
+	 * to get 3 extra bits of accuracy.
+	 */
+	if (distortion - prev_distortion)
+		off = 8 * (target - prev_distortion) /
+		      (distortion - prev_distortion);
+	else
+		off = 0;
+
+	final_ratio = ratio * 255 / 31 + off;
+
+	if (final_ratio > 255)
+		final_ratio = 255;
+
+	/* Never aim for less than 50% of the total backlight */
+	if (final_ratio < 128)
+		final_ratio = 128;
+
+	return final_ratio;
+}
+
+static void histogram_get_levels(struct drm_device *dev, int pipe, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int i;
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+					 ENH_MODIF_TBL_ENABLE |
+					 ENH_PIPE(pipe) |
+					 HIST_MODE_YUV |
+					 ENH_MODE_ADDITIVE |
+					 i);
+		levels[i] = I915_READ(BLM_HIST_ENH);
+	}
+}
+
+static void histogram_set_levels(struct drm_device *dev, int pipe, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int i;
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+					 ENH_MODIF_TBL_ENABLE |
+					 ENH_PIPE(pipe) |
+					 HIST_MODE_YUV |
+					 ENH_MODE_ADDITIVE |
+					 BIN_REGISTER_SET |
+					 i);
+		I915_WRITE(BLM_HIST_ENH, levels[i]);
+	}
+}
+
+/*
+ * This function computes the backlight correction level for an acceptable
+ * distortion and fills up the correction bins adequately.
+ */
+static void
+adaptive_backlight_compute_correction(struct drm_device *dev, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int correction_level;
+	int i, multiplier, one_over_gamma;
+
+	/* Find the correction level for an acceptable distortion */
+	correction_level = histogram_find_correction_level(levels);
+
+	/* If we're not yet at our correction target, we need to decide by how
+	 * much to move.
+	 */
+	if (dev_priv->backlight_correction_level != correction_level) {
+		int delta, direction;
+
+		direction = (correction_level >
+			     dev_priv->backlight_correction_level);
+
+		if (direction == dev_priv->backlight_correction_direction) {
+			dev_priv->backlight_correction_count++;
+		} else {
+			dev_priv->backlight_correction_count = 0;
+			dev_priv->backlight_correction_direction = direction;
+		}
+
+		delta = abs(correction_level -
+			    dev_priv->backlight_correction_level)/4;
+
+		if (delta < 1)
+			delta = 1;
+
+		/* For increasing the brightness, we do it instantly.
+		 * For lowering the brightness, we require at least 10 frames
+		 * below the current value. This avoids ping-ponging of the
+		 * backlight level.
+		 *
+		 * We also never increase the backlight by more than 6% per
+		 * frame, and never lower it by more than 3% per frame, because
+		 * the backlight needs time to adjust and the LCD correction
+		 * would be "ahead" otherwise.
+		 */
+		if (correction_level > dev_priv->backlight_correction_level) {
+			if (delta > 15)
+				delta = 15;
+			dev_priv->backlight_correction_level += delta;
+		} else if ((dev_priv->backlight_correction_count > 10) &&
+		   (correction_level < dev_priv->backlight_correction_level)) {
+			if (delta > 7)
+				delta = 7;
+			dev_priv->backlight_correction_level -= delta;
+		}
+	}
+
+	/* We need to invert the gamma correction of the LCD values,
+	 * but not of the backlight which is linear.
+	 */
+	one_over_gamma = intel_fixed_div(FIXED_ONE,
+				dev_priv->adaptive_backlight_panel_gamma);
+	multiplier = intel_fixed_pow(dev_priv->backlight_correction_level * 256,
+				     one_over_gamma);
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		int base_value = i * 8 * 4;
+		levels[i] = base_value - base_value * multiplier / 65536;
+	}
+}
+
+/*
+ * A quick note about the adaptive backlight implementation:
+ * If we let it run as designed, it will generate a lot of interrupts which
+ * tends to wake the CPU up and waste power. This is a bad idea for a power
+ * saving feature. Instead, we couple it to the vblank interrupt since that
+ * means we drew something. This means that we do not react to non-vsynced GL
+ * updates, or updates to the front buffer, and also adds a little bit of
+ * extra latency. But it is an acceptable tradeoff to make.
+ */
+void intel_adaptive_backlight(struct drm_device *dev, int pipe_vblank_event)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int levels[ENH_NUM_BINS];
+	int pipe;
+	struct drm_connector *connector;
+	struct intel_crtc *intel_crtc;
+
+	if (!dev_priv->adaptive_backlight_enabled)
+		return;
+
+	/* Find the connector */
+	if (dev_priv->int_lvds_connector)
+		connector = dev_priv->int_lvds_connector;
+	else if (dev_priv->int_edp_connector)
+		connector = dev_priv->int_edp_connector;
+	else
+		return;
+
+	if (!connector)
+		return;
+
+	if (!connector->encoder)
+		return;
+
+	if (!connector->encoder->crtc)
+		return;
+
+	/* Find the pipe for the panel. */
+	intel_crtc = to_intel_crtc(connector->encoder->crtc);
+	pipe = intel_crtc->pipe;
+
+	/* The callback happens for both pipe A & B. Now that we know which
+	 * pipe we're doing adaptive backlight on, check that it's the right
+	 * one. Bail if it isn't.
+	 */
+	if (pipe != pipe_vblank_event)
+		return;
+
+	/* Return if no event. */
+	if (I915_READ(BLM_HIST_GUARD_BAND) & BLM_HIST_EVENT_STATUS == 0)
+		return;
+
+	/* Make sure we ack the previous event. Even though we do not get the
+	 * IRQs (see above explanation), we must still ack the events otherwise
+	 * the histogram data doesn't get updated any more.
+	 */
+	I915_WRITE(BLM_HIST_GUARD_BAND, BLM_HIST_INTR_ENABLE |
+					BLM_HIST_EVENT_STATUS |
+					(1 << BLM_HIST_INTR_DELAY_SHIFT));
+
+	histogram_get_levels(dev, pipe, levels);
+
+	adaptive_backlight_compute_correction(dev, levels);
+
+	histogram_set_levels(dev, pipe, levels);
+
+	intel_panel_set_backlight(dev, dev_priv->backlight_level);
+}
+
drivers/gpu/drm/i915/intel_fixedpoint.h new file +140 -0
new file mode 100644
index 0000000..fff5f0b
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+/*
+ * The backlight is corrected in linear space. However the LCD correction is
+ * corrected in gama space. So to be able to compute the correction value for
+ * the LCD, we have to compute the inverse gamma. To do so, we carry this
+ * small fixed point module which allows us to use pow() to compute inverse
+ * gamma.
+ *
+ * The fixed point format used here is 16.16.
+ */
+
+/* intel_fixed_exp_tbl[x*32] = exp(x) * 65536 */
+static const int intel_fixed_exp_tbl[33] = {
+0x00010000, 0x00010820, 0x00011083, 0x00011929, 0x00012216, 0x00012b4b,
+0x000134cc, 0x00013e99, 0x000148b6, 0x00015325, 0x00015de9, 0x00016905,
+0x0001747a, 0x0001804d, 0x00018c80, 0x00019916, 0x0001a613, 0x0001b378,
+0x0001c14b, 0x0001cf8e, 0x0001de45, 0x0001ed74, 0x0001fd1e, 0x00020d47,
+0x00021df4, 0x00022f28, 0x000240e8, 0x00025338, 0x0002661d, 0x0002799b,
+0x00028db8, 0x0002a278, 0x0002b7e1
+};
+
+/* intel_fixed_log_tbl[x*32] = log(x) * 65536 */
+static const int intel_fixed_log_tbl[33] = {
+0x80000000, 0xfffc88c6, 0xfffd3a38, 0xfffda204, 0xfffdebaa, 0xfffe24ca,
+0xfffe5376, 0xfffe7aed, 0xfffe9d1c, 0xfffebb43, 0xfffed63c, 0xfffeeea2,
+0xffff04e8, 0xffff1966, 0xffff2c5f, 0xffff3e08, 0xffff4e8e, 0xffff5e13,
+0xffff6cb5, 0xffff7a8c, 0xffff87ae, 0xffff942b, 0xffffa014, 0xffffab75,
+0xffffb65a, 0xffffc0ce, 0xffffcad8, 0xffffd481, 0xffffddd1, 0xffffe6cd,
+0xffffef7a, 0xfffff7df, 0xffffffff
+};
+
+/* e * 65536 */
+#define FIXED_E (intel_fixed_exp_tbl[32])
+/* 1 * 65536 */
+#define FIXED_ONE 65536
+
+static int intel_fixed_mul(int a, int b)
+{
+	return (int) ((int64_t)a * (int64_t)b / 65536LL);
+}
+
+static int intel_fixed_div(int a, int b)
+{
+	return (int) ((int64_t)a * 65536LL / (int64_t)b);
+}
+
+/*
+ * Approximate fixed point log function.
+ * Only works for inputs in [0,1[
+ */
+static int intel_fixed_log(int val)
+{
+	int index = val * 32 / FIXED_ONE;
+	int remainder = (val & 0x7ff) << 5;
+	int v1 = intel_fixed_log_tbl[index];
+	int v2 = intel_fixed_log_tbl[index+1];
+	int final = v1 + intel_fixed_mul(v2 - v1, remainder);
+	return final;
+}
+
+/*
+ * Approximate fixed point exp function.
+ */
+static int intel_fixed_exp(int val)
+{
+	int count = 0;
+	int index, remainder;
+	int int_part = FIXED_ONE, frac_part;
+	int i, v, v1, v2;
+
+	while (val < 0) {
+		val += FIXED_ONE;
+		count--;
+	}
+
+	while (val > FIXED_ONE) {
+		val -= FIXED_ONE;
+		count++;
+	}
+
+	index = val * 32 / FIXED_ONE;
+	remainder = (val & 0x7ff) << 5;
+
+	v1 = intel_fixed_exp_tbl[index];
+	v2 = intel_fixed_exp_tbl[index+1];
+	frac_part = v1 + intel_fixed_mul(v2 - v1, remainder);
+
+	if (count < 0) {
+		for (i = 0; i < -count; i++)
+			int_part = intel_fixed_mul(int_part, FIXED_E);
+
+		v = intel_fixed_div(frac_part, int_part);
+	} else {
+		for (i = 0; i < count; i++)
+			int_part = intel_fixed_mul(int_part, FIXED_E);
+
+		v = intel_fixed_mul(frac_part, int_part);
+	}
+	return (v >= 0) ? v : 0;
+}
+
+/*
+ * Approximate fixed point pow function.
+ * Only works for x in [0,1[
+ */
+static int intel_fixed_pow(int x, int y)
+{
+	int e, p, r;
+	e = intel_fixed_log(x);
+	p = intel_fixed_mul(e, y);
+	r = intel_fixed_exp(p);
+	return r;
+}
+
+
drivers/gpu/drm/i915/intel_panel.c +6 -0
index 3f9249b..ae95163 100644
@@ -251,6 +251,8 @@ static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level
 	struct drm_i915_private *dev_priv = dev->dev_private;
 	u32 tmp;
 
+	level = level * dev_priv->backlight_correction_level >> 8;
+
 	DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
 
 	if (HAS_PCH_SPLIT(dev))
@@ -306,6 +308,10 @@ static void intel_panel_init_backlight(struct drm_device *dev)
 
 	dev_priv->backlight_level = intel_panel_get_backlight(dev);
 	dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
+	dev_priv->adaptive_backlight_enabled = 0;
+	/* 2.2 as 16.16 fixed point */
+	dev_priv->adaptive_backlight_panel_gamma = 144179;
+	dev_priv->backlight_correction_level = 256;
 }
 
 enum drm_connector_status
include/drm/i915_drm.h +2 -0
index da929bb..70c9cfc 100644
@@ -309,6 +309,8 @@ typedef struct drm_i915_getparam {
 #define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
 #define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
 #define I915_SETPARAM_NUM_USED_FENCES                     4
+#define I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE           5
+#define I915_SETPARAM_PANEL_GAMMA                         6
 
 typedef struct drm_i915_setparam {
 	int param;
Signature · 6 lines
1.7.5.3.367.ga9930

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#2 lore raw
Quoted 18 lines · Jani Nikula
Quoted 9 lines · Stéphane Marchesin

This is an initial implementation of i915 adaptive backlight support. The intended use for the adaptive backlight is to generate interrupts whenever the luminance of the screen changes by some thresholds. The main caveat with that implementation is that those additional interrupts will wake up the CPU and consume more power. Instead, we hook into the vblank handler and handle it from there. This makes the implementation a little less intuitive but a lot more efficient. We also need to compute the gamma correction from the interrupt handler so we do this with a (new) fixed point module.

Hi Stéphane, interesting work.

My understanding is that content adaptive backlight control is basically a function that tries to reduce backlight brightness to save power while adjusting gamma to ensure there is no user detectable change in image. Like this:

Quoted 8 lines

(reduced_brightness, gamma) = f(brightness, luminance)

In my view, the histogram information, interrupt on its meaningful change, brightness and gamma setting are clearly mechanisms the driver should provide. I'm not at all sure if the function that defines the policy for brightness and gamma changes should be part of the driver.

Yes, I don't like it either, but the problem is that if we want fast response time we have to do it from the irq handler, or at least the kernel.

Quoted 8 lines

In fact, looking at the histogram_find_correction_level() and adaptive_backlight_compute_correction() functions below, they are filled with policy conditions. Usually it's preferred to have the policy in user space. The fixed point troubles would all go away as well in user space. And there's the added benefit of being able to handle the user's desired brightness and possible ambient light sensor based adjustment to brightness in a single point of truth.

Quoted 6 lines

I guess the question is how to define sensible interfaces to the mechanisms in a way that the adaptive backlight function is feasible to implement in user space, and the power saving goal could still be reached. I would at least like this to be looked into first.

The issue is that if you have to wake up a user space process every time you want to recompute the backlight, you're going to offset the backlight savings with some CPU losses. I wanted to avoid that.

Stéphane

Quoted 659 lines

BR,
Jani.

Quoted 10 lines
Change-Id I9b9631cacc7d90e2801a542a3789118521bc25f0
---
 drivers/gpu/drm/i915/Makefile                   |    1 +
 drivers/gpu/drm/i915/i915_dma.c                 |   16 ++
 drivers/gpu/drm/i915/i915_drv.h                 |   10 +
 drivers/gpu/drm/i915/i915_irq.c                 |    8 +-
 drivers/gpu/drm/i915/i915_reg.h                 |   22 ++
 drivers/gpu/drm/i915/intel_adaptive_backlight.c |  266

+++++++++++++++++++++++

Review

Quoted 8 lines
 drivers/gpu/drm/i915/intel_fixedpoint.h         |  140 ++++++++++++
 drivers/gpu/drm/i915/intel_panel.c              |    6 +
 include/drm/i915_drm.h                          |    2 +
 9 files changed, 469 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/intel_adaptive_backlight.c
 create mode 100644 drivers/gpu/drm/i915/intel_fixedpoint.h
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/Makefile

Review

Quoted 12 lines

index ce7fc77..5c125c3 100644

drivers/gpu/drm/i915/Makefile +1 -0
@@ -13,6 +13,7 @@ i915-y := i915_drv.o i915_dma.o i915_irq.o \
        i915_gem_gtt.o \
        i915_gem_tiling.o \
        i915_trace_points.o \
+       intel_adaptive_backlight.o \
        intel_display.o \
        intel_crt.o \
        intel_lvds.o \
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_dma.c

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev,

Quoted 4 lines

index ba60f3c..c2626d6 100644

drivers/gpu/drm/i915/i915_dma.c +0 -0
@@ -828,6 +828,22 @@ static int i915_setparam(struct drm_device *dev,

void *data,

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev, void *data,

Quoted 6 lines
drivers/gpu/drm/i915/i915_dma.c +3 -0
              /* Userspace can use first N regs */
              dev_priv->fence_reg_start = param->value;
              break;
+     case I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE:
+             if (INTEL_INFO(dev)->gen == 6) {
+                     dev_priv->adaptive_backlight_enabled =

param->value;

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev, void *data,

Quoted 8 lines
drivers/gpu/drm/i915/i915_dma.c +8 -0
+             } else {
+                     DRM_ERROR("No adaptive backlight on !GEN6\n");
+                     return -EINVAL;
+             }
+             break;
+     case I915_SETPARAM_PANEL_GAMMA:
+             if (INTEL_INFO(dev)->gen == 6) {
+                     dev_priv->adaptive_backlight_panel_gamma =

param->value;

Review

Quoted 9 lines
drivers/gpu/drm/i915/i915_dma.c +5 -0
+             } else {
+                     DRM_ERROR("No adaptive backlight on !GEN6\n");
+                     return -EINVAL;
+             }
+             break;
      default:
              DRM_DEBUG_DRIVER("unknown parameter %d\n",
                                      param->param);
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_drv.h

drivers/gpu/drm/i915/i915_drv.h extern int i915_restore_state(struct drm_device

Quoted 18 lines

index d89f585..f778f93 100644

drivers/gpu/drm/i915/i915_drv.h +7 -0
@@ -401,6 +401,13 @@ typedef struct drm_i915_private {
      struct drm_display_mode *lfp_lvds_vbt_mode; /* if any */
      struct drm_display_mode *sdvo_lvds_vbt_mode; /* if any */
 
+     /* Adaptive backlight */
+     bool adaptive_backlight_enabled;
+     int backlight_correction_level;
+     int backlight_correction_count;
+     int backlight_correction_direction;
+     int adaptive_backlight_panel_gamma;
+
      /* Feature bits from the VBIOS */
      unsigned int int_tv_support:1;
      unsigned int lvds_dither:1;
@@ -1358,6 +1365,9 @@ extern int i915_restore_state(struct drm_device

*dev);

Review

Quoted 10 lines
drivers/gpu/drm/i915/i915_drv.h +3 -0
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
 
+/* intel_adaptive_backlight.c */
+extern void intel_adaptive_backlight(struct drm_device *dev, int pipe);
+
 /* intel_i2c.c */
 extern int intel_setup_gmbus(struct drm_device *dev);
 extern void intel_teardown_gmbus(struct drm_device *dev);
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_irq.c

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t

Quoted 4 lines

index afd4e03..948da6b 100644

drivers/gpu/drm/i915/i915_irq.c +0 -0
@@ -619,11 +619,15 @@ static irqreturn_t

ironlake_irq_handler(DRM_IRQ_ARGS)

Review

Quoted 18 lines
drivers/gpu/drm/i915/i915_irq.c +6 -2
              intel_finish_page_flip_plane(dev, 1);
      }
 
-     if (de_iir & DE_PIPEA_VBLANK)
+     if (de_iir & DE_PIPEA_VBLANK) {
              drm_handle_vblank(dev, 0);
+             intel_adaptive_backlight(dev, 0);
+     }
 
-     if (de_iir & DE_PIPEB_VBLANK)
+     if (de_iir & DE_PIPEB_VBLANK) {
              drm_handle_vblank(dev, 1);
+             intel_adaptive_backlight(dev, 1);
+     }
 
      /* check event from PCH */
      if (de_iir & DE_PCH_EVENT) {
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_reg.h

drivers/gpu/drm/i915/i915_reg.h line 3595

Quoted 13 lines

index 552264c..2db874d 100644

drivers/gpu/drm/i915/i915_reg.h +6 -0
@@ -3587,6 +3587,28 @@
 #define  PWM_PIPE_B          (1 << 29)
 #define BLC_PWM_CPU_CTL              0x48254
 
+#define BLM_HIST_CTL                 0x48260
+#define  ENH_HIST_ENABLE             (1<<31)
+#define  ENH_MODIF_TBL_ENABLE                (1<<30)
+#define  ENH_PIPE_A_SELECT           (0<<29)
+#define  ENH_PIPE_B_SELECT           (1<<29)
+#define  ENH_PIPE(pipe) _PIPE(pipe, ENH_PIPE_A_SELECT,

ENH_PIPE_B_SELECT)

Review

Quoted 20 lines
drivers/gpu/drm/i915/i915_reg.h +16 -0
+#define  HIST_MODE_YUV                       (0<<24)
+#define  HIST_MODE_HSV                       (1<<24)
+#define  ENH_MODE_DIRECT             (0<<13)
+#define  ENH_MODE_ADDITIVE           (1<<13)
+#define  ENH_MODE_MULTIPLICATIVE     (2<<13)
+#define  BIN_REGISTER_SET            (1<<11)
+#define  ENH_NUM_BINS                        32
+
+#define BLM_HIST_ENH                 0x48264
+
+#define BLM_HIST_GUARD_BAND          0x48268
+#define  BLM_HIST_INTR_ENABLE                (1<<31)
+#define  BLM_HIST_EVENT_STATUS               (1<<30)
+#define  BLM_HIST_INTR_DELAY_MASK    (0xFF<<22)
+#define  BLM_HIST_INTR_DELAY_SHIFT   22
+
 #define BLC_PWM_PCH_CTL1     0xc8250
 #define  PWM_PCH_ENABLE              (1 << 31)
 #define  PWM_POLARITY_ACTIVE_LOW     (1 << 29)
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_adaptive_backlight.c

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 5

Quoted 10 lines

new file mode 100644
index 0000000..4234962

drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person

obtaining a

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial

portions

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 19

Quoted 2 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +2 -0
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

NON-INFRINGEMENT.

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 21

Quoted 2 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +2 -0
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF

CONTRACT,

Quoted 12 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +12 -0
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+
+#include "drmP.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "i915_drv.h"
+#include "i915_reg.h"
+#include "intel_drv.h"

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+#include "intel_fixedpoint.h"
+
+/*
+ * This function takes a histogram of buckets as input and determines an
+ * acceptable target backlight level.
+ */
+static int histogram_find_correction_level(int *levels)
+{
+     int i, sum = 0;
+     int ratio, distortion, prev_distortion = 0, off, final_ratio,

target;

Quoted 19 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +19 -0
+
+     for (i = 0; i < ENH_NUM_BINS; i++)
+             sum += levels[i];
+
+     /* Allow 0.33/256 distortion per pixel, on average */
+     target = sum / 3;
+
+     /* Special case where we only have less than 100 pixels
+      * outside of the darkest bin.
+      */
+     if (sum - levels[0] <= 100)
+             return 70;
+
+     for (ratio = ENH_NUM_BINS - 1; ratio >= 0 ; ratio--) {
+             distortion = 0;
+             for (i = ratio; i < ENH_NUM_BINS; i++) {
+                     int pixel_distortion = (i - ratio)*8;
+                     int num_pixels = levels[i];
+                     distortion += num_pixels * pixel_distortion;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             }
+             if (distortion > target)
+                     break;
+             else
+                     prev_distortion = distortion;
+     }
+
+     ratio++;
+
+     /* If we're not exactly at the border between two buckets,

extrapolate

Quoted 11 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +11 -0
+      * to get 3 extra bits of accuracy.
+      */
+     if (distortion - prev_distortion)
+             off = 8 * (target - prev_distortion) /
+                   (distortion - prev_distortion);
+     else
+             off = 0;
+
+     final_ratio = ratio * 255 / 31 + off;
+
+     if (final_ratio > 255)

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             final_ratio = 255;
+
+     /* Never aim for less than 50% of the total backlight */
+     if (final_ratio < 128)
+             final_ratio = 128;
+
+     return final_ratio;
+}
+
+static void histogram_get_levels(struct drm_device *dev, int pipe, int

*levels)

Quoted 6 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +6 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int i;
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+                                      ENH_MODIF_TBL_ENABLE |
+                                      ENH_PIPE(pipe) |
+                                      HIST_MODE_YUV |
+                                      ENH_MODE_ADDITIVE |
+                                      i);
+             levels[i] = I915_READ(BLM_HIST_ENH);
+     }
+}
+
+static void histogram_set_levels(struct drm_device *dev, int pipe, int

*levels)

Quoted 8 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +8 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int i;
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+                                      ENH_MODIF_TBL_ENABLE |
+                                      ENH_PIPE(pipe) |

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+                                      HIST_MODE_YUV |
+                                      ENH_MODE_ADDITIVE |
+                                      BIN_REGISTER_SET |
+                                      i);
+             I915_WRITE(BLM_HIST_ENH, levels[i]);
+     }
+}
+
+/*
+ * This function computes the backlight correction level for an

acceptable

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 4 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +4 -0
+ * distortion and fills up the correction bins adequately.
+ */
+static void
+adaptive_backlight_compute_correction(struct drm_device *dev, int

*levels)

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int correction_level;
+     int i, multiplier, one_over_gamma;
+
+     /* Find the correction level for an acceptable distortion */
+     correction_level = histogram_find_correction_level(levels);
+
+     /* If we're not yet at our correction target, we need to decide by

how

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+      * much to move.
+      */
+     if (dev_priv->backlight_correction_level != correction_level) {
+             int delta, direction;
+
+             direction = (correction_level >
+                          dev_priv->backlight_correction_level);
+
+             if (direction == dev_priv->backlight_correction_direction)

{

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 4 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +4 -0
+                     dev_priv->backlight_correction_count++;
+             } else {
+                     dev_priv->backlight_correction_count = 0;
+                     dev_priv->backlight_correction_direction =

direction;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             }
+
+             delta = abs(correction_level -
+                         dev_priv->backlight_correction_level)/4;
+
+             if (delta < 1)
+                     delta = 1;
+
+             /* For increasing the brightness, we do it instantly.
+              * For lowering the brightness, we require at least 10

frames

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 5 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
+              * below the current value. This avoids ping-ponging of the
+              * backlight level.
+              *
+              * We also never increase the backlight by more than 6% per
+              * frame, and never lower it by more than 3% per frame,

because

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 169

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+              * the backlight needs time to adjust and the LCD

correction

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+              * would be "ahead" otherwise.
+              */
+             if (correction_level >

dev_priv->backlight_correction_level) {

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 5 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
+                     if (delta > 15)
+                             delta = 15;
+                     dev_priv->backlight_correction_level += delta;
+             } else if ((dev_priv->backlight_correction_count > 10) &&
+                (correction_level <

dev_priv->backlight_correction_level)) {

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 12 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +12 -0
+                     if (delta > 7)
+                             delta = 7;
+                     dev_priv->backlight_correction_level -= delta;
+             }
+     }
+
+     /* We need to invert the gamma correction of the LCD values,
+      * but not of the backlight which is linear.
+      */
+     one_over_gamma = intel_fixed_div(FIXED_ONE,
+                             dev_priv->adaptive_backlight_panel_gamma);
+     multiplier = intel_fixed_pow(dev_priv->backlight_correction_level

* 256,

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 11 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +11 -0
+                                  one_over_gamma);
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             int base_value = i * 8 * 4;
+             levels[i] = base_value - base_value * multiplier / 65536;
+     }
+}
+
+/*
+ * A quick note about the adaptive backlight implementation:
+ * If we let it run as designed, it will generate a lot of interrupts

which

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 201

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * tends to wake the CPU up and waste power. This is a bad idea for a

power

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 202

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * saving feature. Instead, we couple it to the vblank interrupt since

that

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 203

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * means we drew something. This means that we do not react to

non-vsynced GL

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 204

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * updates, or updates to the front buffer, and also adds a little bit

of

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+ * extra latency. But it is an acceptable tradeoff to make.
+ */
+void intel_adaptive_backlight(struct drm_device *dev, int

pipe_vblank_event)

Quoted 23 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +23 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int levels[ENH_NUM_BINS];
+     int pipe;
+     struct drm_connector *connector;
+     struct intel_crtc *intel_crtc;
+
+     if (!dev_priv->adaptive_backlight_enabled)
+             return;
+
+     /* Find the connector */
+     if (dev_priv->int_lvds_connector)
+             connector = dev_priv->int_lvds_connector;
+     else if (dev_priv->int_edp_connector)
+             connector = dev_priv->int_edp_connector;
+     else
+             return;
+
+     if (!connector)
+             return;
+
+     if (!connector->encoder)
+             return;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+
+     if (!connector->encoder->crtc)
+             return;
+
+     /* Find the pipe for the panel. */
+     intel_crtc = to_intel_crtc(connector->encoder->crtc);
+     pipe = intel_crtc->pipe;
+
+     /* The callback happens for both pipe A & B. Now that we know which
+      * pipe we're doing adaptive backlight on, check that it's the

right

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+      * one. Bail if it isn't.
+      */
+     if (pipe != pipe_vblank_event)
+             return;
+
+     /* Return if no event. */
+     if (I915_READ(BLM_HIST_GUARD_BAND) & BLM_HIST_EVENT_STATUS == 0)
+             return;
+
+     /* Make sure we ack the previous event. Even though we do not get

the

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 251

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+      * IRQs (see above explanation), we must still ack the events

otherwise

Review

Quoted 16 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +15 -0
+      * the histogram data doesn't get updated any more.
+      */
+     I915_WRITE(BLM_HIST_GUARD_BAND, BLM_HIST_INTR_ENABLE |
+                                     BLM_HIST_EVENT_STATUS |
+                                     (1 << BLM_HIST_INTR_DELAY_SHIFT));
+
+     histogram_get_levels(dev, pipe, levels);
+
+     adaptive_backlight_compute_correction(dev, levels);
+
+     histogram_set_levels(dev, pipe, levels);
+
+     intel_panel_set_backlight(dev, dev_priv->backlight_level);
+}
+
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_fixedpoint.h

drivers/gpu/drm/i915/intel_fixedpoint.h line 5

Quoted 10 lines

new file mode 100644
index 0000000..fff5f0b

drivers/gpu/drm/i915/intel_fixedpoint.h new file +5 -0
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person

obtaining a

drivers/gpu/drm/i915/intel_fixedpoint.h line 14

Quoted 9 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +9 -0
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial

portions

drivers/gpu/drm/i915/intel_fixedpoint.h line 17

Quoted 3 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +3 -0
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS

drivers/gpu/drm/i915/intel_fixedpoint.h line 19

Quoted 2 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +2 -0
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

NON-INFRINGEMENT.

drivers/gpu/drm/i915/intel_fixedpoint.h line 21

Quoted 2 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +2 -0
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF

CONTRACT,

drivers/gpu/drm/i915/intel_fixedpoint.h line 28

Quoted 7 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +7 -0
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+/*
+ * The backlight is corrected in linear space. However the LCD

correction is

drivers/gpu/drm/i915/intel_fixedpoint.h line 29

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * corrected in gama space. So to be able to compute the correction

value for

drivers/gpu/drm/i915/intel_fixedpoint.h line 30

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * the LCD, we have to compute the inverse gamma. To do so, we carry

this

drivers/gpu/drm/i915/intel_fixedpoint.h line 31

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * small fixed point module which allows us to use pow() to compute

inverse

Review

Quoted 110 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +109 -0
+ * gamma.
+ *
+ * The fixed point format used here is 16.16.
+ */
+
+/* intel_fixed_exp_tbl[x*32] = exp(x) * 65536 */
+static const int intel_fixed_exp_tbl[33] = {
+0x00010000, 0x00010820, 0x00011083, 0x00011929, 0x00012216, 0x00012b4b,
+0x000134cc, 0x00013e99, 0x000148b6, 0x00015325, 0x00015de9, 0x00016905,
+0x0001747a, 0x0001804d, 0x00018c80, 0x00019916, 0x0001a613, 0x0001b378,
+0x0001c14b, 0x0001cf8e, 0x0001de45, 0x0001ed74, 0x0001fd1e, 0x00020d47,
+0x00021df4, 0x00022f28, 0x000240e8, 0x00025338, 0x0002661d, 0x0002799b,
+0x00028db8, 0x0002a278, 0x0002b7e1
+};
+
+/* intel_fixed_log_tbl[x*32] = log(x) * 65536 */
+static const int intel_fixed_log_tbl[33] = {
+0x80000000, 0xfffc88c6, 0xfffd3a38, 0xfffda204, 0xfffdebaa, 0xfffe24ca,
+0xfffe5376, 0xfffe7aed, 0xfffe9d1c, 0xfffebb43, 0xfffed63c, 0xfffeeea2,
+0xffff04e8, 0xffff1966, 0xffff2c5f, 0xffff3e08, 0xffff4e8e, 0xffff5e13,
+0xffff6cb5, 0xffff7a8c, 0xffff87ae, 0xffff942b, 0xffffa014, 0xffffab75,
+0xffffb65a, 0xffffc0ce, 0xffffcad8, 0xffffd481, 0xffffddd1, 0xffffe6cd,
+0xffffef7a, 0xfffff7df, 0xffffffff
+};
+
+/* e * 65536 */
+#define FIXED_E (intel_fixed_exp_tbl[32])
+/* 1 * 65536 */
+#define FIXED_ONE 65536
+
+static int intel_fixed_mul(int a, int b)
+{
+     return (int) ((int64_t)a * (int64_t)b / 65536LL);
+}
+
+static int intel_fixed_div(int a, int b)
+{
+     return (int) ((int64_t)a * 65536LL / (int64_t)b);
+}
+
+/*
+ * Approximate fixed point log function.
+ * Only works for inputs in [0,1[
+ */
+static int intel_fixed_log(int val)
+{
+     int index = val * 32 / FIXED_ONE;
+     int remainder = (val & 0x7ff) << 5;
+     int v1 = intel_fixed_log_tbl[index];
+     int v2 = intel_fixed_log_tbl[index+1];
+     int final = v1 + intel_fixed_mul(v2 - v1, remainder);
+     return final;
+}
+
+/*
+ * Approximate fixed point exp function.
+ */
+static int intel_fixed_exp(int val)
+{
+     int count = 0;
+     int index, remainder;
+     int int_part = FIXED_ONE, frac_part;
+     int i, v, v1, v2;
+
+     while (val < 0) {
+             val += FIXED_ONE;
+             count--;
+     }
+
+     while (val > FIXED_ONE) {
+             val -= FIXED_ONE;
+             count++;
+     }
+
+     index = val * 32 / FIXED_ONE;
+     remainder = (val & 0x7ff) << 5;
+
+     v1 = intel_fixed_exp_tbl[index];
+     v2 = intel_fixed_exp_tbl[index+1];
+     frac_part = v1 + intel_fixed_mul(v2 - v1, remainder);
+
+     if (count < 0) {
+             for (i = 0; i < -count; i++)
+                     int_part = intel_fixed_mul(int_part, FIXED_E);
+
+             v = intel_fixed_div(frac_part, int_part);
+     } else {
+             for (i = 0; i < count; i++)
+                     int_part = intel_fixed_mul(int_part, FIXED_E);
+
+             v = intel_fixed_mul(frac_part, int_part);
+     }
+     return (v >= 0) ? v : 0;
+}
+
+/*
+ * Approximate fixed point pow function.
+ * Only works for x in [0,1[
+ */
+static int intel_fixed_pow(int x, int y)
+{
+     int e, p, r;
+     e = intel_fixed_log(x);
+     p = intel_fixed_mul(e, y);
+     r = intel_fixed_exp(p);
+     return r;
+}
+
+
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_panel.c

drivers/gpu/drm/i915/intel_panel.c static void

Quoted 4 lines

index 3f9249b..ae95163 100644

drivers/gpu/drm/i915/intel_panel.c +0 -0
@@ -251,6 +251,8 @@ static void

intel_panel_actually_set_backlight(struct drm_device *dev, u32 level

drivers/gpu/drm/i915/intel_panel.c static void intel_panel_init_backlight(struct

Quoted 9 lines
drivers/gpu/drm/i915/intel_panel.c +2 -0
      struct drm_i915_private *dev_priv = dev->dev_private;
      u32 tmp;
 
+     level = level * dev_priv->backlight_correction_level >> 8;
+
      DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
 
      if (HAS_PCH_SPLIT(dev))
@@ -306,6 +308,10 @@ static void intel_panel_init_backlight(struct

drm_device *dev)

Quoted 26 lines
drivers/gpu/drm/i915/intel_panel.c +4 -0
      dev_priv->backlight_level = intel_panel_get_backlight(dev);
      dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
+     dev_priv->adaptive_backlight_enabled = 0;
+     /* 2.2 as 16.16 fixed point */
+     dev_priv->adaptive_backlight_panel_gamma = 144179;
+     dev_priv->backlight_correction_level = 256;
 }
 
 enum drm_connector_status
include/drm/i915_drm.h +2 -1
index da929bb..70c9cfc 100644
@@ -309,6 +309,8 @@ typedef struct drm_i915_getparam {
 #define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
 #define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
 #define I915_SETPARAM_NUM_USED_FENCES                     4
+#define I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE           5
+#define I915_SETPARAM_PANEL_GAMMA                         6
 
 typedef struct drm_i915_setparam {
      int param;
--

1.7.5.3.367.ga9930

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#3 lore raw
In reply to Stéphane Marchesin
Quoted 10 lines · Stéphane Marchesin

This is an initial implementation of i915 adaptive backlight support. The intended use for the adaptive backlight is to generate interrupts whenever the luminance of the screen changes by some thresholds. The main caveat with that implementation is that those additional interrupts will wake up the CPU and consume more power. Instead, we hook into the vblank handler and handle it from there. This makes the implementation a little less intuitive but a lot more efficient. We also need to compute the gamma correction from the interrupt handler so we do this with a (new) fixed point module.

Quoted 2 lines
Change-Id I9b9631cacc7d90e2801a542a3789118521bc25f0

A few quick comments:
- I don't like setparam for this too much. Imo exposing this as an lvds/eDP connector property makes more sense, and also makes the feature more accessible to mere mortals. btw I think we should do the same with the downclock stuff and expose a panel_downclock_freq plus a panel_downclock enable which is simply initialized at driver load time with the defaults, but could be freely changed afterwards.
- With connector properties we could also expose tuneables in an easy way, at least partially addressing Jani's comment (like the display gamma and maybe also other parameters).
- We need to somehow cope with front-rendering X. Yeah, I know this sucks and all, but such is life. We already mark framebuffers as busys when rendering into them directly, I guess we could hook into to that either disable this feature until the next pageflip. Or switch to a different mode. The disable needs to be gradually, obviously.
- I'm unsure whether we shouldn't move the fixed-point stuff to a common place. Wrt all the fixed-point constant I think a little macro to convert double to 16.16 fixed point would massively help code readability.

Yours, Daniel

Quoted 598 lines
---
 drivers/gpu/drm/i915/Makefile                   |    1 +
 drivers/gpu/drm/i915/i915_dma.c                 |   16 ++
 drivers/gpu/drm/i915/i915_drv.h                 |   10 +
 drivers/gpu/drm/i915/i915_irq.c                 |    8 +-
 drivers/gpu/drm/i915/i915_reg.h                 |   22 ++
 drivers/gpu/drm/i915/intel_adaptive_backlight.c |  266 +++++++++++++++++++++++
 drivers/gpu/drm/i915/intel_fixedpoint.h         |  140 ++++++++++++
 drivers/gpu/drm/i915/intel_panel.c              |    6 +
 include/drm/i915_drm.h                          |    2 +
 9 files changed, 469 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/intel_adaptive_backlight.c
 create mode 100644 drivers/gpu/drm/i915/intel_fixedpoint.h
drivers/gpu/drm/i915/Makefile +1 -0
index ce7fc77..5c125c3 100644
@@ -13,6 +13,7 @@ i915-y := i915_drv.o i915_dma.o i915_irq.o \
 	  i915_gem_gtt.o \
 	  i915_gem_tiling.o \
 	  i915_trace_points.o \
+	  intel_adaptive_backlight.o \
 	  intel_display.o \
 	  intel_crt.o \
 	  intel_lvds.o \
drivers/gpu/drm/i915/i915_dma.c +16 -0
index ba60f3c..c2626d6 100644
@@ -828,6 +828,22 @@ static int i915_setparam(struct drm_device *dev, void *data,
 		/* Userspace can use first N regs */
 		dev_priv->fence_reg_start = param->value;
 		break;
+	case I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_enabled = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
+	case I915_SETPARAM_PANEL_GAMMA:
+		if (INTEL_INFO(dev)->gen == 6) {
+			dev_priv->adaptive_backlight_panel_gamma = param->value;
+		} else {
+			DRM_ERROR("No adaptive backlight on !GEN6\n");
+			return -EINVAL;
+		}
+		break;
 	default:
 		DRM_DEBUG_DRIVER("unknown parameter %d\n",
 					param->param);
drivers/gpu/drm/i915/i915_drv.h +10 -0
index d89f585..f778f93 100644
@@ -401,6 +401,13 @@ typedef struct drm_i915_private {
 	struct drm_display_mode *lfp_lvds_vbt_mode; /* if any */
 	struct drm_display_mode *sdvo_lvds_vbt_mode; /* if any */
 
+	/* Adaptive backlight */
+	bool adaptive_backlight_enabled;
+	int backlight_correction_level;
+	int backlight_correction_count;
+	int backlight_correction_direction;
+	int adaptive_backlight_panel_gamma;
+
 	/* Feature bits from the VBIOS */
 	unsigned int int_tv_support:1;
 	unsigned int lvds_dither:1;
@@ -1358,6 +1365,9 @@ extern int i915_restore_state(struct drm_device *dev);
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
 
+/* intel_adaptive_backlight.c */
+extern void intel_adaptive_backlight(struct drm_device *dev, int pipe);
+
 /* intel_i2c.c */
 extern int intel_setup_gmbus(struct drm_device *dev);
 extern void intel_teardown_gmbus(struct drm_device *dev);
drivers/gpu/drm/i915/i915_irq.c +6 -2
index afd4e03..948da6b 100644
@@ -619,11 +619,15 @@ static irqreturn_t ironlake_irq_handler(DRM_IRQ_ARGS)
 		intel_finish_page_flip_plane(dev, 1);
 	}
 
-	if (de_iir & DE_PIPEA_VBLANK)
+	if (de_iir & DE_PIPEA_VBLANK) {
 		drm_handle_vblank(dev, 0);
+		intel_adaptive_backlight(dev, 0);
+	}
 
-	if (de_iir & DE_PIPEB_VBLANK)
+	if (de_iir & DE_PIPEB_VBLANK) {
 		drm_handle_vblank(dev, 1);
+		intel_adaptive_backlight(dev, 1);
+	}
 
 	/* check event from PCH */
 	if (de_iir & DE_PCH_EVENT) {
drivers/gpu/drm/i915/i915_reg.h +22 -0
index 552264c..2db874d 100644
@@ -3587,6 +3587,28 @@
 #define  PWM_PIPE_B		(1 << 29)
 #define BLC_PWM_CPU_CTL		0x48254
 
+#define BLM_HIST_CTL			0x48260
+#define  ENH_HIST_ENABLE		(1<<31)
+#define  ENH_MODIF_TBL_ENABLE		(1<<30)
+#define  ENH_PIPE_A_SELECT		(0<<29)
+#define  ENH_PIPE_B_SELECT		(1<<29)
+#define  ENH_PIPE(pipe) _PIPE(pipe, ENH_PIPE_A_SELECT, ENH_PIPE_B_SELECT)
+#define  HIST_MODE_YUV			(0<<24)
+#define  HIST_MODE_HSV			(1<<24)
+#define  ENH_MODE_DIRECT		(0<<13)
+#define  ENH_MODE_ADDITIVE		(1<<13)
+#define  ENH_MODE_MULTIPLICATIVE	(2<<13)
+#define  BIN_REGISTER_SET		(1<<11)
+#define  ENH_NUM_BINS			32
+
+#define BLM_HIST_ENH			0x48264
+
+#define BLM_HIST_GUARD_BAND		0x48268
+#define  BLM_HIST_INTR_ENABLE		(1<<31)
+#define  BLM_HIST_EVENT_STATUS		(1<<30)
+#define  BLM_HIST_INTR_DELAY_MASK	(0xFF<<22)
+#define  BLM_HIST_INTR_DELAY_SHIFT	22
+
 #define BLC_PWM_PCH_CTL1	0xc8250
 #define  PWM_PCH_ENABLE		(1 << 31)
 #define  PWM_POLARITY_ACTIVE_LOW	(1 << 29)
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +266 -0
new file mode 100644
index 0000000..4234962
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+
+#include "drmP.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "i915_drv.h"
+#include "i915_reg.h"
+#include "intel_drv.h"
+#include "intel_fixedpoint.h"
+
+/*
+ * This function takes a histogram of buckets as input and determines an
+ * acceptable target backlight level.
+ */
+static int histogram_find_correction_level(int *levels)
+{
+	int i, sum = 0;
+	int ratio, distortion, prev_distortion = 0, off, final_ratio, target;
+
+	for (i = 0; i < ENH_NUM_BINS; i++)
+		sum += levels[i];
+
+	/* Allow 0.33/256 distortion per pixel, on average */
+	target = sum / 3;
+
+	/* Special case where we only have less than 100 pixels
+	 * outside of the darkest bin.
+	 */
+	if (sum - levels[0] <= 100)
+		return 70;
+
+	for (ratio = ENH_NUM_BINS - 1; ratio >= 0 ; ratio--) {
+		distortion = 0;
+		for (i = ratio; i < ENH_NUM_BINS; i++) {
+			int pixel_distortion = (i - ratio)*8;
+			int num_pixels = levels[i];
+			distortion += num_pixels * pixel_distortion;
+		}
+		if (distortion > target)
+			break;
+		else
+			prev_distortion = distortion;
+	}
+
+	ratio++;
+
+	/* If we're not exactly at the border between two buckets, extrapolate
+	 * to get 3 extra bits of accuracy.
+	 */
+	if (distortion - prev_distortion)
+		off = 8 * (target - prev_distortion) /
+		      (distortion - prev_distortion);
+	else
+		off = 0;
+
+	final_ratio = ratio * 255 / 31 + off;
+
+	if (final_ratio > 255)
+		final_ratio = 255;
+
+	/* Never aim for less than 50% of the total backlight */
+	if (final_ratio < 128)
+		final_ratio = 128;
+
+	return final_ratio;
+}
+
+static void histogram_get_levels(struct drm_device *dev, int pipe, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int i;
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+					 ENH_MODIF_TBL_ENABLE |
+					 ENH_PIPE(pipe) |
+					 HIST_MODE_YUV |
+					 ENH_MODE_ADDITIVE |
+					 i);
+		levels[i] = I915_READ(BLM_HIST_ENH);
+	}
+}
+
+static void histogram_set_levels(struct drm_device *dev, int pipe, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int i;
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+					 ENH_MODIF_TBL_ENABLE |
+					 ENH_PIPE(pipe) |
+					 HIST_MODE_YUV |
+					 ENH_MODE_ADDITIVE |
+					 BIN_REGISTER_SET |
+					 i);
+		I915_WRITE(BLM_HIST_ENH, levels[i]);
+	}
+}
+
+/*
+ * This function computes the backlight correction level for an acceptable
+ * distortion and fills up the correction bins adequately.
+ */
+static void
+adaptive_backlight_compute_correction(struct drm_device *dev, int *levels)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int correction_level;
+	int i, multiplier, one_over_gamma;
+
+	/* Find the correction level for an acceptable distortion */
+	correction_level = histogram_find_correction_level(levels);
+
+	/* If we're not yet at our correction target, we need to decide by how
+	 * much to move.
+	 */
+	if (dev_priv->backlight_correction_level != correction_level) {
+		int delta, direction;
+
+		direction = (correction_level >
+			     dev_priv->backlight_correction_level);
+
+		if (direction == dev_priv->backlight_correction_direction) {
+			dev_priv->backlight_correction_count++;
+		} else {
+			dev_priv->backlight_correction_count = 0;
+			dev_priv->backlight_correction_direction = direction;
+		}
+
+		delta = abs(correction_level -
+			    dev_priv->backlight_correction_level)/4;
+
+		if (delta < 1)
+			delta = 1;
+
+		/* For increasing the brightness, we do it instantly.
+		 * For lowering the brightness, we require at least 10 frames
+		 * below the current value. This avoids ping-ponging of the
+		 * backlight level.
+		 *
+		 * We also never increase the backlight by more than 6% per
+		 * frame, and never lower it by more than 3% per frame, because
+		 * the backlight needs time to adjust and the LCD correction
+		 * would be "ahead" otherwise.
+		 */
+		if (correction_level > dev_priv->backlight_correction_level) {
+			if (delta > 15)
+				delta = 15;
+			dev_priv->backlight_correction_level += delta;
+		} else if ((dev_priv->backlight_correction_count > 10) &&
+		   (correction_level < dev_priv->backlight_correction_level)) {
+			if (delta > 7)
+				delta = 7;
+			dev_priv->backlight_correction_level -= delta;
+		}
+	}
+
+	/* We need to invert the gamma correction of the LCD values,
+	 * but not of the backlight which is linear.
+	 */
+	one_over_gamma = intel_fixed_div(FIXED_ONE,
+				dev_priv->adaptive_backlight_panel_gamma);
+	multiplier = intel_fixed_pow(dev_priv->backlight_correction_level * 256,
+				     one_over_gamma);
+
+	for (i = 0; i < ENH_NUM_BINS; i++) {
+		int base_value = i * 8 * 4;
+		levels[i] = base_value - base_value * multiplier / 65536;
+	}
+}
+
+/*
+ * A quick note about the adaptive backlight implementation:
+ * If we let it run as designed, it will generate a lot of interrupts which
+ * tends to wake the CPU up and waste power. This is a bad idea for a power
+ * saving feature. Instead, we couple it to the vblank interrupt since that
+ * means we drew something. This means that we do not react to non-vsynced GL
+ * updates, or updates to the front buffer, and also adds a little bit of
+ * extra latency. But it is an acceptable tradeoff to make.
+ */
+void intel_adaptive_backlight(struct drm_device *dev, int pipe_vblank_event)
+{
+	drm_i915_private_t *dev_priv = dev->dev_private;
+	int levels[ENH_NUM_BINS];
+	int pipe;
+	struct drm_connector *connector;
+	struct intel_crtc *intel_crtc;
+
+	if (!dev_priv->adaptive_backlight_enabled)
+		return;
+
+	/* Find the connector */
+	if (dev_priv->int_lvds_connector)
+		connector = dev_priv->int_lvds_connector;
+	else if (dev_priv->int_edp_connector)
+		connector = dev_priv->int_edp_connector;
+	else
+		return;
+
+	if (!connector)
+		return;
+
+	if (!connector->encoder)
+		return;
+
+	if (!connector->encoder->crtc)
+		return;
+
+	/* Find the pipe for the panel. */
+	intel_crtc = to_intel_crtc(connector->encoder->crtc);
+	pipe = intel_crtc->pipe;
+
+	/* The callback happens for both pipe A & B. Now that we know which
+	 * pipe we're doing adaptive backlight on, check that it's the right
+	 * one. Bail if it isn't.
+	 */
+	if (pipe != pipe_vblank_event)
+		return;
+
+	/* Return if no event. */
+	if (I915_READ(BLM_HIST_GUARD_BAND) & BLM_HIST_EVENT_STATUS == 0)
+		return;
+
+	/* Make sure we ack the previous event. Even though we do not get the
+	 * IRQs (see above explanation), we must still ack the events otherwise
+	 * the histogram data doesn't get updated any more.
+	 */
+	I915_WRITE(BLM_HIST_GUARD_BAND, BLM_HIST_INTR_ENABLE |
+					BLM_HIST_EVENT_STATUS |
+					(1 << BLM_HIST_INTR_DELAY_SHIFT));
+
+	histogram_get_levels(dev, pipe, levels);
+
+	adaptive_backlight_compute_correction(dev, levels);
+
+	histogram_set_levels(dev, pipe, levels);
+
+	intel_panel_set_backlight(dev, dev_priv->backlight_level);
+}
+
drivers/gpu/drm/i915/intel_fixedpoint.h new file +140 -0
new file mode 100644
index 0000000..fff5f0b
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+/*
+ * The backlight is corrected in linear space. However the LCD correction is
+ * corrected in gama space. So to be able to compute the correction value for
+ * the LCD, we have to compute the inverse gamma. To do so, we carry this
+ * small fixed point module which allows us to use pow() to compute inverse
+ * gamma.
+ *
+ * The fixed point format used here is 16.16.
+ */
+
+/* intel_fixed_exp_tbl[x*32] = exp(x) * 65536 */
+static const int intel_fixed_exp_tbl[33] = {
+0x00010000, 0x00010820, 0x00011083, 0x00011929, 0x00012216, 0x00012b4b,
+0x000134cc, 0x00013e99, 0x000148b6, 0x00015325, 0x00015de9, 0x00016905,
+0x0001747a, 0x0001804d, 0x00018c80, 0x00019916, 0x0001a613, 0x0001b378,
+0x0001c14b, 0x0001cf8e, 0x0001de45, 0x0001ed74, 0x0001fd1e, 0x00020d47,
+0x00021df4, 0x00022f28, 0x000240e8, 0x00025338, 0x0002661d, 0x0002799b,
+0x00028db8, 0x0002a278, 0x0002b7e1
+};
+
+/* intel_fixed_log_tbl[x*32] = log(x) * 65536 */
+static const int intel_fixed_log_tbl[33] = {
+0x80000000, 0xfffc88c6, 0xfffd3a38, 0xfffda204, 0xfffdebaa, 0xfffe24ca,
+0xfffe5376, 0xfffe7aed, 0xfffe9d1c, 0xfffebb43, 0xfffed63c, 0xfffeeea2,
+0xffff04e8, 0xffff1966, 0xffff2c5f, 0xffff3e08, 0xffff4e8e, 0xffff5e13,
+0xffff6cb5, 0xffff7a8c, 0xffff87ae, 0xffff942b, 0xffffa014, 0xffffab75,
+0xffffb65a, 0xffffc0ce, 0xffffcad8, 0xffffd481, 0xffffddd1, 0xffffe6cd,
+0xffffef7a, 0xfffff7df, 0xffffffff
+};
+
+/* e * 65536 */
+#define FIXED_E (intel_fixed_exp_tbl[32])
+/* 1 * 65536 */
+#define FIXED_ONE 65536
+
+static int intel_fixed_mul(int a, int b)
+{
+	return (int) ((int64_t)a * (int64_t)b / 65536LL);
+}
+
+static int intel_fixed_div(int a, int b)
+{
+	return (int) ((int64_t)a * 65536LL / (int64_t)b);
+}
+
+/*
+ * Approximate fixed point log function.
+ * Only works for inputs in [0,1[
+ */
+static int intel_fixed_log(int val)
+{
+	int index = val * 32 / FIXED_ONE;
+	int remainder = (val & 0x7ff) << 5;
+	int v1 = intel_fixed_log_tbl[index];
+	int v2 = intel_fixed_log_tbl[index+1];
+	int final = v1 + intel_fixed_mul(v2 - v1, remainder);
+	return final;
+}
+
+/*
+ * Approximate fixed point exp function.
+ */
+static int intel_fixed_exp(int val)
+{
+	int count = 0;
+	int index, remainder;
+	int int_part = FIXED_ONE, frac_part;
+	int i, v, v1, v2;
+
+	while (val < 0) {
+		val += FIXED_ONE;
+		count--;
+	}
+
+	while (val > FIXED_ONE) {
+		val -= FIXED_ONE;
+		count++;
+	}
+
+	index = val * 32 / FIXED_ONE;
+	remainder = (val & 0x7ff) << 5;
+
+	v1 = intel_fixed_exp_tbl[index];
+	v2 = intel_fixed_exp_tbl[index+1];
+	frac_part = v1 + intel_fixed_mul(v2 - v1, remainder);
+
+	if (count < 0) {
+		for (i = 0; i < -count; i++)
+			int_part = intel_fixed_mul(int_part, FIXED_E);
+
+		v = intel_fixed_div(frac_part, int_part);
+	} else {
+		for (i = 0; i < count; i++)
+			int_part = intel_fixed_mul(int_part, FIXED_E);
+
+		v = intel_fixed_mul(frac_part, int_part);
+	}
+	return (v >= 0) ? v : 0;
+}
+
+/*
+ * Approximate fixed point pow function.
+ * Only works for x in [0,1[
+ */
+static int intel_fixed_pow(int x, int y)
+{
+	int e, p, r;
+	e = intel_fixed_log(x);
+	p = intel_fixed_mul(e, y);
+	r = intel_fixed_exp(p);
+	return r;
+}
+
+
drivers/gpu/drm/i915/intel_panel.c +6 -0
index 3f9249b..ae95163 100644
@@ -251,6 +251,8 @@ static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level
 	struct drm_i915_private *dev_priv = dev->dev_private;
 	u32 tmp;
 
+	level = level * dev_priv->backlight_correction_level >> 8;
+
 	DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
 
 	if (HAS_PCH_SPLIT(dev))
@@ -306,6 +308,10 @@ static void intel_panel_init_backlight(struct drm_device *dev)
 
 	dev_priv->backlight_level = intel_panel_get_backlight(dev);
 	dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
+	dev_priv->adaptive_backlight_enabled = 0;
+	/* 2.2 as 16.16 fixed point */
+	dev_priv->adaptive_backlight_panel_gamma = 144179;
+	dev_priv->backlight_correction_level = 256;
 }
 
 enum drm_connector_status
include/drm/i915_drm.h +2 -0
index da929bb..70c9cfc 100644
@@ -309,6 +309,8 @@ typedef struct drm_i915_getparam {
 #define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
 #define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
 #define I915_SETPARAM_NUM_USED_FENCES                     4
+#define I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE           5
+#define I915_SETPARAM_PANEL_GAMMA                         6
 
 typedef struct drm_i915_setparam {
 	int param;
Signature · 6 lines
1.7.5.3.367.ga9930

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 3 lines
Daniel Vetter
Mail: [email protected]
Mobile: +41 (0)79 365 57 48
#4 lore raw
Quoted 14 lines · Daniel Vetter
Quoted 10 lines · Stéphane Marchesin

This is an initial implementation of i915 adaptive backlight support. The intended use for the adaptive backlight is to generate interrupts whenever the luminance of the screen changes by some thresholds. The main caveat with that implementation is that those additional interrupts will wake up the CPU and consume more power. Instead, we hook into the vblank handler and handle it from there. This makes the implementation a little less intuitive but a lot more efficient. We also need to compute the gamma correction from the interrupt handler so we do this with a (new) fixed point module.

Quoted 2 lines
Change-Id I9b9631cacc7d90e2801a542a3789118521bc25f0
Quoted 8 lines

A few quick comments:
- I don't like setparam for this too much. Imo exposing this as an lvds/eDP connector property makes more sense, and also makes the feature more accessible to mere mortals. btw I think we should do the same with the downclock stuff and expose a panel_downclock_freq plus a panel_downclock enable which is simply initialized at driver load time with the defaults, but could be freely changed afterwards.

Agreed, I realized a property would be better after sending that patch :)

Quoted 4 lines

- With connector properties we could also expose tuneables in an easy way, at least partially addressing Jani's comment (like the display gamma and maybe also other parameters).

Agreed.

Quoted 6 lines

- We need to somehow cope with front-rendering X. Yeah, I know this sucks and all, but such is life. We already mark framebuffers as busys when rendering into them directly, I guess we could hook into to that either disable this feature until the next pageflip. Or switch to a different mode. The disable needs to be gradually, obviously.

Ok, that's probably the toughest item on that list. I'll dig into that.

Quoted 5 lines

- I'm unsure whether we shouldn't move the fixed-point stuff to a common place. Wrt all the fixed-point constant I think a little macro to convert double to 16.16 fixed point would massively help code readability.

True, I'll do that.

Stéphane

Quoted 651 lines

Yours, Daniel

Quoted 7 lines
---
 drivers/gpu/drm/i915/Makefile                   |    1 +
 drivers/gpu/drm/i915/i915_dma.c                 |   16 ++
 drivers/gpu/drm/i915/i915_drv.h                 |   10 +
 drivers/gpu/drm/i915/i915_irq.c                 |    8 +-
 drivers/gpu/drm/i915/i915_reg.h                 |   22 ++
 drivers/gpu/drm/i915/intel_adaptive_backlight.c |  266

+++++++++++++++++++++++

Review

Quoted 8 lines
 drivers/gpu/drm/i915/intel_fixedpoint.h         |  140 ++++++++++++
 drivers/gpu/drm/i915/intel_panel.c              |    6 +
 include/drm/i915_drm.h                          |    2 +
 9 files changed, 469 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/intel_adaptive_backlight.c
 create mode 100644 drivers/gpu/drm/i915/intel_fixedpoint.h
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/Makefile

Review

Quoted 12 lines

index ce7fc77..5c125c3 100644

drivers/gpu/drm/i915/Makefile +1 -0
@@ -13,6 +13,7 @@ i915-y := i915_drv.o i915_dma.o i915_irq.o \
        i915_gem_gtt.o \
        i915_gem_tiling.o \
        i915_trace_points.o \
+       intel_adaptive_backlight.o \
        intel_display.o \
        intel_crt.o \
        intel_lvds.o \
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_dma.c

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev,

Quoted 4 lines

index ba60f3c..c2626d6 100644

drivers/gpu/drm/i915/i915_dma.c +0 -0
@@ -828,6 +828,22 @@ static int i915_setparam(struct drm_device *dev,

void *data,

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev, void *data,

Quoted 6 lines
drivers/gpu/drm/i915/i915_dma.c +3 -0
              /* Userspace can use first N regs */
              dev_priv->fence_reg_start = param->value;
              break;
+     case I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE:
+             if (INTEL_INFO(dev)->gen == 6) {
+                     dev_priv->adaptive_backlight_enabled =

param->value;

drivers/gpu/drm/i915/i915_dma.c static int i915_setparam(struct drm_device *dev, void *data,

Quoted 8 lines
drivers/gpu/drm/i915/i915_dma.c +8 -0
+             } else {
+                     DRM_ERROR("No adaptive backlight on !GEN6\n");
+                     return -EINVAL;
+             }
+             break;
+     case I915_SETPARAM_PANEL_GAMMA:
+             if (INTEL_INFO(dev)->gen == 6) {
+                     dev_priv->adaptive_backlight_panel_gamma =

param->value;

Review

Quoted 9 lines
drivers/gpu/drm/i915/i915_dma.c +5 -0
+             } else {
+                     DRM_ERROR("No adaptive backlight on !GEN6\n");
+                     return -EINVAL;
+             }
+             break;
      default:
              DRM_DEBUG_DRIVER("unknown parameter %d\n",
                                      param->param);
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_drv.h

drivers/gpu/drm/i915/i915_drv.h extern int i915_restore_state(struct drm_device

Quoted 18 lines

index d89f585..f778f93 100644

drivers/gpu/drm/i915/i915_drv.h +7 -0
@@ -401,6 +401,13 @@ typedef struct drm_i915_private {
      struct drm_display_mode *lfp_lvds_vbt_mode; /* if any */
      struct drm_display_mode *sdvo_lvds_vbt_mode; /* if any */
 
+     /* Adaptive backlight */
+     bool adaptive_backlight_enabled;
+     int backlight_correction_level;
+     int backlight_correction_count;
+     int backlight_correction_direction;
+     int adaptive_backlight_panel_gamma;
+
      /* Feature bits from the VBIOS */
      unsigned int int_tv_support:1;
      unsigned int lvds_dither:1;
@@ -1358,6 +1365,9 @@ extern int i915_restore_state(struct drm_device

*dev);

Review

Quoted 10 lines
drivers/gpu/drm/i915/i915_drv.h +3 -0
 extern int i915_save_state(struct drm_device *dev);
 extern int i915_restore_state(struct drm_device *dev);
 
+/* intel_adaptive_backlight.c */
+extern void intel_adaptive_backlight(struct drm_device *dev, int pipe);
+
 /* intel_i2c.c */
 extern int intel_setup_gmbus(struct drm_device *dev);
 extern void intel_teardown_gmbus(struct drm_device *dev);
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_irq.c

drivers/gpu/drm/i915/i915_irq.c static irqreturn_t

Quoted 4 lines

index afd4e03..948da6b 100644

drivers/gpu/drm/i915/i915_irq.c +0 -0
@@ -619,11 +619,15 @@ static irqreturn_t

ironlake_irq_handler(DRM_IRQ_ARGS)

Review

Quoted 18 lines
drivers/gpu/drm/i915/i915_irq.c +6 -2
              intel_finish_page_flip_plane(dev, 1);
      }
 
-     if (de_iir & DE_PIPEA_VBLANK)
+     if (de_iir & DE_PIPEA_VBLANK) {
              drm_handle_vblank(dev, 0);
+             intel_adaptive_backlight(dev, 0);
+     }
 
-     if (de_iir & DE_PIPEB_VBLANK)
+     if (de_iir & DE_PIPEB_VBLANK) {
              drm_handle_vblank(dev, 1);
+             intel_adaptive_backlight(dev, 1);
+     }
 
      /* check event from PCH */
      if (de_iir & DE_PCH_EVENT) {
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/i915_reg.h

drivers/gpu/drm/i915/i915_reg.h line 3595

Quoted 13 lines

index 552264c..2db874d 100644

drivers/gpu/drm/i915/i915_reg.h +6 -0
@@ -3587,6 +3587,28 @@
 #define  PWM_PIPE_B          (1 << 29)
 #define BLC_PWM_CPU_CTL              0x48254
 
+#define BLM_HIST_CTL                 0x48260
+#define  ENH_HIST_ENABLE             (1<<31)
+#define  ENH_MODIF_TBL_ENABLE                (1<<30)
+#define  ENH_PIPE_A_SELECT           (0<<29)
+#define  ENH_PIPE_B_SELECT           (1<<29)
+#define  ENH_PIPE(pipe) _PIPE(pipe, ENH_PIPE_A_SELECT,

ENH_PIPE_B_SELECT)

Review

Quoted 20 lines
drivers/gpu/drm/i915/i915_reg.h +16 -0
+#define  HIST_MODE_YUV                       (0<<24)
+#define  HIST_MODE_HSV                       (1<<24)
+#define  ENH_MODE_DIRECT             (0<<13)
+#define  ENH_MODE_ADDITIVE           (1<<13)
+#define  ENH_MODE_MULTIPLICATIVE     (2<<13)
+#define  BIN_REGISTER_SET            (1<<11)
+#define  ENH_NUM_BINS                        32
+
+#define BLM_HIST_ENH                 0x48264
+
+#define BLM_HIST_GUARD_BAND          0x48268
+#define  BLM_HIST_INTR_ENABLE                (1<<31)
+#define  BLM_HIST_EVENT_STATUS               (1<<30)
+#define  BLM_HIST_INTR_DELAY_MASK    (0xFF<<22)
+#define  BLM_HIST_INTR_DELAY_SHIFT   22
+
 #define BLC_PWM_PCH_CTL1     0xc8250
 #define  PWM_PCH_ENABLE              (1 << 31)
 #define  PWM_POLARITY_ACTIVE_LOW     (1 << 29)
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_adaptive_backlight.c

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 5

Quoted 10 lines

new file mode 100644
index 0000000..4234962

drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person

obtaining a

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial

portions

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 19

Quoted 2 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +2 -0
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

NON-INFRINGEMENT.

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 21

Quoted 2 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +2 -0
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF

CONTRACT,

Quoted 12 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +12 -0
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+
+#include "drmP.h"
+#include "drm.h"
+#include "i915_drm.h"
+#include "i915_drv.h"
+#include "i915_reg.h"
+#include "intel_drv.h"

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+#include "intel_fixedpoint.h"
+
+/*
+ * This function takes a histogram of buckets as input and determines an
+ * acceptable target backlight level.
+ */
+static int histogram_find_correction_level(int *levels)
+{
+     int i, sum = 0;
+     int ratio, distortion, prev_distortion = 0, off, final_ratio,

target;

Quoted 19 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +19 -0
+
+     for (i = 0; i < ENH_NUM_BINS; i++)
+             sum += levels[i];
+
+     /* Allow 0.33/256 distortion per pixel, on average */
+     target = sum / 3;
+
+     /* Special case where we only have less than 100 pixels
+      * outside of the darkest bin.
+      */
+     if (sum - levels[0] <= 100)
+             return 70;
+
+     for (ratio = ENH_NUM_BINS - 1; ratio >= 0 ; ratio--) {
+             distortion = 0;
+             for (i = ratio; i < ENH_NUM_BINS; i++) {
+                     int pixel_distortion = (i - ratio)*8;
+                     int num_pixels = levels[i];
+                     distortion += num_pixels * pixel_distortion;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             }
+             if (distortion > target)
+                     break;
+             else
+                     prev_distortion = distortion;
+     }
+
+     ratio++;
+
+     /* If we're not exactly at the border between two buckets,

extrapolate

Quoted 11 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +11 -0
+      * to get 3 extra bits of accuracy.
+      */
+     if (distortion - prev_distortion)
+             off = 8 * (target - prev_distortion) /
+                   (distortion - prev_distortion);
+     else
+             off = 0;
+
+     final_ratio = ratio * 255 / 31 + off;
+
+     if (final_ratio > 255)

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             final_ratio = 255;
+
+     /* Never aim for less than 50% of the total backlight */
+     if (final_ratio < 128)
+             final_ratio = 128;
+
+     return final_ratio;
+}
+
+static void histogram_get_levels(struct drm_device *dev, int pipe, int

*levels)

Quoted 6 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +6 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int i;
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+                                      ENH_MODIF_TBL_ENABLE |
+                                      ENH_PIPE(pipe) |
+                                      HIST_MODE_YUV |
+                                      ENH_MODE_ADDITIVE |
+                                      i);
+             levels[i] = I915_READ(BLM_HIST_ENH);
+     }
+}
+
+static void histogram_set_levels(struct drm_device *dev, int pipe, int

*levels)

Quoted 8 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +8 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int i;
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             I915_WRITE(BLM_HIST_CTL, ENH_HIST_ENABLE |
+                                      ENH_MODIF_TBL_ENABLE |
+                                      ENH_PIPE(pipe) |

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+                                      HIST_MODE_YUV |
+                                      ENH_MODE_ADDITIVE |
+                                      BIN_REGISTER_SET |
+                                      i);
+             I915_WRITE(BLM_HIST_ENH, levels[i]);
+     }
+}
+
+/*
+ * This function computes the backlight correction level for an

acceptable

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 4 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +4 -0
+ * distortion and fills up the correction bins adequately.
+ */
+static void
+adaptive_backlight_compute_correction(struct drm_device *dev, int

*levels)

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int correction_level;
+     int i, multiplier, one_over_gamma;
+
+     /* Find the correction level for an acceptable distortion */
+     correction_level = histogram_find_correction_level(levels);
+
+     /* If we're not yet at our correction target, we need to decide by

how

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 9 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +9 -0
+      * much to move.
+      */
+     if (dev_priv->backlight_correction_level != correction_level) {
+             int delta, direction;
+
+             direction = (correction_level >
+                          dev_priv->backlight_correction_level);
+
+             if (direction == dev_priv->backlight_correction_direction)

{

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 4 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +4 -0
+                     dev_priv->backlight_correction_count++;
+             } else {
+                     dev_priv->backlight_correction_count = 0;
+                     dev_priv->backlight_correction_direction =

direction;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+             }
+
+             delta = abs(correction_level -
+                         dev_priv->backlight_correction_level)/4;
+
+             if (delta < 1)
+                     delta = 1;
+
+             /* For increasing the brightness, we do it instantly.
+              * For lowering the brightness, we require at least 10

frames

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 5 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
+              * below the current value. This avoids ping-ponging of the
+              * backlight level.
+              *
+              * We also never increase the backlight by more than 6% per
+              * frame, and never lower it by more than 3% per frame,

because

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 169

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+              * the backlight needs time to adjust and the LCD

correction

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+              * would be "ahead" otherwise.
+              */
+             if (correction_level >

dev_priv->backlight_correction_level) {

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 5 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +5 -0
+                     if (delta > 15)
+                             delta = 15;
+                     dev_priv->backlight_correction_level += delta;
+             } else if ((dev_priv->backlight_correction_count > 10) &&
+                (correction_level <

dev_priv->backlight_correction_level)) {

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 12 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +12 -0
+                     if (delta > 7)
+                             delta = 7;
+                     dev_priv->backlight_correction_level -= delta;
+             }
+     }
+
+     /* We need to invert the gamma correction of the LCD values,
+      * but not of the backlight which is linear.
+      */
+     one_over_gamma = intel_fixed_div(FIXED_ONE,
+                             dev_priv->adaptive_backlight_panel_gamma);
+     multiplier = intel_fixed_pow(dev_priv->backlight_correction_level

* 256,

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 11 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +11 -0
+                                  one_over_gamma);
+
+     for (i = 0; i < ENH_NUM_BINS; i++) {
+             int base_value = i * 8 * 4;
+             levels[i] = base_value - base_value * multiplier / 65536;
+     }
+}
+
+/*
+ * A quick note about the adaptive backlight implementation:
+ * If we let it run as designed, it will generate a lot of interrupts

which

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 201

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * tends to wake the CPU up and waste power. This is a bad idea for a

power

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 202

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * saving feature. Instead, we couple it to the vblank interrupt since

that

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 203

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * means we drew something. This means that we do not react to

non-vsynced GL

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 204

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+ * updates, or updates to the front buffer, and also adds a little bit

of

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 3 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +3 -0
+ * extra latency. But it is an acceptable tradeoff to make.
+ */
+void intel_adaptive_backlight(struct drm_device *dev, int

pipe_vblank_event)

Quoted 23 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +23 -0
+{
+     drm_i915_private_t *dev_priv = dev->dev_private;
+     int levels[ENH_NUM_BINS];
+     int pipe;
+     struct drm_connector *connector;
+     struct intel_crtc *intel_crtc;
+
+     if (!dev_priv->adaptive_backlight_enabled)
+             return;
+
+     /* Find the connector */
+     if (dev_priv->int_lvds_connector)
+             connector = dev_priv->int_lvds_connector;
+     else if (dev_priv->int_edp_connector)
+             connector = dev_priv->int_edp_connector;
+     else
+             return;
+
+     if (!connector)
+             return;
+
+     if (!connector->encoder)
+             return;

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+
+     if (!connector->encoder->crtc)
+             return;
+
+     /* Find the pipe for the panel. */
+     intel_crtc = to_intel_crtc(connector->encoder->crtc);
+     pipe = intel_crtc->pipe;
+
+     /* The callback happens for both pipe A & B. Now that we know which
+      * pipe we're doing adaptive backlight on, check that it's the

right

drivers/gpu/drm/i915/intel_adaptive_backlight.c

Quoted 10 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +10 -0
+      * one. Bail if it isn't.
+      */
+     if (pipe != pipe_vblank_event)
+             return;
+
+     /* Return if no event. */
+     if (I915_READ(BLM_HIST_GUARD_BAND) & BLM_HIST_EVENT_STATUS == 0)
+             return;
+
+     /* Make sure we ack the previous event. Even though we do not get

the

drivers/gpu/drm/i915/intel_adaptive_backlight.c line 251

Quoted 1 line
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +1 -0
+      * IRQs (see above explanation), we must still ack the events

otherwise

Review

Quoted 16 lines
drivers/gpu/drm/i915/intel_adaptive_backlight.c new file +15 -0
+      * the histogram data doesn't get updated any more.
+      */
+     I915_WRITE(BLM_HIST_GUARD_BAND, BLM_HIST_INTR_ENABLE |
+                                     BLM_HIST_EVENT_STATUS |
+                                     (1 << BLM_HIST_INTR_DELAY_SHIFT));
+
+     histogram_get_levels(dev, pipe, levels);
+
+     adaptive_backlight_compute_correction(dev, levels);
+
+     histogram_set_levels(dev, pipe, levels);
+
+     intel_panel_set_backlight(dev, dev_priv->backlight_level);
+}
+
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_fixedpoint.h

drivers/gpu/drm/i915/intel_fixedpoint.h line 5

Quoted 10 lines

new file mode 100644
index 0000000..fff5f0b

drivers/gpu/drm/i915/intel_fixedpoint.h new file +5 -0
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2012 The Chromium OS Authors.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person

obtaining a

drivers/gpu/drm/i915/intel_fixedpoint.h line 14

Quoted 9 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +9 -0
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial

portions

drivers/gpu/drm/i915/intel_fixedpoint.h line 17

Quoted 3 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +3 -0
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS

drivers/gpu/drm/i915/intel_fixedpoint.h line 19

Quoted 2 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +2 -0
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

NON-INFRINGEMENT.

drivers/gpu/drm/i915/intel_fixedpoint.h line 21

Quoted 2 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +2 -0
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF

CONTRACT,

drivers/gpu/drm/i915/intel_fixedpoint.h line 28

Quoted 7 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +7 -0
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+/*
+ * The backlight is corrected in linear space. However the LCD

correction is

drivers/gpu/drm/i915/intel_fixedpoint.h line 29

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * corrected in gama space. So to be able to compute the correction

value for

drivers/gpu/drm/i915/intel_fixedpoint.h line 30

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * the LCD, we have to compute the inverse gamma. To do so, we carry

this

drivers/gpu/drm/i915/intel_fixedpoint.h line 31

Quoted 1 line
drivers/gpu/drm/i915/intel_fixedpoint.h new file +1 -0
+ * small fixed point module which allows us to use pow() to compute

inverse

Review

Quoted 110 lines
drivers/gpu/drm/i915/intel_fixedpoint.h new file +109 -0
+ * gamma.
+ *
+ * The fixed point format used here is 16.16.
+ */
+
+/* intel_fixed_exp_tbl[x*32] = exp(x) * 65536 */
+static const int intel_fixed_exp_tbl[33] = {
+0x00010000, 0x00010820, 0x00011083, 0x00011929, 0x00012216, 0x00012b4b,
+0x000134cc, 0x00013e99, 0x000148b6, 0x00015325, 0x00015de9, 0x00016905,
+0x0001747a, 0x0001804d, 0x00018c80, 0x00019916, 0x0001a613, 0x0001b378,
+0x0001c14b, 0x0001cf8e, 0x0001de45, 0x0001ed74, 0x0001fd1e, 0x00020d47,
+0x00021df4, 0x00022f28, 0x000240e8, 0x00025338, 0x0002661d, 0x0002799b,
+0x00028db8, 0x0002a278, 0x0002b7e1
+};
+
+/* intel_fixed_log_tbl[x*32] = log(x) * 65536 */
+static const int intel_fixed_log_tbl[33] = {
+0x80000000, 0xfffc88c6, 0xfffd3a38, 0xfffda204, 0xfffdebaa, 0xfffe24ca,
+0xfffe5376, 0xfffe7aed, 0xfffe9d1c, 0xfffebb43, 0xfffed63c, 0xfffeeea2,
+0xffff04e8, 0xffff1966, 0xffff2c5f, 0xffff3e08, 0xffff4e8e, 0xffff5e13,
+0xffff6cb5, 0xffff7a8c, 0xffff87ae, 0xffff942b, 0xffffa014, 0xffffab75,
+0xffffb65a, 0xffffc0ce, 0xffffcad8, 0xffffd481, 0xffffddd1, 0xffffe6cd,
+0xffffef7a, 0xfffff7df, 0xffffffff
+};
+
+/* e * 65536 */
+#define FIXED_E (intel_fixed_exp_tbl[32])
+/* 1 * 65536 */
+#define FIXED_ONE 65536
+
+static int intel_fixed_mul(int a, int b)
+{
+     return (int) ((int64_t)a * (int64_t)b / 65536LL);
+}
+
+static int intel_fixed_div(int a, int b)
+{
+     return (int) ((int64_t)a * 65536LL / (int64_t)b);
+}
+
+/*
+ * Approximate fixed point log function.
+ * Only works for inputs in [0,1[
+ */
+static int intel_fixed_log(int val)
+{
+     int index = val * 32 / FIXED_ONE;
+     int remainder = (val & 0x7ff) << 5;
+     int v1 = intel_fixed_log_tbl[index];
+     int v2 = intel_fixed_log_tbl[index+1];
+     int final = v1 + intel_fixed_mul(v2 - v1, remainder);
+     return final;
+}
+
+/*
+ * Approximate fixed point exp function.
+ */
+static int intel_fixed_exp(int val)
+{
+     int count = 0;
+     int index, remainder;
+     int int_part = FIXED_ONE, frac_part;
+     int i, v, v1, v2;
+
+     while (val < 0) {
+             val += FIXED_ONE;
+             count--;
+     }
+
+     while (val > FIXED_ONE) {
+             val -= FIXED_ONE;
+             count++;
+     }
+
+     index = val * 32 / FIXED_ONE;
+     remainder = (val & 0x7ff) << 5;
+
+     v1 = intel_fixed_exp_tbl[index];
+     v2 = intel_fixed_exp_tbl[index+1];
+     frac_part = v1 + intel_fixed_mul(v2 - v1, remainder);
+
+     if (count < 0) {
+             for (i = 0; i < -count; i++)
+                     int_part = intel_fixed_mul(int_part, FIXED_E);
+
+             v = intel_fixed_div(frac_part, int_part);
+     } else {
+             for (i = 0; i < count; i++)
+                     int_part = intel_fixed_mul(int_part, FIXED_E);
+
+             v = intel_fixed_mul(frac_part, int_part);
+     }
+     return (v >= 0) ? v : 0;
+}
+
+/*
+ * Approximate fixed point pow function.
+ * Only works for x in [0,1[
+ */
+static int intel_fixed_pow(int x, int y)
+{
+     int e, p, r;
+     e = intel_fixed_log(x);
+     p = intel_fixed_mul(e, y);
+     r = intel_fixed_exp(p);
+     return r;
+}
+
+
(quoted diff) +0 -0

b/drivers/gpu/drm/i915/intel_panel.c

drivers/gpu/drm/i915/intel_panel.c static void

Quoted 4 lines

index 3f9249b..ae95163 100644

drivers/gpu/drm/i915/intel_panel.c +0 -0
@@ -251,6 +251,8 @@ static void

intel_panel_actually_set_backlight(struct drm_device *dev, u32 level

drivers/gpu/drm/i915/intel_panel.c static void intel_panel_init_backlight(struct

Quoted 9 lines
drivers/gpu/drm/i915/intel_panel.c +2 -0
      struct drm_i915_private *dev_priv = dev->dev_private;
      u32 tmp;
 
+     level = level * dev_priv->backlight_correction_level >> 8;
+
      DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
 
      if (HAS_PCH_SPLIT(dev))
@@ -306,6 +308,10 @@ static void intel_panel_init_backlight(struct

drm_device *dev)

Quoted 26 lines
drivers/gpu/drm/i915/intel_panel.c +4 -0
      dev_priv->backlight_level = intel_panel_get_backlight(dev);
      dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
+     dev_priv->adaptive_backlight_enabled = 0;
+     /* 2.2 as 16.16 fixed point */
+     dev_priv->adaptive_backlight_panel_gamma = 144179;
+     dev_priv->backlight_correction_level = 256;
 }
 
 enum drm_connector_status
include/drm/i915_drm.h +2 -1
index da929bb..70c9cfc 100644
@@ -309,6 +309,8 @@ typedef struct drm_i915_getparam {
 #define I915_SETPARAM_TEX_LRU_LOG_GRANULARITY             2
 #define I915_SETPARAM_ALLOW_BATCHBUFFER                   3
 #define I915_SETPARAM_NUM_USED_FENCES                     4
+#define I915_SETPARAM_ADAPTIVE_BACKLIGHT_ENABLE           5
+#define I915_SETPARAM_PANEL_GAMMA                         6
 
 typedef struct drm_i915_setparam {
      int param;
--

1.7.5.3.367.ga9930

Quoted 10 lines
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

--
Daniel Vetter
Mail: [email protected]
Mobile: +41 (0)79 365 57 48

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx