xref: /openbmc/linux/include/drm/drm_color_mgmt.h (revision 8de8f000)
1f1e2f66cSDaniel Vetter /*
2f1e2f66cSDaniel Vetter  * Copyright (c) 2016 Intel Corporation
3f1e2f66cSDaniel Vetter  *
4f1e2f66cSDaniel Vetter  * Permission to use, copy, modify, distribute, and sell this software and its
5f1e2f66cSDaniel Vetter  * documentation for any purpose is hereby granted without fee, provided that
6f1e2f66cSDaniel Vetter  * the above copyright notice appear in all copies and that both that copyright
7f1e2f66cSDaniel Vetter  * notice and this permission notice appear in supporting documentation, and
8f1e2f66cSDaniel Vetter  * that the name of the copyright holders not be used in advertising or
9f1e2f66cSDaniel Vetter  * publicity pertaining to distribution of the software without specific,
10f1e2f66cSDaniel Vetter  * written prior permission.  The copyright holders make no representations
11f1e2f66cSDaniel Vetter  * about the suitability of this software for any purpose.  It is provided "as
12f1e2f66cSDaniel Vetter  * is" without express or implied warranty.
13f1e2f66cSDaniel Vetter  *
14f1e2f66cSDaniel Vetter  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15f1e2f66cSDaniel Vetter  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16f1e2f66cSDaniel Vetter  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17f1e2f66cSDaniel Vetter  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18f1e2f66cSDaniel Vetter  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19f1e2f66cSDaniel Vetter  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20f1e2f66cSDaniel Vetter  * OF THIS SOFTWARE.
21f1e2f66cSDaniel Vetter  */
22f1e2f66cSDaniel Vetter 
23f1e2f66cSDaniel Vetter #ifndef __DRM_COLOR_MGMT_H__
24f1e2f66cSDaniel Vetter #define __DRM_COLOR_MGMT_H__
25f1e2f66cSDaniel Vetter 
26f1e2f66cSDaniel Vetter #include <linux/ctype.h>
27*8de8f000SStephen Rothwell #include <linux/math64.h>
28b88ac005SDaniel Vetter #include <drm/drm_property.h>
29f1e2f66cSDaniel Vetter 
30d1841d32SJyri Sarha struct drm_crtc;
3180f690e9SJyri Sarha struct drm_plane;
32d1841d32SJyri Sarha 
3365b2f7c4SVille Syrjälä /**
3465b2f7c4SVille Syrjälä  * drm_color_lut_extract - clamp and round LUT entries
3565b2f7c4SVille Syrjälä  * @user_input: input value
3665b2f7c4SVille Syrjälä  * @bit_precision: number of bits the hw LUT supports
3765b2f7c4SVille Syrjälä  *
3865b2f7c4SVille Syrjälä  * Extract a degamma/gamma LUT value provided by user (in the form of
3965b2f7c4SVille Syrjälä  * &drm_color_lut entries) and round it to the precision supported by the
4065b2f7c4SVille Syrjälä  * hardware.
4165b2f7c4SVille Syrjälä  */
drm_color_lut_extract(u32 user_input,int bit_precision)4265b2f7c4SVille Syrjälä static inline u32 drm_color_lut_extract(u32 user_input, int bit_precision)
4365b2f7c4SVille Syrjälä {
4465b2f7c4SVille Syrjälä 	u32 val = user_input;
4565b2f7c4SVille Syrjälä 	u32 max = 0xffff >> (16 - bit_precision);
4665b2f7c4SVille Syrjälä 
4765b2f7c4SVille Syrjälä 	/* Round only if we're not using full precision. */
4865b2f7c4SVille Syrjälä 	if (bit_precision < 16) {
4965b2f7c4SVille Syrjälä 		val += 1UL << (16 - bit_precision - 1);
5065b2f7c4SVille Syrjälä 		val >>= 16 - bit_precision;
5165b2f7c4SVille Syrjälä 	}
5265b2f7c4SVille Syrjälä 
5365b2f7c4SVille Syrjälä 	return clamp_val(val, 0, max);
5465b2f7c4SVille Syrjälä }
5565b2f7c4SVille Syrjälä 
567ca7fcefSjames qian wang (Arm Technology China) u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n);
578f2e045eSJani Nikula 
58f1e2f66cSDaniel Vetter void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
59f1e2f66cSDaniel Vetter 				uint degamma_lut_size,
60f1e2f66cSDaniel Vetter 				bool has_ctm,
61f1e2f66cSDaniel Vetter 				uint gamma_lut_size);
62f1e2f66cSDaniel Vetter 
63f1e2f66cSDaniel Vetter int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
64f1e2f66cSDaniel Vetter 				 int gamma_size);
65f1e2f66cSDaniel Vetter 
6641204dfeSVille Syrjälä /**
6741204dfeSVille Syrjälä  * drm_color_lut_size - calculate the number of entries in the LUT
6841204dfeSVille Syrjälä  * @blob: blob containing the LUT
6941204dfeSVille Syrjälä  *
7041204dfeSVille Syrjälä  * Returns:
7141204dfeSVille Syrjälä  * The number of entries in the color LUT stored in @blob.
7241204dfeSVille Syrjälä  */
drm_color_lut_size(const struct drm_property_blob * blob)7341204dfeSVille Syrjälä static inline int drm_color_lut_size(const struct drm_property_blob *blob)
7441204dfeSVille Syrjälä {
7541204dfeSVille Syrjälä 	return blob->length / sizeof(struct drm_color_lut);
7641204dfeSVille Syrjälä }
7741204dfeSVille Syrjälä 
7880f690e9SJyri Sarha enum drm_color_encoding {
7980f690e9SJyri Sarha 	DRM_COLOR_YCBCR_BT601,
8080f690e9SJyri Sarha 	DRM_COLOR_YCBCR_BT709,
8180f690e9SJyri Sarha 	DRM_COLOR_YCBCR_BT2020,
8280f690e9SJyri Sarha 	DRM_COLOR_ENCODING_MAX,
8380f690e9SJyri Sarha };
8480f690e9SJyri Sarha 
8580f690e9SJyri Sarha enum drm_color_range {
8680f690e9SJyri Sarha 	DRM_COLOR_YCBCR_LIMITED_RANGE,
8780f690e9SJyri Sarha 	DRM_COLOR_YCBCR_FULL_RANGE,
8880f690e9SJyri Sarha 	DRM_COLOR_RANGE_MAX,
8980f690e9SJyri Sarha };
9080f690e9SJyri Sarha 
9180f690e9SJyri Sarha int drm_plane_create_color_properties(struct drm_plane *plane,
9280f690e9SJyri Sarha 				      u32 supported_encodings,
9380f690e9SJyri Sarha 				      u32 supported_ranges,
9480f690e9SJyri Sarha 				      enum drm_color_encoding default_encoding,
9580f690e9SJyri Sarha 				      enum drm_color_range default_range);
963c8861d8SMatt Roper 
973c8861d8SMatt Roper /**
983c8861d8SMatt Roper  * enum drm_color_lut_tests - hw-specific LUT tests to perform
993c8861d8SMatt Roper  *
1003c8861d8SMatt Roper  * The drm_color_lut_check() function takes a bitmask of the values here to
1013c8861d8SMatt Roper  * determine which tests to apply to a userspace-provided LUT.
1023c8861d8SMatt Roper  */
1033c8861d8SMatt Roper enum drm_color_lut_tests {
1043c8861d8SMatt Roper 	/**
1053c8861d8SMatt Roper 	 * @DRM_COLOR_LUT_EQUAL_CHANNELS:
1063c8861d8SMatt Roper 	 *
1073c8861d8SMatt Roper 	 * Checks whether the entries of a LUT all have equal values for the
1083c8861d8SMatt Roper 	 * red, green, and blue channels.  Intended for hardware that only
1093c8861d8SMatt Roper 	 * accepts a single value per LUT entry and assumes that value applies
1103c8861d8SMatt Roper 	 * to all three color components.
1113c8861d8SMatt Roper 	 */
1123c8861d8SMatt Roper 	DRM_COLOR_LUT_EQUAL_CHANNELS = BIT(0),
1133c8861d8SMatt Roper 
1143c8861d8SMatt Roper 	/**
1153c8861d8SMatt Roper 	 * @DRM_COLOR_LUT_NON_DECREASING:
1163c8861d8SMatt Roper 	 *
1173c8861d8SMatt Roper 	 * Checks whether the entries of a LUT are always flat or increasing
1183c8861d8SMatt Roper 	 * (never decreasing).
1193c8861d8SMatt Roper 	 */
1203c8861d8SMatt Roper 	DRM_COLOR_LUT_NON_DECREASING = BIT(1),
1213c8861d8SMatt Roper };
1223c8861d8SMatt Roper 
1235a3db6f0SVille Syrjälä int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests);
124f1e2f66cSDaniel Vetter #endif
125