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_display_timing.h>
39 #include <video/of_videomode.h>
40 #include <video/videomode.h>
42 #include <drm/drm_crtc.h>
43 #include <drm/drm_device.h>
44 #include <drm/drm_modes.h>
45 #include <drm/drm_print.h>
47 #include "drm_crtc_internal.h"
50 * drm_mode_debug_printmodeline - print a mode to dmesg
51 * @mode: mode to print
53 * Describe @mode using DRM_DEBUG.
55 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
57 DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
59 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
62 * drm_mode_create - create a new display mode
65 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
69 * Pointer to new mode on success, NULL on error.
71 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
73 struct drm_display_mode *nmode;
75 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
81 EXPORT_SYMBOL(drm_mode_create);
84 * drm_mode_destroy - remove a mode
86 * @mode: mode to remove
88 * Release @mode's unique ID, then free it @mode structure itself using kfree.
90 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
97 EXPORT_SYMBOL(drm_mode_destroy);
100 * drm_mode_probed_add - add a mode to a connector's probed_mode list
101 * @connector: connector the new mode
104 * Add @mode to @connector's probed_mode list for later use. This list should
105 * then in a second step get filtered and all the modes actually supported by
106 * the hardware moved to the @connector's modes list.
108 void drm_mode_probed_add(struct drm_connector *connector,
109 struct drm_display_mode *mode)
111 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
113 list_add_tail(&mode->head, &connector->probed_modes);
115 EXPORT_SYMBOL(drm_mode_probed_add);
118 * drm_cvt_mode -create a modeline based on the CVT algorithm
120 * @hdisplay: hdisplay size
121 * @vdisplay: vdisplay size
122 * @vrefresh: vrefresh rate
123 * @reduced: whether to use reduced blanking
124 * @interlaced: whether to compute an interlaced mode
125 * @margins: whether to add margins (borders)
127 * This function is called to generate the modeline based on CVT algorithm
128 * according to the hdisplay, vdisplay, vrefresh.
129 * It is based from the VESA(TM) Coordinated Video Timing Generator by
130 * Graham Loveridge April 9, 2003 available at
131 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
133 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
134 * What I have done is to translate it by using integer calculation.
137 * The modeline based on the CVT algorithm stored in a drm_display_mode object.
138 * The display mode object is allocated with drm_mode_create(). Returns NULL
139 * when no mode could be allocated.
141 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
142 int vdisplay, int vrefresh,
143 bool reduced, bool interlaced, bool margins)
145 #define HV_FACTOR 1000
146 /* 1) top/bottom margin size (% of height) - default: 1.8, */
147 #define CVT_MARGIN_PERCENTAGE 18
148 /* 2) character cell horizontal granularity (pixels) - default 8 */
149 #define CVT_H_GRANULARITY 8
150 /* 3) Minimum vertical porch (lines) - default 3 */
151 #define CVT_MIN_V_PORCH 3
152 /* 4) Minimum number of vertical back porch lines - default 6 */
153 #define CVT_MIN_V_BPORCH 6
154 /* Pixel Clock step (kHz) */
155 #define CVT_CLOCK_STEP 250
156 struct drm_display_mode *drm_mode;
157 unsigned int vfieldrate, hperiod;
158 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
162 if (!hdisplay || !vdisplay)
165 /* allocate the drm_display_mode structure. If failure, we will
168 drm_mode = drm_mode_create(dev);
172 /* the CVT default refresh rate is 60Hz */
176 /* the required field fresh rate */
178 vfieldrate = vrefresh * 2;
180 vfieldrate = vrefresh;
182 /* horizontal pixels */
183 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
185 /* determine the left&right borders */
188 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
189 hmargin -= hmargin % CVT_H_GRANULARITY;
191 /* find the total active pixels */
192 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
194 /* find the number of lines per field */
196 vdisplay_rnd = vdisplay / 2;
198 vdisplay_rnd = vdisplay;
200 /* find the top & bottom borders */
203 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
205 drm_mode->vdisplay = vdisplay + 2 * vmargin;
213 /* Determine VSync Width from aspect ratio */
214 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
216 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
218 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
220 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
222 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
228 /* simplify the GTF calculation */
229 /* 4) Minimum time of vertical sync + back porch interval (µs)
233 #define CVT_MIN_VSYNC_BP 550
234 /* 3) Nominal HSync width (% of line period) - default 8 */
235 #define CVT_HSYNC_PERCENTAGE 8
236 unsigned int hblank_percentage;
237 int vsyncandback_porch, __maybe_unused vback_porch, hblank;
239 /* estimated the horizontal period */
240 tmp1 = HV_FACTOR * 1000000 -
241 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
242 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
244 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
246 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
247 /* 9. Find number of lines in sync + backporch */
248 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
249 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
251 vsyncandback_porch = tmp1;
252 /* 10. Find number of lines in back porch */
253 vback_porch = vsyncandback_porch - vsync;
254 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
255 vsyncandback_porch + CVT_MIN_V_PORCH;
256 /* 5) Definition of Horizontal blanking time limitation */
257 /* Gradient (%/kHz) - default 600 */
258 #define CVT_M_FACTOR 600
259 /* Offset (%) - default 40 */
260 #define CVT_C_FACTOR 40
261 /* Blanking time scaling factor - default 128 */
262 #define CVT_K_FACTOR 128
263 /* Scaling factor weighting - default 20 */
264 #define CVT_J_FACTOR 20
265 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
266 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
268 /* 12. Find ideal blanking duty cycle from formula */
269 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
271 /* 13. Blanking time */
272 if (hblank_percentage < 20 * HV_FACTOR)
273 hblank_percentage = 20 * HV_FACTOR;
274 hblank = drm_mode->hdisplay * hblank_percentage /
275 (100 * HV_FACTOR - hblank_percentage);
276 hblank -= hblank % (2 * CVT_H_GRANULARITY);
277 /* 14. find the total pixels per line */
278 drm_mode->htotal = drm_mode->hdisplay + hblank;
279 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
280 drm_mode->hsync_start = drm_mode->hsync_end -
281 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
282 drm_mode->hsync_start += CVT_H_GRANULARITY -
283 drm_mode->hsync_start % CVT_H_GRANULARITY;
284 /* fill the Vsync values */
285 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
286 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
288 /* Reduced blanking */
289 /* Minimum vertical blanking interval time (µs)- default 460 */
290 #define CVT_RB_MIN_VBLANK 460
291 /* Fixed number of clocks for horizontal sync */
292 #define CVT_RB_H_SYNC 32
293 /* Fixed number of clocks for horizontal blanking */
294 #define CVT_RB_H_BLANK 160
295 /* Fixed number of lines for vertical front porch - default 3*/
296 #define CVT_RB_VFPORCH 3
299 /* 8. Estimate Horizontal period. */
300 tmp1 = HV_FACTOR * 1000000 -
301 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
302 tmp2 = vdisplay_rnd + 2 * vmargin;
303 hperiod = tmp1 / (tmp2 * vfieldrate);
304 /* 9. Find number of lines in vertical blanking */
305 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
306 /* 10. Check if vertical blanking is sufficient */
307 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
308 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
309 /* 11. Find total number of lines in vertical field */
310 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
311 /* 12. Find total number of pixels in a line */
312 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
313 /* Fill in HSync values */
314 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
315 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
316 /* Fill in VSync values */
317 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
318 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
320 /* 15/13. Find pixel clock frequency (kHz for xf86) */
321 tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
322 tmp *= HV_FACTOR * 1000;
323 do_div(tmp, hperiod);
324 tmp -= drm_mode->clock % CVT_CLOCK_STEP;
325 drm_mode->clock = tmp;
326 /* 18/16. Find actual vertical frame frequency */
327 /* ignore - just set the mode flag for interlaced */
329 drm_mode->vtotal *= 2;
330 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
332 /* Fill the mode line name */
333 drm_mode_set_name(drm_mode);
335 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
336 DRM_MODE_FLAG_NVSYNC);
338 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
339 DRM_MODE_FLAG_NHSYNC);
343 EXPORT_SYMBOL(drm_cvt_mode);
346 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
348 * @hdisplay: hdisplay size
349 * @vdisplay: vdisplay size
350 * @vrefresh: vrefresh rate.
351 * @interlaced: whether to compute an interlaced mode
352 * @margins: desired margin (borders) size
353 * @GTF_M: extended GTF formula parameters
354 * @GTF_2C: extended GTF formula parameters
355 * @GTF_K: extended GTF formula parameters
356 * @GTF_2J: extended GTF formula parameters
358 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
359 * in here multiplied by two. For a C of 40, pass in 80.
362 * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
363 * The display mode object is allocated with drm_mode_create(). Returns NULL
364 * when no mode could be allocated.
366 struct drm_display_mode *
367 drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
368 int vrefresh, bool interlaced, int margins,
369 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
370 { /* 1) top/bottom margin size (% of height) - default: 1.8, */
371 #define GTF_MARGIN_PERCENTAGE 18
372 /* 2) character cell horizontal granularity (pixels) - default 8 */
373 #define GTF_CELL_GRAN 8
374 /* 3) Minimum vertical porch (lines) - default 3 */
375 #define GTF_MIN_V_PORCH 1
376 /* width of vsync in lines */
378 /* width of hsync as % of total line */
379 #define H_SYNC_PERCENT 8
380 /* min time of vsync + back porch (microsec) */
381 #define MIN_VSYNC_PLUS_BP 550
382 /* C' and M' are part of the Blanking Duty Cycle computation */
383 #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
384 #define GTF_M_PRIME (GTF_K * GTF_M / 256)
385 struct drm_display_mode *drm_mode;
386 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
387 int top_margin, bottom_margin;
389 unsigned int hfreq_est;
390 int vsync_plus_bp, __maybe_unused vback_porch;
391 unsigned int vtotal_lines, __maybe_unused vfieldrate_est;
392 unsigned int __maybe_unused hperiod;
393 unsigned int vfield_rate, __maybe_unused vframe_rate;
394 int left_margin, right_margin;
395 unsigned int total_active_pixels, ideal_duty_cycle;
396 unsigned int hblank, total_pixels, pixel_freq;
397 int hsync, hfront_porch, vodd_front_porch_lines;
398 unsigned int tmp1, tmp2;
400 if (!hdisplay || !vdisplay)
403 drm_mode = drm_mode_create(dev);
407 /* 1. In order to give correct results, the number of horizontal
408 * pixels requested is first processed to ensure that it is divisible
409 * by the character size, by rounding it to the nearest character
412 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
413 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
415 /* 2. If interlace is requested, the number of vertical lines assumed
416 * by the calculation must be halved, as the computation calculates
417 * the number of vertical lines per field.
420 vdisplay_rnd = vdisplay / 2;
422 vdisplay_rnd = vdisplay;
424 /* 3. Find the frame rate required: */
426 vfieldrate_rqd = vrefresh * 2;
428 vfieldrate_rqd = vrefresh;
430 /* 4. Find number of lines in Top margin: */
433 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
435 /* 5. Find number of lines in bottom margin: */
436 bottom_margin = top_margin;
438 /* 6. If interlace is required, then set variable interlace: */
444 /* 7. Estimate the Horizontal frequency */
446 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
447 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
449 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
452 /* 8. Find the number of lines in V sync + back porch */
453 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
454 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
455 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
456 /* 9. Find the number of lines in V back porch alone: */
457 vback_porch = vsync_plus_bp - V_SYNC_RQD;
458 /* 10. Find the total number of lines in Vertical field period: */
459 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
460 vsync_plus_bp + GTF_MIN_V_PORCH;
461 /* 11. Estimate the Vertical field frequency: */
462 vfieldrate_est = hfreq_est / vtotal_lines;
463 /* 12. Find the actual horizontal period: */
464 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
466 /* 13. Find the actual Vertical field frequency: */
467 vfield_rate = hfreq_est / vtotal_lines;
468 /* 14. Find the Vertical frame frequency: */
470 vframe_rate = vfield_rate / 2;
472 vframe_rate = vfield_rate;
473 /* 15. Find number of pixels in left margin: */
475 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
480 /* 16.Find number of pixels in right margin: */
481 right_margin = left_margin;
482 /* 17.Find total number of active pixels in image and left and right */
483 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
484 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
485 ideal_duty_cycle = GTF_C_PRIME * 1000 -
486 (GTF_M_PRIME * 1000000 / hfreq_est);
487 /* 19.Find the number of pixels in the blanking time to the nearest
488 * double character cell: */
489 hblank = total_active_pixels * ideal_duty_cycle /
490 (100000 - ideal_duty_cycle);
491 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
492 hblank = hblank * 2 * GTF_CELL_GRAN;
493 /* 20.Find total number of pixels: */
494 total_pixels = total_active_pixels + hblank;
495 /* 21.Find pixel clock frequency: */
496 pixel_freq = total_pixels * hfreq_est / 1000;
497 /* Stage 1 computations are now complete; I should really pass
498 * the results to another function and do the Stage 2 computations,
499 * but I only need a few more values so I'll just append the
500 * computations here for now */
501 /* 17. Find the number of pixels in the horizontal sync period: */
502 hsync = H_SYNC_PERCENT * total_pixels / 100;
503 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
504 hsync = hsync * GTF_CELL_GRAN;
505 /* 18. Find the number of pixels in horizontal front porch period */
506 hfront_porch = hblank / 2 - hsync;
507 /* 36. Find the number of lines in the odd front porch period: */
508 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
510 /* finally, pack the results in the mode struct */
511 drm_mode->hdisplay = hdisplay_rnd;
512 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
513 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
514 drm_mode->htotal = total_pixels;
515 drm_mode->vdisplay = vdisplay_rnd;
516 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
517 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
518 drm_mode->vtotal = vtotal_lines;
520 drm_mode->clock = pixel_freq;
523 drm_mode->vtotal *= 2;
524 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
527 drm_mode_set_name(drm_mode);
528 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
529 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
531 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
535 EXPORT_SYMBOL(drm_gtf_mode_complex);
538 * drm_gtf_mode - create the modeline based on the GTF algorithm
540 * @hdisplay: hdisplay size
541 * @vdisplay: vdisplay size
542 * @vrefresh: vrefresh rate.
543 * @interlaced: whether to compute an interlaced mode
544 * @margins: desired margin (borders) size
546 * return the modeline based on GTF algorithm
548 * This function is to create the modeline based on the GTF algorithm.
549 * Generalized Timing Formula is derived from:
551 * GTF Spreadsheet by Andy Morrish (1/5/97)
552 * available at https://www.vesa.org
554 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
555 * What I have done is to translate it by using integer calculation.
556 * I also refer to the function of fb_get_mode in the file of
557 * drivers/video/fbmon.c
559 * Standard GTF parameters::
567 * The modeline based on the GTF algorithm stored in a drm_display_mode object.
568 * The display mode object is allocated with drm_mode_create(). Returns NULL
569 * when no mode could be allocated.
571 struct drm_display_mode *
572 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
573 bool interlaced, int margins)
575 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
577 600, 40 * 2, 128, 20 * 2);
579 EXPORT_SYMBOL(drm_gtf_mode);
581 #ifdef CONFIG_VIDEOMODE_HELPERS
583 * drm_display_mode_from_videomode - fill in @dmode using @vm,
584 * @vm: videomode structure to use as source
585 * @dmode: drm_display_mode structure to use as destination
587 * Fills out @dmode using the display mode specified in @vm.
589 void drm_display_mode_from_videomode(const struct videomode *vm,
590 struct drm_display_mode *dmode)
592 dmode->hdisplay = vm->hactive;
593 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
594 dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
595 dmode->htotal = dmode->hsync_end + vm->hback_porch;
597 dmode->vdisplay = vm->vactive;
598 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
599 dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
600 dmode->vtotal = dmode->vsync_end + vm->vback_porch;
602 dmode->clock = vm->pixelclock / 1000;
605 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
606 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
607 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
608 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
609 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
610 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
611 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
612 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
613 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
614 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
615 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
616 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
617 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
618 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
619 drm_mode_set_name(dmode);
621 EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
624 * drm_display_mode_to_videomode - fill in @vm using @dmode,
625 * @dmode: drm_display_mode structure to use as source
626 * @vm: videomode structure to use as destination
628 * Fills out @vm using the display mode specified in @dmode.
630 void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
631 struct videomode *vm)
633 vm->hactive = dmode->hdisplay;
634 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
635 vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
636 vm->hback_porch = dmode->htotal - dmode->hsync_end;
638 vm->vactive = dmode->vdisplay;
639 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
640 vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
641 vm->vback_porch = dmode->vtotal - dmode->vsync_end;
643 vm->pixelclock = dmode->clock * 1000;
646 if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
647 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
648 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
649 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
650 if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
651 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
652 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
653 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
654 if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
655 vm->flags |= DISPLAY_FLAGS_INTERLACED;
656 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
657 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
658 if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
659 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
661 EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
664 * drm_bus_flags_from_videomode - extract information about pixelclk and
665 * DE polarity from videomode and store it in a separate variable
666 * @vm: videomode structure to use
667 * @bus_flags: information about pixelclk, sync and DE polarity will be stored
670 * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
671 * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
674 void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
677 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
678 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
679 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
680 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
682 if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
683 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
684 if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
685 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
687 if (vm->flags & DISPLAY_FLAGS_DE_LOW)
688 *bus_flags |= DRM_BUS_FLAG_DE_LOW;
689 if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
690 *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
692 EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
696 * of_get_drm_display_mode - get a drm_display_mode from devicetree
697 * @np: device_node with the timing specification
698 * @dmode: will be set to the return value
699 * @bus_flags: information about pixelclk, sync and DE polarity
700 * @index: index into the list of display timings in devicetree
702 * This function is expensive and should only be used, if only one mode is to be
703 * read from DT. To get multiple modes start with of_get_display_timings and
704 * work with that instead.
707 * 0 on success, a negative errno code when no of videomode node was found.
709 int of_get_drm_display_mode(struct device_node *np,
710 struct drm_display_mode *dmode, u32 *bus_flags,
716 ret = of_get_videomode(np, &vm, index);
720 drm_display_mode_from_videomode(&vm, dmode);
722 drm_bus_flags_from_videomode(&vm, bus_flags);
724 pr_debug("%pOF: got %dx%d display mode\n",
725 np, vm.hactive, vm.vactive);
726 drm_mode_debug_printmodeline(dmode);
730 EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
733 * of_get_drm_panel_display_mode - get a panel-timing drm_display_mode from devicetree
734 * @np: device_node with the panel-timing specification
735 * @dmode: will be set to the return value
736 * @bus_flags: information about pixelclk, sync and DE polarity
738 * The Device Tree properties width-mm and height-mm will be read and set on
739 * the display mode if they are present.
742 * Zero on success, negative error code on failure.
744 int of_get_drm_panel_display_mode(struct device_node *np,
745 struct drm_display_mode *dmode, u32 *bus_flags)
747 u32 width_mm = 0, height_mm = 0;
748 struct display_timing timing;
752 ret = of_get_display_timing(np, "panel-timing", &timing);
756 videomode_from_timing(&timing, &vm);
758 memset(dmode, 0, sizeof(*dmode));
759 drm_display_mode_from_videomode(&vm, dmode);
761 drm_bus_flags_from_videomode(&vm, bus_flags);
763 ret = of_property_read_u32(np, "width-mm", &width_mm);
764 if (ret && ret != -EINVAL)
767 ret = of_property_read_u32(np, "height-mm", &height_mm);
768 if (ret && ret != -EINVAL)
771 dmode->width_mm = width_mm;
772 dmode->height_mm = height_mm;
774 drm_mode_debug_printmodeline(dmode);
778 EXPORT_SYMBOL_GPL(of_get_drm_panel_display_mode);
779 #endif /* CONFIG_OF */
780 #endif /* CONFIG_VIDEOMODE_HELPERS */
783 * drm_mode_set_name - set the name on a mode
784 * @mode: name will be set in this mode
786 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
787 * with an optional 'i' suffix for interlaced modes.
789 void drm_mode_set_name(struct drm_display_mode *mode)
791 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
793 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
794 mode->hdisplay, mode->vdisplay,
795 interlaced ? "i" : "");
797 EXPORT_SYMBOL(drm_mode_set_name);
800 * drm_mode_vrefresh - get the vrefresh of a mode
804 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
805 * value first if it is not yet set.
807 int drm_mode_vrefresh(const struct drm_display_mode *mode)
809 unsigned int num, den;
811 if (mode->htotal == 0 || mode->vtotal == 0)
815 den = mode->htotal * mode->vtotal;
817 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
819 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
824 return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(num, 1000), den);
826 EXPORT_SYMBOL(drm_mode_vrefresh);
829 * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
830 * @mode: mode to query
831 * @hdisplay: hdisplay value to fill in
832 * @vdisplay: vdisplay value to fill in
834 * The vdisplay value will be doubled if the specified mode is a stereo mode of
835 * the appropriate layout.
837 void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
838 int *hdisplay, int *vdisplay)
840 struct drm_display_mode adjusted = *mode;
842 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
843 *hdisplay = adjusted.crtc_hdisplay;
844 *vdisplay = adjusted.crtc_vdisplay;
846 EXPORT_SYMBOL(drm_mode_get_hv_timing);
849 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
851 * @adjust_flags: a combination of adjustment flags
853 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
855 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
857 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
858 * buffers containing two eyes (only adjust the timings when needed, eg. for
859 * "frame packing" or "side by side full").
860 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
861 * be performed for doublescan and vscan > 1 modes respectively.
863 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
868 p->crtc_clock = p->clock;
869 p->crtc_hdisplay = p->hdisplay;
870 p->crtc_hsync_start = p->hsync_start;
871 p->crtc_hsync_end = p->hsync_end;
872 p->crtc_htotal = p->htotal;
873 p->crtc_hskew = p->hskew;
874 p->crtc_vdisplay = p->vdisplay;
875 p->crtc_vsync_start = p->vsync_start;
876 p->crtc_vsync_end = p->vsync_end;
877 p->crtc_vtotal = p->vtotal;
879 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
880 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
881 p->crtc_vdisplay /= 2;
882 p->crtc_vsync_start /= 2;
883 p->crtc_vsync_end /= 2;
888 if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
889 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
890 p->crtc_vdisplay *= 2;
891 p->crtc_vsync_start *= 2;
892 p->crtc_vsync_end *= 2;
897 if (!(adjust_flags & CRTC_NO_VSCAN)) {
899 p->crtc_vdisplay *= p->vscan;
900 p->crtc_vsync_start *= p->vscan;
901 p->crtc_vsync_end *= p->vscan;
902 p->crtc_vtotal *= p->vscan;
906 if (adjust_flags & CRTC_STEREO_DOUBLE) {
907 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
910 case DRM_MODE_FLAG_3D_FRAME_PACKING:
912 p->crtc_vdisplay += p->crtc_vtotal;
913 p->crtc_vsync_start += p->crtc_vtotal;
914 p->crtc_vsync_end += p->crtc_vtotal;
915 p->crtc_vtotal += p->crtc_vtotal;
920 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
921 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
922 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
923 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
925 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
928 * drm_mode_copy - copy the mode
929 * @dst: mode to overwrite
932 * Copy an existing mode into another mode, preserving the object id and
933 * list head of the destination mode.
935 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
937 struct list_head head = dst->head;
942 EXPORT_SYMBOL(drm_mode_copy);
945 * drm_mode_duplicate - allocate and duplicate an existing mode
946 * @dev: drm_device to allocate the duplicated mode for
947 * @mode: mode to duplicate
949 * Just allocate a new mode, copy the existing mode into it, and return
950 * a pointer to it. Used to create new instances of established modes.
953 * Pointer to duplicated mode on success, NULL on error.
955 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
956 const struct drm_display_mode *mode)
958 struct drm_display_mode *nmode;
960 nmode = drm_mode_create(dev);
964 drm_mode_copy(nmode, mode);
968 EXPORT_SYMBOL(drm_mode_duplicate);
970 static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
971 const struct drm_display_mode *mode2)
973 return mode1->hdisplay == mode2->hdisplay &&
974 mode1->hsync_start == mode2->hsync_start &&
975 mode1->hsync_end == mode2->hsync_end &&
976 mode1->htotal == mode2->htotal &&
977 mode1->hskew == mode2->hskew &&
978 mode1->vdisplay == mode2->vdisplay &&
979 mode1->vsync_start == mode2->vsync_start &&
980 mode1->vsync_end == mode2->vsync_end &&
981 mode1->vtotal == mode2->vtotal &&
982 mode1->vscan == mode2->vscan;
985 static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
986 const struct drm_display_mode *mode2)
989 * do clock check convert to PICOS
990 * so fb modes get matched the same
992 if (mode1->clock && mode2->clock)
993 return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
995 return mode1->clock == mode2->clock;
998 static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
999 const struct drm_display_mode *mode2)
1001 return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
1002 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
1005 static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
1006 const struct drm_display_mode *mode2)
1008 return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
1009 (mode2->flags & DRM_MODE_FLAG_3D_MASK);
1012 static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
1013 const struct drm_display_mode *mode2)
1015 return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
1019 * drm_mode_match - test modes for (partial) equality
1020 * @mode1: first mode
1021 * @mode2: second mode
1022 * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
1024 * Check to see if @mode1 and @mode2 are equivalent.
1027 * True if the modes are (partially) equal, false otherwise.
1029 bool drm_mode_match(const struct drm_display_mode *mode1,
1030 const struct drm_display_mode *mode2,
1031 unsigned int match_flags)
1033 if (!mode1 && !mode2)
1036 if (!mode1 || !mode2)
1039 if (match_flags & DRM_MODE_MATCH_TIMINGS &&
1040 !drm_mode_match_timings(mode1, mode2))
1043 if (match_flags & DRM_MODE_MATCH_CLOCK &&
1044 !drm_mode_match_clock(mode1, mode2))
1047 if (match_flags & DRM_MODE_MATCH_FLAGS &&
1048 !drm_mode_match_flags(mode1, mode2))
1051 if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
1052 !drm_mode_match_3d_flags(mode1, mode2))
1055 if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
1056 !drm_mode_match_aspect_ratio(mode1, mode2))
1061 EXPORT_SYMBOL(drm_mode_match);
1064 * drm_mode_equal - test modes for equality
1065 * @mode1: first mode
1066 * @mode2: second mode
1068 * Check to see if @mode1 and @mode2 are equivalent.
1071 * True if the modes are equal, false otherwise.
1073 bool drm_mode_equal(const struct drm_display_mode *mode1,
1074 const struct drm_display_mode *mode2)
1076 return drm_mode_match(mode1, mode2,
1077 DRM_MODE_MATCH_TIMINGS |
1078 DRM_MODE_MATCH_CLOCK |
1079 DRM_MODE_MATCH_FLAGS |
1080 DRM_MODE_MATCH_3D_FLAGS|
1081 DRM_MODE_MATCH_ASPECT_RATIO);
1083 EXPORT_SYMBOL(drm_mode_equal);
1086 * drm_mode_equal_no_clocks - test modes for equality
1087 * @mode1: first mode
1088 * @mode2: second mode
1090 * Check to see if @mode1 and @mode2 are equivalent, but
1091 * don't check the pixel clocks.
1094 * True if the modes are equal, false otherwise.
1096 bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
1097 const struct drm_display_mode *mode2)
1099 return drm_mode_match(mode1, mode2,
1100 DRM_MODE_MATCH_TIMINGS |
1101 DRM_MODE_MATCH_FLAGS |
1102 DRM_MODE_MATCH_3D_FLAGS);
1104 EXPORT_SYMBOL(drm_mode_equal_no_clocks);
1107 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
1108 * @mode1: first mode
1109 * @mode2: second mode
1111 * Check to see if @mode1 and @mode2 are equivalent, but
1112 * don't check the pixel clocks nor the stereo layout.
1115 * True if the modes are equal, false otherwise.
1117 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
1118 const struct drm_display_mode *mode2)
1120 return drm_mode_match(mode1, mode2,
1121 DRM_MODE_MATCH_TIMINGS |
1122 DRM_MODE_MATCH_FLAGS);
1124 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
1126 static enum drm_mode_status
1127 drm_mode_validate_basic(const struct drm_display_mode *mode)
1129 if (mode->type & ~DRM_MODE_TYPE_ALL)
1132 if (mode->flags & ~DRM_MODE_FLAG_ALL)
1135 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1138 if (mode->clock == 0)
1139 return MODE_CLOCK_LOW;
1141 if (mode->hdisplay == 0 ||
1142 mode->hsync_start < mode->hdisplay ||
1143 mode->hsync_end < mode->hsync_start ||
1144 mode->htotal < mode->hsync_end)
1145 return MODE_H_ILLEGAL;
1147 if (mode->vdisplay == 0 ||
1148 mode->vsync_start < mode->vdisplay ||
1149 mode->vsync_end < mode->vsync_start ||
1150 mode->vtotal < mode->vsync_end)
1151 return MODE_V_ILLEGAL;
1157 * drm_mode_validate_driver - make sure the mode is somewhat sane
1159 * @mode: mode to check
1161 * First do basic validation on the mode, and then allow the driver
1162 * to check for device/driver specific limitations via the optional
1163 * &drm_mode_config_helper_funcs.mode_valid hook.
1168 enum drm_mode_status
1169 drm_mode_validate_driver(struct drm_device *dev,
1170 const struct drm_display_mode *mode)
1172 enum drm_mode_status status;
1174 status = drm_mode_validate_basic(mode);
1175 if (status != MODE_OK)
1178 if (dev->mode_config.funcs->mode_valid)
1179 return dev->mode_config.funcs->mode_valid(dev, mode);
1183 EXPORT_SYMBOL(drm_mode_validate_driver);
1186 * drm_mode_validate_size - make sure modes adhere to size constraints
1187 * @mode: mode to check
1188 * @maxX: maximum width
1189 * @maxY: maximum height
1191 * This function is a helper which can be used to validate modes against size
1192 * limitations of the DRM device/connector. If a mode is too big its status
1193 * member is updated with the appropriate validation failure code. The list
1194 * itself is not changed.
1199 enum drm_mode_status
1200 drm_mode_validate_size(const struct drm_display_mode *mode,
1203 if (maxX > 0 && mode->hdisplay > maxX)
1204 return MODE_VIRTUAL_X;
1206 if (maxY > 0 && mode->vdisplay > maxY)
1207 return MODE_VIRTUAL_Y;
1211 EXPORT_SYMBOL(drm_mode_validate_size);
1214 * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1215 * @mode: mode to check
1216 * @connector: drm connector under action
1218 * This function is a helper which can be used to filter out any YCBCR420
1219 * only mode, when the source doesn't support it.
1224 enum drm_mode_status
1225 drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1226 struct drm_connector *connector)
1228 if (!connector->ycbcr_420_allowed &&
1229 drm_mode_is_420_only(&connector->display_info, mode))
1234 EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1236 #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1238 static const char * const drm_mode_status_names[] = {
1242 MODE_STATUS(H_ILLEGAL),
1243 MODE_STATUS(V_ILLEGAL),
1244 MODE_STATUS(BAD_WIDTH),
1245 MODE_STATUS(NOMODE),
1246 MODE_STATUS(NO_INTERLACE),
1247 MODE_STATUS(NO_DBLESCAN),
1248 MODE_STATUS(NO_VSCAN),
1250 MODE_STATUS(VIRTUAL_X),
1251 MODE_STATUS(VIRTUAL_Y),
1252 MODE_STATUS(MEM_VIRT),
1253 MODE_STATUS(NOCLOCK),
1254 MODE_STATUS(CLOCK_HIGH),
1255 MODE_STATUS(CLOCK_LOW),
1256 MODE_STATUS(CLOCK_RANGE),
1257 MODE_STATUS(BAD_HVALUE),
1258 MODE_STATUS(BAD_VVALUE),
1259 MODE_STATUS(BAD_VSCAN),
1260 MODE_STATUS(HSYNC_NARROW),
1261 MODE_STATUS(HSYNC_WIDE),
1262 MODE_STATUS(HBLANK_NARROW),
1263 MODE_STATUS(HBLANK_WIDE),
1264 MODE_STATUS(VSYNC_NARROW),
1265 MODE_STATUS(VSYNC_WIDE),
1266 MODE_STATUS(VBLANK_NARROW),
1267 MODE_STATUS(VBLANK_WIDE),
1269 MODE_STATUS(INTERLACE_WIDTH),
1270 MODE_STATUS(ONE_WIDTH),
1271 MODE_STATUS(ONE_HEIGHT),
1272 MODE_STATUS(ONE_SIZE),
1273 MODE_STATUS(NO_REDUCED),
1274 MODE_STATUS(NO_STEREO),
1275 MODE_STATUS(NO_420),
1283 const char *drm_get_mode_status_name(enum drm_mode_status status)
1285 int index = status + 3;
1287 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1290 return drm_mode_status_names[index];
1294 * drm_mode_prune_invalid - remove invalid modes from mode list
1296 * @mode_list: list of modes to check
1297 * @verbose: be verbose about it
1299 * This helper function can be used to prune a display mode list after
1300 * validation has been completed. All modes whose status is not MODE_OK will be
1301 * removed from the list, and if @verbose the status code and mode name is also
1304 void drm_mode_prune_invalid(struct drm_device *dev,
1305 struct list_head *mode_list, bool verbose)
1307 struct drm_display_mode *mode, *t;
1309 list_for_each_entry_safe(mode, t, mode_list, head) {
1310 if (mode->status != MODE_OK) {
1311 list_del(&mode->head);
1313 drm_mode_debug_printmodeline(mode);
1314 DRM_DEBUG_KMS("Not using %s mode: %s\n",
1316 drm_get_mode_status_name(mode->status));
1318 drm_mode_destroy(dev, mode);
1322 EXPORT_SYMBOL(drm_mode_prune_invalid);
1325 * drm_mode_compare - compare modes for favorability
1327 * @lh_a: list_head for first mode
1328 * @lh_b: list_head for second mode
1330 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1334 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1335 * positive if @lh_b is better than @lh_a.
1337 static int drm_mode_compare(void *priv, const struct list_head *lh_a,
1338 const struct list_head *lh_b)
1340 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1341 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1344 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1345 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1348 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1352 diff = drm_mode_vrefresh(b) - drm_mode_vrefresh(a);
1356 diff = b->clock - a->clock;
1361 * drm_mode_sort - sort mode list
1362 * @mode_list: list of drm_display_mode structures to sort
1364 * Sort @mode_list by favorability, moving good modes to the head of the list.
1366 void drm_mode_sort(struct list_head *mode_list)
1368 list_sort(NULL, mode_list, drm_mode_compare);
1370 EXPORT_SYMBOL(drm_mode_sort);
1373 * drm_connector_list_update - update the mode list for the connector
1374 * @connector: the connector to update
1376 * This moves the modes from the @connector probed_modes list
1377 * to the actual mode list. It compares the probed mode against the current
1378 * list and only adds different/new modes.
1380 * This is just a helper functions doesn't validate any modes itself and also
1381 * doesn't prune any invalid modes. Callers need to do that themselves.
1383 void drm_connector_list_update(struct drm_connector *connector)
1385 struct drm_display_mode *pmode, *pt;
1387 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1389 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1390 struct drm_display_mode *mode;
1391 bool found_it = false;
1393 /* go through current modes checking for the new probed mode */
1394 list_for_each_entry(mode, &connector->modes, head) {
1395 if (!drm_mode_equal(pmode, mode))
1401 * If the old matching mode is stale (ie. left over
1402 * from a previous probe) just replace it outright.
1403 * Otherwise just merge the type bits between all
1404 * equal probed modes.
1406 * If two probed modes are considered equal, pick the
1407 * actual timings from the one that's marked as
1408 * preferred (in case the match isn't 100%). If
1409 * multiple or zero preferred modes are present, favor
1410 * the mode added to the probed_modes list first.
1412 if (mode->status == MODE_STALE) {
1413 drm_mode_copy(mode, pmode);
1414 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1415 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1416 pmode->type |= mode->type;
1417 drm_mode_copy(mode, pmode);
1419 mode->type |= pmode->type;
1422 list_del(&pmode->head);
1423 drm_mode_destroy(connector->dev, pmode);
1428 list_move_tail(&pmode->head, &connector->modes);
1432 EXPORT_SYMBOL(drm_connector_list_update);
1434 static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
1435 struct drm_cmdline_mode *mode)
1443 bpp = simple_strtol(str, end_ptr, 10);
1444 if (*end_ptr == str)
1448 mode->bpp_specified = true;
1453 static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
1454 struct drm_cmdline_mode *mode)
1456 unsigned int refresh;
1462 refresh = simple_strtol(str, end_ptr, 10);
1463 if (*end_ptr == str)
1466 mode->refresh = refresh;
1467 mode->refresh_specified = true;
1472 static int drm_mode_parse_cmdline_extra(const char *str, int length,
1474 const struct drm_connector *connector,
1475 struct drm_cmdline_mode *mode)
1479 for (i = 0; i < length; i++) {
1485 mode->interlace = true;
1491 mode->margins = true;
1494 if (mode->force != DRM_FORCE_UNSPECIFIED)
1497 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1498 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1499 mode->force = DRM_FORCE_ON;
1501 mode->force = DRM_FORCE_ON_DIGITAL;
1504 if (mode->force != DRM_FORCE_UNSPECIFIED)
1507 mode->force = DRM_FORCE_OFF;
1510 if (mode->force != DRM_FORCE_UNSPECIFIED)
1513 mode->force = DRM_FORCE_ON;
1523 static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
1525 const struct drm_connector *connector,
1526 struct drm_cmdline_mode *mode)
1528 const char *str_start = str;
1529 bool rb = false, cvt = false;
1530 int xres = 0, yres = 0;
1534 xres = simple_strtol(str, &end_ptr, 10);
1538 if (end_ptr[0] != 'x')
1543 yres = simple_strtol(str, &end_ptr, 10);
1547 remaining = length - (end_ptr - str_start);
1551 for (i = 0; i < remaining; i++) {
1552 switch (end_ptr[i]) {
1561 * Try to pass that to our extras parsing
1562 * function to handle the case where the
1563 * extras are directly after the resolution
1566 int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
1587 static int drm_mode_parse_cmdline_int(const char *delim, unsigned int *int_ret)
1593 * delim must point to the '=', otherwise it is a syntax error and
1594 * if delim points to the terminating zero, then delim + 1 will point
1595 * past the end of the string.
1601 *int_ret = simple_strtol(value, &endp, 10);
1603 /* Make sure we have parsed something */
1610 static int drm_mode_parse_panel_orientation(const char *delim,
1611 struct drm_cmdline_mode *mode)
1619 delim = strchr(value, ',');
1621 delim = value + strlen(value);
1623 if (!strncmp(value, "normal", delim - value))
1624 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
1625 else if (!strncmp(value, "upside_down", delim - value))
1626 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1627 else if (!strncmp(value, "left_side_up", delim - value))
1628 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1629 else if (!strncmp(value, "right_side_up", delim - value))
1630 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1637 static int drm_mode_parse_cmdline_options(const char *str,
1639 const struct drm_connector *connector,
1640 struct drm_cmdline_mode *mode)
1642 unsigned int deg, margin, rotation = 0;
1643 const char *delim, *option, *sep;
1647 delim = strchr(option, '=');
1649 delim = strchr(option, ',');
1652 delim = option + strlen(option);
1655 if (!strncmp(option, "rotate", delim - option)) {
1656 if (drm_mode_parse_cmdline_int(delim, °))
1661 rotation |= DRM_MODE_ROTATE_0;
1665 rotation |= DRM_MODE_ROTATE_90;
1669 rotation |= DRM_MODE_ROTATE_180;
1673 rotation |= DRM_MODE_ROTATE_270;
1679 } else if (!strncmp(option, "reflect_x", delim - option)) {
1680 rotation |= DRM_MODE_REFLECT_X;
1681 } else if (!strncmp(option, "reflect_y", delim - option)) {
1682 rotation |= DRM_MODE_REFLECT_Y;
1683 } else if (!strncmp(option, "margin_right", delim - option)) {
1684 if (drm_mode_parse_cmdline_int(delim, &margin))
1687 mode->tv_margins.right = margin;
1688 } else if (!strncmp(option, "margin_left", delim - option)) {
1689 if (drm_mode_parse_cmdline_int(delim, &margin))
1692 mode->tv_margins.left = margin;
1693 } else if (!strncmp(option, "margin_top", delim - option)) {
1694 if (drm_mode_parse_cmdline_int(delim, &margin))
1697 mode->tv_margins.top = margin;
1698 } else if (!strncmp(option, "margin_bottom", delim - option)) {
1699 if (drm_mode_parse_cmdline_int(delim, &margin))
1702 mode->tv_margins.bottom = margin;
1703 } else if (!strncmp(option, "panel_orientation", delim - option)) {
1704 if (drm_mode_parse_panel_orientation(delim, mode))
1709 sep = strchr(delim, ',');
1713 if (rotation && freestanding)
1716 if (!(rotation & DRM_MODE_ROTATE_MASK))
1717 rotation |= DRM_MODE_ROTATE_0;
1719 /* Make sure there is exactly one rotation defined */
1720 if (!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK))
1723 mode->rotation_reflection = rotation;
1728 static const char * const drm_named_modes_whitelist[] = {
1734 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1735 * @mode_option: optional per connector mode option
1736 * @connector: connector to parse modeline for
1737 * @mode: preallocated drm_cmdline_mode structure to fill out
1739 * This parses @mode_option command line modeline for modes and options to
1740 * configure the connector. If @mode_option is NULL the default command line
1741 * modeline in fb_mode_option will be parsed instead.
1743 * This uses the same parameters as the fb modedb.c, except for an extra
1744 * force-enable, force-enable-digital and force-disable bit at the end::
1746 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1748 * Additionals options can be provided following the mode, using a comma to
1749 * separate each option. Valid options can be found in
1750 * Documentation/fb/modedb.rst.
1752 * The intermediate drm_cmdline_mode structure is required to store additional
1753 * options from the command line modline like the force-enable/disable flag.
1756 * True if a valid modeline has been parsed, false otherwise.
1758 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1759 const struct drm_connector *connector,
1760 struct drm_cmdline_mode *mode)
1763 bool freestanding = false, parse_extras = false;
1764 unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
1765 unsigned int mode_end = 0;
1766 const char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
1767 const char *options_ptr = NULL;
1768 char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
1771 memset(mode, 0, sizeof(*mode));
1772 mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1779 /* Try to locate the bpp and refresh specifiers, if any */
1780 bpp_ptr = strchr(name, '-');
1782 bpp_off = bpp_ptr - name;
1784 refresh_ptr = strchr(name, '@');
1786 refresh_off = refresh_ptr - name;
1788 /* Locate the start of named options */
1789 options_ptr = strchr(name, ',');
1791 options_off = options_ptr - name;
1793 /* Locate the end of the name / resolution, and parse it */
1796 } else if (refresh_ptr) {
1797 mode_end = refresh_off;
1798 } else if (options_ptr) {
1799 mode_end = options_off;
1800 parse_extras = true;
1802 mode_end = strlen(name);
1803 parse_extras = true;
1806 /* First check for a named mode */
1807 for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
1808 ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
1809 if (ret == mode_end) {
1811 return false; /* named + refresh is invalid */
1813 strcpy(mode->name, drm_named_modes_whitelist[i]);
1814 mode->specified = true;
1819 /* No named mode? Check for a normal mode argument, e.g. 1024x768 */
1820 if (!mode->specified && isdigit(name[0])) {
1821 ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
1828 mode->specified = true;
1831 /* No mode? Check for freestanding extras and/or options */
1832 if (!mode->specified) {
1833 unsigned int len = strlen(mode_option);
1835 if (bpp_ptr || refresh_ptr)
1836 return false; /* syntax error */
1838 if (len == 1 || (len >= 2 && mode_option[1] == ','))
1839 extra_ptr = mode_option;
1841 options_ptr = mode_option - 1;
1843 freestanding = true;
1847 ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
1851 mode->bpp_specified = true;
1855 ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
1856 &refresh_end_ptr, mode);
1860 mode->refresh_specified = true;
1864 * Locate the end of the bpp / refresh, and parse the extras
1867 if (bpp_ptr && refresh_ptr)
1868 extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
1870 extra_ptr = bpp_end_ptr;
1871 else if (refresh_ptr)
1872 extra_ptr = refresh_end_ptr;
1876 len = options_ptr - extra_ptr;
1878 len = strlen(extra_ptr);
1880 ret = drm_mode_parse_cmdline_extra(extra_ptr, len, freestanding,
1887 ret = drm_mode_parse_cmdline_options(options_ptr + 1,
1896 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1899 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1900 * @dev: DRM device to create the new mode for
1901 * @cmd: input command line modeline
1904 * Pointer to converted mode on success, NULL on error.
1906 struct drm_display_mode *
1907 drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1908 struct drm_cmdline_mode *cmd)
1910 struct drm_display_mode *mode;
1912 if (cmd->xres == 0 || cmd->yres == 0)
1916 mode = drm_cvt_mode(dev,
1917 cmd->xres, cmd->yres,
1918 cmd->refresh_specified ? cmd->refresh : 60,
1919 cmd->rb, cmd->interlace,
1922 mode = drm_gtf_mode(dev,
1923 cmd->xres, cmd->yres,
1924 cmd->refresh_specified ? cmd->refresh : 60,
1930 mode->type |= DRM_MODE_TYPE_USERDEF;
1931 /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
1932 if (cmd->xres == 1366)
1933 drm_mode_fixup_1366x768(mode);
1934 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1937 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1940 * drm_mode_convert_to_umode - convert a drm_display_mode into a modeinfo
1941 * @out: drm_mode_modeinfo struct to return to the user
1942 * @in: drm_display_mode to use
1944 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1947 void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1948 const struct drm_display_mode *in)
1950 out->clock = in->clock;
1951 out->hdisplay = in->hdisplay;
1952 out->hsync_start = in->hsync_start;
1953 out->hsync_end = in->hsync_end;
1954 out->htotal = in->htotal;
1955 out->hskew = in->hskew;
1956 out->vdisplay = in->vdisplay;
1957 out->vsync_start = in->vsync_start;
1958 out->vsync_end = in->vsync_end;
1959 out->vtotal = in->vtotal;
1960 out->vscan = in->vscan;
1961 out->vrefresh = drm_mode_vrefresh(in);
1962 out->flags = in->flags;
1963 out->type = in->type;
1965 switch (in->picture_aspect_ratio) {
1966 case HDMI_PICTURE_ASPECT_4_3:
1967 out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
1969 case HDMI_PICTURE_ASPECT_16_9:
1970 out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
1972 case HDMI_PICTURE_ASPECT_64_27:
1973 out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
1975 case HDMI_PICTURE_ASPECT_256_135:
1976 out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
1979 WARN(1, "Invalid aspect ratio (0%x) on mode\n",
1980 in->picture_aspect_ratio);
1982 case HDMI_PICTURE_ASPECT_NONE:
1983 out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
1987 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1988 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1992 * drm_mode_convert_umode - convert a modeinfo into a drm_display_mode
1994 * @out: drm_display_mode to return to the user
1995 * @in: drm_mode_modeinfo to use
1997 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
2001 * Zero on success, negative errno on failure.
2003 int drm_mode_convert_umode(struct drm_device *dev,
2004 struct drm_display_mode *out,
2005 const struct drm_mode_modeinfo *in)
2007 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
2010 out->clock = in->clock;
2011 out->hdisplay = in->hdisplay;
2012 out->hsync_start = in->hsync_start;
2013 out->hsync_end = in->hsync_end;
2014 out->htotal = in->htotal;
2015 out->hskew = in->hskew;
2016 out->vdisplay = in->vdisplay;
2017 out->vsync_start = in->vsync_start;
2018 out->vsync_end = in->vsync_end;
2019 out->vtotal = in->vtotal;
2020 out->vscan = in->vscan;
2021 out->flags = in->flags;
2023 * Old xf86-video-vmware (possibly others too) used to
2024 * leave 'type' uninitialized. Just ignore any bits we
2025 * don't like. It's a just hint after all, and more
2026 * useful for the kernel->userspace direction anyway.
2028 out->type = in->type & DRM_MODE_TYPE_ALL;
2029 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
2030 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
2032 /* Clearing picture aspect ratio bits from out flags,
2033 * as the aspect-ratio information is not stored in
2034 * flags for kernel-mode, but in picture_aspect_ratio.
2036 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2038 switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
2039 case DRM_MODE_FLAG_PIC_AR_4_3:
2040 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3;
2042 case DRM_MODE_FLAG_PIC_AR_16_9:
2043 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
2045 case DRM_MODE_FLAG_PIC_AR_64_27:
2046 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27;
2048 case DRM_MODE_FLAG_PIC_AR_256_135:
2049 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135;
2051 case DRM_MODE_FLAG_PIC_AR_NONE:
2052 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
2058 out->status = drm_mode_validate_driver(dev, out);
2059 if (out->status != MODE_OK)
2062 drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
2068 * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
2071 * @display: display under action
2072 * @mode: video mode to be tested.
2075 * true if the mode can be supported in YCBCR420 format
2078 bool drm_mode_is_420_only(const struct drm_display_info *display,
2079 const struct drm_display_mode *mode)
2081 u8 vic = drm_match_cea_mode(mode);
2083 return test_bit(vic, display->hdmi.y420_vdb_modes);
2085 EXPORT_SYMBOL(drm_mode_is_420_only);
2088 * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
2089 * output format also (along with RGB/YCBCR444/422)
2091 * @display: display under action.
2092 * @mode: video mode to be tested.
2095 * true if the mode can be support YCBCR420 format
2098 bool drm_mode_is_420_also(const struct drm_display_info *display,
2099 const struct drm_display_mode *mode)
2101 u8 vic = drm_match_cea_mode(mode);
2103 return test_bit(vic, display->hdmi.y420_cmdb_modes);
2105 EXPORT_SYMBOL(drm_mode_is_420_also);
2107 * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
2110 * @display: display under action.
2111 * @mode: video mode to be tested.
2114 * true if the mode can be supported in YCBCR420 format
2117 bool drm_mode_is_420(const struct drm_display_info *display,
2118 const struct drm_display_mode *mode)
2120 return drm_mode_is_420_only(display, mode) ||
2121 drm_mode_is_420_also(display, mode);
2123 EXPORT_SYMBOL(drm_mode_is_420);