kernel - Fix bugs in recent RSS/swap commits
[dragonfly.git] / sys / dev / drm / drm_modes.c
blobd4917569672c4b53644884298d4315a281c13ef5
1 /*
2 * Copyright © 1997-2003 by The XFree86 Project, Inc.
3 * Copyright © 2007 Dave Airlie
4 * Copyright © 2007-2008 Intel Corporation
5 * Jesse Barnes <jesse.barnes@intel.com>
6 * Copyright 2005-2006 Luc Verhaegen
7 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
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/list.h>
34 #include <linux/list_sort.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <video/videomode.h>
39 #include <drm/drm_modes.h>
41 #include "drm_crtc_internal.h"
43 /**
44 * drm_mode_debug_printmodeline - print a mode to dmesg
45 * @mode: mode to print
47 * Describe @mode using DRM_DEBUG.
49 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
51 DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
52 "0x%x 0x%x\n",
53 mode->base.id, mode->name, mode->vrefresh, mode->clock,
54 mode->hdisplay, mode->hsync_start,
55 mode->hsync_end, mode->htotal,
56 mode->vdisplay, mode->vsync_start,
57 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
59 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
61 /**
62 * drm_mode_create - create a new display mode
63 * @dev: DRM device
65 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
66 * and return it.
68 * Returns:
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);
76 if (!nmode)
77 return NULL;
79 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
80 kfree(nmode);
81 return NULL;
84 return nmode;
86 EXPORT_SYMBOL(drm_mode_create);
88 /**
89 * drm_mode_destroy - remove a mode
90 * @dev: DRM device
91 * @mode: mode to remove
93 * Release @mode's unique ID, then free it @mode structure itself using kfree.
95 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
97 if (!mode)
98 return;
100 drm_mode_object_put(dev, &mode->base);
102 kfree(mode);
104 EXPORT_SYMBOL(drm_mode_destroy);
107 * drm_mode_probed_add - add a mode to a connector's probed_mode list
108 * @connector: connector the new mode
109 * @mode: mode data
111 * Add @mode to @connector's probed_mode list for later use. This list should
112 * then in a second step get filtered and all the modes actually supported by
113 * the hardware moved to the @connector's modes list.
115 void drm_mode_probed_add(struct drm_connector *connector,
116 struct drm_display_mode *mode)
118 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
120 list_add_tail(&mode->head, &connector->probed_modes);
122 EXPORT_SYMBOL(drm_mode_probed_add);
125 * drm_cvt_mode -create a modeline based on the CVT algorithm
126 * @dev: drm device
127 * @hdisplay: hdisplay size
128 * @vdisplay: vdisplay size
129 * @vrefresh: vrefresh rate
130 * @reduced: whether to use reduced blanking
131 * @interlaced: whether to compute an interlaced mode
132 * @margins: whether to add margins (borders)
134 * This function is called to generate the modeline based on CVT algorithm
135 * according to the hdisplay, vdisplay, vrefresh.
136 * It is based from the VESA(TM) Coordinated Video Timing Generator by
137 * Graham Loveridge April 9, 2003 available at
138 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
140 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
141 * What I have done is to translate it by using integer calculation.
143 * Returns:
144 * The modeline based on the CVT algorithm stored in a drm_display_mode object.
145 * The display mode object is allocated with drm_mode_create(). Returns NULL
146 * when no mode could be allocated.
148 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
149 int vdisplay, int vrefresh,
150 bool reduced, bool interlaced, bool margins)
152 #define HV_FACTOR 1000
153 /* 1) top/bottom margin size (% of height) - default: 1.8, */
154 #define CVT_MARGIN_PERCENTAGE 18
155 /* 2) character cell horizontal granularity (pixels) - default 8 */
156 #define CVT_H_GRANULARITY 8
157 /* 3) Minimum vertical porch (lines) - default 3 */
158 #define CVT_MIN_V_PORCH 3
159 /* 4) Minimum number of vertical back porch lines - default 6 */
160 #define CVT_MIN_V_BPORCH 6
161 /* Pixel Clock step (kHz) */
162 #define CVT_CLOCK_STEP 250
163 struct drm_display_mode *drm_mode;
164 unsigned int vfieldrate, hperiod;
165 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
166 int interlace;
168 /* allocate the drm_display_mode structure. If failure, we will
169 * return directly
171 drm_mode = drm_mode_create(dev);
172 if (!drm_mode)
173 return NULL;
175 /* the CVT default refresh rate is 60Hz */
176 if (!vrefresh)
177 vrefresh = 60;
179 /* the required field fresh rate */
180 if (interlaced)
181 vfieldrate = vrefresh * 2;
182 else
183 vfieldrate = vrefresh;
185 /* horizontal pixels */
186 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
188 /* determine the left&right borders */
189 hmargin = 0;
190 if (margins) {
191 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
192 hmargin -= hmargin % CVT_H_GRANULARITY;
194 /* find the total active pixels */
195 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
197 /* find the number of lines per field */
198 if (interlaced)
199 vdisplay_rnd = vdisplay / 2;
200 else
201 vdisplay_rnd = vdisplay;
203 /* find the top & bottom borders */
204 vmargin = 0;
205 if (margins)
206 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
208 drm_mode->vdisplay = vdisplay + 2 * vmargin;
210 /* Interlaced */
211 if (interlaced)
212 interlace = 1;
213 else
214 interlace = 0;
216 /* Determine VSync Width from aspect ratio */
217 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
218 vsync = 4;
219 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
220 vsync = 5;
221 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
222 vsync = 6;
223 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
224 vsync = 7;
225 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
226 vsync = 7;
227 else /* custom */
228 vsync = 10;
230 if (!reduced) {
231 /* simplify the GTF calculation */
232 /* 4) Minimum time of vertical sync + back porch interval (µs)
233 * default 550.0
235 int tmp1, tmp2;
236 #define CVT_MIN_VSYNC_BP 550
237 /* 3) Nominal HSync width (% of line period) - default 8 */
238 #define CVT_HSYNC_PERCENTAGE 8
239 unsigned int hblank_percentage;
240 int vsyncandback_porch, vback_porch, hblank;
242 /* estimated the horizontal period */
243 tmp1 = HV_FACTOR * 1000000 -
244 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
245 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
246 interlace;
247 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
249 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
250 /* 9. Find number of lines in sync + backporch */
251 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
252 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
253 else
254 vsyncandback_porch = tmp1;
255 /* 10. Find number of lines in back porch */
256 vback_porch = vsyncandback_porch - vsync;
257 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
258 vsyncandback_porch + CVT_MIN_V_PORCH;
259 /* 5) Definition of Horizontal blanking time limitation */
260 /* Gradient (%/kHz) - default 600 */
261 #define CVT_M_FACTOR 600
262 /* Offset (%) - default 40 */
263 #define CVT_C_FACTOR 40
264 /* Blanking time scaling factor - default 128 */
265 #define CVT_K_FACTOR 128
266 /* Scaling factor weighting - default 20 */
267 #define CVT_J_FACTOR 20
268 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
269 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
270 CVT_J_FACTOR)
271 /* 12. Find ideal blanking duty cycle from formula */
272 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
273 hperiod / 1000;
274 /* 13. Blanking time */
275 if (hblank_percentage < 20 * HV_FACTOR)
276 hblank_percentage = 20 * HV_FACTOR;
277 hblank = drm_mode->hdisplay * hblank_percentage /
278 (100 * HV_FACTOR - hblank_percentage);
279 hblank -= hblank % (2 * CVT_H_GRANULARITY);
280 /* 14. find the total pixels per line */
281 drm_mode->htotal = drm_mode->hdisplay + hblank;
282 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
283 drm_mode->hsync_start = drm_mode->hsync_end -
284 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
285 drm_mode->hsync_start += CVT_H_GRANULARITY -
286 drm_mode->hsync_start % CVT_H_GRANULARITY;
287 /* fill the Vsync values */
288 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
289 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
290 } else {
291 /* Reduced blanking */
292 /* Minimum vertical blanking interval time (µs)- default 460 */
293 #define CVT_RB_MIN_VBLANK 460
294 /* Fixed number of clocks for horizontal sync */
295 #define CVT_RB_H_SYNC 32
296 /* Fixed number of clocks for horizontal blanking */
297 #define CVT_RB_H_BLANK 160
298 /* Fixed number of lines for vertical front porch - default 3*/
299 #define CVT_RB_VFPORCH 3
300 int vbilines;
301 int tmp1, tmp2;
302 /* 8. Estimate Horizontal period. */
303 tmp1 = HV_FACTOR * 1000000 -
304 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
305 tmp2 = vdisplay_rnd + 2 * vmargin;
306 hperiod = tmp1 / (tmp2 * vfieldrate);
307 /* 9. Find number of lines in vertical blanking */
308 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
309 /* 10. Check if vertical blanking is sufficient */
310 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
311 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
312 /* 11. Find total number of lines in vertical field */
313 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
314 /* 12. Find total number of pixels in a line */
315 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
316 /* Fill in HSync values */
317 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
318 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
319 /* Fill in VSync values */
320 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
321 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
323 /* 15/13. Find pixel clock frequency (kHz for xf86) */
324 drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
325 drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
326 /* 18/16. Find actual vertical frame frequency */
327 /* ignore - just set the mode flag for interlaced */
328 if (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);
334 if (reduced)
335 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
336 DRM_MODE_FLAG_NVSYNC);
337 else
338 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
339 DRM_MODE_FLAG_NHSYNC);
341 return drm_mode;
343 EXPORT_SYMBOL(drm_cvt_mode);
346 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
347 * @dev: drm device
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.
361 * Returns:
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 */
377 #define V_SYNC_RQD 3
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;
388 int interlace;
389 unsigned int hfreq_est;
390 int vsync_plus_bp, vback_porch;
391 unsigned int vtotal_lines, vfieldrate_est, hperiod;
392 unsigned int vfield_rate, vframe_rate;
393 int left_margin, right_margin;
394 unsigned int total_active_pixels, ideal_duty_cycle;
395 unsigned int hblank, total_pixels, pixel_freq;
396 int hsync, hfront_porch, vodd_front_porch_lines;
397 unsigned int tmp1, tmp2;
399 drm_mode = drm_mode_create(dev);
400 if (!drm_mode)
401 return NULL;
403 /* 1. In order to give correct results, the number of horizontal
404 * pixels requested is first processed to ensure that it is divisible
405 * by the character size, by rounding it to the nearest character
406 * cell boundary:
408 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
409 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
411 /* 2. If interlace is requested, the number of vertical lines assumed
412 * by the calculation must be halved, as the computation calculates
413 * the number of vertical lines per field.
415 if (interlaced)
416 vdisplay_rnd = vdisplay / 2;
417 else
418 vdisplay_rnd = vdisplay;
420 /* 3. Find the frame rate required: */
421 if (interlaced)
422 vfieldrate_rqd = vrefresh * 2;
423 else
424 vfieldrate_rqd = vrefresh;
426 /* 4. Find number of lines in Top margin: */
427 top_margin = 0;
428 if (margins)
429 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
430 1000;
431 /* 5. Find number of lines in bottom margin: */
432 bottom_margin = top_margin;
434 /* 6. If interlace is required, then set variable interlace: */
435 if (interlaced)
436 interlace = 1;
437 else
438 interlace = 0;
440 /* 7. Estimate the Horizontal frequency */
442 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
443 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
444 2 + interlace;
445 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
448 /* 8. Find the number of lines in V sync + back porch */
449 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
450 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
451 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
452 /* 9. Find the number of lines in V back porch alone: */
453 vback_porch = vsync_plus_bp - V_SYNC_RQD;
454 /* 10. Find the total number of lines in Vertical field period: */
455 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
456 vsync_plus_bp + GTF_MIN_V_PORCH;
457 /* 11. Estimate the Vertical field frequency: */
458 vfieldrate_est = hfreq_est / vtotal_lines;
459 /* 12. Find the actual horizontal period: */
460 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
462 /* 13. Find the actual Vertical field frequency: */
463 vfield_rate = hfreq_est / vtotal_lines;
464 /* 14. Find the Vertical frame frequency: */
465 if (interlaced)
466 vframe_rate = vfield_rate / 2;
467 else
468 vframe_rate = vfield_rate;
469 /* 15. Find number of pixels in left margin: */
470 if (margins)
471 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
472 1000;
473 else
474 left_margin = 0;
476 /* 16.Find number of pixels in right margin: */
477 right_margin = left_margin;
478 /* 17.Find total number of active pixels in image and left and right */
479 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
480 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
481 ideal_duty_cycle = GTF_C_PRIME * 1000 -
482 (GTF_M_PRIME * 1000000 / hfreq_est);
483 /* 19.Find the number of pixels in the blanking time to the nearest
484 * double character cell: */
485 hblank = total_active_pixels * ideal_duty_cycle /
486 (100000 - ideal_duty_cycle);
487 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
488 hblank = hblank * 2 * GTF_CELL_GRAN;
489 /* 20.Find total number of pixels: */
490 total_pixels = total_active_pixels + hblank;
491 /* 21.Find pixel clock frequency: */
492 pixel_freq = total_pixels * hfreq_est / 1000;
493 /* Stage 1 computations are now complete; I should really pass
494 * the results to another function and do the Stage 2 computations,
495 * but I only need a few more values so I'll just append the
496 * computations here for now */
497 /* 17. Find the number of pixels in the horizontal sync period: */
498 hsync = H_SYNC_PERCENT * total_pixels / 100;
499 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
500 hsync = hsync * GTF_CELL_GRAN;
501 /* 18. Find the number of pixels in horizontal front porch period */
502 hfront_porch = hblank / 2 - hsync;
503 /* 36. Find the number of lines in the odd front porch period: */
504 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
506 /* finally, pack the results in the mode struct */
507 drm_mode->hdisplay = hdisplay_rnd;
508 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
509 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
510 drm_mode->htotal = total_pixels;
511 drm_mode->vdisplay = vdisplay_rnd;
512 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
513 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
514 drm_mode->vtotal = vtotal_lines;
516 drm_mode->clock = pixel_freq;
518 if (interlaced) {
519 drm_mode->vtotal *= 2;
520 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
523 drm_mode_set_name(drm_mode);
524 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
525 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
526 else
527 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
529 return drm_mode;
531 EXPORT_SYMBOL(drm_gtf_mode_complex);
534 * drm_gtf_mode - create the modeline based on the GTF algorithm
535 * @dev: drm device
536 * @hdisplay: hdisplay size
537 * @vdisplay: vdisplay size
538 * @vrefresh: vrefresh rate.
539 * @interlaced: whether to compute an interlaced mode
540 * @margins: desired margin (borders) size
542 * return the modeline based on GTF algorithm
544 * This function is to create the modeline based on the GTF algorithm.
545 * Generalized Timing Formula is derived from:
546 * GTF Spreadsheet by Andy Morrish (1/5/97)
547 * available at http://www.vesa.org
549 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
550 * What I have done is to translate it by using integer calculation.
551 * I also refer to the function of fb_get_mode in the file of
552 * drivers/video/fbmon.c
554 * Standard GTF parameters:
555 * M = 600
556 * C = 40
557 * K = 128
558 * J = 20
560 * Returns:
561 * The modeline based on the GTF algorithm stored in a drm_display_mode object.
562 * The display mode object is allocated with drm_mode_create(). Returns NULL
563 * when no mode could be allocated.
565 struct drm_display_mode *
566 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
567 bool interlaced, int margins)
569 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
570 interlaced, margins,
571 600, 40 * 2, 128, 20 * 2);
573 EXPORT_SYMBOL(drm_gtf_mode);
575 #ifdef CONFIG_VIDEOMODE_HELPERS
577 * drm_display_mode_from_videomode - fill in @dmode using @vm,
578 * @vm: videomode structure to use as source
579 * @dmode: drm_display_mode structure to use as destination
581 * Fills out @dmode using the display mode specified in @vm.
583 void drm_display_mode_from_videomode(const struct videomode *vm,
584 struct drm_display_mode *dmode)
586 dmode->hdisplay = vm->hactive;
587 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
588 dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
589 dmode->htotal = dmode->hsync_end + vm->hback_porch;
591 dmode->vdisplay = vm->vactive;
592 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
593 dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
594 dmode->vtotal = dmode->vsync_end + vm->vback_porch;
596 dmode->clock = vm->pixelclock / 1000;
598 dmode->flags = 0;
599 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
600 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
601 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
602 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
603 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
604 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
605 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
606 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
607 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
608 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
609 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
610 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
611 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
612 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
613 drm_mode_set_name(dmode);
617 * drm_display_mode_to_videomode - fill in @vm using @dmode,
618 * @dmode: drm_display_mode structure to use as source
619 * @vm: videomode structure to use as destination
621 * Fills out @vm using the display mode specified in @dmode.
623 void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
624 struct videomode *vm)
626 vm->hactive = dmode->hdisplay;
627 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
628 vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
629 vm->hback_porch = dmode->htotal - dmode->hsync_end;
631 vm->vactive = dmode->vdisplay;
632 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
633 vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
634 vm->vback_porch = dmode->vtotal - dmode->vsync_end;
636 vm->pixelclock = dmode->clock * 1000;
638 vm->flags = 0;
639 if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
640 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
641 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
642 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
643 if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
644 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
645 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
646 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
647 if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
648 vm->flags |= DISPLAY_FLAGS_INTERLACED;
649 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
650 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
651 if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
652 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
655 #ifdef CONFIG_OF
657 * of_get_drm_display_mode - get a drm_display_mode from devicetree
658 * @np: device_node with the timing specification
659 * @dmode: will be set to the return value
660 * @index: index into the list of display timings in devicetree
662 * This function is expensive and should only be used, if only one mode is to be
663 * read from DT. To get multiple modes start with of_get_display_timings and
664 * work with that instead.
666 * Returns:
667 * 0 on success, a negative errno code when no of videomode node was found.
669 int of_get_drm_display_mode(struct device_node *np,
670 struct drm_display_mode *dmode, int index)
672 struct videomode vm;
673 int ret;
675 ret = of_get_videomode(np, &vm, index);
676 if (ret)
677 return ret;
679 drm_display_mode_from_videomode(&vm, dmode);
681 pr_debug("%s: got %dx%d display mode from %s\n",
682 of_node_full_name(np), vm.hactive, vm.vactive, np->name);
683 drm_mode_debug_printmodeline(dmode);
685 return 0;
687 #endif /* CONFIG_OF */
688 #endif /* CONFIG_VIDEOMODE_HELPERS */
691 * drm_mode_set_name - set the name on a mode
692 * @mode: name will be set in this mode
694 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
695 * with an optional 'i' suffix for interlaced modes.
697 void drm_mode_set_name(struct drm_display_mode *mode)
699 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
701 ksnprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
702 mode->hdisplay, mode->vdisplay,
703 interlaced ? "i" : "");
705 EXPORT_SYMBOL(drm_mode_set_name);
708 * drm_mode_hsync - get the hsync of a mode
709 * @mode: mode
711 * Returns:
712 * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
713 * value first if it is not yet set.
715 int drm_mode_hsync(const struct drm_display_mode *mode)
717 unsigned int calc_val;
719 if (mode->hsync)
720 return mode->hsync;
722 if (mode->htotal < 0)
723 return 0;
725 calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
726 calc_val += 500; /* round to 1000Hz */
727 calc_val /= 1000; /* truncate to kHz */
729 return calc_val;
731 EXPORT_SYMBOL(drm_mode_hsync);
734 * drm_mode_vrefresh - get the vrefresh of a mode
735 * @mode: mode
737 * Returns:
738 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
739 * value first if it is not yet set.
741 int drm_mode_vrefresh(const struct drm_display_mode *mode)
743 int refresh = 0;
744 unsigned int calc_val;
746 if (mode->vrefresh > 0)
747 refresh = mode->vrefresh;
748 else if (mode->htotal > 0 && mode->vtotal > 0) {
749 int vtotal;
750 vtotal = mode->vtotal;
751 /* work out vrefresh the value will be x1000 */
752 calc_val = (mode->clock * 1000);
753 calc_val /= mode->htotal;
754 refresh = (calc_val + vtotal / 2) / vtotal;
756 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
757 refresh *= 2;
758 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
759 refresh /= 2;
760 if (mode->vscan > 1)
761 refresh /= mode->vscan;
763 return refresh;
765 EXPORT_SYMBOL(drm_mode_vrefresh);
768 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
769 * @p: mode
770 * @adjust_flags: a combination of adjustment flags
772 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
774 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
775 * interlaced modes.
776 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
777 * buffers containing two eyes (only adjust the timings when needed, eg. for
778 * "frame packing" or "side by side full").
779 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
780 * be performed for doublescan and vscan > 1 modes respectively.
782 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
784 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
785 return;
787 p->crtc_clock = p->clock;
788 p->crtc_hdisplay = p->hdisplay;
789 p->crtc_hsync_start = p->hsync_start;
790 p->crtc_hsync_end = p->hsync_end;
791 p->crtc_htotal = p->htotal;
792 p->crtc_hskew = p->hskew;
793 p->crtc_vdisplay = p->vdisplay;
794 p->crtc_vsync_start = p->vsync_start;
795 p->crtc_vsync_end = p->vsync_end;
796 p->crtc_vtotal = p->vtotal;
798 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
799 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
800 p->crtc_vdisplay /= 2;
801 p->crtc_vsync_start /= 2;
802 p->crtc_vsync_end /= 2;
803 p->crtc_vtotal /= 2;
807 if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
808 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
809 p->crtc_vdisplay *= 2;
810 p->crtc_vsync_start *= 2;
811 p->crtc_vsync_end *= 2;
812 p->crtc_vtotal *= 2;
816 if (!(adjust_flags & CRTC_NO_VSCAN)) {
817 if (p->vscan > 1) {
818 p->crtc_vdisplay *= p->vscan;
819 p->crtc_vsync_start *= p->vscan;
820 p->crtc_vsync_end *= p->vscan;
821 p->crtc_vtotal *= p->vscan;
825 if (adjust_flags & CRTC_STEREO_DOUBLE) {
826 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
828 switch (layout) {
829 case DRM_MODE_FLAG_3D_FRAME_PACKING:
830 p->crtc_clock *= 2;
831 p->crtc_vdisplay += p->crtc_vtotal;
832 p->crtc_vsync_start += p->crtc_vtotal;
833 p->crtc_vsync_end += p->crtc_vtotal;
834 p->crtc_vtotal += p->crtc_vtotal;
835 break;
839 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
840 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
841 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
842 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
844 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
847 * drm_mode_copy - copy the mode
848 * @dst: mode to overwrite
849 * @src: mode to copy
851 * Copy an existing mode into another mode, preserving the object id and
852 * list head of the destination mode.
854 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
856 int id = dst->base.id;
857 struct list_head head = dst->head;
859 *dst = *src;
860 dst->base.id = id;
861 dst->head = head;
863 EXPORT_SYMBOL(drm_mode_copy);
866 * drm_mode_duplicate - allocate and duplicate an existing mode
867 * @dev: drm_device to allocate the duplicated mode for
868 * @mode: mode to duplicate
870 * Just allocate a new mode, copy the existing mode into it, and return
871 * a pointer to it. Used to create new instances of established modes.
873 * Returns:
874 * Pointer to duplicated mode on success, NULL on error.
876 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
877 const struct drm_display_mode *mode)
879 struct drm_display_mode *nmode;
881 nmode = drm_mode_create(dev);
882 if (!nmode)
883 return NULL;
885 drm_mode_copy(nmode, mode);
887 return nmode;
889 EXPORT_SYMBOL(drm_mode_duplicate);
892 * drm_mode_equal - test modes for equality
893 * @mode1: first mode
894 * @mode2: second mode
896 * Check to see if @mode1 and @mode2 are equivalent.
898 * Returns:
899 * True if the modes are equal, false otherwise.
901 bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
903 if (!mode1 && !mode2)
904 return true;
906 if (!mode1 || !mode2)
907 return false;
909 /* do clock check convert to PICOS so fb modes get matched
910 * the same */
911 if (mode1->clock && mode2->clock) {
912 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
913 return false;
914 } else if (mode1->clock != mode2->clock)
915 return false;
917 return drm_mode_equal_no_clocks(mode1, mode2);
919 EXPORT_SYMBOL(drm_mode_equal);
922 * drm_mode_equal_no_clocks - test modes for equality
923 * @mode1: first mode
924 * @mode2: second mode
926 * Check to see if @mode1 and @mode2 are equivalent, but
927 * don't check the pixel clocks.
929 * Returns:
930 * True if the modes are equal, false otherwise.
932 bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
934 if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
935 (mode2->flags & DRM_MODE_FLAG_3D_MASK))
936 return false;
938 return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
940 EXPORT_SYMBOL(drm_mode_equal_no_clocks);
943 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
944 * @mode1: first mode
945 * @mode2: second mode
947 * Check to see if @mode1 and @mode2 are equivalent, but
948 * don't check the pixel clocks nor the stereo layout.
950 * Returns:
951 * True if the modes are equal, false otherwise.
953 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
954 const struct drm_display_mode *mode2)
956 if (mode1->hdisplay == mode2->hdisplay &&
957 mode1->hsync_start == mode2->hsync_start &&
958 mode1->hsync_end == mode2->hsync_end &&
959 mode1->htotal == mode2->htotal &&
960 mode1->hskew == mode2->hskew &&
961 mode1->vdisplay == mode2->vdisplay &&
962 mode1->vsync_start == mode2->vsync_start &&
963 mode1->vsync_end == mode2->vsync_end &&
964 mode1->vtotal == mode2->vtotal &&
965 mode1->vscan == mode2->vscan &&
966 (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
967 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
968 return true;
970 return false;
972 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
975 * drm_mode_validate_basic - make sure the mode is somewhat sane
976 * @mode: mode to check
978 * Check that the mode timings are at least somewhat reasonable.
979 * Any hardware specific limits are left up for each driver to check.
981 * Returns:
982 * The mode status
984 enum drm_mode_status
985 drm_mode_validate_basic(const struct drm_display_mode *mode)
987 if (mode->clock == 0)
988 return MODE_CLOCK_LOW;
990 if (mode->hdisplay == 0 ||
991 mode->hsync_start < mode->hdisplay ||
992 mode->hsync_end < mode->hsync_start ||
993 mode->htotal < mode->hsync_end)
994 return MODE_H_ILLEGAL;
996 if (mode->vdisplay == 0 ||
997 mode->vsync_start < mode->vdisplay ||
998 mode->vsync_end < mode->vsync_start ||
999 mode->vtotal < mode->vsync_end)
1000 return MODE_V_ILLEGAL;
1002 return MODE_OK;
1004 EXPORT_SYMBOL(drm_mode_validate_basic);
1007 * drm_mode_validate_size - make sure modes adhere to size constraints
1008 * @mode: mode to check
1009 * @maxX: maximum width
1010 * @maxY: maximum height
1012 * This function is a helper which can be used to validate modes against size
1013 * limitations of the DRM device/connector. If a mode is too big its status
1014 * member is updated with the appropriate validation failure code. The list
1015 * itself is not changed.
1017 * Returns:
1018 * The mode status
1020 enum drm_mode_status
1021 drm_mode_validate_size(const struct drm_display_mode *mode,
1022 int maxX, int maxY)
1024 if (maxX > 0 && mode->hdisplay > maxX)
1025 return MODE_VIRTUAL_X;
1027 if (maxY > 0 && mode->vdisplay > maxY)
1028 return MODE_VIRTUAL_Y;
1030 return MODE_OK;
1032 EXPORT_SYMBOL(drm_mode_validate_size);
1034 #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1036 static const char * const drm_mode_status_names[] = {
1037 MODE_STATUS(OK),
1038 MODE_STATUS(HSYNC),
1039 MODE_STATUS(VSYNC),
1040 MODE_STATUS(H_ILLEGAL),
1041 MODE_STATUS(V_ILLEGAL),
1042 MODE_STATUS(BAD_WIDTH),
1043 MODE_STATUS(NOMODE),
1044 MODE_STATUS(NO_INTERLACE),
1045 MODE_STATUS(NO_DBLESCAN),
1046 MODE_STATUS(NO_VSCAN),
1047 MODE_STATUS(MEM),
1048 MODE_STATUS(VIRTUAL_X),
1049 MODE_STATUS(VIRTUAL_Y),
1050 MODE_STATUS(MEM_VIRT),
1051 MODE_STATUS(NOCLOCK),
1052 MODE_STATUS(CLOCK_HIGH),
1053 MODE_STATUS(CLOCK_LOW),
1054 MODE_STATUS(CLOCK_RANGE),
1055 MODE_STATUS(BAD_HVALUE),
1056 MODE_STATUS(BAD_VVALUE),
1057 MODE_STATUS(BAD_VSCAN),
1058 MODE_STATUS(HSYNC_NARROW),
1059 MODE_STATUS(HSYNC_WIDE),
1060 MODE_STATUS(HBLANK_NARROW),
1061 MODE_STATUS(HBLANK_WIDE),
1062 MODE_STATUS(VSYNC_NARROW),
1063 MODE_STATUS(VSYNC_WIDE),
1064 MODE_STATUS(VBLANK_NARROW),
1065 MODE_STATUS(VBLANK_WIDE),
1066 MODE_STATUS(PANEL),
1067 MODE_STATUS(INTERLACE_WIDTH),
1068 MODE_STATUS(ONE_WIDTH),
1069 MODE_STATUS(ONE_HEIGHT),
1070 MODE_STATUS(ONE_SIZE),
1071 MODE_STATUS(NO_REDUCED),
1072 MODE_STATUS(NO_STEREO),
1073 MODE_STATUS(STALE),
1074 MODE_STATUS(BAD),
1075 MODE_STATUS(ERROR),
1078 #undef MODE_STATUS
1080 static const char *drm_get_mode_status_name(enum drm_mode_status status)
1082 int index = status + 3;
1084 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1085 return "";
1087 return drm_mode_status_names[index];
1091 * drm_mode_prune_invalid - remove invalid modes from mode list
1092 * @dev: DRM device
1093 * @mode_list: list of modes to check
1094 * @verbose: be verbose about it
1096 * This helper function can be used to prune a display mode list after
1097 * validation has been completed. All modes who's status is not MODE_OK will be
1098 * removed from the list, and if @verbose the status code and mode name is also
1099 * printed to dmesg.
1101 void drm_mode_prune_invalid(struct drm_device *dev,
1102 struct list_head *mode_list, bool verbose)
1104 struct drm_display_mode *mode, *t;
1106 list_for_each_entry_safe(mode, t, mode_list, head) {
1107 if (mode->status != MODE_OK) {
1108 list_del(&mode->head);
1109 if (verbose) {
1110 drm_mode_debug_printmodeline(mode);
1111 DRM_DEBUG_KMS("Not using %s mode: %s\n",
1112 mode->name,
1113 drm_get_mode_status_name(mode->status));
1115 drm_mode_destroy(dev, mode);
1119 EXPORT_SYMBOL(drm_mode_prune_invalid);
1122 * drm_mode_compare - compare modes for favorability
1123 * @priv: unused
1124 * @lh_a: list_head for first mode
1125 * @lh_b: list_head for second mode
1127 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1128 * which is better.
1130 * Returns:
1131 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1132 * positive if @lh_b is better than @lh_a.
1134 static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
1136 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1137 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1138 int diff;
1140 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1141 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1142 if (diff)
1143 return diff;
1144 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1145 if (diff)
1146 return diff;
1148 diff = b->vrefresh - a->vrefresh;
1149 if (diff)
1150 return diff;
1152 diff = b->clock - a->clock;
1153 return diff;
1157 * drm_mode_sort - sort mode list
1158 * @mode_list: list of drm_display_mode structures to sort
1160 * Sort @mode_list by favorability, moving good modes to the head of the list.
1162 void drm_mode_sort(struct list_head *mode_list)
1164 list_sort(NULL, mode_list, drm_mode_compare);
1166 EXPORT_SYMBOL(drm_mode_sort);
1169 * drm_mode_connector_list_update - update the mode list for the connector
1170 * @connector: the connector to update
1172 * This moves the modes from the @connector probed_modes list
1173 * to the actual mode list. It compares the probed mode against the current
1174 * list and only adds different/new modes.
1176 * This is just a helper functions doesn't validate any modes itself and also
1177 * doesn't prune any invalid modes. Callers need to do that themselves.
1179 void drm_mode_connector_list_update(struct drm_connector *connector)
1181 struct drm_display_mode *pmode, *pt;
1183 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1185 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1186 struct drm_display_mode *mode;
1187 bool found_it = false;
1189 /* go through current modes checking for the new probed mode */
1190 list_for_each_entry(mode, &connector->modes, head) {
1191 if (!drm_mode_equal(pmode, mode))
1192 continue;
1194 found_it = true;
1197 * If the old matching mode is stale (ie. left over
1198 * from a previous probe) just replace it outright.
1199 * Otherwise just merge the type bits between all
1200 * equal probed modes.
1202 * If two probed modes are considered equal, pick the
1203 * actual timings from the one that's marked as
1204 * preferred (in case the match isn't 100%). If
1205 * multiple or zero preferred modes are present, favor
1206 * the mode added to the probed_modes list first.
1208 if (mode->status == MODE_STALE) {
1209 drm_mode_copy(mode, pmode);
1210 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1211 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1212 pmode->type |= mode->type;
1213 drm_mode_copy(mode, pmode);
1214 } else {
1215 mode->type |= pmode->type;
1218 list_del(&pmode->head);
1219 drm_mode_destroy(connector->dev, pmode);
1220 break;
1223 if (!found_it) {
1224 list_move_tail(&pmode->head, &connector->modes);
1228 EXPORT_SYMBOL(drm_mode_connector_list_update);
1231 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1232 * @mode_option: optional per connector mode option
1233 * @connector: connector to parse modeline for
1234 * @mode: preallocated drm_cmdline_mode structure to fill out
1236 * This parses @mode_option command line modeline for modes and options to
1237 * configure the connector. If @mode_option is NULL the default command line
1238 * modeline in fb_mode_option will be parsed instead.
1240 * This uses the same parameters as the fb modedb.c, except for an extra
1241 * force-enable, force-enable-digital and force-disable bit at the end:
1243 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1245 * The intermediate drm_cmdline_mode structure is required to store additional
1246 * options from the command line modline like the force-enable/disable flag.
1248 * Returns:
1249 * True if a valid modeline has been parsed, false otherwise.
1251 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1252 struct drm_connector *connector,
1253 struct drm_cmdline_mode *mode)
1255 const char *name;
1256 unsigned int namelen;
1257 bool res_specified = false, bpp_specified = false, refresh_specified = false;
1258 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
1259 bool yres_specified = false, cvt = false, rb = false;
1260 bool interlace = false, margins = false, was_digit = false;
1261 int i;
1262 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
1264 #ifdef CONFIG_FB
1265 if (!mode_option)
1266 mode_option = fb_mode_option;
1267 #endif
1269 if (!mode_option) {
1270 mode->specified = false;
1271 return false;
1274 name = mode_option;
1275 namelen = strlen(name);
1276 for (i = namelen-1; i >= 0; i--) {
1277 switch (name[i]) {
1278 case '@':
1279 if (!refresh_specified && !bpp_specified &&
1280 !yres_specified && !cvt && !rb && was_digit) {
1281 refresh = simple_strtol(&name[i+1], NULL, 10);
1282 refresh_specified = true;
1283 was_digit = false;
1284 } else
1285 goto done;
1286 break;
1287 case '-':
1288 if (!bpp_specified && !yres_specified && !cvt &&
1289 !rb && was_digit) {
1290 bpp = simple_strtol(&name[i+1], NULL, 10);
1291 bpp_specified = true;
1292 was_digit = false;
1293 } else
1294 goto done;
1295 break;
1296 case 'x':
1297 if (!yres_specified && was_digit) {
1298 yres = simple_strtol(&name[i+1], NULL, 10);
1299 yres_specified = true;
1300 was_digit = false;
1301 } else
1302 goto done;
1303 break;
1304 case '0' ... '9':
1305 was_digit = true;
1306 break;
1307 case 'M':
1308 if (yres_specified || cvt || was_digit)
1309 goto done;
1310 cvt = true;
1311 break;
1312 case 'R':
1313 if (yres_specified || cvt || rb || was_digit)
1314 goto done;
1315 rb = true;
1316 break;
1317 case 'm':
1318 if (cvt || yres_specified || was_digit)
1319 goto done;
1320 margins = true;
1321 break;
1322 case 'i':
1323 if (cvt || yres_specified || was_digit)
1324 goto done;
1325 interlace = true;
1326 break;
1327 case 'e':
1328 if (yres_specified || bpp_specified || refresh_specified ||
1329 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1330 goto done;
1332 force = DRM_FORCE_ON;
1333 break;
1334 case 'D':
1335 if (yres_specified || bpp_specified || refresh_specified ||
1336 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1337 goto done;
1339 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1340 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1341 force = DRM_FORCE_ON;
1342 else
1343 force = DRM_FORCE_ON_DIGITAL;
1344 break;
1345 case 'd':
1346 if (yres_specified || bpp_specified || refresh_specified ||
1347 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1348 goto done;
1350 force = DRM_FORCE_OFF;
1351 break;
1352 default:
1353 goto done;
1357 if (i < 0 && yres_specified) {
1358 char *ch;
1359 xres = simple_strtol(name, &ch, 10);
1360 if ((ch != NULL) && (*ch == 'x'))
1361 res_specified = true;
1362 else
1363 i = ch - name;
1364 } else if (!yres_specified && was_digit) {
1365 /* catch mode that begins with digits but has no 'x' */
1366 i = 0;
1368 done:
1369 if (i >= 0) {
1370 printk(KERN_WARNING
1371 "parse error at position %i in video mode '%s'\n",
1372 i, name);
1373 mode->specified = false;
1374 return false;
1377 if (res_specified) {
1378 mode->specified = true;
1379 mode->xres = xres;
1380 mode->yres = yres;
1383 if (refresh_specified) {
1384 mode->refresh_specified = true;
1385 mode->refresh = refresh;
1388 if (bpp_specified) {
1389 mode->bpp_specified = true;
1390 mode->bpp = bpp;
1392 mode->rb = rb;
1393 mode->cvt = cvt;
1394 mode->interlace = interlace;
1395 mode->margins = margins;
1396 mode->force = force;
1398 return true;
1400 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1403 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1404 * @dev: DRM device to create the new mode for
1405 * @cmd: input command line modeline
1407 * Returns:
1408 * Pointer to converted mode on success, NULL on error.
1410 struct drm_display_mode *
1411 drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1412 struct drm_cmdline_mode *cmd)
1414 struct drm_display_mode *mode;
1416 if (cmd->cvt)
1417 mode = drm_cvt_mode(dev,
1418 cmd->xres, cmd->yres,
1419 cmd->refresh_specified ? cmd->refresh : 60,
1420 cmd->rb, cmd->interlace,
1421 cmd->margins);
1422 else
1423 mode = drm_gtf_mode(dev,
1424 cmd->xres, cmd->yres,
1425 cmd->refresh_specified ? cmd->refresh : 60,
1426 cmd->interlace,
1427 cmd->margins);
1428 if (!mode)
1429 return NULL;
1431 mode->type |= DRM_MODE_TYPE_USERDEF;
1432 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1433 return mode;
1435 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1438 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1439 * @out: drm_mode_modeinfo struct to return to the user
1440 * @in: drm_display_mode to use
1442 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1443 * the user.
1445 void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1446 const struct drm_display_mode *in)
1448 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1449 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1450 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1451 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1452 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1453 "timing values too large for mode info\n");
1455 out->clock = in->clock;
1456 out->hdisplay = in->hdisplay;
1457 out->hsync_start = in->hsync_start;
1458 out->hsync_end = in->hsync_end;
1459 out->htotal = in->htotal;
1460 out->hskew = in->hskew;
1461 out->vdisplay = in->vdisplay;
1462 out->vsync_start = in->vsync_start;
1463 out->vsync_end = in->vsync_end;
1464 out->vtotal = in->vtotal;
1465 out->vscan = in->vscan;
1466 out->vrefresh = in->vrefresh;
1467 out->flags = in->flags;
1468 out->type = in->type;
1469 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1470 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1474 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1475 * @out: drm_display_mode to return to the user
1476 * @in: drm_mode_modeinfo to use
1478 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1479 * the caller.
1481 * Returns:
1482 * Zero on success, negative errno on failure.
1484 int drm_mode_convert_umode(struct drm_display_mode *out,
1485 const struct drm_mode_modeinfo *in)
1487 int ret = -EINVAL;
1489 if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
1490 ret = -ERANGE;
1491 goto out;
1494 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1495 goto out;
1497 out->clock = in->clock;
1498 out->hdisplay = in->hdisplay;
1499 out->hsync_start = in->hsync_start;
1500 out->hsync_end = in->hsync_end;
1501 out->htotal = in->htotal;
1502 out->hskew = in->hskew;
1503 out->vdisplay = in->vdisplay;
1504 out->vsync_start = in->vsync_start;
1505 out->vsync_end = in->vsync_end;
1506 out->vtotal = in->vtotal;
1507 out->vscan = in->vscan;
1508 out->vrefresh = in->vrefresh;
1509 out->flags = in->flags;
1510 out->type = in->type;
1511 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1512 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1514 out->status = drm_mode_validate_basic(out);
1515 if (out->status != MODE_OK)
1516 goto out;
1518 ret = 0;
1520 out:
1521 return ret;