1 /* 2 * Copyright © 1997-2003 by The XFree86 Project, Inc. 3 * Copyright © 2007 Dave Airlie 4 * Copyright © 2007-2008 Intel Corporation 5 * Jesse Barnes <jesse.barnes@intel.com> 6 * Copyright 2005-2006 Luc Verhaegen 7 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in 17 * all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 * OTHER DEALINGS IN THE SOFTWARE. 26 * 27 * Except as contained in this notice, the name of the copyright holder(s) 28 * and author(s) shall not be used in advertising or otherwise to promote 29 * the sale, use or other dealings in this Software without prior written 30 * authorization from the copyright holder(s) and author(s). 31 */ 32 33 #include <linux/ctype.h> 34 #include <linux/list.h> 35 #include <linux/list_sort.h> 36 #include <linux/export.h> 37 38 #include <video/of_display_timing.h> 39 #include <video/of_videomode.h> 40 #include <video/videomode.h> 41 42 #include <drm/drm_crtc.h> 43 #include <drm/drm_device.h> 44 #include <drm/drm_edid.h> 45 #include <drm/drm_modes.h> 46 #include <drm/drm_print.h> 47 48 #include "drm_crtc_internal.h" 49 50 /** 51 * drm_mode_debug_printmodeline - print a mode to dmesg 52 * @mode: mode to print 53 * 54 * Describe @mode using DRM_DEBUG. 55 */ 56 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode) 57 { 58 DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode)); 59 } 60 EXPORT_SYMBOL(drm_mode_debug_printmodeline); 61 62 /** 63 * drm_mode_create - create a new display mode 64 * @dev: DRM device 65 * 66 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it 67 * and return it. 68 * 69 * Returns: 70 * Pointer to new mode on success, NULL on error. 71 */ 72 struct drm_display_mode *drm_mode_create(struct drm_device *dev) 73 { 74 struct drm_display_mode *nmode; 75 76 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL); 77 if (!nmode) 78 return NULL; 79 80 return nmode; 81 } 82 EXPORT_SYMBOL(drm_mode_create); 83 84 /** 85 * drm_mode_destroy - remove a mode 86 * @dev: DRM device 87 * @mode: mode to remove 88 * 89 * Release @mode's unique ID, then free it @mode structure itself using kfree. 90 */ 91 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode) 92 { 93 if (!mode) 94 return; 95 96 kfree(mode); 97 } 98 EXPORT_SYMBOL(drm_mode_destroy); 99 100 /** 101 * drm_mode_probed_add - add a mode to a connector's probed_mode list 102 * @connector: connector the new mode 103 * @mode: mode data 104 * 105 * Add @mode to @connector's probed_mode list for later use. This list should 106 * then in a second step get filtered and all the modes actually supported by 107 * the hardware moved to the @connector's modes list. 108 */ 109 void drm_mode_probed_add(struct drm_connector *connector, 110 struct drm_display_mode *mode) 111 { 112 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex)); 113 114 list_add_tail(&mode->head, &connector->probed_modes); 115 } 116 EXPORT_SYMBOL(drm_mode_probed_add); 117 118 /** 119 * drm_cvt_mode -create a modeline based on the CVT algorithm 120 * @dev: drm device 121 * @hdisplay: hdisplay size 122 * @vdisplay: vdisplay size 123 * @vrefresh: vrefresh rate 124 * @reduced: whether to use reduced blanking 125 * @interlaced: whether to compute an interlaced mode 126 * @margins: whether to add margins (borders) 127 * 128 * This function is called to generate the modeline based on CVT algorithm 129 * according to the hdisplay, vdisplay, vrefresh. 130 * It is based from the VESA(TM) Coordinated Video Timing Generator by 131 * Graham Loveridge April 9, 2003 available at 132 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls 133 * 134 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c. 135 * What I have done is to translate it by using integer calculation. 136 * 137 * Returns: 138 * The modeline based on the CVT algorithm stored in a drm_display_mode object. 139 * The display mode object is allocated with drm_mode_create(). Returns NULL 140 * when no mode could be allocated. 141 */ 142 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay, 143 int vdisplay, int vrefresh, 144 bool reduced, bool interlaced, bool margins) 145 { 146 #define HV_FACTOR 1000 147 /* 1) top/bottom margin size (% of height) - default: 1.8, */ 148 #define CVT_MARGIN_PERCENTAGE 18 149 /* 2) character cell horizontal granularity (pixels) - default 8 */ 150 #define CVT_H_GRANULARITY 8 151 /* 3) Minimum vertical porch (lines) - default 3 */ 152 #define CVT_MIN_V_PORCH 3 153 /* 4) Minimum number of vertical back porch lines - default 6 */ 154 #define CVT_MIN_V_BPORCH 6 155 /* Pixel Clock step (kHz) */ 156 #define CVT_CLOCK_STEP 250 157 struct drm_display_mode *drm_mode; 158 unsigned int vfieldrate, hperiod; 159 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync; 160 int interlace; 161 u64 tmp; 162 163 if (!hdisplay || !vdisplay) 164 return NULL; 165 166 /* allocate the drm_display_mode structure. If failure, we will 167 * return directly 168 */ 169 drm_mode = drm_mode_create(dev); 170 if (!drm_mode) 171 return NULL; 172 173 /* the CVT default refresh rate is 60Hz */ 174 if (!vrefresh) 175 vrefresh = 60; 176 177 /* the required field fresh rate */ 178 if (interlaced) 179 vfieldrate = vrefresh * 2; 180 else 181 vfieldrate = vrefresh; 182 183 /* horizontal pixels */ 184 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY); 185 186 /* determine the left&right borders */ 187 hmargin = 0; 188 if (margins) { 189 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000; 190 hmargin -= hmargin % CVT_H_GRANULARITY; 191 } 192 /* find the total active pixels */ 193 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin; 194 195 /* find the number of lines per field */ 196 if (interlaced) 197 vdisplay_rnd = vdisplay / 2; 198 else 199 vdisplay_rnd = vdisplay; 200 201 /* find the top & bottom borders */ 202 vmargin = 0; 203 if (margins) 204 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000; 205 206 drm_mode->vdisplay = vdisplay + 2 * vmargin; 207 208 /* Interlaced */ 209 if (interlaced) 210 interlace = 1; 211 else 212 interlace = 0; 213 214 /* Determine VSync Width from aspect ratio */ 215 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay)) 216 vsync = 4; 217 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay)) 218 vsync = 5; 219 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay)) 220 vsync = 6; 221 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay)) 222 vsync = 7; 223 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay)) 224 vsync = 7; 225 else /* custom */ 226 vsync = 10; 227 228 if (!reduced) { 229 /* simplify the GTF calculation */ 230 /* 4) Minimum time of vertical sync + back porch interval (µs) 231 * default 550.0 232 */ 233 int tmp1, tmp2; 234 #define CVT_MIN_VSYNC_BP 550 235 /* 3) Nominal HSync width (% of line period) - default 8 */ 236 #define CVT_HSYNC_PERCENTAGE 8 237 unsigned int hblank_percentage; 238 int vsyncandback_porch, __maybe_unused vback_porch, hblank; 239 240 /* estimated the horizontal period */ 241 tmp1 = HV_FACTOR * 1000000 - 242 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate; 243 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 + 244 interlace; 245 hperiod = tmp1 * 2 / (tmp2 * vfieldrate); 246 247 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1; 248 /* 9. Find number of lines in sync + backporch */ 249 if (tmp1 < (vsync + CVT_MIN_V_PORCH)) 250 vsyncandback_porch = vsync + CVT_MIN_V_PORCH; 251 else 252 vsyncandback_porch = tmp1; 253 /* 10. Find number of lines in back porch */ 254 vback_porch = vsyncandback_porch - vsync; 255 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + 256 vsyncandback_porch + CVT_MIN_V_PORCH; 257 /* 5) Definition of Horizontal blanking time limitation */ 258 /* Gradient (%/kHz) - default 600 */ 259 #define CVT_M_FACTOR 600 260 /* Offset (%) - default 40 */ 261 #define CVT_C_FACTOR 40 262 /* Blanking time scaling factor - default 128 */ 263 #define CVT_K_FACTOR 128 264 /* Scaling factor weighting - default 20 */ 265 #define CVT_J_FACTOR 20 266 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256) 267 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \ 268 CVT_J_FACTOR) 269 /* 12. Find ideal blanking duty cycle from formula */ 270 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME * 271 hperiod / 1000; 272 /* 13. Blanking time */ 273 if (hblank_percentage < 20 * HV_FACTOR) 274 hblank_percentage = 20 * HV_FACTOR; 275 hblank = drm_mode->hdisplay * hblank_percentage / 276 (100 * HV_FACTOR - hblank_percentage); 277 hblank -= hblank % (2 * CVT_H_GRANULARITY); 278 /* 14. find the total pixels per line */ 279 drm_mode->htotal = drm_mode->hdisplay + hblank; 280 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2; 281 drm_mode->hsync_start = drm_mode->hsync_end - 282 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100; 283 drm_mode->hsync_start += CVT_H_GRANULARITY - 284 drm_mode->hsync_start % CVT_H_GRANULARITY; 285 /* fill the Vsync values */ 286 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH; 287 drm_mode->vsync_end = drm_mode->vsync_start + vsync; 288 } else { 289 /* Reduced blanking */ 290 /* Minimum vertical blanking interval time (µs)- default 460 */ 291 #define CVT_RB_MIN_VBLANK 460 292 /* Fixed number of clocks for horizontal sync */ 293 #define CVT_RB_H_SYNC 32 294 /* Fixed number of clocks for horizontal blanking */ 295 #define CVT_RB_H_BLANK 160 296 /* Fixed number of lines for vertical front porch - default 3*/ 297 #define CVT_RB_VFPORCH 3 298 int vbilines; 299 int tmp1, tmp2; 300 /* 8. Estimate Horizontal period. */ 301 tmp1 = HV_FACTOR * 1000000 - 302 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate; 303 tmp2 = vdisplay_rnd + 2 * vmargin; 304 hperiod = tmp1 / (tmp2 * vfieldrate); 305 /* 9. Find number of lines in vertical blanking */ 306 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1; 307 /* 10. Check if vertical blanking is sufficient */ 308 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH)) 309 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH; 310 /* 11. Find total number of lines in vertical field */ 311 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines; 312 /* 12. Find total number of pixels in a line */ 313 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK; 314 /* Fill in HSync values */ 315 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2; 316 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC; 317 /* Fill in VSync values */ 318 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH; 319 drm_mode->vsync_end = drm_mode->vsync_start + vsync; 320 } 321 /* 15/13. Find pixel clock frequency (kHz for xf86) */ 322 tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */ 323 tmp *= HV_FACTOR * 1000; 324 do_div(tmp, hperiod); 325 tmp -= drm_mode->clock % CVT_CLOCK_STEP; 326 drm_mode->clock = tmp; 327 /* 18/16. Find actual vertical frame frequency */ 328 /* ignore - just set the mode flag for interlaced */ 329 if (interlaced) { 330 drm_mode->vtotal *= 2; 331 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE; 332 } 333 /* Fill the mode line name */ 334 drm_mode_set_name(drm_mode); 335 if (reduced) 336 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC | 337 DRM_MODE_FLAG_NVSYNC); 338 else 339 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC | 340 DRM_MODE_FLAG_NHSYNC); 341 342 return drm_mode; 343 } 344 EXPORT_SYMBOL(drm_cvt_mode); 345 346 /** 347 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm 348 * @dev: drm device 349 * @hdisplay: hdisplay size 350 * @vdisplay: vdisplay size 351 * @vrefresh: vrefresh rate. 352 * @interlaced: whether to compute an interlaced mode 353 * @margins: desired margin (borders) size 354 * @GTF_M: extended GTF formula parameters 355 * @GTF_2C: extended GTF formula parameters 356 * @GTF_K: extended GTF formula parameters 357 * @GTF_2J: extended GTF formula parameters 358 * 359 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them 360 * in here multiplied by two. For a C of 40, pass in 80. 361 * 362 * Returns: 363 * The modeline based on the full GTF algorithm stored in a drm_display_mode object. 364 * The display mode object is allocated with drm_mode_create(). Returns NULL 365 * when no mode could be allocated. 366 */ 367 struct drm_display_mode * 368 drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay, 369 int vrefresh, bool interlaced, int margins, 370 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J) 371 { /* 1) top/bottom margin size (% of height) - default: 1.8, */ 372 #define GTF_MARGIN_PERCENTAGE 18 373 /* 2) character cell horizontal granularity (pixels) - default 8 */ 374 #define GTF_CELL_GRAN 8 375 /* 3) Minimum vertical porch (lines) - default 3 */ 376 #define GTF_MIN_V_PORCH 1 377 /* width of vsync in lines */ 378 #define V_SYNC_RQD 3 379 /* width of hsync as % of total line */ 380 #define H_SYNC_PERCENT 8 381 /* min time of vsync + back porch (microsec) */ 382 #define MIN_VSYNC_PLUS_BP 550 383 /* C' and M' are part of the Blanking Duty Cycle computation */ 384 #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2) 385 #define GTF_M_PRIME (GTF_K * GTF_M / 256) 386 struct drm_display_mode *drm_mode; 387 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd; 388 int top_margin, bottom_margin; 389 int interlace; 390 unsigned int hfreq_est; 391 int vsync_plus_bp, __maybe_unused vback_porch; 392 unsigned int vtotal_lines, __maybe_unused vfieldrate_est; 393 unsigned int __maybe_unused hperiod; 394 unsigned int vfield_rate, __maybe_unused vframe_rate; 395 int left_margin, right_margin; 396 unsigned int total_active_pixels, ideal_duty_cycle; 397 unsigned int hblank, total_pixels, pixel_freq; 398 int hsync, hfront_porch, vodd_front_porch_lines; 399 unsigned int tmp1, tmp2; 400 401 if (!hdisplay || !vdisplay) 402 return NULL; 403 404 drm_mode = drm_mode_create(dev); 405 if (!drm_mode) 406 return NULL; 407 408 /* 1. In order to give correct results, the number of horizontal 409 * pixels requested is first processed to ensure that it is divisible 410 * by the character size, by rounding it to the nearest character 411 * cell boundary: 412 */ 413 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN; 414 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN; 415 416 /* 2. If interlace is requested, the number of vertical lines assumed 417 * by the calculation must be halved, as the computation calculates 418 * the number of vertical lines per field. 419 */ 420 if (interlaced) 421 vdisplay_rnd = vdisplay / 2; 422 else 423 vdisplay_rnd = vdisplay; 424 425 /* 3. Find the frame rate required: */ 426 if (interlaced) 427 vfieldrate_rqd = vrefresh * 2; 428 else 429 vfieldrate_rqd = vrefresh; 430 431 /* 4. Find number of lines in Top margin: */ 432 top_margin = 0; 433 if (margins) 434 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) / 435 1000; 436 /* 5. Find number of lines in bottom margin: */ 437 bottom_margin = top_margin; 438 439 /* 6. If interlace is required, then set variable interlace: */ 440 if (interlaced) 441 interlace = 1; 442 else 443 interlace = 0; 444 445 /* 7. Estimate the Horizontal frequency */ 446 { 447 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500; 448 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) * 449 2 + interlace; 450 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1; 451 } 452 453 /* 8. Find the number of lines in V sync + back porch */ 454 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */ 455 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000; 456 vsync_plus_bp = (vsync_plus_bp + 500) / 1000; 457 /* 9. Find the number of lines in V back porch alone: */ 458 vback_porch = vsync_plus_bp - V_SYNC_RQD; 459 /* 10. Find the total number of lines in Vertical field period: */ 460 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin + 461 vsync_plus_bp + GTF_MIN_V_PORCH; 462 /* 11. Estimate the Vertical field frequency: */ 463 vfieldrate_est = hfreq_est / vtotal_lines; 464 /* 12. Find the actual horizontal period: */ 465 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines); 466 467 /* 13. Find the actual Vertical field frequency: */ 468 vfield_rate = hfreq_est / vtotal_lines; 469 /* 14. Find the Vertical frame frequency: */ 470 if (interlaced) 471 vframe_rate = vfield_rate / 2; 472 else 473 vframe_rate = vfield_rate; 474 /* 15. Find number of pixels in left margin: */ 475 if (margins) 476 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) / 477 1000; 478 else 479 left_margin = 0; 480 481 /* 16.Find number of pixels in right margin: */ 482 right_margin = left_margin; 483 /* 17.Find total number of active pixels in image and left and right */ 484 total_active_pixels = hdisplay_rnd + left_margin + right_margin; 485 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */ 486 ideal_duty_cycle = GTF_C_PRIME * 1000 - 487 (GTF_M_PRIME * 1000000 / hfreq_est); 488 /* 19.Find the number of pixels in the blanking time to the nearest 489 * double character cell: */ 490 hblank = total_active_pixels * ideal_duty_cycle / 491 (100000 - ideal_duty_cycle); 492 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN); 493 hblank = hblank * 2 * GTF_CELL_GRAN; 494 /* 20.Find total number of pixels: */ 495 total_pixels = total_active_pixels + hblank; 496 /* 21.Find pixel clock frequency: */ 497 pixel_freq = total_pixels * hfreq_est / 1000; 498 /* Stage 1 computations are now complete; I should really pass 499 * the results to another function and do the Stage 2 computations, 500 * but I only need a few more values so I'll just append the 501 * computations here for now */ 502 /* 17. Find the number of pixels in the horizontal sync period: */ 503 hsync = H_SYNC_PERCENT * total_pixels / 100; 504 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN; 505 hsync = hsync * GTF_CELL_GRAN; 506 /* 18. Find the number of pixels in horizontal front porch period */ 507 hfront_porch = hblank / 2 - hsync; 508 /* 36. Find the number of lines in the odd front porch period: */ 509 vodd_front_porch_lines = GTF_MIN_V_PORCH ; 510 511 /* finally, pack the results in the mode struct */ 512 drm_mode->hdisplay = hdisplay_rnd; 513 drm_mode->hsync_start = hdisplay_rnd + hfront_porch; 514 drm_mode->hsync_end = drm_mode->hsync_start + hsync; 515 drm_mode->htotal = total_pixels; 516 drm_mode->vdisplay = vdisplay_rnd; 517 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines; 518 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD; 519 drm_mode->vtotal = vtotal_lines; 520 521 drm_mode->clock = pixel_freq; 522 523 if (interlaced) { 524 drm_mode->vtotal *= 2; 525 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE; 526 } 527 528 drm_mode_set_name(drm_mode); 529 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40) 530 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC; 531 else 532 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC; 533 534 return drm_mode; 535 } 536 EXPORT_SYMBOL(drm_gtf_mode_complex); 537 538 /** 539 * drm_gtf_mode - create the modeline based on the GTF algorithm 540 * @dev: drm device 541 * @hdisplay: hdisplay size 542 * @vdisplay: vdisplay size 543 * @vrefresh: vrefresh rate. 544 * @interlaced: whether to compute an interlaced mode 545 * @margins: desired margin (borders) size 546 * 547 * return the modeline based on GTF algorithm 548 * 549 * This function is to create the modeline based on the GTF algorithm. 550 * Generalized Timing Formula is derived from: 551 * 552 * GTF Spreadsheet by Andy Morrish (1/5/97) 553 * available at https://www.vesa.org 554 * 555 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c. 556 * What I have done is to translate it by using integer calculation. 557 * I also refer to the function of fb_get_mode in the file of 558 * drivers/video/fbmon.c 559 * 560 * Standard GTF parameters:: 561 * 562 * M = 600 563 * C = 40 564 * K = 128 565 * J = 20 566 * 567 * Returns: 568 * The modeline based on the GTF algorithm stored in a drm_display_mode object. 569 * The display mode object is allocated with drm_mode_create(). Returns NULL 570 * when no mode could be allocated. 571 */ 572 struct drm_display_mode * 573 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh, 574 bool interlaced, int margins) 575 { 576 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh, 577 interlaced, margins, 578 600, 40 * 2, 128, 20 * 2); 579 } 580 EXPORT_SYMBOL(drm_gtf_mode); 581 582 #ifdef CONFIG_VIDEOMODE_HELPERS 583 /** 584 * drm_display_mode_from_videomode - fill in @dmode using @vm, 585 * @vm: videomode structure to use as source 586 * @dmode: drm_display_mode structure to use as destination 587 * 588 * Fills out @dmode using the display mode specified in @vm. 589 */ 590 void drm_display_mode_from_videomode(const struct videomode *vm, 591 struct drm_display_mode *dmode) 592 { 593 dmode->hdisplay = vm->hactive; 594 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch; 595 dmode->hsync_end = dmode->hsync_start + vm->hsync_len; 596 dmode->htotal = dmode->hsync_end + vm->hback_porch; 597 598 dmode->vdisplay = vm->vactive; 599 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch; 600 dmode->vsync_end = dmode->vsync_start + vm->vsync_len; 601 dmode->vtotal = dmode->vsync_end + vm->vback_porch; 602 603 dmode->clock = vm->pixelclock / 1000; 604 605 dmode->flags = 0; 606 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH) 607 dmode->flags |= DRM_MODE_FLAG_PHSYNC; 608 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW) 609 dmode->flags |= DRM_MODE_FLAG_NHSYNC; 610 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH) 611 dmode->flags |= DRM_MODE_FLAG_PVSYNC; 612 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW) 613 dmode->flags |= DRM_MODE_FLAG_NVSYNC; 614 if (vm->flags & DISPLAY_FLAGS_INTERLACED) 615 dmode->flags |= DRM_MODE_FLAG_INTERLACE; 616 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN) 617 dmode->flags |= DRM_MODE_FLAG_DBLSCAN; 618 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK) 619 dmode->flags |= DRM_MODE_FLAG_DBLCLK; 620 drm_mode_set_name(dmode); 621 } 622 EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode); 623 624 /** 625 * drm_display_mode_to_videomode - fill in @vm using @dmode, 626 * @dmode: drm_display_mode structure to use as source 627 * @vm: videomode structure to use as destination 628 * 629 * Fills out @vm using the display mode specified in @dmode. 630 */ 631 void drm_display_mode_to_videomode(const struct drm_display_mode *dmode, 632 struct videomode *vm) 633 { 634 vm->hactive = dmode->hdisplay; 635 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay; 636 vm->hsync_len = dmode->hsync_end - dmode->hsync_start; 637 vm->hback_porch = dmode->htotal - dmode->hsync_end; 638 639 vm->vactive = dmode->vdisplay; 640 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay; 641 vm->vsync_len = dmode->vsync_end - dmode->vsync_start; 642 vm->vback_porch = dmode->vtotal - dmode->vsync_end; 643 644 vm->pixelclock = dmode->clock * 1000; 645 646 vm->flags = 0; 647 if (dmode->flags & DRM_MODE_FLAG_PHSYNC) 648 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH; 649 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC) 650 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW; 651 if (dmode->flags & DRM_MODE_FLAG_PVSYNC) 652 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH; 653 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC) 654 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW; 655 if (dmode->flags & DRM_MODE_FLAG_INTERLACE) 656 vm->flags |= DISPLAY_FLAGS_INTERLACED; 657 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN) 658 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN; 659 if (dmode->flags & DRM_MODE_FLAG_DBLCLK) 660 vm->flags |= DISPLAY_FLAGS_DOUBLECLK; 661 } 662 EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode); 663 664 /** 665 * drm_bus_flags_from_videomode - extract information about pixelclk and 666 * DE polarity from videomode and store it in a separate variable 667 * @vm: videomode structure to use 668 * @bus_flags: information about pixelclk, sync and DE polarity will be stored 669 * here 670 * 671 * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE 672 * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS 673 * found in @vm 674 */ 675 void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags) 676 { 677 *bus_flags = 0; 678 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE) 679 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE; 680 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) 681 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE; 682 683 if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE) 684 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE; 685 if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE) 686 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE; 687 688 if (vm->flags & DISPLAY_FLAGS_DE_LOW) 689 *bus_flags |= DRM_BUS_FLAG_DE_LOW; 690 if (vm->flags & DISPLAY_FLAGS_DE_HIGH) 691 *bus_flags |= DRM_BUS_FLAG_DE_HIGH; 692 } 693 EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode); 694 695 #ifdef CONFIG_OF 696 /** 697 * of_get_drm_display_mode - get a drm_display_mode from devicetree 698 * @np: device_node with the timing specification 699 * @dmode: will be set to the return value 700 * @bus_flags: information about pixelclk, sync and DE polarity 701 * @index: index into the list of display timings in devicetree 702 * 703 * This function is expensive and should only be used, if only one mode is to be 704 * read from DT. To get multiple modes start with of_get_display_timings and 705 * work with that instead. 706 * 707 * Returns: 708 * 0 on success, a negative errno code when no of videomode node was found. 709 */ 710 int of_get_drm_display_mode(struct device_node *np, 711 struct drm_display_mode *dmode, u32 *bus_flags, 712 int index) 713 { 714 struct videomode vm; 715 int ret; 716 717 ret = of_get_videomode(np, &vm, index); 718 if (ret) 719 return ret; 720 721 drm_display_mode_from_videomode(&vm, dmode); 722 if (bus_flags) 723 drm_bus_flags_from_videomode(&vm, bus_flags); 724 725 pr_debug("%pOF: got %dx%d display mode\n", 726 np, vm.hactive, vm.vactive); 727 drm_mode_debug_printmodeline(dmode); 728 729 return 0; 730 } 731 EXPORT_SYMBOL_GPL(of_get_drm_display_mode); 732 733 /** 734 * of_get_drm_panel_display_mode - get a panel-timing drm_display_mode from devicetree 735 * @np: device_node with the panel-timing specification 736 * @dmode: will be set to the return value 737 * @bus_flags: information about pixelclk, sync and DE polarity 738 * 739 * The mandatory Device Tree properties width-mm and height-mm 740 * are read and set on the display mode. 741 * 742 * Returns: 743 * Zero on success, negative error code on failure. 744 */ 745 int of_get_drm_panel_display_mode(struct device_node *np, 746 struct drm_display_mode *dmode, u32 *bus_flags) 747 { 748 u32 width_mm = 0, height_mm = 0; 749 struct display_timing timing; 750 struct videomode vm; 751 int ret; 752 753 ret = of_get_display_timing(np, "panel-timing", &timing); 754 if (ret) 755 return ret; 756 757 videomode_from_timing(&timing, &vm); 758 759 memset(dmode, 0, sizeof(*dmode)); 760 drm_display_mode_from_videomode(&vm, dmode); 761 if (bus_flags) 762 drm_bus_flags_from_videomode(&vm, bus_flags); 763 764 ret = of_property_read_u32(np, "width-mm", &width_mm); 765 if (ret) 766 return ret; 767 768 ret = of_property_read_u32(np, "height-mm", &height_mm); 769 if (ret) 770 return ret; 771 772 dmode->width_mm = width_mm; 773 dmode->height_mm = height_mm; 774 775 drm_mode_debug_printmodeline(dmode); 776 777 return 0; 778 } 779 EXPORT_SYMBOL_GPL(of_get_drm_panel_display_mode); 780 #endif /* CONFIG_OF */ 781 #endif /* CONFIG_VIDEOMODE_HELPERS */ 782 783 /** 784 * drm_mode_set_name - set the name on a mode 785 * @mode: name will be set in this mode 786 * 787 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay> 788 * with an optional 'i' suffix for interlaced modes. 789 */ 790 void drm_mode_set_name(struct drm_display_mode *mode) 791 { 792 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE); 793 794 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s", 795 mode->hdisplay, mode->vdisplay, 796 interlaced ? "i" : ""); 797 } 798 EXPORT_SYMBOL(drm_mode_set_name); 799 800 /** 801 * drm_mode_vrefresh - get the vrefresh of a mode 802 * @mode: mode 803 * 804 * Returns: 805 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the 806 * value first if it is not yet set. 807 */ 808 int drm_mode_vrefresh(const struct drm_display_mode *mode) 809 { 810 unsigned int num, den; 811 812 if (mode->htotal == 0 || mode->vtotal == 0) 813 return 0; 814 815 num = mode->clock; 816 den = mode->htotal * mode->vtotal; 817 818 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 819 num *= 2; 820 if (mode->flags & DRM_MODE_FLAG_DBLSCAN) 821 den *= 2; 822 if (mode->vscan > 1) 823 den *= mode->vscan; 824 825 return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den); 826 } 827 EXPORT_SYMBOL(drm_mode_vrefresh); 828 829 /** 830 * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode 831 * @mode: mode to query 832 * @hdisplay: hdisplay value to fill in 833 * @vdisplay: vdisplay value to fill in 834 * 835 * The vdisplay value will be doubled if the specified mode is a stereo mode of 836 * the appropriate layout. 837 */ 838 void drm_mode_get_hv_timing(const struct drm_display_mode *mode, 839 int *hdisplay, int *vdisplay) 840 { 841 struct drm_display_mode adjusted; 842 843 drm_mode_init(&adjusted, mode); 844 845 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY); 846 *hdisplay = adjusted.crtc_hdisplay; 847 *vdisplay = adjusted.crtc_vdisplay; 848 } 849 EXPORT_SYMBOL(drm_mode_get_hv_timing); 850 851 /** 852 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters 853 * @p: mode 854 * @adjust_flags: a combination of adjustment flags 855 * 856 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary. 857 * 858 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of 859 * interlaced modes. 860 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for 861 * buffers containing two eyes (only adjust the timings when needed, eg. for 862 * "frame packing" or "side by side full"). 863 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not* 864 * be performed for doublescan and vscan > 1 modes respectively. 865 */ 866 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags) 867 { 868 if (!p) 869 return; 870 871 p->crtc_clock = p->clock; 872 p->crtc_hdisplay = p->hdisplay; 873 p->crtc_hsync_start = p->hsync_start; 874 p->crtc_hsync_end = p->hsync_end; 875 p->crtc_htotal = p->htotal; 876 p->crtc_hskew = p->hskew; 877 p->crtc_vdisplay = p->vdisplay; 878 p->crtc_vsync_start = p->vsync_start; 879 p->crtc_vsync_end = p->vsync_end; 880 p->crtc_vtotal = p->vtotal; 881 882 if (p->flags & DRM_MODE_FLAG_INTERLACE) { 883 if (adjust_flags & CRTC_INTERLACE_HALVE_V) { 884 p->crtc_vdisplay /= 2; 885 p->crtc_vsync_start /= 2; 886 p->crtc_vsync_end /= 2; 887 p->crtc_vtotal /= 2; 888 } 889 } 890 891 if (!(adjust_flags & CRTC_NO_DBLSCAN)) { 892 if (p->flags & DRM_MODE_FLAG_DBLSCAN) { 893 p->crtc_vdisplay *= 2; 894 p->crtc_vsync_start *= 2; 895 p->crtc_vsync_end *= 2; 896 p->crtc_vtotal *= 2; 897 } 898 } 899 900 if (!(adjust_flags & CRTC_NO_VSCAN)) { 901 if (p->vscan > 1) { 902 p->crtc_vdisplay *= p->vscan; 903 p->crtc_vsync_start *= p->vscan; 904 p->crtc_vsync_end *= p->vscan; 905 p->crtc_vtotal *= p->vscan; 906 } 907 } 908 909 if (adjust_flags & CRTC_STEREO_DOUBLE) { 910 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK; 911 912 switch (layout) { 913 case DRM_MODE_FLAG_3D_FRAME_PACKING: 914 p->crtc_clock *= 2; 915 p->crtc_vdisplay += p->crtc_vtotal; 916 p->crtc_vsync_start += p->crtc_vtotal; 917 p->crtc_vsync_end += p->crtc_vtotal; 918 p->crtc_vtotal += p->crtc_vtotal; 919 break; 920 } 921 } 922 923 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay); 924 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal); 925 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay); 926 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal); 927 } 928 EXPORT_SYMBOL(drm_mode_set_crtcinfo); 929 930 /** 931 * drm_mode_copy - copy the mode 932 * @dst: mode to overwrite 933 * @src: mode to copy 934 * 935 * Copy an existing mode into another mode, preserving the 936 * list head of the destination mode. 937 */ 938 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src) 939 { 940 struct list_head head = dst->head; 941 942 *dst = *src; 943 dst->head = head; 944 } 945 EXPORT_SYMBOL(drm_mode_copy); 946 947 /** 948 * drm_mode_init - initialize the mode from another mode 949 * @dst: mode to overwrite 950 * @src: mode to copy 951 * 952 * Copy an existing mode into another mode, zeroing the 953 * list head of the destination mode. Typically used 954 * to guarantee the list head is not left with stack 955 * garbage in on-stack modes. 956 */ 957 void drm_mode_init(struct drm_display_mode *dst, const struct drm_display_mode *src) 958 { 959 memset(dst, 0, sizeof(*dst)); 960 drm_mode_copy(dst, src); 961 } 962 EXPORT_SYMBOL(drm_mode_init); 963 964 /** 965 * drm_mode_duplicate - allocate and duplicate an existing mode 966 * @dev: drm_device to allocate the duplicated mode for 967 * @mode: mode to duplicate 968 * 969 * Just allocate a new mode, copy the existing mode into it, and return 970 * a pointer to it. Used to create new instances of established modes. 971 * 972 * Returns: 973 * Pointer to duplicated mode on success, NULL on error. 974 */ 975 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev, 976 const struct drm_display_mode *mode) 977 { 978 struct drm_display_mode *nmode; 979 980 nmode = drm_mode_create(dev); 981 if (!nmode) 982 return NULL; 983 984 drm_mode_copy(nmode, mode); 985 986 return nmode; 987 } 988 EXPORT_SYMBOL(drm_mode_duplicate); 989 990 static bool drm_mode_match_timings(const struct drm_display_mode *mode1, 991 const struct drm_display_mode *mode2) 992 { 993 return mode1->hdisplay == mode2->hdisplay && 994 mode1->hsync_start == mode2->hsync_start && 995 mode1->hsync_end == mode2->hsync_end && 996 mode1->htotal == mode2->htotal && 997 mode1->hskew == mode2->hskew && 998 mode1->vdisplay == mode2->vdisplay && 999 mode1->vsync_start == mode2->vsync_start && 1000 mode1->vsync_end == mode2->vsync_end && 1001 mode1->vtotal == mode2->vtotal && 1002 mode1->vscan == mode2->vscan; 1003 } 1004 1005 static bool drm_mode_match_clock(const struct drm_display_mode *mode1, 1006 const struct drm_display_mode *mode2) 1007 { 1008 /* 1009 * do clock check convert to PICOS 1010 * so fb modes get matched the same 1011 */ 1012 if (mode1->clock && mode2->clock) 1013 return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock); 1014 else 1015 return mode1->clock == mode2->clock; 1016 } 1017 1018 static bool drm_mode_match_flags(const struct drm_display_mode *mode1, 1019 const struct drm_display_mode *mode2) 1020 { 1021 return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) == 1022 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK); 1023 } 1024 1025 static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1, 1026 const struct drm_display_mode *mode2) 1027 { 1028 return (mode1->flags & DRM_MODE_FLAG_3D_MASK) == 1029 (mode2->flags & DRM_MODE_FLAG_3D_MASK); 1030 } 1031 1032 static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1, 1033 const struct drm_display_mode *mode2) 1034 { 1035 return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio; 1036 } 1037 1038 /** 1039 * drm_mode_match - test modes for (partial) equality 1040 * @mode1: first mode 1041 * @mode2: second mode 1042 * @match_flags: which parts need to match (DRM_MODE_MATCH_*) 1043 * 1044 * Check to see if @mode1 and @mode2 are equivalent. 1045 * 1046 * Returns: 1047 * True if the modes are (partially) equal, false otherwise. 1048 */ 1049 bool drm_mode_match(const struct drm_display_mode *mode1, 1050 const struct drm_display_mode *mode2, 1051 unsigned int match_flags) 1052 { 1053 if (!mode1 && !mode2) 1054 return true; 1055 1056 if (!mode1 || !mode2) 1057 return false; 1058 1059 if (match_flags & DRM_MODE_MATCH_TIMINGS && 1060 !drm_mode_match_timings(mode1, mode2)) 1061 return false; 1062 1063 if (match_flags & DRM_MODE_MATCH_CLOCK && 1064 !drm_mode_match_clock(mode1, mode2)) 1065 return false; 1066 1067 if (match_flags & DRM_MODE_MATCH_FLAGS && 1068 !drm_mode_match_flags(mode1, mode2)) 1069 return false; 1070 1071 if (match_flags & DRM_MODE_MATCH_3D_FLAGS && 1072 !drm_mode_match_3d_flags(mode1, mode2)) 1073 return false; 1074 1075 if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO && 1076 !drm_mode_match_aspect_ratio(mode1, mode2)) 1077 return false; 1078 1079 return true; 1080 } 1081 EXPORT_SYMBOL(drm_mode_match); 1082 1083 /** 1084 * drm_mode_equal - test modes for equality 1085 * @mode1: first mode 1086 * @mode2: second mode 1087 * 1088 * Check to see if @mode1 and @mode2 are equivalent. 1089 * 1090 * Returns: 1091 * True if the modes are equal, false otherwise. 1092 */ 1093 bool drm_mode_equal(const struct drm_display_mode *mode1, 1094 const struct drm_display_mode *mode2) 1095 { 1096 return drm_mode_match(mode1, mode2, 1097 DRM_MODE_MATCH_TIMINGS | 1098 DRM_MODE_MATCH_CLOCK | 1099 DRM_MODE_MATCH_FLAGS | 1100 DRM_MODE_MATCH_3D_FLAGS| 1101 DRM_MODE_MATCH_ASPECT_RATIO); 1102 } 1103 EXPORT_SYMBOL(drm_mode_equal); 1104 1105 /** 1106 * drm_mode_equal_no_clocks - test modes for equality 1107 * @mode1: first mode 1108 * @mode2: second mode 1109 * 1110 * Check to see if @mode1 and @mode2 are equivalent, but 1111 * don't check the pixel clocks. 1112 * 1113 * Returns: 1114 * True if the modes are equal, false otherwise. 1115 */ 1116 bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, 1117 const struct drm_display_mode *mode2) 1118 { 1119 return drm_mode_match(mode1, mode2, 1120 DRM_MODE_MATCH_TIMINGS | 1121 DRM_MODE_MATCH_FLAGS | 1122 DRM_MODE_MATCH_3D_FLAGS); 1123 } 1124 EXPORT_SYMBOL(drm_mode_equal_no_clocks); 1125 1126 /** 1127 * drm_mode_equal_no_clocks_no_stereo - test modes for equality 1128 * @mode1: first mode 1129 * @mode2: second mode 1130 * 1131 * Check to see if @mode1 and @mode2 are equivalent, but 1132 * don't check the pixel clocks nor the stereo layout. 1133 * 1134 * Returns: 1135 * True if the modes are equal, false otherwise. 1136 */ 1137 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1, 1138 const struct drm_display_mode *mode2) 1139 { 1140 return drm_mode_match(mode1, mode2, 1141 DRM_MODE_MATCH_TIMINGS | 1142 DRM_MODE_MATCH_FLAGS); 1143 } 1144 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo); 1145 1146 static enum drm_mode_status 1147 drm_mode_validate_basic(const struct drm_display_mode *mode) 1148 { 1149 if (mode->type & ~DRM_MODE_TYPE_ALL) 1150 return MODE_BAD; 1151 1152 if (mode->flags & ~DRM_MODE_FLAG_ALL) 1153 return MODE_BAD; 1154 1155 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX) 1156 return MODE_BAD; 1157 1158 if (mode->clock == 0) 1159 return MODE_CLOCK_LOW; 1160 1161 if (mode->hdisplay == 0 || 1162 mode->hsync_start < mode->hdisplay || 1163 mode->hsync_end < mode->hsync_start || 1164 mode->htotal < mode->hsync_end) 1165 return MODE_H_ILLEGAL; 1166 1167 if (mode->vdisplay == 0 || 1168 mode->vsync_start < mode->vdisplay || 1169 mode->vsync_end < mode->vsync_start || 1170 mode->vtotal < mode->vsync_end) 1171 return MODE_V_ILLEGAL; 1172 1173 return MODE_OK; 1174 } 1175 1176 /** 1177 * drm_mode_validate_driver - make sure the mode is somewhat sane 1178 * @dev: drm device 1179 * @mode: mode to check 1180 * 1181 * First do basic validation on the mode, and then allow the driver 1182 * to check for device/driver specific limitations via the optional 1183 * &drm_mode_config_helper_funcs.mode_valid hook. 1184 * 1185 * Returns: 1186 * The mode status 1187 */ 1188 enum drm_mode_status 1189 drm_mode_validate_driver(struct drm_device *dev, 1190 const struct drm_display_mode *mode) 1191 { 1192 enum drm_mode_status status; 1193 1194 status = drm_mode_validate_basic(mode); 1195 if (status != MODE_OK) 1196 return status; 1197 1198 if (dev->mode_config.funcs->mode_valid) 1199 return dev->mode_config.funcs->mode_valid(dev, mode); 1200 else 1201 return MODE_OK; 1202 } 1203 EXPORT_SYMBOL(drm_mode_validate_driver); 1204 1205 /** 1206 * drm_mode_validate_size - make sure modes adhere to size constraints 1207 * @mode: mode to check 1208 * @maxX: maximum width 1209 * @maxY: maximum height 1210 * 1211 * This function is a helper which can be used to validate modes against size 1212 * limitations of the DRM device/connector. If a mode is too big its status 1213 * member is updated with the appropriate validation failure code. The list 1214 * itself is not changed. 1215 * 1216 * Returns: 1217 * The mode status 1218 */ 1219 enum drm_mode_status 1220 drm_mode_validate_size(const struct drm_display_mode *mode, 1221 int maxX, int maxY) 1222 { 1223 if (maxX > 0 && mode->hdisplay > maxX) 1224 return MODE_VIRTUAL_X; 1225 1226 if (maxY > 0 && mode->vdisplay > maxY) 1227 return MODE_VIRTUAL_Y; 1228 1229 return MODE_OK; 1230 } 1231 EXPORT_SYMBOL(drm_mode_validate_size); 1232 1233 /** 1234 * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed 1235 * @mode: mode to check 1236 * @connector: drm connector under action 1237 * 1238 * This function is a helper which can be used to filter out any YCBCR420 1239 * only mode, when the source doesn't support it. 1240 * 1241 * Returns: 1242 * The mode status 1243 */ 1244 enum drm_mode_status 1245 drm_mode_validate_ycbcr420(const struct drm_display_mode *mode, 1246 struct drm_connector *connector) 1247 { 1248 if (!connector->ycbcr_420_allowed && 1249 drm_mode_is_420_only(&connector->display_info, mode)) 1250 return MODE_NO_420; 1251 1252 return MODE_OK; 1253 } 1254 EXPORT_SYMBOL(drm_mode_validate_ycbcr420); 1255 1256 #define MODE_STATUS(status) [MODE_ ## status + 3] = #status 1257 1258 static const char * const drm_mode_status_names[] = { 1259 MODE_STATUS(OK), 1260 MODE_STATUS(HSYNC), 1261 MODE_STATUS(VSYNC), 1262 MODE_STATUS(H_ILLEGAL), 1263 MODE_STATUS(V_ILLEGAL), 1264 MODE_STATUS(BAD_WIDTH), 1265 MODE_STATUS(NOMODE), 1266 MODE_STATUS(NO_INTERLACE), 1267 MODE_STATUS(NO_DBLESCAN), 1268 MODE_STATUS(NO_VSCAN), 1269 MODE_STATUS(MEM), 1270 MODE_STATUS(VIRTUAL_X), 1271 MODE_STATUS(VIRTUAL_Y), 1272 MODE_STATUS(MEM_VIRT), 1273 MODE_STATUS(NOCLOCK), 1274 MODE_STATUS(CLOCK_HIGH), 1275 MODE_STATUS(CLOCK_LOW), 1276 MODE_STATUS(CLOCK_RANGE), 1277 MODE_STATUS(BAD_HVALUE), 1278 MODE_STATUS(BAD_VVALUE), 1279 MODE_STATUS(BAD_VSCAN), 1280 MODE_STATUS(HSYNC_NARROW), 1281 MODE_STATUS(HSYNC_WIDE), 1282 MODE_STATUS(HBLANK_NARROW), 1283 MODE_STATUS(HBLANK_WIDE), 1284 MODE_STATUS(VSYNC_NARROW), 1285 MODE_STATUS(VSYNC_WIDE), 1286 MODE_STATUS(VBLANK_NARROW), 1287 MODE_STATUS(VBLANK_WIDE), 1288 MODE_STATUS(PANEL), 1289 MODE_STATUS(INTERLACE_WIDTH), 1290 MODE_STATUS(ONE_WIDTH), 1291 MODE_STATUS(ONE_HEIGHT), 1292 MODE_STATUS(ONE_SIZE), 1293 MODE_STATUS(NO_REDUCED), 1294 MODE_STATUS(NO_STEREO), 1295 MODE_STATUS(NO_420), 1296 MODE_STATUS(STALE), 1297 MODE_STATUS(BAD), 1298 MODE_STATUS(ERROR), 1299 }; 1300 1301 #undef MODE_STATUS 1302 1303 const char *drm_get_mode_status_name(enum drm_mode_status status) 1304 { 1305 int index = status + 3; 1306 1307 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names))) 1308 return ""; 1309 1310 return drm_mode_status_names[index]; 1311 } 1312 1313 /** 1314 * drm_mode_prune_invalid - remove invalid modes from mode list 1315 * @dev: DRM device 1316 * @mode_list: list of modes to check 1317 * @verbose: be verbose about it 1318 * 1319 * This helper function can be used to prune a display mode list after 1320 * validation has been completed. All modes whose status is not MODE_OK will be 1321 * removed from the list, and if @verbose the status code and mode name is also 1322 * printed to dmesg. 1323 */ 1324 void drm_mode_prune_invalid(struct drm_device *dev, 1325 struct list_head *mode_list, bool verbose) 1326 { 1327 struct drm_display_mode *mode, *t; 1328 1329 list_for_each_entry_safe(mode, t, mode_list, head) { 1330 if (mode->status != MODE_OK) { 1331 list_del(&mode->head); 1332 if (mode->type & DRM_MODE_TYPE_USERDEF) { 1333 drm_warn(dev, "User-defined mode not supported: " 1334 DRM_MODE_FMT "\n", DRM_MODE_ARG(mode)); 1335 } 1336 if (verbose) { 1337 drm_mode_debug_printmodeline(mode); 1338 DRM_DEBUG_KMS("Not using %s mode: %s\n", 1339 mode->name, 1340 drm_get_mode_status_name(mode->status)); 1341 } 1342 drm_mode_destroy(dev, mode); 1343 } 1344 } 1345 } 1346 EXPORT_SYMBOL(drm_mode_prune_invalid); 1347 1348 /** 1349 * drm_mode_compare - compare modes for favorability 1350 * @priv: unused 1351 * @lh_a: list_head for first mode 1352 * @lh_b: list_head for second mode 1353 * 1354 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating 1355 * which is better. 1356 * 1357 * Returns: 1358 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or 1359 * positive if @lh_b is better than @lh_a. 1360 */ 1361 static int drm_mode_compare(void *priv, const struct list_head *lh_a, 1362 const struct list_head *lh_b) 1363 { 1364 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head); 1365 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head); 1366 int diff; 1367 1368 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) - 1369 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0); 1370 if (diff) 1371 return diff; 1372 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay; 1373 if (diff) 1374 return diff; 1375 1376 diff = drm_mode_vrefresh(b) - drm_mode_vrefresh(a); 1377 if (diff) 1378 return diff; 1379 1380 diff = b->clock - a->clock; 1381 return diff; 1382 } 1383 1384 /** 1385 * drm_mode_sort - sort mode list 1386 * @mode_list: list of drm_display_mode structures to sort 1387 * 1388 * Sort @mode_list by favorability, moving good modes to the head of the list. 1389 */ 1390 void drm_mode_sort(struct list_head *mode_list) 1391 { 1392 list_sort(NULL, mode_list, drm_mode_compare); 1393 } 1394 EXPORT_SYMBOL(drm_mode_sort); 1395 1396 /** 1397 * drm_connector_list_update - update the mode list for the connector 1398 * @connector: the connector to update 1399 * 1400 * This moves the modes from the @connector probed_modes list 1401 * to the actual mode list. It compares the probed mode against the current 1402 * list and only adds different/new modes. 1403 * 1404 * This is just a helper functions doesn't validate any modes itself and also 1405 * doesn't prune any invalid modes. Callers need to do that themselves. 1406 */ 1407 void drm_connector_list_update(struct drm_connector *connector) 1408 { 1409 struct drm_display_mode *pmode, *pt; 1410 1411 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex)); 1412 1413 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) { 1414 struct drm_display_mode *mode; 1415 bool found_it = false; 1416 1417 /* go through current modes checking for the new probed mode */ 1418 list_for_each_entry(mode, &connector->modes, head) { 1419 if (!drm_mode_equal(pmode, mode)) 1420 continue; 1421 1422 found_it = true; 1423 1424 /* 1425 * If the old matching mode is stale (ie. left over 1426 * from a previous probe) just replace it outright. 1427 * Otherwise just merge the type bits between all 1428 * equal probed modes. 1429 * 1430 * If two probed modes are considered equal, pick the 1431 * actual timings from the one that's marked as 1432 * preferred (in case the match isn't 100%). If 1433 * multiple or zero preferred modes are present, favor 1434 * the mode added to the probed_modes list first. 1435 */ 1436 if (mode->status == MODE_STALE) { 1437 drm_mode_copy(mode, pmode); 1438 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 && 1439 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) { 1440 pmode->type |= mode->type; 1441 drm_mode_copy(mode, pmode); 1442 } else { 1443 mode->type |= pmode->type; 1444 } 1445 1446 list_del(&pmode->head); 1447 drm_mode_destroy(connector->dev, pmode); 1448 break; 1449 } 1450 1451 if (!found_it) { 1452 list_move_tail(&pmode->head, &connector->modes); 1453 } 1454 } 1455 } 1456 EXPORT_SYMBOL(drm_connector_list_update); 1457 1458 static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr, 1459 struct drm_cmdline_mode *mode) 1460 { 1461 unsigned int bpp; 1462 1463 if (str[0] != '-') 1464 return -EINVAL; 1465 1466 str++; 1467 bpp = simple_strtol(str, end_ptr, 10); 1468 if (*end_ptr == str) 1469 return -EINVAL; 1470 1471 mode->bpp = bpp; 1472 mode->bpp_specified = true; 1473 1474 return 0; 1475 } 1476 1477 static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr, 1478 struct drm_cmdline_mode *mode) 1479 { 1480 unsigned int refresh; 1481 1482 if (str[0] != '@') 1483 return -EINVAL; 1484 1485 str++; 1486 refresh = simple_strtol(str, end_ptr, 10); 1487 if (*end_ptr == str) 1488 return -EINVAL; 1489 1490 mode->refresh = refresh; 1491 mode->refresh_specified = true; 1492 1493 return 0; 1494 } 1495 1496 static int drm_mode_parse_cmdline_extra(const char *str, int length, 1497 bool freestanding, 1498 const struct drm_connector *connector, 1499 struct drm_cmdline_mode *mode) 1500 { 1501 int i; 1502 1503 for (i = 0; i < length; i++) { 1504 switch (str[i]) { 1505 case 'i': 1506 if (freestanding) 1507 return -EINVAL; 1508 1509 mode->interlace = true; 1510 break; 1511 case 'm': 1512 if (freestanding) 1513 return -EINVAL; 1514 1515 mode->margins = true; 1516 break; 1517 case 'D': 1518 if (mode->force != DRM_FORCE_UNSPECIFIED) 1519 return -EINVAL; 1520 1521 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) && 1522 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB)) 1523 mode->force = DRM_FORCE_ON; 1524 else 1525 mode->force = DRM_FORCE_ON_DIGITAL; 1526 break; 1527 case 'd': 1528 if (mode->force != DRM_FORCE_UNSPECIFIED) 1529 return -EINVAL; 1530 1531 mode->force = DRM_FORCE_OFF; 1532 break; 1533 case 'e': 1534 if (mode->force != DRM_FORCE_UNSPECIFIED) 1535 return -EINVAL; 1536 1537 mode->force = DRM_FORCE_ON; 1538 break; 1539 default: 1540 return -EINVAL; 1541 } 1542 } 1543 1544 return 0; 1545 } 1546 1547 static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length, 1548 bool extras, 1549 const struct drm_connector *connector, 1550 struct drm_cmdline_mode *mode) 1551 { 1552 const char *str_start = str; 1553 bool rb = false, cvt = false; 1554 int xres = 0, yres = 0; 1555 int remaining, i; 1556 char *end_ptr; 1557 1558 xres = simple_strtol(str, &end_ptr, 10); 1559 if (end_ptr == str) 1560 return -EINVAL; 1561 1562 if (end_ptr[0] != 'x') 1563 return -EINVAL; 1564 end_ptr++; 1565 1566 str = end_ptr; 1567 yres = simple_strtol(str, &end_ptr, 10); 1568 if (end_ptr == str) 1569 return -EINVAL; 1570 1571 remaining = length - (end_ptr - str_start); 1572 if (remaining < 0) 1573 return -EINVAL; 1574 1575 for (i = 0; i < remaining; i++) { 1576 switch (end_ptr[i]) { 1577 case 'M': 1578 cvt = true; 1579 break; 1580 case 'R': 1581 rb = true; 1582 break; 1583 default: 1584 /* 1585 * Try to pass that to our extras parsing 1586 * function to handle the case where the 1587 * extras are directly after the resolution 1588 */ 1589 if (extras) { 1590 int ret = drm_mode_parse_cmdline_extra(end_ptr + i, 1591 1, 1592 false, 1593 connector, 1594 mode); 1595 if (ret) 1596 return ret; 1597 } else { 1598 return -EINVAL; 1599 } 1600 } 1601 } 1602 1603 mode->xres = xres; 1604 mode->yres = yres; 1605 mode->cvt = cvt; 1606 mode->rb = rb; 1607 1608 return 0; 1609 } 1610 1611 static int drm_mode_parse_cmdline_int(const char *delim, unsigned int *int_ret) 1612 { 1613 const char *value; 1614 char *endp; 1615 1616 /* 1617 * delim must point to the '=', otherwise it is a syntax error and 1618 * if delim points to the terminating zero, then delim + 1 will point 1619 * past the end of the string. 1620 */ 1621 if (*delim != '=') 1622 return -EINVAL; 1623 1624 value = delim + 1; 1625 *int_ret = simple_strtol(value, &endp, 10); 1626 1627 /* Make sure we have parsed something */ 1628 if (endp == value) 1629 return -EINVAL; 1630 1631 return 0; 1632 } 1633 1634 static int drm_mode_parse_panel_orientation(const char *delim, 1635 struct drm_cmdline_mode *mode) 1636 { 1637 const char *value; 1638 1639 if (*delim != '=') 1640 return -EINVAL; 1641 1642 value = delim + 1; 1643 delim = strchr(value, ','); 1644 if (!delim) 1645 delim = value + strlen(value); 1646 1647 if (!strncmp(value, "normal", delim - value)) 1648 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL; 1649 else if (!strncmp(value, "upside_down", delim - value)) 1650 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP; 1651 else if (!strncmp(value, "left_side_up", delim - value)) 1652 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP; 1653 else if (!strncmp(value, "right_side_up", delim - value)) 1654 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP; 1655 else 1656 return -EINVAL; 1657 1658 return 0; 1659 } 1660 1661 static int drm_mode_parse_cmdline_options(const char *str, 1662 bool freestanding, 1663 const struct drm_connector *connector, 1664 struct drm_cmdline_mode *mode) 1665 { 1666 unsigned int deg, margin, rotation = 0; 1667 const char *delim, *option, *sep; 1668 1669 option = str; 1670 do { 1671 delim = strchr(option, '='); 1672 if (!delim) { 1673 delim = strchr(option, ','); 1674 1675 if (!delim) 1676 delim = option + strlen(option); 1677 } 1678 1679 if (!strncmp(option, "rotate", delim - option)) { 1680 if (drm_mode_parse_cmdline_int(delim, °)) 1681 return -EINVAL; 1682 1683 switch (deg) { 1684 case 0: 1685 rotation |= DRM_MODE_ROTATE_0; 1686 break; 1687 1688 case 90: 1689 rotation |= DRM_MODE_ROTATE_90; 1690 break; 1691 1692 case 180: 1693 rotation |= DRM_MODE_ROTATE_180; 1694 break; 1695 1696 case 270: 1697 rotation |= DRM_MODE_ROTATE_270; 1698 break; 1699 1700 default: 1701 return -EINVAL; 1702 } 1703 } else if (!strncmp(option, "reflect_x", delim - option)) { 1704 rotation |= DRM_MODE_REFLECT_X; 1705 } else if (!strncmp(option, "reflect_y", delim - option)) { 1706 rotation |= DRM_MODE_REFLECT_Y; 1707 } else if (!strncmp(option, "margin_right", delim - option)) { 1708 if (drm_mode_parse_cmdline_int(delim, &margin)) 1709 return -EINVAL; 1710 1711 mode->tv_margins.right = margin; 1712 } else if (!strncmp(option, "margin_left", delim - option)) { 1713 if (drm_mode_parse_cmdline_int(delim, &margin)) 1714 return -EINVAL; 1715 1716 mode->tv_margins.left = margin; 1717 } else if (!strncmp(option, "margin_top", delim - option)) { 1718 if (drm_mode_parse_cmdline_int(delim, &margin)) 1719 return -EINVAL; 1720 1721 mode->tv_margins.top = margin; 1722 } else if (!strncmp(option, "margin_bottom", delim - option)) { 1723 if (drm_mode_parse_cmdline_int(delim, &margin)) 1724 return -EINVAL; 1725 1726 mode->tv_margins.bottom = margin; 1727 } else if (!strncmp(option, "panel_orientation", delim - option)) { 1728 if (drm_mode_parse_panel_orientation(delim, mode)) 1729 return -EINVAL; 1730 } else { 1731 return -EINVAL; 1732 } 1733 sep = strchr(delim, ','); 1734 option = sep + 1; 1735 } while (sep); 1736 1737 if (rotation && freestanding) 1738 return -EINVAL; 1739 1740 if (!(rotation & DRM_MODE_ROTATE_MASK)) 1741 rotation |= DRM_MODE_ROTATE_0; 1742 1743 /* Make sure there is exactly one rotation defined */ 1744 if (!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK)) 1745 return -EINVAL; 1746 1747 mode->rotation_reflection = rotation; 1748 1749 return 0; 1750 } 1751 1752 static const char * const drm_named_modes_whitelist[] = { 1753 "NTSC", 1754 "PAL", 1755 }; 1756 1757 /** 1758 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector 1759 * @mode_option: optional per connector mode option 1760 * @connector: connector to parse modeline for 1761 * @mode: preallocated drm_cmdline_mode structure to fill out 1762 * 1763 * This parses @mode_option command line modeline for modes and options to 1764 * configure the connector. If @mode_option is NULL the default command line 1765 * modeline in fb_mode_option will be parsed instead. 1766 * 1767 * This uses the same parameters as the fb modedb.c, except for an extra 1768 * force-enable, force-enable-digital and force-disable bit at the end:: 1769 * 1770 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd] 1771 * 1772 * Additionals options can be provided following the mode, using a comma to 1773 * separate each option. Valid options can be found in 1774 * Documentation/fb/modedb.rst. 1775 * 1776 * The intermediate drm_cmdline_mode structure is required to store additional 1777 * options from the command line modline like the force-enable/disable flag. 1778 * 1779 * Returns: 1780 * True if a valid modeline has been parsed, false otherwise. 1781 */ 1782 bool drm_mode_parse_command_line_for_connector(const char *mode_option, 1783 const struct drm_connector *connector, 1784 struct drm_cmdline_mode *mode) 1785 { 1786 const char *name; 1787 bool freestanding = false, parse_extras = false; 1788 unsigned int bpp_off = 0, refresh_off = 0, options_off = 0; 1789 unsigned int mode_end = 0; 1790 const char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL; 1791 const char *options_ptr = NULL; 1792 char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL; 1793 int i, len, ret; 1794 1795 memset(mode, 0, sizeof(*mode)); 1796 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN; 1797 1798 if (!mode_option) 1799 return false; 1800 1801 name = mode_option; 1802 1803 /* Try to locate the bpp and refresh specifiers, if any */ 1804 bpp_ptr = strchr(name, '-'); 1805 if (bpp_ptr) 1806 bpp_off = bpp_ptr - name; 1807 1808 refresh_ptr = strchr(name, '@'); 1809 if (refresh_ptr) 1810 refresh_off = refresh_ptr - name; 1811 1812 /* Locate the start of named options */ 1813 options_ptr = strchr(name, ','); 1814 if (options_ptr) 1815 options_off = options_ptr - name; 1816 1817 /* Locate the end of the name / resolution, and parse it */ 1818 if (bpp_ptr) { 1819 mode_end = bpp_off; 1820 } else if (refresh_ptr) { 1821 mode_end = refresh_off; 1822 } else if (options_ptr) { 1823 mode_end = options_off; 1824 parse_extras = true; 1825 } else { 1826 mode_end = strlen(name); 1827 parse_extras = true; 1828 } 1829 1830 /* First check for a named mode */ 1831 for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) { 1832 ret = str_has_prefix(name, drm_named_modes_whitelist[i]); 1833 if (ret == mode_end) { 1834 if (refresh_ptr) 1835 return false; /* named + refresh is invalid */ 1836 1837 strcpy(mode->name, drm_named_modes_whitelist[i]); 1838 mode->specified = true; 1839 break; 1840 } 1841 } 1842 1843 /* No named mode? Check for a normal mode argument, e.g. 1024x768 */ 1844 if (!mode->specified && isdigit(name[0])) { 1845 ret = drm_mode_parse_cmdline_res_mode(name, mode_end, 1846 parse_extras, 1847 connector, 1848 mode); 1849 if (ret) 1850 return false; 1851 1852 mode->specified = true; 1853 } 1854 1855 /* No mode? Check for freestanding extras and/or options */ 1856 if (!mode->specified) { 1857 unsigned int len = strlen(mode_option); 1858 1859 if (bpp_ptr || refresh_ptr) 1860 return false; /* syntax error */ 1861 1862 if (len == 1 || (len >= 2 && mode_option[1] == ',')) 1863 extra_ptr = mode_option; 1864 else 1865 options_ptr = mode_option - 1; 1866 1867 freestanding = true; 1868 } 1869 1870 if (bpp_ptr) { 1871 ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode); 1872 if (ret) 1873 return false; 1874 1875 mode->bpp_specified = true; 1876 } 1877 1878 if (refresh_ptr) { 1879 ret = drm_mode_parse_cmdline_refresh(refresh_ptr, 1880 &refresh_end_ptr, mode); 1881 if (ret) 1882 return false; 1883 1884 mode->refresh_specified = true; 1885 } 1886 1887 /* 1888 * Locate the end of the bpp / refresh, and parse the extras 1889 * if relevant 1890 */ 1891 if (bpp_ptr && refresh_ptr) 1892 extra_ptr = max(bpp_end_ptr, refresh_end_ptr); 1893 else if (bpp_ptr) 1894 extra_ptr = bpp_end_ptr; 1895 else if (refresh_ptr) 1896 extra_ptr = refresh_end_ptr; 1897 1898 if (extra_ptr) { 1899 if (options_ptr) 1900 len = options_ptr - extra_ptr; 1901 else 1902 len = strlen(extra_ptr); 1903 1904 ret = drm_mode_parse_cmdline_extra(extra_ptr, len, freestanding, 1905 connector, mode); 1906 if (ret) 1907 return false; 1908 } 1909 1910 if (options_ptr) { 1911 ret = drm_mode_parse_cmdline_options(options_ptr + 1, 1912 freestanding, 1913 connector, mode); 1914 if (ret) 1915 return false; 1916 } 1917 1918 return true; 1919 } 1920 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector); 1921 1922 /** 1923 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode 1924 * @dev: DRM device to create the new mode for 1925 * @cmd: input command line modeline 1926 * 1927 * Returns: 1928 * Pointer to converted mode on success, NULL on error. 1929 */ 1930 struct drm_display_mode * 1931 drm_mode_create_from_cmdline_mode(struct drm_device *dev, 1932 struct drm_cmdline_mode *cmd) 1933 { 1934 struct drm_display_mode *mode; 1935 1936 if (cmd->xres == 0 || cmd->yres == 0) 1937 return NULL; 1938 1939 if (cmd->cvt) 1940 mode = drm_cvt_mode(dev, 1941 cmd->xres, cmd->yres, 1942 cmd->refresh_specified ? cmd->refresh : 60, 1943 cmd->rb, cmd->interlace, 1944 cmd->margins); 1945 else 1946 mode = drm_gtf_mode(dev, 1947 cmd->xres, cmd->yres, 1948 cmd->refresh_specified ? cmd->refresh : 60, 1949 cmd->interlace, 1950 cmd->margins); 1951 if (!mode) 1952 return NULL; 1953 1954 mode->type |= DRM_MODE_TYPE_USERDEF; 1955 /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */ 1956 if (cmd->xres == 1366) 1957 drm_mode_fixup_1366x768(mode); 1958 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 1959 return mode; 1960 } 1961 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode); 1962 1963 /** 1964 * drm_mode_convert_to_umode - convert a drm_display_mode into a modeinfo 1965 * @out: drm_mode_modeinfo struct to return to the user 1966 * @in: drm_display_mode to use 1967 * 1968 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to 1969 * the user. 1970 */ 1971 void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out, 1972 const struct drm_display_mode *in) 1973 { 1974 out->clock = in->clock; 1975 out->hdisplay = in->hdisplay; 1976 out->hsync_start = in->hsync_start; 1977 out->hsync_end = in->hsync_end; 1978 out->htotal = in->htotal; 1979 out->hskew = in->hskew; 1980 out->vdisplay = in->vdisplay; 1981 out->vsync_start = in->vsync_start; 1982 out->vsync_end = in->vsync_end; 1983 out->vtotal = in->vtotal; 1984 out->vscan = in->vscan; 1985 out->vrefresh = drm_mode_vrefresh(in); 1986 out->flags = in->flags; 1987 out->type = in->type; 1988 1989 switch (in->picture_aspect_ratio) { 1990 case HDMI_PICTURE_ASPECT_4_3: 1991 out->flags |= DRM_MODE_FLAG_PIC_AR_4_3; 1992 break; 1993 case HDMI_PICTURE_ASPECT_16_9: 1994 out->flags |= DRM_MODE_FLAG_PIC_AR_16_9; 1995 break; 1996 case HDMI_PICTURE_ASPECT_64_27: 1997 out->flags |= DRM_MODE_FLAG_PIC_AR_64_27; 1998 break; 1999 case HDMI_PICTURE_ASPECT_256_135: 2000 out->flags |= DRM_MODE_FLAG_PIC_AR_256_135; 2001 break; 2002 default: 2003 WARN(1, "Invalid aspect ratio (0%x) on mode\n", 2004 in->picture_aspect_ratio); 2005 fallthrough; 2006 case HDMI_PICTURE_ASPECT_NONE: 2007 out->flags |= DRM_MODE_FLAG_PIC_AR_NONE; 2008 break; 2009 } 2010 2011 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 2012 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 2013 } 2014 2015 /** 2016 * drm_mode_convert_umode - convert a modeinfo into a drm_display_mode 2017 * @dev: drm device 2018 * @out: drm_display_mode to return to the user 2019 * @in: drm_mode_modeinfo to use 2020 * 2021 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to 2022 * the caller. 2023 * 2024 * Returns: 2025 * Zero on success, negative errno on failure. 2026 */ 2027 int drm_mode_convert_umode(struct drm_device *dev, 2028 struct drm_display_mode *out, 2029 const struct drm_mode_modeinfo *in) 2030 { 2031 if (in->clock > INT_MAX || in->vrefresh > INT_MAX) 2032 return -ERANGE; 2033 2034 out->clock = in->clock; 2035 out->hdisplay = in->hdisplay; 2036 out->hsync_start = in->hsync_start; 2037 out->hsync_end = in->hsync_end; 2038 out->htotal = in->htotal; 2039 out->hskew = in->hskew; 2040 out->vdisplay = in->vdisplay; 2041 out->vsync_start = in->vsync_start; 2042 out->vsync_end = in->vsync_end; 2043 out->vtotal = in->vtotal; 2044 out->vscan = in->vscan; 2045 out->flags = in->flags; 2046 /* 2047 * Old xf86-video-vmware (possibly others too) used to 2048 * leave 'type' uninitialized. Just ignore any bits we 2049 * don't like. It's a just hint after all, and more 2050 * useful for the kernel->userspace direction anyway. 2051 */ 2052 out->type = in->type & DRM_MODE_TYPE_ALL; 2053 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN); 2054 out->name[DRM_DISPLAY_MODE_LEN-1] = 0; 2055 2056 /* Clearing picture aspect ratio bits from out flags, 2057 * as the aspect-ratio information is not stored in 2058 * flags for kernel-mode, but in picture_aspect_ratio. 2059 */ 2060 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK; 2061 2062 switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) { 2063 case DRM_MODE_FLAG_PIC_AR_4_3: 2064 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3; 2065 break; 2066 case DRM_MODE_FLAG_PIC_AR_16_9: 2067 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9; 2068 break; 2069 case DRM_MODE_FLAG_PIC_AR_64_27: 2070 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27; 2071 break; 2072 case DRM_MODE_FLAG_PIC_AR_256_135: 2073 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135; 2074 break; 2075 case DRM_MODE_FLAG_PIC_AR_NONE: 2076 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE; 2077 break; 2078 default: 2079 return -EINVAL; 2080 } 2081 2082 out->status = drm_mode_validate_driver(dev, out); 2083 if (out->status != MODE_OK) 2084 return -EINVAL; 2085 2086 drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V); 2087 2088 return 0; 2089 } 2090 2091 /** 2092 * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420 2093 * output format 2094 * 2095 * @display: display under action 2096 * @mode: video mode to be tested. 2097 * 2098 * Returns: 2099 * true if the mode can be supported in YCBCR420 format 2100 * false if not. 2101 */ 2102 bool drm_mode_is_420_only(const struct drm_display_info *display, 2103 const struct drm_display_mode *mode) 2104 { 2105 u8 vic = drm_match_cea_mode(mode); 2106 2107 return test_bit(vic, display->hdmi.y420_vdb_modes); 2108 } 2109 EXPORT_SYMBOL(drm_mode_is_420_only); 2110 2111 /** 2112 * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420 2113 * output format also (along with RGB/YCBCR444/422) 2114 * 2115 * @display: display under action. 2116 * @mode: video mode to be tested. 2117 * 2118 * Returns: 2119 * true if the mode can be support YCBCR420 format 2120 * false if not. 2121 */ 2122 bool drm_mode_is_420_also(const struct drm_display_info *display, 2123 const struct drm_display_mode *mode) 2124 { 2125 u8 vic = drm_match_cea_mode(mode); 2126 2127 return test_bit(vic, display->hdmi.y420_cmdb_modes); 2128 } 2129 EXPORT_SYMBOL(drm_mode_is_420_also); 2130 /** 2131 * drm_mode_is_420 - if a given videomode can be supported in YCBCR420 2132 * output format 2133 * 2134 * @display: display under action. 2135 * @mode: video mode to be tested. 2136 * 2137 * Returns: 2138 * true if the mode can be supported in YCBCR420 format 2139 * false if not. 2140 */ 2141 bool drm_mode_is_420(const struct drm_display_info *display, 2142 const struct drm_display_mode *mode) 2143 { 2144 return drm_mode_is_420_only(display, mode) || 2145 drm_mode_is_420_also(display, mode); 2146 } 2147 EXPORT_SYMBOL(drm_mode_is_420); 2148