diff mbox series

[2/3] drm/komeda: Add drm_ctm_to_coeffs()

Message ID 20190930045408.8053-3-james.qian.wang@arm.com (mailing list archive)
State New, archived
Headers show
Series drm/komeda: Enable CRTC color-mgmt | expand

Commit Message

James Qian Wang Sept. 30, 2019, 4:54 a.m. UTC
This function is used to convert drm_color_ctm S31.32 sign-magnitude
value to komeda required Q2.12 2's complement

Signed-off-by: james qian wang (Arm Technology China) <james.qian.wang@arm.com>
---
 .../arm/display/komeda/komeda_color_mgmt.c    | 27 +++++++++++++++++++
 .../arm/display/komeda/komeda_color_mgmt.h    |  1 +
 2 files changed, 28 insertions(+)

Comments

Brian Starkey Sept. 30, 2019, 11:04 a.m. UTC | #1
Hi James,

On Mon, Sep 30, 2019 at 04:54:41AM +0000, james qian wang (Arm Technology China) wrote:
> This function is used to convert drm_color_ctm S31.32 sign-magnitude
> value to komeda required Q2.12 2's complement
> 
> Signed-off-by: james qian wang (Arm Technology China) <james.qian.wang@arm.com>
> ---
>  .../arm/display/komeda/komeda_color_mgmt.c    | 27 +++++++++++++++++++
>  .../arm/display/komeda/komeda_color_mgmt.h    |  1 +
>  2 files changed, 28 insertions(+)
> 
> diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> index c180ce70c26c..c92c82eebddb 100644
> --- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> +++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> @@ -117,3 +117,30 @@ void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs)
>  {
>  	drm_lut_to_coeffs(lut_blob, coeffs, sector_tbl, ARRAY_SIZE(sector_tbl));
>  }
> +
> +/* Convert from S31.32 sign-magnitude to HW Q2.12 2's complement */
> +static s32 drm_ctm_s31_32_to_q2_12(u64 input)
> +{
> +	u64 mag = (input & ~BIT_ULL(63)) >> 20;
> +	bool negative = !!(input & BIT_ULL(63));
> +	u32 val;
> +
> +	/* the range of signed 2s complement is [-2^n, 2^n - 1] */
> +	val = clamp_val(mag, 0, negative ? BIT(14) : BIT(14) - 1);
> +
> +	return negative ? 0 - val : val;
> +}

This function looks generally useful. Should it be in DRM core
(assuming there isn't already one there)?

You can use a parameter to determine the number of bits desired in the
output.

Thanks,
-Brian

> +
> +void drm_ctm_to_coeffs(struct drm_property_blob *ctm_blob, u32 *coeffs)
> +{
> +	struct drm_color_ctm *ctm;
> +	u32 i;
> +
> +	if (!ctm_blob)
> +		return;
> +
> +	ctm = ctm_blob->data;
> +
> +	for (i = 0; i < KOMEDA_N_CTM_COEFFS; i++)
> +		coeffs[i] = drm_ctm_s31_32_to_q2_12(ctm->matrix[i]);
> +}
> diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
> index 08ab69281648..2f4668466112 100644
> --- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
> +++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
> @@ -18,6 +18,7 @@
>  #define KOMEDA_N_CTM_COEFFS		9
>  
>  void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs);
> +void drm_ctm_to_coeffs(struct drm_property_blob *ctm_blob, u32 *coeffs);
>  
>  const s32 *komeda_select_yuv2rgb_coeffs(u32 color_encoding, u32 color_range);
>  
> -- 
> 2.20.1
>
Ilia Mirkin Sept. 30, 2019, 11:56 a.m. UTC | #2
On Mon, Sep 30, 2019 at 7:05 AM Brian Starkey <Brian.Starkey@arm.com> wrote:
>
> Hi James,
>
> On Mon, Sep 30, 2019 at 04:54:41AM +0000, james qian wang (Arm Technology China) wrote:
> > This function is used to convert drm_color_ctm S31.32 sign-magnitude
> > value to komeda required Q2.12 2's complement
> >
> > Signed-off-by: james qian wang (Arm Technology China) <james.qian.wang@arm.com>
> > ---
> >  .../arm/display/komeda/komeda_color_mgmt.c    | 27 +++++++++++++++++++
> >  .../arm/display/komeda/komeda_color_mgmt.h    |  1 +
> >  2 files changed, 28 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > index c180ce70c26c..c92c82eebddb 100644
> > --- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > +++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > @@ -117,3 +117,30 @@ void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs)
> >  {
> >       drm_lut_to_coeffs(lut_blob, coeffs, sector_tbl, ARRAY_SIZE(sector_tbl));
> >  }
> > +
> > +/* Convert from S31.32 sign-magnitude to HW Q2.12 2's complement */
> > +static s32 drm_ctm_s31_32_to_q2_12(u64 input)
> > +{
> > +     u64 mag = (input & ~BIT_ULL(63)) >> 20;
> > +     bool negative = !!(input & BIT_ULL(63));
> > +     u32 val;
> > +
> > +     /* the range of signed 2s complement is [-2^n, 2^n - 1] */
> > +     val = clamp_val(mag, 0, negative ? BIT(14) : BIT(14) - 1);
> > +
> > +     return negative ? 0 - val : val;
> > +}
>
> This function looks generally useful. Should it be in DRM core
> (assuming there isn't already one there)?
>
> You can use a parameter to determine the number of bits desired in the
> output.

I suspect every driver needs to do something similar. You can see what
I did for nouveau here:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=88b703527ba70659365d989f29579f1292ebf9c3

(look for csc_drm_to_base)

Would be great to have a common helper which correctly accounts for
all the variability.

Cheers,

  -ilia
Daniel Vetter Oct. 9, 2019, 3:10 p.m. UTC | #3
On Mon, Sep 30, 2019 at 07:56:13AM -0400, Ilia Mirkin wrote:
> On Mon, Sep 30, 2019 at 7:05 AM Brian Starkey <Brian.Starkey@arm.com> wrote:
> >
> > Hi James,
> >
> > On Mon, Sep 30, 2019 at 04:54:41AM +0000, james qian wang (Arm Technology China) wrote:
> > > This function is used to convert drm_color_ctm S31.32 sign-magnitude
> > > value to komeda required Q2.12 2's complement
> > >
> > > Signed-off-by: james qian wang (Arm Technology China) <james.qian.wang@arm.com>
> > > ---
> > >  .../arm/display/komeda/komeda_color_mgmt.c    | 27 +++++++++++++++++++
> > >  .../arm/display/komeda/komeda_color_mgmt.h    |  1 +
> > >  2 files changed, 28 insertions(+)
> > >
> > > diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > > index c180ce70c26c..c92c82eebddb 100644
> > > --- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > > +++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
> > > @@ -117,3 +117,30 @@ void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs)
> > >  {
> > >       drm_lut_to_coeffs(lut_blob, coeffs, sector_tbl, ARRAY_SIZE(sector_tbl));
> > >  }
> > > +
> > > +/* Convert from S31.32 sign-magnitude to HW Q2.12 2's complement */
> > > +static s32 drm_ctm_s31_32_to_q2_12(u64 input)
> > > +{
> > > +     u64 mag = (input & ~BIT_ULL(63)) >> 20;
> > > +     bool negative = !!(input & BIT_ULL(63));
> > > +     u32 val;
> > > +
> > > +     /* the range of signed 2s complement is [-2^n, 2^n - 1] */
> > > +     val = clamp_val(mag, 0, negative ? BIT(14) : BIT(14) - 1);
> > > +
> > > +     return negative ? 0 - val : val;
> > > +}
> >
> > This function looks generally useful. Should it be in DRM core
> > (assuming there isn't already one there)?
> >
> > You can use a parameter to determine the number of bits desired in the
> > output.
> 
> I suspect every driver needs to do something similar. You can see what
> I did for nouveau here:
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=88b703527ba70659365d989f29579f1292ebf9c3
> 
> (look for csc_drm_to_base)
> 
> Would be great to have a common helper which correctly accounts for
> all the variability.

Yeah the sign-bit 31.32 fixed point format is probably the most ludicrous
uapi fumble we've ever done. A shared function, rolled out to drivers, to
switch it to a signed 2 complements integer sounds like a _very_ good
idea.
-Daniel
diff mbox series

Patch

diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
index c180ce70c26c..c92c82eebddb 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.c
@@ -117,3 +117,30 @@  void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs)
 {
 	drm_lut_to_coeffs(lut_blob, coeffs, sector_tbl, ARRAY_SIZE(sector_tbl));
 }
+
+/* Convert from S31.32 sign-magnitude to HW Q2.12 2's complement */
+static s32 drm_ctm_s31_32_to_q2_12(u64 input)
+{
+	u64 mag = (input & ~BIT_ULL(63)) >> 20;
+	bool negative = !!(input & BIT_ULL(63));
+	u32 val;
+
+	/* the range of signed 2s complement is [-2^n, 2^n - 1] */
+	val = clamp_val(mag, 0, negative ? BIT(14) : BIT(14) - 1);
+
+	return negative ? 0 - val : val;
+}
+
+void drm_ctm_to_coeffs(struct drm_property_blob *ctm_blob, u32 *coeffs)
+{
+	struct drm_color_ctm *ctm;
+	u32 i;
+
+	if (!ctm_blob)
+		return;
+
+	ctm = ctm_blob->data;
+
+	for (i = 0; i < KOMEDA_N_CTM_COEFFS; i++)
+		coeffs[i] = drm_ctm_s31_32_to_q2_12(ctm->matrix[i]);
+}
diff --git a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
index 08ab69281648..2f4668466112 100644
--- a/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
+++ b/drivers/gpu/drm/arm/display/komeda/komeda_color_mgmt.h
@@ -18,6 +18,7 @@ 
 #define KOMEDA_N_CTM_COEFFS		9
 
 void drm_lut_to_fgamma_coeffs(struct drm_property_blob *lut_blob, u32 *coeffs);
+void drm_ctm_to_coeffs(struct drm_property_blob *ctm_blob, u32 *coeffs);
 
 const s32 *komeda_select_yuv2rgb_coeffs(u32 color_encoding, u32 color_range);