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