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.
49 * Unsinged range property to give the size of the lookup table to be set
50 * on the DEGAMMA_LUT property (the size depends on the underlying
51 * hardware). If drivers support multiple LUT sizes then they should
52 * publish the largest size, and sub-sample smaller sized LUTs (e.g. for
53 * split-gamma modes) appropriately.
56 * Blob property to set the current transformation matrix (CTM) apply to
57 * pixel data after the lookup through the degamma LUT and before the
58 * lookup through the gamma LUT. The data is interpreted as a struct
61 * Setting this to NULL (blob property value set to 0) means a
62 * unit/pass-thru matrix should be used. This is generally the driver
66 * Blob property to set the gamma lookup table (LUT) mapping pixel data
67 * after the transformation matrix to data sent to the connector. The
68 * data is interpreted as an array of struct &drm_color_lut elements.
69 * Hardware might choose not to use the full precision of the LUT elements
70 * nor use all the elements of the LUT (for example the hardware might
71 * choose to interpolate between LUT[0] and LUT[4]).
73 * Setting this to NULL (blob property value set to 0) means a
74 * linear/pass-thru gamma table should be used. This is generally the
75 * driver boot-up state too.
78 * Unsigned range property to give the size of the lookup table to be set
79 * on the GAMMA_LUT property (the size depends on the underlying hardware).
80 * If drivers support multiple LUT sizes then they should publish the
81 * largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
82 * modes) appropriately.
84 * There is also support for a legacy gamma table, which is set up by calling
85 * drm_mode_crtc_set_gamma_size(). Drivers which support both should use
86 * drm_atomic_helper_legacy_gamma_set() to alias the legacy gamma ramp with the
87 * "GAMMA_LUT" property above.
91 * drm_crtc_enable_color_mgmt - enable color management properties
93 * @degamma_lut_size: the size of the degamma lut (before CSC)
94 * @has_ctm: whether to attach ctm_property for CSC matrix
95 * @gamma_lut_size: the size of the gamma lut (after CSC)
97 * This function lets the driver enable the color correction
98 * properties on a CRTC. This includes 3 degamma, csc and gamma
99 * properties that userspace can set and 2 size properties to inform
100 * the userspace of the lut sizes. Each of the properties are
101 * optional. The gamma and degamma properties are only attached if
102 * their size is not 0 and ctm_property is only attached if has_ctm is
105 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
106 uint degamma_lut_size,
110 struct drm_device *dev = crtc->dev;
111 struct drm_mode_config *config = &dev->mode_config;
113 if (degamma_lut_size) {
114 drm_object_attach_property(&crtc->base,
115 config->degamma_lut_property, 0);
116 drm_object_attach_property(&crtc->base,
117 config->degamma_lut_size_property,
122 drm_object_attach_property(&crtc->base,
123 config->ctm_property, 0);
125 if (gamma_lut_size) {
126 drm_object_attach_property(&crtc->base,
127 config->gamma_lut_property, 0);
128 drm_object_attach_property(&crtc->base,
129 config->gamma_lut_size_property,
133 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
136 * drm_mode_crtc_set_gamma_size - set the gamma table size
137 * @crtc: CRTC to set the gamma table size for
138 * @gamma_size: size of the gamma table
140 * Drivers which support gamma tables should set this to the supported gamma
141 * table size when initializing the CRTC. Currently the drm core only supports a
142 * fixed gamma table size.
145 * Zero on success, negative errno on failure.
147 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
150 uint16_t *r_base, *g_base, *b_base;
153 crtc->gamma_size = gamma_size;
155 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
157 if (!crtc->gamma_store) {
158 crtc->gamma_size = 0;
162 r_base = crtc->gamma_store;
163 g_base = r_base + gamma_size;
164 b_base = g_base + gamma_size;
165 for (i = 0; i < gamma_size; i++) {
174 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
177 * drm_mode_gamma_set_ioctl - set the gamma table
180 * @file_priv: DRM file info
182 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
183 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
185 * Called by the user via ioctl.
188 * Zero on success, negative errno on failure.
190 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
191 void *data, struct drm_file *file_priv)
193 struct drm_mode_crtc_lut *crtc_lut = data;
194 struct drm_crtc *crtc;
195 void *r_base, *g_base, *b_base;
199 if (!drm_core_check_feature(dev, DRIVER_MODESET))
202 drm_modeset_lock_all(dev);
203 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
209 if (crtc->funcs->gamma_set == NULL) {
214 /* memcpy into gamma store */
215 if (crtc_lut->gamma_size != crtc->gamma_size) {
220 size = crtc_lut->gamma_size * (sizeof(uint16_t));
221 r_base = crtc->gamma_store;
222 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
227 g_base = r_base + size;
228 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
233 b_base = g_base + size;
234 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
239 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
242 drm_modeset_unlock_all(dev);
248 * drm_mode_gamma_get_ioctl - get the gamma table
251 * @file_priv: DRM file info
253 * Copy the current gamma table into the storage provided. This also provides
254 * the gamma table size the driver expects, which can be used to size the
257 * Called by the user via ioctl.
260 * Zero on success, negative errno on failure.
262 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
263 void *data, struct drm_file *file_priv)
265 struct drm_mode_crtc_lut *crtc_lut = data;
266 struct drm_crtc *crtc;
267 void *r_base, *g_base, *b_base;
271 if (!drm_core_check_feature(dev, DRIVER_MODESET))
274 drm_modeset_lock_all(dev);
275 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
281 /* memcpy into gamma store */
282 if (crtc_lut->gamma_size != crtc->gamma_size) {
287 size = crtc_lut->gamma_size * (sizeof(uint16_t));
288 r_base = crtc->gamma_store;
289 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
294 g_base = r_base + size;
295 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
300 b_base = g_base + size;
301 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
306 drm_modeset_unlock_all(dev);