Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / drm_color_mgmt.c
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #include <linux/uaccess.h>
24
25 #include <drm/drm_atomic.h>
26 #include <drm/drm_color_mgmt.h>
27 #include <drm/drm_crtc.h>
28 #include <drm/drm_device.h>
29 #include <drm/drm_drv.h>
30 #include <drm/drm_print.h>
31
32 #include "drm_crtc_internal.h"
33
34 /**
35  * DOC: overview
36  *
37  * Color management or color space adjustments is supported through a set of 5
38  * properties on the &drm_crtc object. They are set up by calling
39  * drm_crtc_enable_color_mgmt().
40  *
41  * "DEGAMMA_LUT”:
42  *      Blob property to set the degamma lookup table (LUT) mapping pixel data
43  *      from the framebuffer before it is given to the transformation matrix.
44  *      The data is interpreted as an array of &struct drm_color_lut elements.
45  *      Hardware might choose not to use the full precision of the LUT elements
46  *      nor use all the elements of the LUT (for example the hardware might
47  *      choose to interpolate between LUT[0] and LUT[4]).
48  *
49  *      Setting this to NULL (blob property value set to 0) means a
50  *      linear/pass-thru gamma table should be used. This is generally the
51  *      driver boot-up state too. Drivers can access this blob through
52  *      &drm_crtc_state.degamma_lut.
53  *
54  * “DEGAMMA_LUT_SIZE”:
55  *      Unsinged range property to give the size of the lookup table to be set
56  *      on the DEGAMMA_LUT property (the size depends on the underlying
57  *      hardware). If drivers support multiple LUT sizes then they should
58  *      publish the largest size, and sub-sample smaller sized LUTs (e.g. for
59  *      split-gamma modes) appropriately.
60  *
61  * “CTM”:
62  *      Blob property to set the current transformation matrix (CTM) apply to
63  *      pixel data after the lookup through the degamma LUT and before the
64  *      lookup through the gamma LUT. The data is interpreted as a struct
65  *      &drm_color_ctm.
66  *
67  *      Setting this to NULL (blob property value set to 0) means a
68  *      unit/pass-thru matrix should be used. This is generally the driver
69  *      boot-up state too. Drivers can access the blob for the color conversion
70  *      matrix through &drm_crtc_state.ctm.
71  *
72  * “GAMMA_LUT”:
73  *      Blob property to set the gamma lookup table (LUT) mapping pixel data
74  *      after the transformation matrix to data sent to the connector. The
75  *      data is interpreted as an array of &struct drm_color_lut elements.
76  *      Hardware might choose not to use the full precision of the LUT elements
77  *      nor use all the elements of the LUT (for example the hardware might
78  *      choose to interpolate between LUT[0] and LUT[4]).
79  *
80  *      Setting this to NULL (blob property value set to 0) means a
81  *      linear/pass-thru gamma table should be used. This is generally the
82  *      driver boot-up state too. Drivers can access this blob through
83  *      &drm_crtc_state.gamma_lut.
84  *
85  * “GAMMA_LUT_SIZE”:
86  *      Unsigned range property to give the size of the lookup table to be set
87  *      on the GAMMA_LUT property (the size depends on the underlying hardware).
88  *      If drivers support multiple LUT sizes then they should publish the
89  *      largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
90  *      modes) appropriately.
91  *
92  * There is also support for a legacy gamma table, which is set up by calling
93  * drm_mode_crtc_set_gamma_size(). The DRM core will then alias the legacy gamma
94  * ramp with "GAMMA_LUT" or, if that is unavailable, "DEGAMMA_LUT".
95  *
96  * Support for different non RGB color encodings is controlled through
97  * &drm_plane specific COLOR_ENCODING and COLOR_RANGE properties. They
98  * are set up by calling drm_plane_create_color_properties().
99  *
100  * "COLOR_ENCODING":
101  *      Optional plane enum property to support different non RGB
102  *      color encodings. The driver can provide a subset of standard
103  *      enum values supported by the DRM plane.
104  *
105  * "COLOR_RANGE":
106  *      Optional plane enum property to support different non RGB
107  *      color parameter ranges. The driver can provide a subset of
108  *      standard enum values supported by the DRM plane.
109  */
110
111 /**
112  * drm_color_ctm_s31_32_to_qm_n
113  *
114  * @user_input: input value
115  * @m: number of integer bits, only support m <= 32, include the sign-bit
116  * @n: number of fractional bits, only support n <= 32
117  *
118  * Convert and clamp S31.32 sign-magnitude to Qm.n (signed 2's complement).
119  * The sign-bit BIT(m+n-1) and above are 0 for positive value and 1 for negative
120  * the range of value is [-2^(m-1), 2^(m-1) - 2^-n]
121  *
122  * For example
123  * A Q3.12 format number:
124  * - required bit: 3 + 12 = 15bits
125  * - range: [-2^2, 2^2 - 2^−15]
126  *
127  * NOTE: the m can be zero if all bit_precision are used to present fractional
128  *       bits like Q0.32
129  */
130 u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n)
131 {
132         u64 mag = (user_input & ~BIT_ULL(63)) >> (32 - n);
133         bool negative = !!(user_input & BIT_ULL(63));
134         s64 val;
135
136         WARN_ON(m > 32 || n > 32);
137
138         val = clamp_val(mag, 0, negative ?
139                                 BIT_ULL(n + m - 1) : BIT_ULL(n + m - 1) - 1);
140
141         return negative ? -val : val;
142 }
143 EXPORT_SYMBOL(drm_color_ctm_s31_32_to_qm_n);
144
145 /**
146  * drm_crtc_enable_color_mgmt - enable color management properties
147  * @crtc: DRM CRTC
148  * @degamma_lut_size: the size of the degamma lut (before CSC)
149  * @has_ctm: whether to attach ctm_property for CSC matrix
150  * @gamma_lut_size: the size of the gamma lut (after CSC)
151  *
152  * This function lets the driver enable the color correction
153  * properties on a CRTC. This includes 3 degamma, csc and gamma
154  * properties that userspace can set and 2 size properties to inform
155  * the userspace of the lut sizes. Each of the properties are
156  * optional. The gamma and degamma properties are only attached if
157  * their size is not 0 and ctm_property is only attached if has_ctm is
158  * true.
159  */
160 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
161                                 uint degamma_lut_size,
162                                 bool has_ctm,
163                                 uint gamma_lut_size)
164 {
165         struct drm_device *dev = crtc->dev;
166         struct drm_mode_config *config = &dev->mode_config;
167
168         if (degamma_lut_size) {
169                 drm_object_attach_property(&crtc->base,
170                                            config->degamma_lut_property, 0);
171                 drm_object_attach_property(&crtc->base,
172                                            config->degamma_lut_size_property,
173                                            degamma_lut_size);
174         }
175
176         if (has_ctm)
177                 drm_object_attach_property(&crtc->base,
178                                            config->ctm_property, 0);
179
180         if (gamma_lut_size) {
181                 drm_object_attach_property(&crtc->base,
182                                            config->gamma_lut_property, 0);
183                 drm_object_attach_property(&crtc->base,
184                                            config->gamma_lut_size_property,
185                                            gamma_lut_size);
186         }
187 }
188 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
189
190 /**
191  * drm_mode_crtc_set_gamma_size - set the gamma table size
192  * @crtc: CRTC to set the gamma table size for
193  * @gamma_size: size of the gamma table
194  *
195  * Drivers which support gamma tables should set this to the supported gamma
196  * table size when initializing the CRTC. Currently the drm core only supports a
197  * fixed gamma table size.
198  *
199  * Returns:
200  * Zero on success, negative errno on failure.
201  */
202 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
203                                  int gamma_size)
204 {
205         uint16_t *r_base, *g_base, *b_base;
206         int i;
207
208         crtc->gamma_size = gamma_size;
209
210         crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
211                                     GFP_KERNEL);
212         if (!crtc->gamma_store) {
213                 crtc->gamma_size = 0;
214                 return -ENOMEM;
215         }
216
217         r_base = crtc->gamma_store;
218         g_base = r_base + gamma_size;
219         b_base = g_base + gamma_size;
220         for (i = 0; i < gamma_size; i++) {
221                 r_base[i] = i << 8;
222                 g_base[i] = i << 8;
223                 b_base[i] = i << 8;
224         }
225
226
227         return 0;
228 }
229 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
230
231 /**
232  * drm_crtc_supports_legacy_gamma - does the crtc support legacy gamma correction table
233  * @crtc: CRTC object
234  *
235  * Returns true/false if the given crtc supports setting the legacy gamma
236  * correction table.
237  */
238 static bool drm_crtc_supports_legacy_gamma(struct drm_crtc *crtc)
239 {
240         u32 gamma_id = crtc->dev->mode_config.gamma_lut_property->base.id;
241         u32 degamma_id = crtc->dev->mode_config.degamma_lut_property->base.id;
242
243         if (!crtc->gamma_size)
244                 return false;
245
246         if (crtc->funcs->gamma_set)
247                 return true;
248
249         return !!(drm_mode_obj_find_prop_id(&crtc->base, gamma_id) ||
250                   drm_mode_obj_find_prop_id(&crtc->base, degamma_id));
251 }
252
253 /**
254  * drm_crtc_legacy_gamma_set - set the legacy gamma correction table
255  * @crtc: CRTC object
256  * @red: red correction table
257  * @green: green correction table
258  * @blue: green correction table
259  * @size: size of the tables
260  * @ctx: lock acquire context
261  *
262  * Implements support for legacy gamma correction table for drivers
263  * that have set drm_crtc_funcs.gamma_set or that support color management
264  * through the DEGAMMA_LUT/GAMMA_LUT properties. See
265  * drm_crtc_enable_color_mgmt() and the containing chapter for
266  * how the atomic color management and gamma tables work.
267  *
268  * This function sets the gamma using drm_crtc_funcs.gamma_set if set, or
269  * alternatively using crtc color management properties.
270  */
271 static int drm_crtc_legacy_gamma_set(struct drm_crtc *crtc,
272                                      u16 *red, u16 *green, u16 *blue,
273                                      u32 size,
274                                      struct drm_modeset_acquire_ctx *ctx)
275 {
276         struct drm_device *dev = crtc->dev;
277         struct drm_atomic_state *state;
278         struct drm_crtc_state *crtc_state;
279         struct drm_property_blob *blob;
280         struct drm_color_lut *blob_data;
281         u32 gamma_id = dev->mode_config.gamma_lut_property->base.id;
282         u32 degamma_id = dev->mode_config.degamma_lut_property->base.id;
283         bool use_gamma_lut;
284         int i, ret = 0;
285         bool replaced;
286
287         if (crtc->funcs->gamma_set)
288                 return crtc->funcs->gamma_set(crtc, red, green, blue, size, ctx);
289
290         if (drm_mode_obj_find_prop_id(&crtc->base, gamma_id))
291                 use_gamma_lut = true;
292         else if (drm_mode_obj_find_prop_id(&crtc->base, degamma_id))
293                 use_gamma_lut = false;
294         else
295                 return -ENODEV;
296
297         state = drm_atomic_state_alloc(crtc->dev);
298         if (!state)
299                 return -ENOMEM;
300
301         blob = drm_property_create_blob(dev,
302                                         sizeof(struct drm_color_lut) * size,
303                                         NULL);
304         if (IS_ERR(blob)) {
305                 ret = PTR_ERR(blob);
306                 blob = NULL;
307                 goto fail;
308         }
309
310         /* Prepare GAMMA_LUT with the legacy values. */
311         blob_data = blob->data;
312         for (i = 0; i < size; i++) {
313                 blob_data[i].red = red[i];
314                 blob_data[i].green = green[i];
315                 blob_data[i].blue = blue[i];
316         }
317
318         state->acquire_ctx = ctx;
319         crtc_state = drm_atomic_get_crtc_state(state, crtc);
320         if (IS_ERR(crtc_state)) {
321                 ret = PTR_ERR(crtc_state);
322                 goto fail;
323         }
324
325         /* Set GAMMA_LUT and reset DEGAMMA_LUT and CTM */
326         replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
327                                              use_gamma_lut ? NULL : blob);
328         replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
329         if (!crtc_state->gamma_lut || !crtc_state->gamma_lut->data ||
330             memcmp(crtc_state->gamma_lut->data, blob_data, blob->length))
331                 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
332                                               use_gamma_lut ? blob : NULL);
333         crtc_state->color_mgmt_changed |= replaced;
334
335         ret = drm_atomic_commit(state);
336
337 fail:
338         drm_atomic_state_put(state);
339         drm_property_blob_put(blob);
340         return ret;
341 }
342
343 /**
344  * drm_mode_gamma_set_ioctl - set the gamma table
345  * @dev: DRM device
346  * @data: ioctl data
347  * @file_priv: DRM file info
348  *
349  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
350  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
351  *
352  * Called by the user via ioctl.
353  *
354  * Returns:
355  * Zero on success, negative errno on failure.
356  */
357 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
358                              void *data, struct drm_file *file_priv)
359 {
360         struct drm_mode_crtc_lut *crtc_lut = data;
361         struct drm_crtc *crtc;
362         void *r_base, *g_base, *b_base;
363         int size;
364         struct drm_modeset_acquire_ctx ctx;
365         int ret = 0;
366
367         if (!drm_core_check_feature(dev, DRIVER_MODESET))
368                 return -EOPNOTSUPP;
369
370         crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
371         if (!crtc)
372                 return -ENOENT;
373
374         if (!drm_crtc_supports_legacy_gamma(crtc))
375                 return -ENOSYS;
376
377         /* memcpy into gamma store */
378         if (crtc_lut->gamma_size != crtc->gamma_size)
379                 return -EINVAL;
380
381         DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
382
383         size = crtc_lut->gamma_size * (sizeof(uint16_t));
384         r_base = crtc->gamma_store;
385         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
386                 ret = -EFAULT;
387                 goto out;
388         }
389
390         g_base = r_base + size;
391         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
392                 ret = -EFAULT;
393                 goto out;
394         }
395
396         b_base = g_base + size;
397         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
398                 ret = -EFAULT;
399                 goto out;
400         }
401
402         ret = drm_crtc_legacy_gamma_set(crtc, r_base, g_base, b_base,
403                                         crtc->gamma_size, &ctx);
404
405 out:
406         DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
407         return ret;
408
409 }
410
411 /**
412  * drm_mode_gamma_get_ioctl - get the gamma table
413  * @dev: DRM device
414  * @data: ioctl data
415  * @file_priv: DRM file info
416  *
417  * Copy the current gamma table into the storage provided. This also provides
418  * the gamma table size the driver expects, which can be used to size the
419  * allocated storage.
420  *
421  * Called by the user via ioctl.
422  *
423  * Returns:
424  * Zero on success, negative errno on failure.
425  */
426 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
427                              void *data, struct drm_file *file_priv)
428 {
429         struct drm_mode_crtc_lut *crtc_lut = data;
430         struct drm_crtc *crtc;
431         void *r_base, *g_base, *b_base;
432         int size;
433         int ret = 0;
434
435         if (!drm_core_check_feature(dev, DRIVER_MODESET))
436                 return -EOPNOTSUPP;
437
438         crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
439         if (!crtc)
440                 return -ENOENT;
441
442         /* memcpy into gamma store */
443         if (crtc_lut->gamma_size != crtc->gamma_size)
444                 return -EINVAL;
445
446         drm_modeset_lock(&crtc->mutex, NULL);
447         size = crtc_lut->gamma_size * (sizeof(uint16_t));
448         r_base = crtc->gamma_store;
449         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
450                 ret = -EFAULT;
451                 goto out;
452         }
453
454         g_base = r_base + size;
455         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
456                 ret = -EFAULT;
457                 goto out;
458         }
459
460         b_base = g_base + size;
461         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
462                 ret = -EFAULT;
463                 goto out;
464         }
465 out:
466         drm_modeset_unlock(&crtc->mutex);
467         return ret;
468 }
469
470 static const char * const color_encoding_name[] = {
471         [DRM_COLOR_YCBCR_BT601] = "ITU-R BT.601 YCbCr",
472         [DRM_COLOR_YCBCR_BT709] = "ITU-R BT.709 YCbCr",
473         [DRM_COLOR_YCBCR_BT2020] = "ITU-R BT.2020 YCbCr",
474 };
475
476 static const char * const color_range_name[] = {
477         [DRM_COLOR_YCBCR_FULL_RANGE] = "YCbCr full range",
478         [DRM_COLOR_YCBCR_LIMITED_RANGE] = "YCbCr limited range",
479 };
480
481 /**
482  * drm_get_color_encoding_name - return a string for color encoding
483  * @encoding: color encoding to compute name of
484  *
485  * In contrast to the other drm_get_*_name functions this one here returns a
486  * const pointer and hence is threadsafe.
487  */
488 const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)
489 {
490         if (WARN_ON(encoding >= ARRAY_SIZE(color_encoding_name)))
491                 return "unknown";
492
493         return color_encoding_name[encoding];
494 }
495
496 /**
497  * drm_get_color_range_name - return a string for color range
498  * @range: color range to compute name of
499  *
500  * In contrast to the other drm_get_*_name functions this one here returns a
501  * const pointer and hence is threadsafe.
502  */
503 const char *drm_get_color_range_name(enum drm_color_range range)
504 {
505         if (WARN_ON(range >= ARRAY_SIZE(color_range_name)))
506                 return "unknown";
507
508         return color_range_name[range];
509 }
510
511 /**
512  * drm_plane_create_color_properties - color encoding related plane properties
513  * @plane: plane object
514  * @supported_encodings: bitfield indicating supported color encodings
515  * @supported_ranges: bitfileld indicating supported color ranges
516  * @default_encoding: default color encoding
517  * @default_range: default color range
518  *
519  * Create and attach plane specific COLOR_ENCODING and COLOR_RANGE
520  * properties to @plane. The supported encodings and ranges should
521  * be provided in supported_encodings and supported_ranges bitmasks.
522  * Each bit set in the bitmask indicates that its number as enum
523  * value is supported.
524  */
525 int drm_plane_create_color_properties(struct drm_plane *plane,
526                                       u32 supported_encodings,
527                                       u32 supported_ranges,
528                                       enum drm_color_encoding default_encoding,
529                                       enum drm_color_range default_range)
530 {
531         struct drm_device *dev = plane->dev;
532         struct drm_property *prop;
533         struct drm_prop_enum_list enum_list[max_t(int, DRM_COLOR_ENCODING_MAX,
534                                                        DRM_COLOR_RANGE_MAX)];
535         int i, len;
536
537         if (WARN_ON(supported_encodings == 0 ||
538                     (supported_encodings & -BIT(DRM_COLOR_ENCODING_MAX)) != 0 ||
539                     (supported_encodings & BIT(default_encoding)) == 0))
540                 return -EINVAL;
541
542         if (WARN_ON(supported_ranges == 0 ||
543                     (supported_ranges & -BIT(DRM_COLOR_RANGE_MAX)) != 0 ||
544                     (supported_ranges & BIT(default_range)) == 0))
545                 return -EINVAL;
546
547         len = 0;
548         for (i = 0; i < DRM_COLOR_ENCODING_MAX; i++) {
549                 if ((supported_encodings & BIT(i)) == 0)
550                         continue;
551
552                 enum_list[len].type = i;
553                 enum_list[len].name = color_encoding_name[i];
554                 len++;
555         }
556
557         prop = drm_property_create_enum(dev, 0, "COLOR_ENCODING",
558                                         enum_list, len);
559         if (!prop)
560                 return -ENOMEM;
561         plane->color_encoding_property = prop;
562         drm_object_attach_property(&plane->base, prop, default_encoding);
563         if (plane->state)
564                 plane->state->color_encoding = default_encoding;
565
566         len = 0;
567         for (i = 0; i < DRM_COLOR_RANGE_MAX; i++) {
568                 if ((supported_ranges & BIT(i)) == 0)
569                         continue;
570
571                 enum_list[len].type = i;
572                 enum_list[len].name = color_range_name[i];
573                 len++;
574         }
575
576         prop = drm_property_create_enum(dev, 0, "COLOR_RANGE",
577                                         enum_list, len);
578         if (!prop)
579                 return -ENOMEM;
580         plane->color_range_property = prop;
581         drm_object_attach_property(&plane->base, prop, default_range);
582         if (plane->state)
583                 plane->state->color_range = default_range;
584
585         return 0;
586 }
587 EXPORT_SYMBOL(drm_plane_create_color_properties);
588
589 /**
590  * drm_plane_create_chroma_siting_properties - chroma siting related plane properties
591  * @plane: plane object
592  *
593  * Create and attach plane specific CHROMA_SITING
594  * properties to @plane.
595  */
596 int drm_plane_create_chroma_siting_properties(struct drm_plane *plane,
597                                                 int32_t default_chroma_siting_h,
598                                                 int32_t default_chroma_siting_v)
599 {
600         struct drm_device *dev = plane->dev;
601         struct drm_property *prop;
602
603         prop = drm_property_create_range(dev, 0, "CHROMA_SITING_H",
604                                         0, 1<<16);
605         if (!prop)
606                 return -ENOMEM;
607         plane->chroma_siting_h_property = prop;
608         drm_object_attach_property(&plane->base, prop, default_chroma_siting_h);
609
610         prop = drm_property_create_range(dev, 0, "CHROMA_SITING_V",
611                                         0, 1<<16);
612         if (!prop)
613                 return -ENOMEM;
614         plane->chroma_siting_v_property = prop;
615         drm_object_attach_property(&plane->base, prop, default_chroma_siting_v);
616
617         if (plane->state) {
618                 plane->state->chroma_siting_h = default_chroma_siting_h;
619                 plane->state->chroma_siting_v = default_chroma_siting_v;
620         }
621         return 0;
622 }
623 EXPORT_SYMBOL(drm_plane_create_chroma_siting_properties);
624
625 /**
626  * drm_color_lut_check - check validity of lookup table
627  * @lut: property blob containing LUT to check
628  * @tests: bitmask of tests to run
629  *
630  * Helper to check whether a userspace-provided lookup table is valid and
631  * satisfies hardware requirements.  Drivers pass a bitmask indicating which of
632  * the tests in &drm_color_lut_tests should be performed.
633  *
634  * Returns 0 on success, -EINVAL on failure.
635  */
636 int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests)
637 {
638         const struct drm_color_lut *entry;
639         int i;
640
641         if (!lut || !tests)
642                 return 0;
643
644         entry = lut->data;
645         for (i = 0; i < drm_color_lut_size(lut); i++) {
646                 if (tests & DRM_COLOR_LUT_EQUAL_CHANNELS) {
647                         if (entry[i].red != entry[i].blue ||
648                             entry[i].red != entry[i].green) {
649                                 DRM_DEBUG_KMS("All LUT entries must have equal r/g/b\n");
650                                 return -EINVAL;
651                         }
652                 }
653
654                 if (i > 0 && tests & DRM_COLOR_LUT_NON_DECREASING) {
655                         if (entry[i].red < entry[i - 1].red ||
656                             entry[i].green < entry[i - 1].green ||
657                             entry[i].blue < entry[i - 1].blue) {
658                                 DRM_DEBUG_KMS("LUT entries must never decrease.\n");
659                                 return -EINVAL;
660                         }
661                 }
662         }
663
664         return 0;
665 }
666 EXPORT_SYMBOL(drm_color_lut_check);