2 * Copyright (c) 2016 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 #include <drm/drm_crtc.h>
25 #include <drm/drm_color_mgmt.h>
27 #include "drm_crtc_internal.h"
32 * Color management or color space adjustments is supported through a set of 5
33 * properties on the &drm_crtc object. They are set up by calling
34 * drm_crtc_enable_color_mgmt().
37 * Blob property to set the degamma lookup table (LUT) mapping pixel data
38 * from the framebuffer before it is given to the transformation matrix.
39 * The data is interpreted as an array of &struct drm_color_lut elements.
40 * Hardware might choose not to use the full precision of the LUT elements
41 * nor use all the elements of the LUT (for example the hardware might
42 * choose to interpolate between LUT[0] and LUT[4]).
44 * Setting this to NULL (blob property value set to 0) means a
45 * linear/pass-thru gamma table should be used. This is generally the
46 * driver boot-up state too. Drivers can access this blob through
47 * &drm_crtc_state.degamma_lut.
50 * Unsinged range property to give the size of the lookup table to be set
51 * on the DEGAMMA_LUT property (the size depends on the underlying
52 * hardware). If drivers support multiple LUT sizes then they should
53 * publish the largest size, and sub-sample smaller sized LUTs (e.g. for
54 * split-gamma modes) appropriately.
57 * Blob property to set the current transformation matrix (CTM) apply to
58 * pixel data after the lookup through the degamma LUT and before the
59 * lookup through the gamma LUT. The data is interpreted as a struct
62 * Setting this to NULL (blob property value set to 0) means a
63 * unit/pass-thru matrix should be used. This is generally the driver
64 * boot-up state too. Drivers can access the blob for the color conversion
65 * matrix through &drm_crtc_state.ctm.
68 * Blob property to set the gamma lookup table (LUT) mapping pixel data
69 * after the transformation matrix to data sent to the connector. The
70 * data is interpreted as an array of &struct drm_color_lut elements.
71 * Hardware might choose not to use the full precision of the LUT elements
72 * nor use all the elements of the LUT (for example the hardware might
73 * choose to interpolate between LUT[0] and LUT[4]).
75 * Setting this to NULL (blob property value set to 0) means a
76 * linear/pass-thru gamma table should be used. This is generally the
77 * driver boot-up state too. Drivers can access this blob through
78 * &drm_crtc_state.gamma_lut.
81 * Unsigned range property to give the size of the lookup table to be set
82 * on the GAMMA_LUT property (the size depends on the underlying hardware).
83 * If drivers support multiple LUT sizes then they should publish the
84 * largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
85 * modes) appropriately.
87 * There is also support for a legacy gamma table, which is set up by calling
88 * drm_mode_crtc_set_gamma_size(). Drivers which support both should use
89 * drm_atomic_helper_legacy_gamma_set() to alias the legacy gamma ramp with the
90 * "GAMMA_LUT" property above.
92 * Support for different non RGB color encodings is controlled through
93 * &drm_plane specific COLOR_ENCODING and COLOR_RANGE properties. They
94 * are set up by calling drm_plane_create_color_properties().
97 * Optional plane enum property to support different non RGB
98 * color encodings. The driver can provide a subset of standard
99 * enum values supported by the DRM plane.
102 * Optional plane enum property to support different non RGB
103 * color parameter ranges. The driver can provide a subset of
104 * standard enum values supported by the DRM plane.
108 * drm_color_lut_extract - clamp and round LUT entries
109 * @user_input: input value
110 * @bit_precision: number of bits the hw LUT supports
112 * Extract a degamma/gamma LUT value provided by user (in the form of
113 * &drm_color_lut entries) and round it to the precision supported by the
116 uint32_t drm_color_lut_extract(uint32_t user_input, uint32_t bit_precision)
118 uint32_t val = user_input;
119 uint32_t max = 0xffff >> (16 - bit_precision);
121 /* Round only if we're not using full precision. */
122 if (bit_precision < 16) {
123 val += 1UL << (16 - bit_precision - 1);
124 val >>= 16 - bit_precision;
127 return clamp_val(val, 0, max);
129 EXPORT_SYMBOL(drm_color_lut_extract);
132 * drm_crtc_enable_color_mgmt - enable color management properties
134 * @degamma_lut_size: the size of the degamma lut (before CSC)
135 * @has_ctm: whether to attach ctm_property for CSC matrix
136 * @gamma_lut_size: the size of the gamma lut (after CSC)
138 * This function lets the driver enable the color correction
139 * properties on a CRTC. This includes 3 degamma, csc and gamma
140 * properties that userspace can set and 2 size properties to inform
141 * the userspace of the lut sizes. Each of the properties are
142 * optional. The gamma and degamma properties are only attached if
143 * their size is not 0 and ctm_property is only attached if has_ctm is
146 * Drivers should use drm_atomic_helper_legacy_gamma_set() to implement the
147 * legacy &drm_crtc_funcs.gamma_set callback.
149 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
150 uint degamma_lut_size,
154 struct drm_device *dev = crtc->dev;
155 struct drm_mode_config *config = &dev->mode_config;
157 if (degamma_lut_size) {
158 drm_object_attach_property(&crtc->base,
159 config->degamma_lut_property, 0);
160 drm_object_attach_property(&crtc->base,
161 config->degamma_lut_size_property,
166 drm_object_attach_property(&crtc->base,
167 config->ctm_property, 0);
169 if (gamma_lut_size) {
170 drm_object_attach_property(&crtc->base,
171 config->gamma_lut_property, 0);
172 drm_object_attach_property(&crtc->base,
173 config->gamma_lut_size_property,
177 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
180 * drm_mode_crtc_set_gamma_size - set the gamma table size
181 * @crtc: CRTC to set the gamma table size for
182 * @gamma_size: size of the gamma table
184 * Drivers which support gamma tables should set this to the supported gamma
185 * table size when initializing the CRTC. Currently the drm core only supports a
186 * fixed gamma table size.
189 * Zero on success, negative errno on failure.
191 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
194 uint16_t *r_base, *g_base, *b_base;
197 crtc->gamma_size = gamma_size;
199 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
201 if (!crtc->gamma_store) {
202 crtc->gamma_size = 0;
206 r_base = crtc->gamma_store;
207 g_base = r_base + gamma_size;
208 b_base = g_base + gamma_size;
209 for (i = 0; i < gamma_size; i++) {
218 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
221 * drm_mode_gamma_set_ioctl - set the gamma table
224 * @file_priv: DRM file info
226 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
227 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
229 * Called by the user via ioctl.
232 * Zero on success, negative errno on failure.
234 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
235 void *data, struct drm_file *file_priv)
237 struct drm_mode_crtc_lut *crtc_lut = data;
238 struct drm_crtc *crtc;
239 void *r_base, *g_base, *b_base;
241 struct drm_modeset_acquire_ctx ctx;
244 if (!drm_core_check_feature(dev, DRIVER_MODESET))
247 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
251 if (crtc->funcs->gamma_set == NULL)
254 /* memcpy into gamma store */
255 if (crtc_lut->gamma_size != crtc->gamma_size)
258 drm_modeset_acquire_init(&ctx, 0);
260 ret = drm_modeset_lock_all_ctx(dev, &ctx);
264 size = crtc_lut->gamma_size * (sizeof(uint16_t));
265 r_base = crtc->gamma_store;
266 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
271 g_base = r_base + size;
272 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
277 b_base = g_base + size;
278 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
283 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
284 crtc->gamma_size, &ctx);
287 if (ret == -EDEADLK) {
288 drm_modeset_backoff(&ctx);
291 drm_modeset_drop_locks(&ctx);
292 drm_modeset_acquire_fini(&ctx);
299 * drm_mode_gamma_get_ioctl - get the gamma table
302 * @file_priv: DRM file info
304 * Copy the current gamma table into the storage provided. This also provides
305 * the gamma table size the driver expects, which can be used to size the
308 * Called by the user via ioctl.
311 * Zero on success, negative errno on failure.
313 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
314 void *data, struct drm_file *file_priv)
316 struct drm_mode_crtc_lut *crtc_lut = data;
317 struct drm_crtc *crtc;
318 void *r_base, *g_base, *b_base;
322 if (!drm_core_check_feature(dev, DRIVER_MODESET))
325 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
329 /* memcpy into gamma store */
330 if (crtc_lut->gamma_size != crtc->gamma_size)
333 drm_modeset_lock(&crtc->mutex, NULL);
334 size = crtc_lut->gamma_size * (sizeof(uint16_t));
335 r_base = crtc->gamma_store;
336 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
341 g_base = r_base + size;
342 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
347 b_base = g_base + size;
348 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
353 drm_modeset_unlock(&crtc->mutex);
357 static const char * const color_encoding_name[] = {
358 [DRM_COLOR_YCBCR_BT601] = "ITU-R BT.601 YCbCr",
359 [DRM_COLOR_YCBCR_BT709] = "ITU-R BT.709 YCbCr",
360 [DRM_COLOR_YCBCR_BT2020] = "ITU-R BT.2020 YCbCr",
363 static const char * const color_range_name[] = {
364 [DRM_COLOR_YCBCR_FULL_RANGE] = "YCbCr full range",
365 [DRM_COLOR_YCBCR_LIMITED_RANGE] = "YCbCr limited range",
369 * drm_get_color_encoding_name - return a string for color encoding
370 * @encoding: color encoding to compute name of
372 * In contrast to the other drm_get_*_name functions this one here returns a
373 * const pointer and hence is threadsafe.
375 const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)
377 if (WARN_ON(encoding >= ARRAY_SIZE(color_encoding_name)))
380 return color_encoding_name[encoding];
384 * drm_get_color_range_name - return a string for color range
385 * @range: color range to compute name of
387 * In contrast to the other drm_get_*_name functions this one here returns a
388 * const pointer and hence is threadsafe.
390 const char *drm_get_color_range_name(enum drm_color_range range)
392 if (WARN_ON(range >= ARRAY_SIZE(color_range_name)))
395 return color_range_name[range];
399 * drm_plane_create_color_properties - color encoding related plane properties
400 * @plane: plane object
401 * @supported_encodings: bitfield indicating supported color encodings
402 * @supported_ranges: bitfileld indicating supported color ranges
403 * @default_encoding: default color encoding
404 * @default_range: default color range
406 * Create and attach plane specific COLOR_ENCODING and COLOR_RANGE
407 * properties to @plane. The supported encodings and ranges should
408 * be provided in supported_encodings and supported_ranges bitmasks.
409 * Each bit set in the bitmask indicates that its number as enum
410 * value is supported.
412 int drm_plane_create_color_properties(struct drm_plane *plane,
413 u32 supported_encodings,
414 u32 supported_ranges,
415 enum drm_color_encoding default_encoding,
416 enum drm_color_range default_range)
418 struct drm_device *dev = plane->dev;
419 struct drm_property *prop;
420 struct drm_prop_enum_list enum_list[max_t(int, DRM_COLOR_ENCODING_MAX,
421 DRM_COLOR_RANGE_MAX)];
424 if (WARN_ON(supported_encodings == 0 ||
425 (supported_encodings & -BIT(DRM_COLOR_ENCODING_MAX)) != 0 ||
426 (supported_encodings & BIT(default_encoding)) == 0))
429 if (WARN_ON(supported_ranges == 0 ||
430 (supported_ranges & -BIT(DRM_COLOR_RANGE_MAX)) != 0 ||
431 (supported_ranges & BIT(default_range)) == 0))
435 for (i = 0; i < DRM_COLOR_ENCODING_MAX; i++) {
436 if ((supported_encodings & BIT(i)) == 0)
439 enum_list[len].type = i;
440 enum_list[len].name = color_encoding_name[i];
444 prop = drm_property_create_enum(dev, 0, "COLOR_ENCODING",
448 plane->color_encoding_property = prop;
449 drm_object_attach_property(&plane->base, prop, default_encoding);
451 plane->state->color_encoding = default_encoding;
454 for (i = 0; i < DRM_COLOR_RANGE_MAX; i++) {
455 if ((supported_ranges & BIT(i)) == 0)
458 enum_list[len].type = i;
459 enum_list[len].name = color_range_name[i];
463 prop = drm_property_create_enum(dev, 0, "COLOR_RANGE",
467 plane->color_range_property = prop;
468 drm_object_attach_property(&plane->base, prop, default_range);
470 plane->state->color_range = default_range;
474 EXPORT_SYMBOL(drm_plane_create_color_properties);