Split luminance min/max calculation using static hdr metadata from
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c:update_connector_ext_caps
into drm/drm_edid.c.

Cc: Rodrigo Siqueira <rodrigo.sique...@amd.com>
Cc: Harry Wentland <harry.wentl...@amd.com>
Cc: Lyude Paul <ly...@redhat.com>
Cc: Mika Kahola <mika.kah...@intel.com>
Cc: Jani Nikula <jani.nik...@intel.com>
Signed-off-by: Jouni Högander <jouni.hogan...@intel.com>
---
 drivers/gpu/drm/drm_edid.c | 55 ++++++++++++++++++++++++++++++++++++++
 include/drm/drm_edid.h     |  4 +++
 2 files changed, 59 insertions(+)

diff --git a/drivers/gpu/drm/drm_edid.c b/drivers/gpu/drm/drm_edid.c
index 7a8482b75071..1cb886debbbe 100644
--- a/drivers/gpu/drm/drm_edid.c
+++ b/drivers/gpu/drm/drm_edid.c
@@ -4005,6 +4005,61 @@ drm_display_mode_from_cea_vic(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drm_display_mode_from_cea_vic);
 
+/**
+ * drm_luminance_range_from_static_hdr_metadata() - luminance range from 
static hdr
+ * metadata
+ * @connector: connector we're calculating for
+ * @min: Calculated min value
+ * @max: Calculated max value
+ *
+ * Calculates backlight min and max as described in CTA-861-G
+ *
+ * Returns: True when calculation succeeds.
+ */
+bool
+drm_luminance_range_from_static_hdr_metadata(struct drm_connector *connector,
+                                            u32 *min, u32 *max)
+{
+       struct hdr_sink_metadata *hdr_metadata = &connector->hdr_sink_metadata;
+       static const u8 pre_computed_values[] = {
+               50, 51, 52, 53, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 68, 69,
+               71, 72, 74, 75, 77, 79, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98};
+       u32 min_cll, max_cll, q, r;
+
+       if (!(hdr_metadata->hdmi_type1.metadata_type &
+             BIT(HDMI_STATIC_METADATA_TYPE1)))
+               return false;
+
+       max_cll = hdr_metadata->hdmi_type1.max_cll;
+       min_cll = hdr_metadata->hdmi_type1.min_cll;
+
+       /* From the specification (CTA-861-G), for calculating the maximum
+        * luminance we need to use:
+        *      Luminance = 50*2**(CV/32)
+        * Where CV is a one-byte value.
+        * For calculating this expression we may need float point precision;
+        * to avoid this complexity level, we take advantage that CV is divided
+        * by a constant. From the Euclids division algorithm, we know that CV
+        * can be written as: CV = 32*q + r. Next, we replace CV in the
+        * Luminance expression and get 50*(2**q)*(2**(r/32)), hence we just
+        * need to pre-compute the value of r/32. For pre-computing the values
+        * We just used the following Ruby line:
+        *      (0...32).each {|cv| puts (50*2**(cv/32.0)).round}
+        * The results of the above expressions can be verified at
+        * pre_computed_values.
+        */
+       q = max_cll >> 5;
+       r = max_cll % 32;
+       *max = (1 << q) * pre_computed_values[r];
+
+       /* min luminance: maxLum * (CV/255)^2 / 100 */
+       q = DIV_ROUND_CLOSEST(min_cll, 255);
+       *min = *max * DIV_ROUND_CLOSEST((q * q), 100);
+
+       return true;
+}
+EXPORT_SYMBOL(drm_luminance_range_from_static_hdr_metadata);
+
 static int
 do_cea_modes(struct drm_connector *connector, const u8 *db, u8 len)
 {
diff --git a/include/drm/drm_edid.h b/include/drm/drm_edid.h
index c3204a58fb09..63e441c84d72 100644
--- a/include/drm/drm_edid.h
+++ b/include/drm/drm_edid.h
@@ -406,6 +406,10 @@ drm_hdmi_avi_infoframe_quant_range(struct 
hdmi_avi_infoframe *frame,
                                   const struct drm_display_mode *mode,
                                   enum hdmi_quantization_range 
rgb_quant_range);
 
+bool
+drm_luminance_range_from_static_hdr_metadata(struct drm_connector *connector,
+                                            u32 *min, u32 *max);
+
 /**
  * drm_eld_mnl - Get ELD monitor name length in bytes.
  * @eld: pointer to an eld memory structure with mnl set
-- 
2.25.1

Reply via email to