1/*	$NetBSD: drm_color_mgmt.h,v 1.2 2021/12/18 23:45:45 riastradh Exp $	*/
2
3/*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission.  The copyright holders make no representations
13 * about the suitability of this software for any purpose.  It is provided "as
14 * is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25#ifndef __DRM_COLOR_MGMT_H__
26#define __DRM_COLOR_MGMT_H__
27
28#include <linux/ctype.h>
29#include <drm/drm_property.h>
30
31struct drm_crtc;
32struct drm_plane;
33
34/**
35 * drm_color_lut_extract - clamp and round LUT entries
36 * @user_input: input value
37 * @bit_precision: number of bits the hw LUT supports
38 *
39 * Extract a degamma/gamma LUT value provided by user (in the form of
40 * &drm_color_lut entries) and round it to the precision supported by the
41 * hardware.
42 */
43static inline u32 drm_color_lut_extract(u32 user_input, int bit_precision)
44{
45	u32 val = user_input;
46	u32 max = 0xffff >> (16 - bit_precision);
47
48	/* Round only if we're not using full precision. */
49	if (bit_precision < 16) {
50		val += 1UL << (16 - bit_precision - 1);
51		val >>= 16 - bit_precision;
52	}
53
54	return clamp_val(val, 0, max);
55}
56
57u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n);
58
59void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
60				uint degamma_lut_size,
61				bool has_ctm,
62				uint gamma_lut_size);
63
64int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
65				 int gamma_size);
66
67/**
68 * drm_color_lut_size - calculate the number of entries in the LUT
69 * @blob: blob containing the LUT
70 *
71 * Returns:
72 * The number of entries in the color LUT stored in @blob.
73 */
74static inline int drm_color_lut_size(const struct drm_property_blob *blob)
75{
76	return blob->length / sizeof(struct drm_color_lut);
77}
78
79enum drm_color_encoding {
80	DRM_COLOR_YCBCR_BT601,
81	DRM_COLOR_YCBCR_BT709,
82	DRM_COLOR_YCBCR_BT2020,
83	DRM_COLOR_ENCODING_MAX,
84};
85
86enum drm_color_range {
87	DRM_COLOR_YCBCR_LIMITED_RANGE,
88	DRM_COLOR_YCBCR_FULL_RANGE,
89	DRM_COLOR_RANGE_MAX,
90};
91
92int drm_plane_create_color_properties(struct drm_plane *plane,
93				      u32 supported_encodings,
94				      u32 supported_ranges,
95				      enum drm_color_encoding default_encoding,
96				      enum drm_color_range default_range);
97
98/**
99 * enum drm_color_lut_tests - hw-specific LUT tests to perform
100 *
101 * The drm_color_lut_check() function takes a bitmask of the values here to
102 * determine which tests to apply to a userspace-provided LUT.
103 */
104enum drm_color_lut_tests {
105	/**
106	 * @DRM_COLOR_LUT_EQUAL_CHANNELS:
107	 *
108	 * Checks whether the entries of a LUT all have equal values for the
109	 * red, green, and blue channels.  Intended for hardware that only
110	 * accepts a single value per LUT entry and assumes that value applies
111	 * to all three color components.
112	 */
113	DRM_COLOR_LUT_EQUAL_CHANNELS = BIT(0),
114
115	/**
116	 * @DRM_COLOR_LUT_NON_DECREASING:
117	 *
118	 * Checks whether the entries of a LUT are always flat or increasing
119	 * (never decreasing).
120	 */
121	DRM_COLOR_LUT_NON_DECREASING = BIT(1),
122};
123
124int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests);
125#endif
126