While the drm_color_format_enum enum and the hdmi_colorspace enum have
similar values, they're not identical, and HDMI's enum is defined as per
the HDMI standard.

Meanwhile, each DRM_COLOR_FORMAT_* define has a corresponding
drm_color_format_enum, which allows conversion from the bitshifted
defines to the enum values.

Implement conversion functions from DRM_COLOR_FORMAT bitshifted defines
to drm_color_format_enum, and from hdmi_colorspace enum values to
drm_color_format_enum enum values.

In both conversions, an unexpected input results in a
DRM_COLOR_FORMAT_ENUM_INVALID result. The functions are kept inline
__pure to give the compiler maximum freedom to do as it pleases.

Co-developed-by: Marius Vlad <[email protected]>
Signed-off-by: Marius Vlad <[email protected]>
Signed-off-by: Nicolas Frattaroli <[email protected]>
---
 include/drm/drm_connector.h | 53 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/include/drm/drm_connector.h b/include/drm/drm_connector.h
index 18bd875b6918..886defdd069b 100644
--- a/include/drm/drm_connector.h
+++ b/include/drm/drm_connector.h
@@ -2694,6 +2694,59 @@ int drm_connector_attach_color_format_property(struct 
drm_connector *connector);
 
 const char *drm_get_color_format_name(enum drm_color_format_enum color_fmt);
 
+/**
+ * drm_color_format_to_enum - convert a single DRM_COLOR_FORMAT\_ to enum
+ * @fmt: One of the possible DRM_COLOR_FORMAT\_ values
+ *
+ * Converts a single DRM_COLOR_FORMAT\_ value to a corresponding
+ * &enum drm_color_format_enum value. Bitmasks of multiple DRM_COLOR_FORMAT\_
+ * values are not supported, as they would not map to a single enum value.
+ *
+ * Returns converted enum value on success, or %DRM_COLOR_FORMAT_ENUM_INVALID 
on
+ * failure.
+ */
+static inline enum drm_color_format_enum __pure
+drm_color_format_to_enum(u32 fmt)
+{
+       switch (fmt) {
+       case DRM_COLOR_FORMAT_RGB444:
+               return DRM_COLOR_FORMAT_ENUM_RGB444;
+       case DRM_COLOR_FORMAT_YCBCR444:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR444;
+       case DRM_COLOR_FORMAT_YCBCR422:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR422;
+       case DRM_COLOR_FORMAT_YCBCR420:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR420;
+       default:
+               return DRM_COLOR_FORMAT_ENUM_INVALID;
+       }
+}
+
+/**
+ * drm_color_format_enum_from_hdmi_colorspace - convert hdmi_colorspace enum to
+ *                                              drm_color_format_enum
+ * @fmt: The &enum hdmi_colorspace to convert
+ *
+ * Returns the converted result on success, or %DRM_COLOR_FORMAT_ENUM_INVALID 
on
+ * failure.
+ */
+static inline enum drm_color_format_enum __pure
+drm_color_format_enum_from_hdmi_colorspace(enum hdmi_colorspace fmt)
+{
+       switch (fmt) {
+       case HDMI_COLORSPACE_RGB:
+               return DRM_COLOR_FORMAT_ENUM_RGB444;
+       case HDMI_COLORSPACE_YUV444:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR444;
+       case HDMI_COLORSPACE_YUV422:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR422;
+       case HDMI_COLORSPACE_YUV420:
+               return DRM_COLOR_FORMAT_ENUM_YCBCR420;
+       default:
+               return DRM_COLOR_FORMAT_ENUM_INVALID;
+       }
+}
+
 /**
  * drm_for_each_connector_iter - connector_list iterator macro
  * @connector: &struct drm_connector pointer used as cursor

-- 
2.53.0

Reply via email to