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
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
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).
33 #include <linux/ctype.h>
34 #include <linux/list.h>
35 #include <linux/list_sort.h>
36 #include <linux/export.h>
38 #include <video/of_videomode.h>
39 #include <video/videomode.h>
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_device.h>
43 #include <drm/drm_modes.h>
44 #include <drm/drm_print.h>
46 #include "drm_crtc_internal.h"
49 * drm_mode_debug_printmodeline - print a mode to dmesg
50 * @mode: mode to print
52 * Describe @mode using DRM_DEBUG.
54 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
56 DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
58 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
61 * drm_mode_create - create a new display mode
64 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
68 * Pointer to new mode on success, NULL on error.
70 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
72 struct drm_display_mode *nmode;
74 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
80 EXPORT_SYMBOL(drm_mode_create);
83 * drm_mode_destroy - remove a mode
85 * @mode: mode to remove
87 * Release @mode's unique ID, then free it @mode structure itself using kfree.
89 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
96 EXPORT_SYMBOL(drm_mode_destroy);
99 * drm_mode_probed_add - add a mode to a connector's probed_mode list
100 * @connector: connector the new mode
103 * Add @mode to @connector's probed_mode list for later use. This list should
104 * then in a second step get filtered and all the modes actually supported by
105 * the hardware moved to the @connector's modes list.
107 void drm_mode_probed_add(struct drm_connector *connector,
108 struct drm_display_mode *mode)
110 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
112 list_add_tail(&mode->head, &connector->probed_modes);
114 EXPORT_SYMBOL(drm_mode_probed_add);
117 * drm_cvt_mode -create a modeline based on the CVT algorithm
119 * @hdisplay: hdisplay size
120 * @vdisplay: vdisplay size
121 * @vrefresh: vrefresh rate
122 * @reduced: whether to use reduced blanking
123 * @interlaced: whether to compute an interlaced mode
124 * @margins: whether to add margins (borders)
126 * This function is called to generate the modeline based on CVT algorithm
127 * according to the hdisplay, vdisplay, vrefresh.
128 * It is based from the VESA(TM) Coordinated Video Timing Generator by
129 * Graham Loveridge April 9, 2003 available at
130 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
132 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
133 * What I have done is to translate it by using integer calculation.
136 * The modeline based on the CVT algorithm stored in a drm_display_mode object.
137 * The display mode object is allocated with drm_mode_create(). Returns NULL
138 * when no mode could be allocated.
140 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
141 int vdisplay, int vrefresh,
142 bool reduced, bool interlaced, bool margins)
144 #define HV_FACTOR 1000
145 /* 1) top/bottom margin size (% of height) - default: 1.8, */
146 #define CVT_MARGIN_PERCENTAGE 18
147 /* 2) character cell horizontal granularity (pixels) - default 8 */
148 #define CVT_H_GRANULARITY 8
149 /* 3) Minimum vertical porch (lines) - default 3 */
150 #define CVT_MIN_V_PORCH 3
151 /* 4) Minimum number of vertical back porch lines - default 6 */
152 #define CVT_MIN_V_BPORCH 6
153 /* Pixel Clock step (kHz) */
154 #define CVT_CLOCK_STEP 250
155 struct drm_display_mode *drm_mode;
156 unsigned int vfieldrate, hperiod;
157 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
161 /* allocate the drm_display_mode structure. If failure, we will
164 drm_mode = drm_mode_create(dev);
168 /* the CVT default refresh rate is 60Hz */
172 /* the required field fresh rate */
174 vfieldrate = vrefresh * 2;
176 vfieldrate = vrefresh;
178 /* horizontal pixels */
179 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
181 /* determine the left&right borders */
184 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
185 hmargin -= hmargin % CVT_H_GRANULARITY;
187 /* find the total active pixels */
188 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
190 /* find the number of lines per field */
192 vdisplay_rnd = vdisplay / 2;
194 vdisplay_rnd = vdisplay;
196 /* find the top & bottom borders */
199 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
201 drm_mode->vdisplay = vdisplay + 2 * vmargin;
209 /* Determine VSync Width from aspect ratio */
210 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
212 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
214 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
216 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
218 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
224 /* simplify the GTF calculation */
225 /* 4) Minimum time of vertical sync + back porch interval (µs)
229 #define CVT_MIN_VSYNC_BP 550
230 /* 3) Nominal HSync width (% of line period) - default 8 */
231 #define CVT_HSYNC_PERCENTAGE 8
232 unsigned int hblank_percentage;
233 int vsyncandback_porch, vback_porch, hblank;
235 /* estimated the horizontal period */
236 tmp1 = HV_FACTOR * 1000000 -
237 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
238 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
240 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
242 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
243 /* 9. Find number of lines in sync + backporch */
244 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
245 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
247 vsyncandback_porch = tmp1;
248 /* 10. Find number of lines in back porch */
249 vback_porch = vsyncandback_porch - vsync;
250 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
251 vsyncandback_porch + CVT_MIN_V_PORCH;
252 /* 5) Definition of Horizontal blanking time limitation */
253 /* Gradient (%/kHz) - default 600 */
254 #define CVT_M_FACTOR 600
255 /* Offset (%) - default 40 */
256 #define CVT_C_FACTOR 40
257 /* Blanking time scaling factor - default 128 */
258 #define CVT_K_FACTOR 128
259 /* Scaling factor weighting - default 20 */
260 #define CVT_J_FACTOR 20
261 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
262 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
264 /* 12. Find ideal blanking duty cycle from formula */
265 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
267 /* 13. Blanking time */
268 if (hblank_percentage < 20 * HV_FACTOR)
269 hblank_percentage = 20 * HV_FACTOR;
270 hblank = drm_mode->hdisplay * hblank_percentage /
271 (100 * HV_FACTOR - hblank_percentage);
272 hblank -= hblank % (2 * CVT_H_GRANULARITY);
273 /* 14. find the total pixels per line */
274 drm_mode->htotal = drm_mode->hdisplay + hblank;
275 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
276 drm_mode->hsync_start = drm_mode->hsync_end -
277 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
278 drm_mode->hsync_start += CVT_H_GRANULARITY -
279 drm_mode->hsync_start % CVT_H_GRANULARITY;
280 /* fill the Vsync values */
281 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
282 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
284 /* Reduced blanking */
285 /* Minimum vertical blanking interval time (µs)- default 460 */
286 #define CVT_RB_MIN_VBLANK 460
287 /* Fixed number of clocks for horizontal sync */
288 #define CVT_RB_H_SYNC 32
289 /* Fixed number of clocks for horizontal blanking */
290 #define CVT_RB_H_BLANK 160
291 /* Fixed number of lines for vertical front porch - default 3*/
292 #define CVT_RB_VFPORCH 3
295 /* 8. Estimate Horizontal period. */
296 tmp1 = HV_FACTOR * 1000000 -
297 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
298 tmp2 = vdisplay_rnd + 2 * vmargin;
299 hperiod = tmp1 / (tmp2 * vfieldrate);
300 /* 9. Find number of lines in vertical blanking */
301 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
302 /* 10. Check if vertical blanking is sufficient */
303 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
304 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
305 /* 11. Find total number of lines in vertical field */
306 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
307 /* 12. Find total number of pixels in a line */
308 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
309 /* Fill in HSync values */
310 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
311 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
312 /* Fill in VSync values */
313 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
314 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
316 /* 15/13. Find pixel clock frequency (kHz for xf86) */
317 tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
318 tmp *= HV_FACTOR * 1000;
319 do_div(tmp, hperiod);
320 tmp -= drm_mode->clock % CVT_CLOCK_STEP;
321 drm_mode->clock = tmp;
322 /* 18/16. Find actual vertical frame frequency */
323 /* ignore - just set the mode flag for interlaced */
325 drm_mode->vtotal *= 2;
326 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
328 /* Fill the mode line name */
329 drm_mode_set_name(drm_mode);
331 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
332 DRM_MODE_FLAG_NVSYNC);
334 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
335 DRM_MODE_FLAG_NHSYNC);
339 EXPORT_SYMBOL(drm_cvt_mode);
342 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
344 * @hdisplay: hdisplay size
345 * @vdisplay: vdisplay size
346 * @vrefresh: vrefresh rate.
347 * @interlaced: whether to compute an interlaced mode
348 * @margins: desired margin (borders) size
349 * @GTF_M: extended GTF formula parameters
350 * @GTF_2C: extended GTF formula parameters
351 * @GTF_K: extended GTF formula parameters
352 * @GTF_2J: extended GTF formula parameters
354 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
355 * in here multiplied by two. For a C of 40, pass in 80.
358 * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
359 * The display mode object is allocated with drm_mode_create(). Returns NULL
360 * when no mode could be allocated.
362 struct drm_display_mode *
363 drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
364 int vrefresh, bool interlaced, int margins,
365 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
366 { /* 1) top/bottom margin size (% of height) - default: 1.8, */
367 #define GTF_MARGIN_PERCENTAGE 18
368 /* 2) character cell horizontal granularity (pixels) - default 8 */
369 #define GTF_CELL_GRAN 8
370 /* 3) Minimum vertical porch (lines) - default 3 */
371 #define GTF_MIN_V_PORCH 1
372 /* width of vsync in lines */
374 /* width of hsync as % of total line */
375 #define H_SYNC_PERCENT 8
376 /* min time of vsync + back porch (microsec) */
377 #define MIN_VSYNC_PLUS_BP 550
378 /* C' and M' are part of the Blanking Duty Cycle computation */
379 #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
380 #define GTF_M_PRIME (GTF_K * GTF_M / 256)
381 struct drm_display_mode *drm_mode;
382 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
383 int top_margin, bottom_margin;
385 unsigned int hfreq_est;
386 int vsync_plus_bp, vback_porch;
387 unsigned int vtotal_lines, vfieldrate_est, hperiod;
388 unsigned int vfield_rate, vframe_rate;
389 int left_margin, right_margin;
390 unsigned int total_active_pixels, ideal_duty_cycle;
391 unsigned int hblank, total_pixels, pixel_freq;
392 int hsync, hfront_porch, vodd_front_porch_lines;
393 unsigned int tmp1, tmp2;
395 drm_mode = drm_mode_create(dev);
399 /* 1. In order to give correct results, the number of horizontal
400 * pixels requested is first processed to ensure that it is divisible
401 * by the character size, by rounding it to the nearest character
404 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
405 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
407 /* 2. If interlace is requested, the number of vertical lines assumed
408 * by the calculation must be halved, as the computation calculates
409 * the number of vertical lines per field.
412 vdisplay_rnd = vdisplay / 2;
414 vdisplay_rnd = vdisplay;
416 /* 3. Find the frame rate required: */
418 vfieldrate_rqd = vrefresh * 2;
420 vfieldrate_rqd = vrefresh;
422 /* 4. Find number of lines in Top margin: */
425 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
427 /* 5. Find number of lines in bottom margin: */
428 bottom_margin = top_margin;
430 /* 6. If interlace is required, then set variable interlace: */
436 /* 7. Estimate the Horizontal frequency */
438 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
439 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
441 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
444 /* 8. Find the number of lines in V sync + back porch */
445 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
446 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
447 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
448 /* 9. Find the number of lines in V back porch alone: */
449 vback_porch = vsync_plus_bp - V_SYNC_RQD;
450 /* 10. Find the total number of lines in Vertical field period: */
451 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
452 vsync_plus_bp + GTF_MIN_V_PORCH;
453 /* 11. Estimate the Vertical field frequency: */
454 vfieldrate_est = hfreq_est / vtotal_lines;
455 /* 12. Find the actual horizontal period: */
456 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
458 /* 13. Find the actual Vertical field frequency: */
459 vfield_rate = hfreq_est / vtotal_lines;
460 /* 14. Find the Vertical frame frequency: */
462 vframe_rate = vfield_rate / 2;
464 vframe_rate = vfield_rate;
465 /* 15. Find number of pixels in left margin: */
467 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
472 /* 16.Find number of pixels in right margin: */
473 right_margin = left_margin;
474 /* 17.Find total number of active pixels in image and left and right */
475 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
476 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
477 ideal_duty_cycle = GTF_C_PRIME * 1000 -
478 (GTF_M_PRIME * 1000000 / hfreq_est);
479 /* 19.Find the number of pixels in the blanking time to the nearest
480 * double character cell: */
481 hblank = total_active_pixels * ideal_duty_cycle /
482 (100000 - ideal_duty_cycle);
483 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
484 hblank = hblank * 2 * GTF_CELL_GRAN;
485 /* 20.Find total number of pixels: */
486 total_pixels = total_active_pixels + hblank;
487 /* 21.Find pixel clock frequency: */
488 pixel_freq = total_pixels * hfreq_est / 1000;
489 /* Stage 1 computations are now complete; I should really pass
490 * the results to another function and do the Stage 2 computations,
491 * but I only need a few more values so I'll just append the
492 * computations here for now */
493 /* 17. Find the number of pixels in the horizontal sync period: */
494 hsync = H_SYNC_PERCENT * total_pixels / 100;
495 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
496 hsync = hsync * GTF_CELL_GRAN;
497 /* 18. Find the number of pixels in horizontal front porch period */
498 hfront_porch = hblank / 2 - hsync;
499 /* 36. Find the number of lines in the odd front porch period: */
500 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
502 /* finally, pack the results in the mode struct */
503 drm_mode->hdisplay = hdisplay_rnd;
504 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
505 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
506 drm_mode->htotal = total_pixels;
507 drm_mode->vdisplay = vdisplay_rnd;
508 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
509 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
510 drm_mode->vtotal = vtotal_lines;
512 drm_mode->clock = pixel_freq;
515 drm_mode->vtotal *= 2;
516 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
519 drm_mode_set_name(drm_mode);
520 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
521 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
523 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
527 EXPORT_SYMBOL(drm_gtf_mode_complex);
530 * drm_gtf_mode - create the modeline based on the GTF algorithm
532 * @hdisplay: hdisplay size
533 * @vdisplay: vdisplay size
534 * @vrefresh: vrefresh rate.
535 * @interlaced: whether to compute an interlaced mode
536 * @margins: desired margin (borders) size
538 * return the modeline based on GTF algorithm
540 * This function is to create the modeline based on the GTF algorithm.
541 * Generalized Timing Formula is derived from:
543 * GTF Spreadsheet by Andy Morrish (1/5/97)
544 * available at http://www.vesa.org
546 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
547 * What I have done is to translate it by using integer calculation.
548 * I also refer to the function of fb_get_mode in the file of
549 * drivers/video/fbmon.c
551 * Standard GTF parameters::
559 * The modeline based on the GTF algorithm stored in a drm_display_mode object.
560 * The display mode object is allocated with drm_mode_create(). Returns NULL
561 * when no mode could be allocated.
563 struct drm_display_mode *
564 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
565 bool interlaced, int margins)
567 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
569 600, 40 * 2, 128, 20 * 2);
571 EXPORT_SYMBOL(drm_gtf_mode);
573 #ifdef CONFIG_VIDEOMODE_HELPERS
575 * drm_display_mode_from_videomode - fill in @dmode using @vm,
576 * @vm: videomode structure to use as source
577 * @dmode: drm_display_mode structure to use as destination
579 * Fills out @dmode using the display mode specified in @vm.
581 void drm_display_mode_from_videomode(const struct videomode *vm,
582 struct drm_display_mode *dmode)
584 dmode->hdisplay = vm->hactive;
585 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
586 dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
587 dmode->htotal = dmode->hsync_end + vm->hback_porch;
589 dmode->vdisplay = vm->vactive;
590 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
591 dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
592 dmode->vtotal = dmode->vsync_end + vm->vback_porch;
594 dmode->clock = vm->pixelclock / 1000;
597 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
598 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
599 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
600 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
601 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
602 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
603 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
604 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
605 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
606 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
607 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
608 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
609 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
610 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
611 drm_mode_set_name(dmode);
613 EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
616 * drm_display_mode_to_videomode - fill in @vm using @dmode,
617 * @dmode: drm_display_mode structure to use as source
618 * @vm: videomode structure to use as destination
620 * Fills out @vm using the display mode specified in @dmode.
622 void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
623 struct videomode *vm)
625 vm->hactive = dmode->hdisplay;
626 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
627 vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
628 vm->hback_porch = dmode->htotal - dmode->hsync_end;
630 vm->vactive = dmode->vdisplay;
631 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
632 vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
633 vm->vback_porch = dmode->vtotal - dmode->vsync_end;
635 vm->pixelclock = dmode->clock * 1000;
638 if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
639 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
640 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
641 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
642 if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
643 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
644 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
645 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
646 if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
647 vm->flags |= DISPLAY_FLAGS_INTERLACED;
648 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
649 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
650 if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
651 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
653 EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
656 * drm_bus_flags_from_videomode - extract information about pixelclk and
657 * DE polarity from videomode and store it in a separate variable
658 * @vm: videomode structure to use
659 * @bus_flags: information about pixelclk, sync and DE polarity will be stored
662 * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
663 * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
666 void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
669 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
670 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
671 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
672 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
674 if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
675 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
676 if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
677 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
679 if (vm->flags & DISPLAY_FLAGS_DE_LOW)
680 *bus_flags |= DRM_BUS_FLAG_DE_LOW;
681 if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
682 *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
684 EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
688 * of_get_drm_display_mode - get a drm_display_mode from devicetree
689 * @np: device_node with the timing specification
690 * @dmode: will be set to the return value
691 * @bus_flags: information about pixelclk, sync and DE polarity
692 * @index: index into the list of display timings in devicetree
694 * This function is expensive and should only be used, if only one mode is to be
695 * read from DT. To get multiple modes start with of_get_display_timings and
696 * work with that instead.
699 * 0 on success, a negative errno code when no of videomode node was found.
701 int of_get_drm_display_mode(struct device_node *np,
702 struct drm_display_mode *dmode, u32 *bus_flags,
708 ret = of_get_videomode(np, &vm, index);
712 drm_display_mode_from_videomode(&vm, dmode);
714 drm_bus_flags_from_videomode(&vm, bus_flags);
716 pr_debug("%pOF: got %dx%d display mode\n",
717 np, vm.hactive, vm.vactive);
718 drm_mode_debug_printmodeline(dmode);
722 EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
723 #endif /* CONFIG_OF */
724 #endif /* CONFIG_VIDEOMODE_HELPERS */
727 * drm_mode_set_name - set the name on a mode
728 * @mode: name will be set in this mode
730 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
731 * with an optional 'i' suffix for interlaced modes.
733 void drm_mode_set_name(struct drm_display_mode *mode)
735 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
737 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
738 mode->hdisplay, mode->vdisplay,
739 interlaced ? "i" : "");
741 EXPORT_SYMBOL(drm_mode_set_name);
744 * drm_mode_hsync - get the hsync of a mode
748 * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
749 * value first if it is not yet set.
751 int drm_mode_hsync(const struct drm_display_mode *mode)
753 unsigned int calc_val;
758 if (mode->htotal <= 0)
761 calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
762 calc_val += 500; /* round to 1000Hz */
763 calc_val /= 1000; /* truncate to kHz */
767 EXPORT_SYMBOL(drm_mode_hsync);
770 * drm_mode_vrefresh - get the vrefresh of a mode
774 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
775 * value first if it is not yet set.
777 int drm_mode_vrefresh(const struct drm_display_mode *mode)
781 if (mode->vrefresh > 0)
782 refresh = mode->vrefresh;
783 else if (mode->htotal > 0 && mode->vtotal > 0) {
784 unsigned int num, den;
786 num = mode->clock * 1000;
787 den = mode->htotal * mode->vtotal;
789 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
791 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
796 refresh = DIV_ROUND_CLOSEST(num, den);
800 EXPORT_SYMBOL(drm_mode_vrefresh);
803 * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
804 * @mode: mode to query
805 * @hdisplay: hdisplay value to fill in
806 * @vdisplay: vdisplay value to fill in
808 * The vdisplay value will be doubled if the specified mode is a stereo mode of
809 * the appropriate layout.
811 void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
812 int *hdisplay, int *vdisplay)
814 struct drm_display_mode adjusted = *mode;
816 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
817 *hdisplay = adjusted.crtc_hdisplay;
818 *vdisplay = adjusted.crtc_vdisplay;
820 EXPORT_SYMBOL(drm_mode_get_hv_timing);
823 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
825 * @adjust_flags: a combination of adjustment flags
827 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
829 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
831 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
832 * buffers containing two eyes (only adjust the timings when needed, eg. for
833 * "frame packing" or "side by side full").
834 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
835 * be performed for doublescan and vscan > 1 modes respectively.
837 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
842 p->crtc_clock = p->clock;
843 p->crtc_hdisplay = p->hdisplay;
844 p->crtc_hsync_start = p->hsync_start;
845 p->crtc_hsync_end = p->hsync_end;
846 p->crtc_htotal = p->htotal;
847 p->crtc_hskew = p->hskew;
848 p->crtc_vdisplay = p->vdisplay;
849 p->crtc_vsync_start = p->vsync_start;
850 p->crtc_vsync_end = p->vsync_end;
851 p->crtc_vtotal = p->vtotal;
853 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
854 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
855 p->crtc_vdisplay /= 2;
856 p->crtc_vsync_start /= 2;
857 p->crtc_vsync_end /= 2;
862 if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
863 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
864 p->crtc_vdisplay *= 2;
865 p->crtc_vsync_start *= 2;
866 p->crtc_vsync_end *= 2;
871 if (!(adjust_flags & CRTC_NO_VSCAN)) {
873 p->crtc_vdisplay *= p->vscan;
874 p->crtc_vsync_start *= p->vscan;
875 p->crtc_vsync_end *= p->vscan;
876 p->crtc_vtotal *= p->vscan;
880 if (adjust_flags & CRTC_STEREO_DOUBLE) {
881 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
884 case DRM_MODE_FLAG_3D_FRAME_PACKING:
886 p->crtc_vdisplay += p->crtc_vtotal;
887 p->crtc_vsync_start += p->crtc_vtotal;
888 p->crtc_vsync_end += p->crtc_vtotal;
889 p->crtc_vtotal += p->crtc_vtotal;
894 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
895 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
896 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
897 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
899 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
902 * drm_mode_copy - copy the mode
903 * @dst: mode to overwrite
906 * Copy an existing mode into another mode, preserving the object id and
907 * list head of the destination mode.
909 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
911 struct list_head head = dst->head;
916 EXPORT_SYMBOL(drm_mode_copy);
919 * drm_mode_duplicate - allocate and duplicate an existing mode
920 * @dev: drm_device to allocate the duplicated mode for
921 * @mode: mode to duplicate
923 * Just allocate a new mode, copy the existing mode into it, and return
924 * a pointer to it. Used to create new instances of established modes.
927 * Pointer to duplicated mode on success, NULL on error.
929 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
930 const struct drm_display_mode *mode)
932 struct drm_display_mode *nmode;
934 nmode = drm_mode_create(dev);
938 drm_mode_copy(nmode, mode);
942 EXPORT_SYMBOL(drm_mode_duplicate);
944 static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
945 const struct drm_display_mode *mode2)
947 return mode1->hdisplay == mode2->hdisplay &&
948 mode1->hsync_start == mode2->hsync_start &&
949 mode1->hsync_end == mode2->hsync_end &&
950 mode1->htotal == mode2->htotal &&
951 mode1->hskew == mode2->hskew &&
952 mode1->vdisplay == mode2->vdisplay &&
953 mode1->vsync_start == mode2->vsync_start &&
954 mode1->vsync_end == mode2->vsync_end &&
955 mode1->vtotal == mode2->vtotal &&
956 mode1->vscan == mode2->vscan;
959 static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
960 const struct drm_display_mode *mode2)
963 * do clock check convert to PICOS
964 * so fb modes get matched the same
966 if (mode1->clock && mode2->clock)
967 return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
969 return mode1->clock == mode2->clock;
972 static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
973 const struct drm_display_mode *mode2)
975 return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
976 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
979 static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
980 const struct drm_display_mode *mode2)
982 return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
983 (mode2->flags & DRM_MODE_FLAG_3D_MASK);
986 static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
987 const struct drm_display_mode *mode2)
989 return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
993 * drm_mode_match - test modes for (partial) equality
995 * @mode2: second mode
996 * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
998 * Check to see if @mode1 and @mode2 are equivalent.
1001 * True if the modes are (partially) equal, false otherwise.
1003 bool drm_mode_match(const struct drm_display_mode *mode1,
1004 const struct drm_display_mode *mode2,
1005 unsigned int match_flags)
1007 if (!mode1 && !mode2)
1010 if (!mode1 || !mode2)
1013 if (match_flags & DRM_MODE_MATCH_TIMINGS &&
1014 !drm_mode_match_timings(mode1, mode2))
1017 if (match_flags & DRM_MODE_MATCH_CLOCK &&
1018 !drm_mode_match_clock(mode1, mode2))
1021 if (match_flags & DRM_MODE_MATCH_FLAGS &&
1022 !drm_mode_match_flags(mode1, mode2))
1025 if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
1026 !drm_mode_match_3d_flags(mode1, mode2))
1029 if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
1030 !drm_mode_match_aspect_ratio(mode1, mode2))
1035 EXPORT_SYMBOL(drm_mode_match);
1038 * drm_mode_equal - test modes for equality
1039 * @mode1: first mode
1040 * @mode2: second mode
1042 * Check to see if @mode1 and @mode2 are equivalent.
1045 * True if the modes are equal, false otherwise.
1047 bool drm_mode_equal(const struct drm_display_mode *mode1,
1048 const struct drm_display_mode *mode2)
1050 return drm_mode_match(mode1, mode2,
1051 DRM_MODE_MATCH_TIMINGS |
1052 DRM_MODE_MATCH_CLOCK |
1053 DRM_MODE_MATCH_FLAGS |
1054 DRM_MODE_MATCH_3D_FLAGS|
1055 DRM_MODE_MATCH_ASPECT_RATIO);
1057 EXPORT_SYMBOL(drm_mode_equal);
1060 * drm_mode_equal_no_clocks - test modes for equality
1061 * @mode1: first mode
1062 * @mode2: second mode
1064 * Check to see if @mode1 and @mode2 are equivalent, but
1065 * don't check the pixel clocks.
1068 * True if the modes are equal, false otherwise.
1070 bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
1071 const struct drm_display_mode *mode2)
1073 return drm_mode_match(mode1, mode2,
1074 DRM_MODE_MATCH_TIMINGS |
1075 DRM_MODE_MATCH_FLAGS |
1076 DRM_MODE_MATCH_3D_FLAGS);
1078 EXPORT_SYMBOL(drm_mode_equal_no_clocks);
1081 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
1082 * @mode1: first mode
1083 * @mode2: second mode
1085 * Check to see if @mode1 and @mode2 are equivalent, but
1086 * don't check the pixel clocks nor the stereo layout.
1089 * True if the modes are equal, false otherwise.
1091 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
1092 const struct drm_display_mode *mode2)
1094 return drm_mode_match(mode1, mode2,
1095 DRM_MODE_MATCH_TIMINGS |
1096 DRM_MODE_MATCH_FLAGS);
1098 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
1100 static enum drm_mode_status
1101 drm_mode_validate_basic(const struct drm_display_mode *mode)
1103 if (mode->type & ~DRM_MODE_TYPE_ALL)
1106 if (mode->flags & ~DRM_MODE_FLAG_ALL)
1109 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1112 if (mode->clock == 0)
1113 return MODE_CLOCK_LOW;
1115 if (mode->hdisplay == 0 ||
1116 mode->hsync_start < mode->hdisplay ||
1117 mode->hsync_end < mode->hsync_start ||
1118 mode->htotal < mode->hsync_end)
1119 return MODE_H_ILLEGAL;
1121 if (mode->vdisplay == 0 ||
1122 mode->vsync_start < mode->vdisplay ||
1123 mode->vsync_end < mode->vsync_start ||
1124 mode->vtotal < mode->vsync_end)
1125 return MODE_V_ILLEGAL;
1131 * drm_mode_validate_driver - make sure the mode is somewhat sane
1133 * @mode: mode to check
1135 * First do basic validation on the mode, and then allow the driver
1136 * to check for device/driver specific limitations via the optional
1137 * &drm_mode_config_helper_funcs.mode_valid hook.
1142 enum drm_mode_status
1143 drm_mode_validate_driver(struct drm_device *dev,
1144 const struct drm_display_mode *mode)
1146 enum drm_mode_status status;
1148 status = drm_mode_validate_basic(mode);
1149 if (status != MODE_OK)
1152 if (dev->mode_config.funcs->mode_valid)
1153 return dev->mode_config.funcs->mode_valid(dev, mode);
1157 EXPORT_SYMBOL(drm_mode_validate_driver);
1160 * drm_mode_validate_size - make sure modes adhere to size constraints
1161 * @mode: mode to check
1162 * @maxX: maximum width
1163 * @maxY: maximum height
1165 * This function is a helper which can be used to validate modes against size
1166 * limitations of the DRM device/connector. If a mode is too big its status
1167 * member is updated with the appropriate validation failure code. The list
1168 * itself is not changed.
1173 enum drm_mode_status
1174 drm_mode_validate_size(const struct drm_display_mode *mode,
1177 if (maxX > 0 && mode->hdisplay > maxX)
1178 return MODE_VIRTUAL_X;
1180 if (maxY > 0 && mode->vdisplay > maxY)
1181 return MODE_VIRTUAL_Y;
1185 EXPORT_SYMBOL(drm_mode_validate_size);
1188 * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1189 * @mode: mode to check
1190 * @connector: drm connector under action
1192 * This function is a helper which can be used to filter out any YCBCR420
1193 * only mode, when the source doesn't support it.
1198 enum drm_mode_status
1199 drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1200 struct drm_connector *connector)
1202 u8 vic = drm_match_cea_mode(mode);
1203 enum drm_mode_status status = MODE_OK;
1204 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
1206 if (test_bit(vic, hdmi->y420_vdb_modes)) {
1207 if (!connector->ycbcr_420_allowed)
1208 status = MODE_NO_420;
1213 EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1215 #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1217 static const char * const drm_mode_status_names[] = {
1221 MODE_STATUS(H_ILLEGAL),
1222 MODE_STATUS(V_ILLEGAL),
1223 MODE_STATUS(BAD_WIDTH),
1224 MODE_STATUS(NOMODE),
1225 MODE_STATUS(NO_INTERLACE),
1226 MODE_STATUS(NO_DBLESCAN),
1227 MODE_STATUS(NO_VSCAN),
1229 MODE_STATUS(VIRTUAL_X),
1230 MODE_STATUS(VIRTUAL_Y),
1231 MODE_STATUS(MEM_VIRT),
1232 MODE_STATUS(NOCLOCK),
1233 MODE_STATUS(CLOCK_HIGH),
1234 MODE_STATUS(CLOCK_LOW),
1235 MODE_STATUS(CLOCK_RANGE),
1236 MODE_STATUS(BAD_HVALUE),
1237 MODE_STATUS(BAD_VVALUE),
1238 MODE_STATUS(BAD_VSCAN),
1239 MODE_STATUS(HSYNC_NARROW),
1240 MODE_STATUS(HSYNC_WIDE),
1241 MODE_STATUS(HBLANK_NARROW),
1242 MODE_STATUS(HBLANK_WIDE),
1243 MODE_STATUS(VSYNC_NARROW),
1244 MODE_STATUS(VSYNC_WIDE),
1245 MODE_STATUS(VBLANK_NARROW),
1246 MODE_STATUS(VBLANK_WIDE),
1248 MODE_STATUS(INTERLACE_WIDTH),
1249 MODE_STATUS(ONE_WIDTH),
1250 MODE_STATUS(ONE_HEIGHT),
1251 MODE_STATUS(ONE_SIZE),
1252 MODE_STATUS(NO_REDUCED),
1253 MODE_STATUS(NO_STEREO),
1254 MODE_STATUS(NO_420),
1262 const char *drm_get_mode_status_name(enum drm_mode_status status)
1264 int index = status + 3;
1266 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1269 return drm_mode_status_names[index];
1273 * drm_mode_prune_invalid - remove invalid modes from mode list
1275 * @mode_list: list of modes to check
1276 * @verbose: be verbose about it
1278 * This helper function can be used to prune a display mode list after
1279 * validation has been completed. All modes whose status is not MODE_OK will be
1280 * removed from the list, and if @verbose the status code and mode name is also
1283 void drm_mode_prune_invalid(struct drm_device *dev,
1284 struct list_head *mode_list, bool verbose)
1286 struct drm_display_mode *mode, *t;
1288 list_for_each_entry_safe(mode, t, mode_list, head) {
1289 if (mode->status != MODE_OK) {
1290 list_del(&mode->head);
1292 drm_mode_debug_printmodeline(mode);
1293 DRM_DEBUG_KMS("Not using %s mode: %s\n",
1295 drm_get_mode_status_name(mode->status));
1297 drm_mode_destroy(dev, mode);
1301 EXPORT_SYMBOL(drm_mode_prune_invalid);
1304 * drm_mode_compare - compare modes for favorability
1306 * @lh_a: list_head for first mode
1307 * @lh_b: list_head for second mode
1309 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1313 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1314 * positive if @lh_b is better than @lh_a.
1316 static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
1318 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1319 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1322 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1323 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1326 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1330 diff = b->vrefresh - a->vrefresh;
1334 diff = b->clock - a->clock;
1339 * drm_mode_sort - sort mode list
1340 * @mode_list: list of drm_display_mode structures to sort
1342 * Sort @mode_list by favorability, moving good modes to the head of the list.
1344 void drm_mode_sort(struct list_head *mode_list)
1346 list_sort(NULL, mode_list, drm_mode_compare);
1348 EXPORT_SYMBOL(drm_mode_sort);
1351 * drm_connector_list_update - update the mode list for the connector
1352 * @connector: the connector to update
1354 * This moves the modes from the @connector probed_modes list
1355 * to the actual mode list. It compares the probed mode against the current
1356 * list and only adds different/new modes.
1358 * This is just a helper functions doesn't validate any modes itself and also
1359 * doesn't prune any invalid modes. Callers need to do that themselves.
1361 void drm_connector_list_update(struct drm_connector *connector)
1363 struct drm_display_mode *pmode, *pt;
1365 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1367 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1368 struct drm_display_mode *mode;
1369 bool found_it = false;
1371 /* go through current modes checking for the new probed mode */
1372 list_for_each_entry(mode, &connector->modes, head) {
1373 if (!drm_mode_equal(pmode, mode))
1379 * If the old matching mode is stale (ie. left over
1380 * from a previous probe) just replace it outright.
1381 * Otherwise just merge the type bits between all
1382 * equal probed modes.
1384 * If two probed modes are considered equal, pick the
1385 * actual timings from the one that's marked as
1386 * preferred (in case the match isn't 100%). If
1387 * multiple or zero preferred modes are present, favor
1388 * the mode added to the probed_modes list first.
1390 if (mode->status == MODE_STALE) {
1391 drm_mode_copy(mode, pmode);
1392 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1393 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1394 pmode->type |= mode->type;
1395 drm_mode_copy(mode, pmode);
1397 mode->type |= pmode->type;
1400 list_del(&pmode->head);
1401 drm_mode_destroy(connector->dev, pmode);
1406 list_move_tail(&pmode->head, &connector->modes);
1410 EXPORT_SYMBOL(drm_connector_list_update);
1412 static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
1413 struct drm_cmdline_mode *mode)
1421 bpp = simple_strtol(str, end_ptr, 10);
1422 if (*end_ptr == str)
1426 mode->bpp_specified = true;
1431 static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
1432 struct drm_cmdline_mode *mode)
1434 unsigned int refresh;
1440 refresh = simple_strtol(str, end_ptr, 10);
1441 if (*end_ptr == str)
1444 mode->refresh = refresh;
1445 mode->refresh_specified = true;
1450 static int drm_mode_parse_cmdline_extra(const char *str, int length,
1451 struct drm_connector *connector,
1452 struct drm_cmdline_mode *mode)
1456 for (i = 0; i < length; i++) {
1459 mode->interlace = true;
1462 mode->margins = true;
1465 if (mode->force != DRM_FORCE_UNSPECIFIED)
1468 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1469 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1470 mode->force = DRM_FORCE_ON;
1472 mode->force = DRM_FORCE_ON_DIGITAL;
1475 if (mode->force != DRM_FORCE_UNSPECIFIED)
1478 mode->force = DRM_FORCE_OFF;
1481 if (mode->force != DRM_FORCE_UNSPECIFIED)
1484 mode->force = DRM_FORCE_ON;
1494 static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
1496 struct drm_connector *connector,
1497 struct drm_cmdline_mode *mode)
1499 const char *str_start = str;
1500 bool rb = false, cvt = false;
1501 int xres = 0, yres = 0;
1505 xres = simple_strtol(str, &end_ptr, 10);
1509 if (end_ptr[0] != 'x')
1514 yres = simple_strtol(str, &end_ptr, 10);
1518 remaining = length - (end_ptr - str_start);
1522 for (i = 0; i < remaining; i++) {
1523 switch (end_ptr[i]) {
1532 * Try to pass that to our extras parsing
1533 * function to handle the case where the
1534 * extras are directly after the resolution
1537 int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
1557 static int drm_mode_parse_cmdline_options(char *str, size_t len,
1558 struct drm_connector *connector,
1559 struct drm_cmdline_mode *mode)
1561 unsigned int rotation = 0;
1564 while ((sep = strchr(sep, ','))) {
1565 char *delim, *option;
1568 delim = strchr(option, '=');
1570 delim = strchr(option, ',');
1576 if (!strncmp(option, "rotate", delim - option)) {
1577 const char *value = delim + 1;
1580 deg = simple_strtol(value, &sep, 10);
1582 /* Make sure we have parsed something */
1588 rotation |= DRM_MODE_ROTATE_0;
1592 rotation |= DRM_MODE_ROTATE_90;
1596 rotation |= DRM_MODE_ROTATE_180;
1600 rotation |= DRM_MODE_ROTATE_270;
1606 } else if (!strncmp(option, "reflect_x", delim - option)) {
1607 rotation |= DRM_MODE_REFLECT_X;
1609 } else if (!strncmp(option, "reflect_y", delim - option)) {
1610 rotation |= DRM_MODE_REFLECT_Y;
1612 } else if (!strncmp(option, "margin_right", delim - option)) {
1613 const char *value = delim + 1;
1614 unsigned int margin;
1616 margin = simple_strtol(value, &sep, 10);
1618 /* Make sure we have parsed something */
1622 mode->tv_margins.right = margin;
1623 } else if (!strncmp(option, "margin_left", delim - option)) {
1624 const char *value = delim + 1;
1625 unsigned int margin;
1627 margin = simple_strtol(value, &sep, 10);
1629 /* Make sure we have parsed something */
1633 mode->tv_margins.left = margin;
1634 } else if (!strncmp(option, "margin_top", delim - option)) {
1635 const char *value = delim + 1;
1636 unsigned int margin;
1638 margin = simple_strtol(value, &sep, 10);
1640 /* Make sure we have parsed something */
1644 mode->tv_margins.top = margin;
1645 } else if (!strncmp(option, "margin_bottom", delim - option)) {
1646 const char *value = delim + 1;
1647 unsigned int margin;
1649 margin = simple_strtol(value, &sep, 10);
1651 /* Make sure we have parsed something */
1655 mode->tv_margins.bottom = margin;
1661 mode->rotation_reflection = rotation;
1667 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1668 * @mode_option: optional per connector mode option
1669 * @connector: connector to parse modeline for
1670 * @mode: preallocated drm_cmdline_mode structure to fill out
1672 * This parses @mode_option command line modeline for modes and options to
1673 * configure the connector. If @mode_option is NULL the default command line
1674 * modeline in fb_mode_option will be parsed instead.
1676 * This uses the same parameters as the fb modedb.c, except for an extra
1677 * force-enable, force-enable-digital and force-disable bit at the end::
1679 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1681 * Additionals options can be provided following the mode, using a comma to
1682 * separate each option. Valid options can be found in
1683 * Documentation/fb/modedb.txt.
1685 * The intermediate drm_cmdline_mode structure is required to store additional
1686 * options from the command line modline like the force-enable/disable flag.
1689 * True if a valid modeline has been parsed, false otherwise.
1691 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1692 struct drm_connector *connector,
1693 struct drm_cmdline_mode *mode)
1696 bool named_mode = false, parse_extras = false;
1697 unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
1698 unsigned int mode_end = 0;
1699 char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
1700 char *options_ptr = NULL;
1701 char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
1706 mode_option = fb_mode_option;
1710 mode->specified = false;
1717 * This is a bit convoluted. To differentiate between the
1718 * named modes and poorly formatted resolutions, we need a
1720 * - We need to make sure that the first character (which
1721 * would be our resolution in X) is a digit.
1722 * - However, if the X resolution is missing, then we end up
1723 * with something like x<yres>, with our first character
1724 * being an alpha-numerical character, which would be
1725 * considered a named mode.
1727 * If this isn't enough, we should add more heuristics here,
1728 * and matching unit-tests.
1730 if (!isdigit(name[0]) && name[0] != 'x')
1733 /* Try to locate the bpp and refresh specifiers, if any */
1734 bpp_ptr = strchr(name, '-');
1736 bpp_off = bpp_ptr - name;
1737 mode->bpp_specified = true;
1740 refresh_ptr = strchr(name, '@');
1745 refresh_off = refresh_ptr - name;
1746 mode->refresh_specified = true;
1749 /* Locate the start of named options */
1750 options_ptr = strchr(name, ',');
1752 options_off = options_ptr - name;
1754 /* Locate the end of the name / resolution, and parse it */
1757 } else if (refresh_ptr) {
1758 mode_end = refresh_off;
1759 } else if (options_ptr) {
1760 mode_end = options_off;
1762 mode_end = strlen(name);
1763 parse_extras = true;
1767 strncpy(mode->name, name, mode_end);
1769 ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
1776 mode->specified = true;
1779 ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
1785 ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
1786 &refresh_end_ptr, mode);
1792 * Locate the end of the bpp / refresh, and parse the extras
1795 if (bpp_ptr && refresh_ptr)
1796 extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
1798 extra_ptr = bpp_end_ptr;
1799 else if (refresh_ptr)
1800 extra_ptr = refresh_end_ptr;
1803 extra_ptr != options_ptr) {
1804 int len = strlen(name) - (extra_ptr - name);
1806 ret = drm_mode_parse_cmdline_extra(extra_ptr, len,
1813 int len = strlen(name) - (options_ptr - name);
1815 ret = drm_mode_parse_cmdline_options(options_ptr, len,
1823 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1826 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1827 * @dev: DRM device to create the new mode for
1828 * @cmd: input command line modeline
1831 * Pointer to converted mode on success, NULL on error.
1833 struct drm_display_mode *
1834 drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1835 struct drm_cmdline_mode *cmd)
1837 struct drm_display_mode *mode;
1840 mode = drm_cvt_mode(dev,
1841 cmd->xres, cmd->yres,
1842 cmd->refresh_specified ? cmd->refresh : 60,
1843 cmd->rb, cmd->interlace,
1846 mode = drm_gtf_mode(dev,
1847 cmd->xres, cmd->yres,
1848 cmd->refresh_specified ? cmd->refresh : 60,
1854 mode->type |= DRM_MODE_TYPE_USERDEF;
1855 /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
1856 if (cmd->xres == 1366)
1857 drm_mode_fixup_1366x768(mode);
1858 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1861 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1864 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1865 * @out: drm_mode_modeinfo struct to return to the user
1866 * @in: drm_display_mode to use
1868 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1871 void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1872 const struct drm_display_mode *in)
1874 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1875 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1876 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1877 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1878 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1879 "timing values too large for mode info\n");
1881 out->clock = in->clock;
1882 out->hdisplay = in->hdisplay;
1883 out->hsync_start = in->hsync_start;
1884 out->hsync_end = in->hsync_end;
1885 out->htotal = in->htotal;
1886 out->hskew = in->hskew;
1887 out->vdisplay = in->vdisplay;
1888 out->vsync_start = in->vsync_start;
1889 out->vsync_end = in->vsync_end;
1890 out->vtotal = in->vtotal;
1891 out->vscan = in->vscan;
1892 out->vrefresh = in->vrefresh;
1893 out->flags = in->flags;
1894 out->type = in->type;
1896 switch (in->picture_aspect_ratio) {
1897 case HDMI_PICTURE_ASPECT_4_3:
1898 out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
1900 case HDMI_PICTURE_ASPECT_16_9:
1901 out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
1903 case HDMI_PICTURE_ASPECT_64_27:
1904 out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
1906 case HDMI_PICTURE_ASPECT_256_135:
1907 out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
1909 case HDMI_PICTURE_ASPECT_RESERVED:
1911 out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
1915 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1916 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1920 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1922 * @out: drm_display_mode to return to the user
1923 * @in: drm_mode_modeinfo to use
1925 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1929 * Zero on success, negative errno on failure.
1931 int drm_mode_convert_umode(struct drm_device *dev,
1932 struct drm_display_mode *out,
1933 const struct drm_mode_modeinfo *in)
1935 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1938 out->clock = in->clock;
1939 out->hdisplay = in->hdisplay;
1940 out->hsync_start = in->hsync_start;
1941 out->hsync_end = in->hsync_end;
1942 out->htotal = in->htotal;
1943 out->hskew = in->hskew;
1944 out->vdisplay = in->vdisplay;
1945 out->vsync_start = in->vsync_start;
1946 out->vsync_end = in->vsync_end;
1947 out->vtotal = in->vtotal;
1948 out->vscan = in->vscan;
1949 out->vrefresh = in->vrefresh;
1950 out->flags = in->flags;
1952 * Old xf86-video-vmware (possibly others too) used to
1953 * leave 'type' unititialized. Just ignore any bits we
1954 * don't like. It's a just hint after all, and more
1955 * useful for the kernel->userspace direction anyway.
1957 out->type = in->type & DRM_MODE_TYPE_ALL;
1958 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1959 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1961 /* Clearing picture aspect ratio bits from out flags,
1962 * as the aspect-ratio information is not stored in
1963 * flags for kernel-mode, but in picture_aspect_ratio.
1965 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
1967 switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
1968 case DRM_MODE_FLAG_PIC_AR_4_3:
1969 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_4_3;
1971 case DRM_MODE_FLAG_PIC_AR_16_9:
1972 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_16_9;
1974 case DRM_MODE_FLAG_PIC_AR_64_27:
1975 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_64_27;
1977 case DRM_MODE_FLAG_PIC_AR_256_135:
1978 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_256_135;
1981 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
1985 out->status = drm_mode_validate_driver(dev, out);
1986 if (out->status != MODE_OK)
1989 drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
1995 * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
1998 * @display: display under action
1999 * @mode: video mode to be tested.
2002 * true if the mode can be supported in YCBCR420 format
2005 bool drm_mode_is_420_only(const struct drm_display_info *display,
2006 const struct drm_display_mode *mode)
2008 u8 vic = drm_match_cea_mode(mode);
2010 return test_bit(vic, display->hdmi.y420_vdb_modes);
2012 EXPORT_SYMBOL(drm_mode_is_420_only);
2015 * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
2016 * output format also (along with RGB/YCBCR444/422)
2018 * @display: display under action.
2019 * @mode: video mode to be tested.
2022 * true if the mode can be support YCBCR420 format
2025 bool drm_mode_is_420_also(const struct drm_display_info *display,
2026 const struct drm_display_mode *mode)
2028 u8 vic = drm_match_cea_mode(mode);
2030 return test_bit(vic, display->hdmi.y420_cmdb_modes);
2032 EXPORT_SYMBOL(drm_mode_is_420_also);
2034 * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
2037 * @display: display under action.
2038 * @mode: video mode to be tested.
2041 * true if the mode can be supported in YCBCR420 format
2044 bool drm_mode_is_420(const struct drm_display_info *display,
2045 const struct drm_display_mode *mode)
2047 return drm_mode_is_420_only(display, mode) ||
2048 drm_mode_is_420_also(display, mode);
2050 EXPORT_SYMBOL(drm_mode_is_420);