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 <drm/drmP.h> 24 #include <drm/drm_crtc.h> 25 #include <drm/drm_color_mgmt.h> 26 27 #include "drm_crtc_internal.h" 28 29 /** 30 * DOC: overview 31 * 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(). 35 * 36 * "DEGAMMA_LUT”: 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]). 43 * 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. 47 * 48 * “DEGAMMA_LUT_SIZE”: 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. 54 * 55 * “CTM”: 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 59 * &drm_color_ctm. 60 * 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 63 * boot-up state too. 64 * 65 * “GAMMA_LUT”: 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]). 72 * 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. 76 * 77 * “GAMMA_LUT_SIZE”: 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. 83 * 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. 88 */ 89 90 /** 91 * drm_crtc_enable_color_mgmt - enable color management properties 92 * @crtc: DRM CRTC 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) 96 * 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 103 * true. 104 */ 105 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc, 106 uint degamma_lut_size, 107 bool has_ctm, 108 uint gamma_lut_size) 109 { 110 struct drm_device *dev = crtc->dev; 111 struct drm_mode_config *config = &dev->mode_config; 112 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, 118 degamma_lut_size); 119 } 120 121 if (has_ctm) 122 drm_object_attach_property(&crtc->base, 123 config->ctm_property, 0); 124 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, 130 gamma_lut_size); 131 } 132 } 133 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt); 134 135 /** 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 139 * 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. 143 * 144 * Returns: 145 * Zero on success, negative errno on failure. 146 */ 147 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc, 148 int gamma_size) 149 { 150 uint16_t *r_base, *g_base, *b_base; 151 int i; 152 153 crtc->gamma_size = gamma_size; 154 155 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3, 156 GFP_KERNEL); 157 if (!crtc->gamma_store) { 158 crtc->gamma_size = 0; 159 return -ENOMEM; 160 } 161 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++) { 166 r_base[i] = i << 8; 167 g_base[i] = i << 8; 168 b_base[i] = i << 8; 169 } 170 171 172 return 0; 173 } 174 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size); 175 176 /** 177 * drm_mode_gamma_set_ioctl - set the gamma table 178 * @dev: DRM device 179 * @data: ioctl data 180 * @file_priv: DRM file info 181 * 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. 184 * 185 * Called by the user via ioctl. 186 * 187 * Returns: 188 * Zero on success, negative errno on failure. 189 */ 190 int drm_mode_gamma_set_ioctl(struct drm_device *dev, 191 void *data, struct drm_file *file_priv) 192 { 193 struct drm_mode_crtc_lut *crtc_lut = data; 194 struct drm_crtc *crtc; 195 void *r_base, *g_base, *b_base; 196 int size; 197 int ret = 0; 198 199 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 200 return -EINVAL; 201 202 drm_modeset_lock_all(dev); 203 crtc = drm_crtc_find(dev, crtc_lut->crtc_id); 204 if (!crtc) { 205 ret = -ENOENT; 206 goto out; 207 } 208 209 if (crtc->funcs->gamma_set == NULL) { 210 ret = -ENOSYS; 211 goto out; 212 } 213 214 /* memcpy into gamma store */ 215 if (crtc_lut->gamma_size != crtc->gamma_size) { 216 ret = -EINVAL; 217 goto out; 218 } 219 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)) { 223 ret = -EFAULT; 224 goto out; 225 } 226 227 g_base = r_base + size; 228 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) { 229 ret = -EFAULT; 230 goto out; 231 } 232 233 b_base = g_base + size; 234 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) { 235 ret = -EFAULT; 236 goto out; 237 } 238 239 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size); 240 241 out: 242 drm_modeset_unlock_all(dev); 243 return ret; 244 245 } 246 247 /** 248 * drm_mode_gamma_get_ioctl - get the gamma table 249 * @dev: DRM device 250 * @data: ioctl data 251 * @file_priv: DRM file info 252 * 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 255 * allocated storage. 256 * 257 * Called by the user via ioctl. 258 * 259 * Returns: 260 * Zero on success, negative errno on failure. 261 */ 262 int drm_mode_gamma_get_ioctl(struct drm_device *dev, 263 void *data, struct drm_file *file_priv) 264 { 265 struct drm_mode_crtc_lut *crtc_lut = data; 266 struct drm_crtc *crtc; 267 void *r_base, *g_base, *b_base; 268 int size; 269 int ret = 0; 270 271 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 272 return -EINVAL; 273 274 drm_modeset_lock_all(dev); 275 crtc = drm_crtc_find(dev, crtc_lut->crtc_id); 276 if (!crtc) { 277 ret = -ENOENT; 278 goto out; 279 } 280 281 /* memcpy into gamma store */ 282 if (crtc_lut->gamma_size != crtc->gamma_size) { 283 ret = -EINVAL; 284 goto out; 285 } 286 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)) { 290 ret = -EFAULT; 291 goto out; 292 } 293 294 g_base = r_base + size; 295 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) { 296 ret = -EFAULT; 297 goto out; 298 } 299 300 b_base = g_base + size; 301 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) { 302 ret = -EFAULT; 303 goto out; 304 } 305 out: 306 drm_modeset_unlock_all(dev); 307 return ret; 308 } 309