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