1 /*
2 * Copyright © 1997-2003 by The XFree86 Project, Inc.
3 * Copyright © 2007 Dave Airlie
4 * Copyright © 2007-2008 Intel Corporation
5 * Jesse Barnes <jesse.barnes@intel.com>
6 * Copyright 2005-2006 Luc Verhaegen
7 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the copyright holder(s)
28 * and author(s) shall not be used in advertising or otherwise to promote
29 * the sale, use or other dealings in this Software without prior written
30 * authorization from the copyright holder(s) and author(s).
31 */
32
33 #include <sys/cdefs.h>
34 #include <dev/drm2/drmP.h>
35 #include <dev/drm2/drm_crtc.h>
36
37 /**
38 * drm_mode_debug_printmodeline - debug print a mode
39 * @dev: DRM device
40 * @mode: mode to print
41 *
42 * LOCKING:
43 * None.
44 *
45 * Describe @mode using DRM_DEBUG.
46 */
drm_mode_debug_printmodeline(const struct drm_display_mode * mode)47 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
48 {
49 DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
50 "0x%x 0x%x\n",
51 mode->base.id, mode->name, mode->vrefresh, mode->clock,
52 mode->hdisplay, mode->hsync_start,
53 mode->hsync_end, mode->htotal,
54 mode->vdisplay, mode->vsync_start,
55 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
56 }
57 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
58
59 /**
60 * drm_cvt_mode -create a modeline based on CVT algorithm
61 * @dev: DRM device
62 * @hdisplay: hdisplay size
63 * @vdisplay: vdisplay size
64 * @vrefresh : vrefresh rate
65 * @reduced : Whether the GTF calculation is simplified
66 * @interlaced:Whether the interlace is supported
67 *
68 * LOCKING:
69 * none.
70 *
71 * return the modeline based on CVT algorithm
72 *
73 * This function is called to generate the modeline based on CVT algorithm
74 * according to the hdisplay, vdisplay, vrefresh.
75 * It is based from the VESA(TM) Coordinated Video Timing Generator by
76 * Graham Loveridge April 9, 2003 available at
77 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
78 *
79 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
80 * What I have done is to translate it by using integer calculation.
81 */
82 #define HV_FACTOR 1000
drm_cvt_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool reduced,bool interlaced,bool margins)83 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
84 int vdisplay, int vrefresh,
85 bool reduced, bool interlaced, bool margins)
86 {
87 /* 1) top/bottom margin size (% of height) - default: 1.8, */
88 #define CVT_MARGIN_PERCENTAGE 18
89 /* 2) character cell horizontal granularity (pixels) - default 8 */
90 #define CVT_H_GRANULARITY 8
91 /* 3) Minimum vertical porch (lines) - default 3 */
92 #define CVT_MIN_V_PORCH 3
93 /* 4) Minimum number of vertical back porch lines - default 6 */
94 #define CVT_MIN_V_BPORCH 6
95 /* Pixel Clock step (kHz) */
96 #define CVT_CLOCK_STEP 250
97 struct drm_display_mode *drm_mode;
98 unsigned int vfieldrate, hperiod;
99 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
100 int interlace;
101
102 /* allocate the drm_display_mode structure. If failure, we will
103 * return directly
104 */
105 drm_mode = drm_mode_create(dev);
106 if (!drm_mode)
107 return NULL;
108
109 /* the CVT default refresh rate is 60Hz */
110 if (!vrefresh)
111 vrefresh = 60;
112
113 /* the required field fresh rate */
114 if (interlaced)
115 vfieldrate = vrefresh * 2;
116 else
117 vfieldrate = vrefresh;
118
119 /* horizontal pixels */
120 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
121
122 /* determine the left&right borders */
123 hmargin = 0;
124 if (margins) {
125 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
126 hmargin -= hmargin % CVT_H_GRANULARITY;
127 }
128 /* find the total active pixels */
129 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
130
131 /* find the number of lines per field */
132 if (interlaced)
133 vdisplay_rnd = vdisplay / 2;
134 else
135 vdisplay_rnd = vdisplay;
136
137 /* find the top & bottom borders */
138 vmargin = 0;
139 if (margins)
140 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
141
142 drm_mode->vdisplay = vdisplay + 2 * vmargin;
143
144 /* Interlaced */
145 if (interlaced)
146 interlace = 1;
147 else
148 interlace = 0;
149
150 /* Determine VSync Width from aspect ratio */
151 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
152 vsync = 4;
153 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
154 vsync = 5;
155 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
156 vsync = 6;
157 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
158 vsync = 7;
159 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
160 vsync = 7;
161 else /* custom */
162 vsync = 10;
163
164 if (!reduced) {
165 /* simplify the GTF calculation */
166 /* 4) Minimum time of vertical sync + back porch interval (µs)
167 * default 550.0
168 */
169 int tmp1, tmp2;
170 #define CVT_MIN_VSYNC_BP 550
171 /* 3) Nominal HSync width (% of line period) - default 8 */
172 #define CVT_HSYNC_PERCENTAGE 8
173 unsigned int hblank_percentage;
174 int vsyncandback_porch, vback_porch, hblank;
175
176 /* estimated the horizontal period */
177 tmp1 = HV_FACTOR * 1000000 -
178 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
179 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
180 interlace;
181 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
182
183 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
184 /* 9. Find number of lines in sync + backporch */
185 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
186 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
187 else
188 vsyncandback_porch = tmp1;
189 /* 10. Find number of lines in back porch */
190 vback_porch = vsyncandback_porch - vsync;
191 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
192 vsyncandback_porch + CVT_MIN_V_PORCH;
193 /* 5) Definition of Horizontal blanking time limitation */
194 /* Gradient (%/kHz) - default 600 */
195 #define CVT_M_FACTOR 600
196 /* Offset (%) - default 40 */
197 #define CVT_C_FACTOR 40
198 /* Blanking time scaling factor - default 128 */
199 #define CVT_K_FACTOR 128
200 /* Scaling factor weighting - default 20 */
201 #define CVT_J_FACTOR 20
202 #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
203 #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
204 CVT_J_FACTOR)
205 /* 12. Find ideal blanking duty cycle from formula */
206 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
207 hperiod / 1000;
208 /* 13. Blanking time */
209 if (hblank_percentage < 20 * HV_FACTOR)
210 hblank_percentage = 20 * HV_FACTOR;
211 hblank = drm_mode->hdisplay * hblank_percentage /
212 (100 * HV_FACTOR - hblank_percentage);
213 hblank -= hblank % (2 * CVT_H_GRANULARITY);
214 /* 14. find the total pixes per line */
215 drm_mode->htotal = drm_mode->hdisplay + hblank;
216 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
217 drm_mode->hsync_start = drm_mode->hsync_end -
218 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
219 drm_mode->hsync_start += CVT_H_GRANULARITY -
220 drm_mode->hsync_start % CVT_H_GRANULARITY;
221 /* fill the Vsync values */
222 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
223 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
224 } else {
225 /* Reduced blanking */
226 /* Minimum vertical blanking interval time (µs)- default 460 */
227 #define CVT_RB_MIN_VBLANK 460
228 /* Fixed number of clocks for horizontal sync */
229 #define CVT_RB_H_SYNC 32
230 /* Fixed number of clocks for horizontal blanking */
231 #define CVT_RB_H_BLANK 160
232 /* Fixed number of lines for vertical front porch - default 3*/
233 #define CVT_RB_VFPORCH 3
234 int vbilines;
235 int tmp1, tmp2;
236 /* 8. Estimate Horizontal period. */
237 tmp1 = HV_FACTOR * 1000000 -
238 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
239 tmp2 = vdisplay_rnd + 2 * vmargin;
240 hperiod = tmp1 / (tmp2 * vfieldrate);
241 /* 9. Find number of lines in vertical blanking */
242 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
243 /* 10. Check if vertical blanking is sufficient */
244 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
245 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
246 /* 11. Find total number of lines in vertical field */
247 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
248 /* 12. Find total number of pixels in a line */
249 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
250 /* Fill in HSync values */
251 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
252 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
253 /* Fill in VSync values */
254 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
255 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
256 }
257 /* 15/13. Find pixel clock frequency (kHz for xf86) */
258 drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
259 drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
260 /* 18/16. Find actual vertical frame frequency */
261 /* ignore - just set the mode flag for interlaced */
262 if (interlaced) {
263 drm_mode->vtotal *= 2;
264 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
265 }
266 /* Fill the mode line name */
267 drm_mode_set_name(drm_mode);
268 if (reduced)
269 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
270 DRM_MODE_FLAG_NVSYNC);
271 else
272 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
273 DRM_MODE_FLAG_NHSYNC);
274
275 return drm_mode;
276 }
277 EXPORT_SYMBOL(drm_cvt_mode);
278
279 /**
280 * drm_gtf_mode_complex - create the modeline based on full GTF algorithm
281 *
282 * @dev :drm device
283 * @hdisplay :hdisplay size
284 * @vdisplay :vdisplay size
285 * @vrefresh :vrefresh rate.
286 * @interlaced :whether the interlace is supported
287 * @margins :desired margin size
288 * @GTF_[MCKJ] :extended GTF formula parameters
289 *
290 * LOCKING.
291 * none.
292 *
293 * return the modeline based on full GTF algorithm.
294 *
295 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
296 * in here multiplied by two. For a C of 40, pass in 80.
297 */
298 struct drm_display_mode *
drm_gtf_mode_complex(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool interlaced,int margins,int GTF_M,int GTF_2C,int GTF_K,int GTF_2J)299 drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
300 int vrefresh, bool interlaced, int margins,
301 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
302 { /* 1) top/bottom margin size (% of height) - default: 1.8, */
303 #define GTF_MARGIN_PERCENTAGE 18
304 /* 2) character cell horizontal granularity (pixels) - default 8 */
305 #define GTF_CELL_GRAN 8
306 /* 3) Minimum vertical porch (lines) - default 3 */
307 #define GTF_MIN_V_PORCH 1
308 /* width of vsync in lines */
309 #define V_SYNC_RQD 3
310 /* width of hsync as % of total line */
311 #define H_SYNC_PERCENT 8
312 /* min time of vsync + back porch (microsec) */
313 #define MIN_VSYNC_PLUS_BP 550
314 /* C' and M' are part of the Blanking Duty Cycle computation */
315 #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
316 #define GTF_M_PRIME (GTF_K * GTF_M / 256)
317 struct drm_display_mode *drm_mode;
318 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
319 int top_margin, bottom_margin;
320 int interlace;
321 unsigned int hfreq_est;
322 int vsync_plus_bp, vback_porch;
323 unsigned int vtotal_lines, vfieldrate_est, hperiod;
324 unsigned int vfield_rate, vframe_rate;
325 int left_margin, right_margin;
326 unsigned int total_active_pixels, ideal_duty_cycle;
327 unsigned int hblank, total_pixels, pixel_freq;
328 int hsync, hfront_porch, vodd_front_porch_lines;
329 unsigned int tmp1, tmp2;
330
331 drm_mode = drm_mode_create(dev);
332 if (!drm_mode)
333 return NULL;
334
335 /* 1. In order to give correct results, the number of horizontal
336 * pixels requested is first processed to ensure that it is divisible
337 * by the character size, by rounding it to the nearest character
338 * cell boundary:
339 */
340 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
341 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
342
343 /* 2. If interlace is requested, the number of vertical lines assumed
344 * by the calculation must be halved, as the computation calculates
345 * the number of vertical lines per field.
346 */
347 if (interlaced)
348 vdisplay_rnd = vdisplay / 2;
349 else
350 vdisplay_rnd = vdisplay;
351
352 /* 3. Find the frame rate required: */
353 if (interlaced)
354 vfieldrate_rqd = vrefresh * 2;
355 else
356 vfieldrate_rqd = vrefresh;
357
358 /* 4. Find number of lines in Top margin: */
359 top_margin = 0;
360 if (margins)
361 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
362 1000;
363 /* 5. Find number of lines in bottom margin: */
364 bottom_margin = top_margin;
365
366 /* 6. If interlace is required, then set variable interlace: */
367 if (interlaced)
368 interlace = 1;
369 else
370 interlace = 0;
371
372 /* 7. Estimate the Horizontal frequency */
373 {
374 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
375 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
376 2 + interlace;
377 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
378 }
379
380 /* 8. Find the number of lines in V sync + back porch */
381 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
382 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
383 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
384 /* 9. Find the number of lines in V back porch alone: */
385 vback_porch = vsync_plus_bp - V_SYNC_RQD;
386 /* 10. Find the total number of lines in Vertical field period: */
387 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
388 vsync_plus_bp + GTF_MIN_V_PORCH;
389 /* 11. Estimate the Vertical field frequency: */
390 vfieldrate_est = hfreq_est / vtotal_lines;
391 /* 12. Find the actual horizontal period: */
392 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
393
394 /* 13. Find the actual Vertical field frequency: */
395 vfield_rate = hfreq_est / vtotal_lines;
396 /* 14. Find the Vertical frame frequency: */
397 if (interlaced)
398 vframe_rate = vfield_rate / 2;
399 else
400 vframe_rate = vfield_rate;
401 /* 15. Find number of pixels in left margin: */
402 if (margins)
403 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
404 1000;
405 else
406 left_margin = 0;
407
408 /* 16.Find number of pixels in right margin: */
409 right_margin = left_margin;
410 /* 17.Find total number of active pixels in image and left and right */
411 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
412 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
413 ideal_duty_cycle = GTF_C_PRIME * 1000 -
414 (GTF_M_PRIME * 1000000 / hfreq_est);
415 /* 19.Find the number of pixels in the blanking time to the nearest
416 * double character cell: */
417 hblank = total_active_pixels * ideal_duty_cycle /
418 (100000 - ideal_duty_cycle);
419 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
420 hblank = hblank * 2 * GTF_CELL_GRAN;
421 /* 20.Find total number of pixels: */
422 total_pixels = total_active_pixels + hblank;
423 /* 21.Find pixel clock frequency: */
424 pixel_freq = total_pixels * hfreq_est / 1000;
425 /* Stage 1 computations are now complete; I should really pass
426 * the results to another function and do the Stage 2 computations,
427 * but I only need a few more values so I'll just append the
428 * computations here for now */
429 /* 17. Find the number of pixels in the horizontal sync period: */
430 hsync = H_SYNC_PERCENT * total_pixels / 100;
431 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
432 hsync = hsync * GTF_CELL_GRAN;
433 /* 18. Find the number of pixels in horizontal front porch period */
434 hfront_porch = hblank / 2 - hsync;
435 /* 36. Find the number of lines in the odd front porch period: */
436 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
437
438 /* finally, pack the results in the mode struct */
439 drm_mode->hdisplay = hdisplay_rnd;
440 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
441 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
442 drm_mode->htotal = total_pixels;
443 drm_mode->vdisplay = vdisplay_rnd;
444 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
445 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
446 drm_mode->vtotal = vtotal_lines;
447
448 drm_mode->clock = pixel_freq;
449
450 if (interlaced) {
451 drm_mode->vtotal *= 2;
452 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
453 }
454
455 drm_mode_set_name(drm_mode);
456 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
457 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
458 else
459 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
460
461 return drm_mode;
462 }
463 EXPORT_SYMBOL(drm_gtf_mode_complex);
464
465 /**
466 * drm_gtf_mode - create the modeline based on GTF algorithm
467 *
468 * @dev :drm device
469 * @hdisplay :hdisplay size
470 * @vdisplay :vdisplay size
471 * @vrefresh :vrefresh rate.
472 * @interlaced :whether the interlace is supported
473 * @margins :whether the margin is supported
474 *
475 * LOCKING.
476 * none.
477 *
478 * return the modeline based on GTF algorithm
479 *
480 * This function is to create the modeline based on the GTF algorithm.
481 * Generalized Timing Formula is derived from:
482 * GTF Spreadsheet by Andy Morrish (1/5/97)
483 * available at http://www.vesa.org
484 *
485 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
486 * What I have done is to translate it by using integer calculation.
487 * I also refer to the function of fb_get_mode in the file of
488 * drivers/video/fbmon.c
489 *
490 * Standard GTF parameters:
491 * M = 600
492 * C = 40
493 * K = 128
494 * J = 20
495 */
496 struct drm_display_mode *
drm_gtf_mode(struct drm_device * dev,int hdisplay,int vdisplay,int vrefresh,bool lace,int margins)497 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
498 bool lace, int margins)
499 {
500 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh, lace,
501 margins, 600, 40 * 2, 128, 20 * 2);
502 }
503 EXPORT_SYMBOL(drm_gtf_mode);
504
505 /**
506 * drm_mode_set_name - set the name on a mode
507 * @mode: name will be set in this mode
508 *
509 * LOCKING:
510 * None.
511 *
512 * Set the name of @mode to a standard format.
513 */
drm_mode_set_name(struct drm_display_mode * mode)514 void drm_mode_set_name(struct drm_display_mode *mode)
515 {
516 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
517
518 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
519 mode->hdisplay, mode->vdisplay,
520 interlaced ? "i" : "");
521 }
522 EXPORT_SYMBOL(drm_mode_set_name);
523
524 /**
525 * drm_mode_list_concat - move modes from one list to another
526 * @head: source list
527 * @new: dst list
528 *
529 * LOCKING:
530 * Caller must ensure both lists are locked.
531 *
532 * Move all the modes from @head to @new.
533 */
drm_mode_list_concat(struct list_head * head,struct list_head * new)534 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
535 {
536
537 struct list_head *entry, *tmp;
538
539 list_for_each_safe(entry, tmp, head) {
540 list_move_tail(entry, new);
541 }
542 }
543 EXPORT_SYMBOL(drm_mode_list_concat);
544
545 /**
546 * drm_mode_width - get the width of a mode
547 * @mode: mode
548 *
549 * LOCKING:
550 * None.
551 *
552 * Return @mode's width (hdisplay) value.
553 *
554 * FIXME: is this needed?
555 *
556 * RETURNS:
557 * @mode->hdisplay
558 */
drm_mode_width(const struct drm_display_mode * mode)559 int drm_mode_width(const struct drm_display_mode *mode)
560 {
561 return mode->hdisplay;
562
563 }
564 EXPORT_SYMBOL(drm_mode_width);
565
566 /**
567 * drm_mode_height - get the height of a mode
568 * @mode: mode
569 *
570 * LOCKING:
571 * None.
572 *
573 * Return @mode's height (vdisplay) value.
574 *
575 * FIXME: is this needed?
576 *
577 * RETURNS:
578 * @mode->vdisplay
579 */
drm_mode_height(const struct drm_display_mode * mode)580 int drm_mode_height(const struct drm_display_mode *mode)
581 {
582 return mode->vdisplay;
583 }
584 EXPORT_SYMBOL(drm_mode_height);
585
586 /** drm_mode_hsync - get the hsync of a mode
587 * @mode: mode
588 *
589 * LOCKING:
590 * None.
591 *
592 * Return @modes's hsync rate in kHz, rounded to the nearest int.
593 */
drm_mode_hsync(const struct drm_display_mode * mode)594 int drm_mode_hsync(const struct drm_display_mode *mode)
595 {
596 unsigned int calc_val;
597
598 if (mode->hsync)
599 return mode->hsync;
600
601 if (mode->htotal < 0)
602 return 0;
603
604 calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
605 calc_val += 500; /* round to 1000Hz */
606 calc_val /= 1000; /* truncate to kHz */
607
608 return calc_val;
609 }
610 EXPORT_SYMBOL(drm_mode_hsync);
611
612 /**
613 * drm_mode_vrefresh - get the vrefresh of a mode
614 * @mode: mode
615 *
616 * LOCKING:
617 * None.
618 *
619 * Return @mode's vrefresh rate in Hz or calculate it if necessary.
620 *
621 * FIXME: why is this needed? shouldn't vrefresh be set already?
622 *
623 * RETURNS:
624 * Vertical refresh rate. It will be the result of actual value plus 0.5.
625 * If it is 70.288, it will return 70Hz.
626 * If it is 59.6, it will return 60Hz.
627 */
drm_mode_vrefresh(const struct drm_display_mode * mode)628 int drm_mode_vrefresh(const struct drm_display_mode *mode)
629 {
630 int refresh = 0;
631 unsigned int calc_val;
632
633 if (mode->vrefresh > 0)
634 refresh = mode->vrefresh;
635 else if (mode->htotal > 0 && mode->vtotal > 0) {
636 int vtotal;
637 vtotal = mode->vtotal;
638 /* work out vrefresh the value will be x1000 */
639 calc_val = (mode->clock * 1000);
640 calc_val /= mode->htotal;
641 refresh = (calc_val + vtotal / 2) / vtotal;
642
643 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
644 refresh *= 2;
645 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
646 refresh /= 2;
647 if (mode->vscan > 1)
648 refresh /= mode->vscan;
649 }
650 return refresh;
651 }
652 EXPORT_SYMBOL(drm_mode_vrefresh);
653
654 /**
655 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
656 * @p: mode
657 * @adjust_flags: unused? (FIXME)
658 *
659 * LOCKING:
660 * None.
661 *
662 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
663 */
drm_mode_set_crtcinfo(struct drm_display_mode * p,int adjust_flags)664 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
665 {
666 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
667 return;
668
669 p->crtc_hdisplay = p->hdisplay;
670 p->crtc_hsync_start = p->hsync_start;
671 p->crtc_hsync_end = p->hsync_end;
672 p->crtc_htotal = p->htotal;
673 p->crtc_hskew = p->hskew;
674 p->crtc_vdisplay = p->vdisplay;
675 p->crtc_vsync_start = p->vsync_start;
676 p->crtc_vsync_end = p->vsync_end;
677 p->crtc_vtotal = p->vtotal;
678
679 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
680 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
681 p->crtc_vdisplay /= 2;
682 p->crtc_vsync_start /= 2;
683 p->crtc_vsync_end /= 2;
684 p->crtc_vtotal /= 2;
685 }
686 }
687
688 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
689 p->crtc_vdisplay *= 2;
690 p->crtc_vsync_start *= 2;
691 p->crtc_vsync_end *= 2;
692 p->crtc_vtotal *= 2;
693 }
694
695 if (p->vscan > 1) {
696 p->crtc_vdisplay *= p->vscan;
697 p->crtc_vsync_start *= p->vscan;
698 p->crtc_vsync_end *= p->vscan;
699 p->crtc_vtotal *= p->vscan;
700 }
701
702 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
703 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
704 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
705 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
706 }
707 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
708
709
710 /**
711 * drm_mode_copy - copy the mode
712 * @dst: mode to overwrite
713 * @src: mode to copy
714 *
715 * LOCKING:
716 * None.
717 *
718 * Copy an existing mode into another mode, preserving the object id
719 * of the destination mode.
720 */
drm_mode_copy(struct drm_display_mode * dst,const struct drm_display_mode * src)721 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
722 {
723 int id = dst->base.id;
724
725 *dst = *src;
726 dst->base.id = id;
727 INIT_LIST_HEAD(&dst->head);
728 }
729 EXPORT_SYMBOL(drm_mode_copy);
730
731 /**
732 * drm_mode_duplicate - allocate and duplicate an existing mode
733 * @m: mode to duplicate
734 *
735 * LOCKING:
736 * None.
737 *
738 * Just allocate a new mode, copy the existing mode into it, and return
739 * a pointer to it. Used to create new instances of established modes.
740 */
drm_mode_duplicate(struct drm_device * dev,const struct drm_display_mode * mode)741 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
742 const struct drm_display_mode *mode)
743 {
744 struct drm_display_mode *nmode;
745
746 nmode = drm_mode_create(dev);
747 if (!nmode)
748 return NULL;
749
750 drm_mode_copy(nmode, mode);
751
752 return nmode;
753 }
754 EXPORT_SYMBOL(drm_mode_duplicate);
755
756 /**
757 * drm_mode_equal - test modes for equality
758 * @mode1: first mode
759 * @mode2: second mode
760 *
761 * LOCKING:
762 * None.
763 *
764 * Check to see if @mode1 and @mode2 are equivalent.
765 *
766 * RETURNS:
767 * True if the modes are equal, false otherwise.
768 */
drm_mode_equal(const struct drm_display_mode * mode1,const struct drm_display_mode * mode2)769 bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
770 {
771 /* do clock check convert to PICOS so fb modes get matched
772 * the same */
773 if (mode1->clock && mode2->clock) {
774 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
775 return false;
776 } else if (mode1->clock != mode2->clock)
777 return false;
778
779 if (mode1->hdisplay == mode2->hdisplay &&
780 mode1->hsync_start == mode2->hsync_start &&
781 mode1->hsync_end == mode2->hsync_end &&
782 mode1->htotal == mode2->htotal &&
783 mode1->hskew == mode2->hskew &&
784 mode1->vdisplay == mode2->vdisplay &&
785 mode1->vsync_start == mode2->vsync_start &&
786 mode1->vsync_end == mode2->vsync_end &&
787 mode1->vtotal == mode2->vtotal &&
788 mode1->vscan == mode2->vscan &&
789 mode1->flags == mode2->flags)
790 return true;
791
792 return false;
793 }
794 EXPORT_SYMBOL(drm_mode_equal);
795
796 /**
797 * drm_mode_validate_size - make sure modes adhere to size constraints
798 * @dev: DRM device
799 * @mode_list: list of modes to check
800 * @maxX: maximum width
801 * @maxY: maximum height
802 * @maxPitch: max pitch
803 *
804 * LOCKING:
805 * Caller must hold a lock protecting @mode_list.
806 *
807 * The DRM device (@dev) has size and pitch limits. Here we validate the
808 * modes we probed for @dev against those limits and set their status as
809 * necessary.
810 */
drm_mode_validate_size(struct drm_device * dev,struct list_head * mode_list,int maxX,int maxY,int maxPitch)811 void drm_mode_validate_size(struct drm_device *dev,
812 struct list_head *mode_list,
813 int maxX, int maxY, int maxPitch)
814 {
815 struct drm_display_mode *mode;
816
817 list_for_each_entry(mode, mode_list, head) {
818 if (maxPitch > 0 && mode->hdisplay > maxPitch)
819 mode->status = MODE_BAD_WIDTH;
820
821 if (maxX > 0 && mode->hdisplay > maxX)
822 mode->status = MODE_VIRTUAL_X;
823
824 if (maxY > 0 && mode->vdisplay > maxY)
825 mode->status = MODE_VIRTUAL_Y;
826 }
827 }
828 EXPORT_SYMBOL(drm_mode_validate_size);
829
830 /**
831 * drm_mode_validate_clocks - validate modes against clock limits
832 * @dev: DRM device
833 * @mode_list: list of modes to check
834 * @min: minimum clock rate array
835 * @max: maximum clock rate array
836 * @n_ranges: number of clock ranges (size of arrays)
837 *
838 * LOCKING:
839 * Caller must hold a lock protecting @mode_list.
840 *
841 * Some code may need to check a mode list against the clock limits of the
842 * device in question. This function walks the mode list, testing to make
843 * sure each mode falls within a given range (defined by @min and @max
844 * arrays) and sets @mode->status as needed.
845 */
drm_mode_validate_clocks(struct drm_device * dev,struct list_head * mode_list,int * min,int * max,int n_ranges)846 void drm_mode_validate_clocks(struct drm_device *dev,
847 struct list_head *mode_list,
848 int *min, int *max, int n_ranges)
849 {
850 struct drm_display_mode *mode;
851 int i;
852
853 list_for_each_entry(mode, mode_list, head) {
854 bool good = false;
855 for (i = 0; i < n_ranges; i++) {
856 if (mode->clock >= min[i] && mode->clock <= max[i]) {
857 good = true;
858 break;
859 }
860 }
861 if (!good)
862 mode->status = MODE_CLOCK_RANGE;
863 }
864 }
865 EXPORT_SYMBOL(drm_mode_validate_clocks);
866
867 /**
868 * drm_mode_prune_invalid - remove invalid modes from mode list
869 * @dev: DRM device
870 * @mode_list: list of modes to check
871 * @verbose: be verbose about it
872 *
873 * LOCKING:
874 * Caller must hold a lock protecting @mode_list.
875 *
876 * Once mode list generation is complete, a caller can use this routine to
877 * remove invalid modes from a mode list. If any of the modes have a
878 * status other than %MODE_OK, they are removed from @mode_list and freed.
879 */
drm_mode_prune_invalid(struct drm_device * dev,struct list_head * mode_list,bool verbose)880 void drm_mode_prune_invalid(struct drm_device *dev,
881 struct list_head *mode_list, bool verbose)
882 {
883 struct drm_display_mode *mode, *t;
884
885 list_for_each_entry_safe(mode, t, mode_list, head) {
886 if (mode->status != MODE_OK) {
887 list_del(&mode->head);
888 if (verbose) {
889 drm_mode_debug_printmodeline(mode);
890 DRM_DEBUG_KMS("Not using %s mode %d\n",
891 mode->name, mode->status);
892 }
893 drm_mode_destroy(dev, mode);
894 }
895 }
896 }
897 EXPORT_SYMBOL(drm_mode_prune_invalid);
898
899 /**
900 * drm_mode_compare - compare modes for favorability
901 * @priv: unused
902 * @lh_a: list_head for first mode
903 * @lh_b: list_head for second mode
904 *
905 * LOCKING:
906 * None.
907 *
908 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
909 * which is better.
910 *
911 * RETURNS:
912 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
913 * positive if @lh_b is better than @lh_a.
914 */
drm_mode_compare(void * priv,struct list_head * lh_a,struct list_head * lh_b)915 static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
916 {
917 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
918 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
919 int diff;
920
921 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
922 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
923 if (diff)
924 return diff;
925 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
926 if (diff)
927 return diff;
928
929 diff = b->vrefresh - a->vrefresh;
930 if (diff)
931 return diff;
932
933 diff = b->clock - a->clock;
934 return diff;
935 }
936
937 /**
938 * drm_mode_sort - sort mode list
939 * @mode_list: list to sort
940 *
941 * LOCKING:
942 * Caller must hold a lock protecting @mode_list.
943 *
944 * Sort @mode_list by favorability, putting good modes first.
945 */
drm_mode_sort(struct list_head * mode_list)946 void drm_mode_sort(struct list_head *mode_list)
947 {
948 drm_list_sort(NULL, mode_list, drm_mode_compare);
949 }
950 EXPORT_SYMBOL(drm_mode_sort);
951
952 /**
953 * drm_mode_connector_list_update - update the mode list for the connector
954 * @connector: the connector to update
955 *
956 * LOCKING:
957 * Caller must hold a lock protecting @mode_list.
958 *
959 * This moves the modes from the @connector probed_modes list
960 * to the actual mode list. It compares the probed mode against the current
961 * list and only adds different modes. All modes unverified after this point
962 * will be removed by the prune invalid modes.
963 */
drm_mode_connector_list_update(struct drm_connector * connector)964 void drm_mode_connector_list_update(struct drm_connector *connector)
965 {
966 struct drm_display_mode *mode;
967 struct drm_display_mode *pmode, *pt;
968 int found_it;
969
970 list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
971 head) {
972 found_it = 0;
973 /* go through current modes checking for the new probed mode */
974 list_for_each_entry(mode, &connector->modes, head) {
975 if (drm_mode_equal(pmode, mode)) {
976 found_it = 1;
977 /* if equal delete the probed mode */
978 mode->status = pmode->status;
979 /* Merge type bits together */
980 mode->type |= pmode->type;
981 list_del(&pmode->head);
982 drm_mode_destroy(connector->dev, pmode);
983 break;
984 }
985 }
986
987 if (!found_it) {
988 list_move_tail(&pmode->head, &connector->modes);
989 }
990 }
991 }
992 EXPORT_SYMBOL(drm_mode_connector_list_update);
993
994 /**
995 * drm_mode_parse_command_line_for_connector - parse command line for connector
996 * @mode_option - per connector mode option
997 * @connector - connector to parse line for
998 *
999 * This parses the connector specific then generic command lines for
1000 * modes and options to configure the connector.
1001 *
1002 * This uses the same parameters as the fb modedb.c, except for extra
1003 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1004 *
1005 * enable/enable Digital/disable bit at the end
1006 */
drm_mode_parse_command_line_for_connector(const char * mode_option,struct drm_connector * connector,struct drm_cmdline_mode * mode)1007 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1008 struct drm_connector *connector,
1009 struct drm_cmdline_mode *mode)
1010 {
1011 const char *name;
1012 unsigned int namelen;
1013 bool res_specified = false, bpp_specified = false, refresh_specified = false;
1014 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
1015 bool yres_specified = false, cvt = false, rb = false;
1016 bool interlace = false, margins = false, was_digit = false;
1017 int i;
1018 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
1019
1020 #ifdef CONFIG_FB
1021 if (!mode_option)
1022 mode_option = fb_mode_option;
1023 #endif
1024
1025 if (!mode_option) {
1026 mode->specified = false;
1027 return false;
1028 }
1029
1030 name = mode_option;
1031 namelen = strlen(name);
1032 for (i = namelen-1; i >= 0; i--) {
1033 switch (name[i]) {
1034 case '@':
1035 if (!refresh_specified && !bpp_specified &&
1036 !yres_specified && !cvt && !rb && was_digit) {
1037 refresh = simple_strtol(&name[i+1], NULL, 10);
1038 refresh_specified = true;
1039 was_digit = false;
1040 } else
1041 goto done;
1042 break;
1043 case '-':
1044 if (!bpp_specified && !yres_specified && !cvt &&
1045 !rb && was_digit) {
1046 bpp = simple_strtol(&name[i+1], NULL, 10);
1047 bpp_specified = true;
1048 was_digit = false;
1049 } else
1050 goto done;
1051 break;
1052 case 'x':
1053 if (!yres_specified && was_digit) {
1054 yres = simple_strtol(&name[i+1], NULL, 10);
1055 yres_specified = true;
1056 was_digit = false;
1057 } else
1058 goto done;
1059 case '0' ... '9':
1060 was_digit = true;
1061 break;
1062 case 'M':
1063 if (yres_specified || cvt || was_digit)
1064 goto done;
1065 cvt = true;
1066 break;
1067 case 'R':
1068 if (yres_specified || cvt || rb || was_digit)
1069 goto done;
1070 rb = true;
1071 break;
1072 case 'm':
1073 if (cvt || yres_specified || was_digit)
1074 goto done;
1075 margins = true;
1076 break;
1077 case 'i':
1078 if (cvt || yres_specified || was_digit)
1079 goto done;
1080 interlace = true;
1081 break;
1082 case 'e':
1083 if (yres_specified || bpp_specified || refresh_specified ||
1084 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1085 goto done;
1086
1087 force = DRM_FORCE_ON;
1088 break;
1089 case 'D':
1090 if (yres_specified || bpp_specified || refresh_specified ||
1091 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1092 goto done;
1093
1094 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1095 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1096 force = DRM_FORCE_ON;
1097 else
1098 force = DRM_FORCE_ON_DIGITAL;
1099 break;
1100 case 'd':
1101 if (yres_specified || bpp_specified || refresh_specified ||
1102 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1103 goto done;
1104
1105 force = DRM_FORCE_OFF;
1106 break;
1107 default:
1108 goto done;
1109 }
1110 }
1111
1112 if (i < 0 && yres_specified) {
1113 char *ch;
1114 xres = simple_strtol(name, &ch, 10);
1115 if ((ch != NULL) && (*ch == 'x'))
1116 res_specified = true;
1117 else
1118 i = ch - name;
1119 } else if (!yres_specified && was_digit) {
1120 /* catch mode that begins with digits but has no 'x' */
1121 i = 0;
1122 }
1123 done:
1124 if (i >= 0) {
1125 DRM_WARNING(
1126 "parse error at position %i in video mode '%s'\n",
1127 i, name);
1128 mode->specified = false;
1129 return false;
1130 }
1131
1132 if (res_specified) {
1133 mode->specified = true;
1134 mode->xres = xres;
1135 mode->yres = yres;
1136 }
1137
1138 if (refresh_specified) {
1139 mode->refresh_specified = true;
1140 mode->refresh = refresh;
1141 }
1142
1143 if (bpp_specified) {
1144 mode->bpp_specified = true;
1145 mode->bpp = bpp;
1146 }
1147 mode->rb = rb;
1148 mode->cvt = cvt;
1149 mode->interlace = interlace;
1150 mode->margins = margins;
1151 mode->force = force;
1152
1153 return true;
1154 }
1155 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1156
1157 struct drm_display_mode *
drm_mode_create_from_cmdline_mode(struct drm_device * dev,struct drm_cmdline_mode * cmd)1158 drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1159 struct drm_cmdline_mode *cmd)
1160 {
1161 struct drm_display_mode *mode;
1162
1163 if (cmd->cvt)
1164 mode = drm_cvt_mode(dev,
1165 cmd->xres, cmd->yres,
1166 cmd->refresh_specified ? cmd->refresh : 60,
1167 cmd->rb, cmd->interlace,
1168 cmd->margins);
1169 else
1170 mode = drm_gtf_mode(dev,
1171 cmd->xres, cmd->yres,
1172 cmd->refresh_specified ? cmd->refresh : 60,
1173 cmd->interlace,
1174 cmd->margins);
1175 if (!mode)
1176 return NULL;
1177
1178 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1179 return mode;
1180 }
1181 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1182