drm_modes.c revision 1.7 1 1.7 riastrad /* $NetBSD: drm_modes.c,v 1.7 2018/08/27 04:58:19 riastradh Exp $ */
2 1.7 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright 1997-2003 by The XFree86 Project, Inc.
5 1.1 riastrad * Copyright 2007 Dave Airlie
6 1.1 riastrad * Copyright 2007-2008 Intel Corporation
7 1.1 riastrad * Jesse Barnes <jesse.barnes (at) intel.com>
8 1.1 riastrad * Copyright 2005-2006 Luc Verhaegen
9 1.1 riastrad * Copyright (c) 2001, Andy Ritger aritger (at) nvidia.com
10 1.1 riastrad *
11 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
12 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
13 1.1 riastrad * to deal in the Software without restriction, including without limitation
14 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
16 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
17 1.1 riastrad *
18 1.1 riastrad * The above copyright notice and this permission notice shall be included in
19 1.1 riastrad * all copies or substantial portions of the Software.
20 1.1 riastrad *
21 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
28 1.1 riastrad *
29 1.1 riastrad * Except as contained in this notice, the name of the copyright holder(s)
30 1.1 riastrad * and author(s) shall not be used in advertising or otherwise to promote
31 1.1 riastrad * the sale, use or other dealings in this Software without prior written
32 1.1 riastrad * authorization from the copyright holder(s) and author(s).
33 1.1 riastrad */
34 1.1 riastrad
35 1.7 riastrad #include <sys/cdefs.h>
36 1.7 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_modes.c,v 1.7 2018/08/27 04:58:19 riastradh Exp $");
37 1.7 riastrad
38 1.1 riastrad #include <linux/list.h>
39 1.1 riastrad #include <linux/list_sort.h>
40 1.1 riastrad #include <linux/export.h>
41 1.1 riastrad #include <drm/drmP.h>
42 1.1 riastrad #include <drm/drm_crtc.h>
43 1.5 riastrad #ifdef CONFIG_VIDEOMODE_HELPERS
44 1.5 riastrad #ifdef CONFIG_OF
45 1.4 riastrad #include <video/of_videomode.h>
46 1.5 riastrad #endif
47 1.4 riastrad #include <video/videomode.h>
48 1.5 riastrad #endif
49 1.4 riastrad #include <drm/drm_modes.h>
50 1.4 riastrad
51 1.4 riastrad #include "drm_crtc_internal.h"
52 1.1 riastrad
53 1.1 riastrad /**
54 1.4 riastrad * drm_mode_debug_printmodeline - print a mode to dmesg
55 1.1 riastrad * @mode: mode to print
56 1.1 riastrad *
57 1.1 riastrad * Describe @mode using DRM_DEBUG.
58 1.1 riastrad */
59 1.1 riastrad void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
60 1.1 riastrad {
61 1.1 riastrad DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
62 1.1 riastrad "0x%x 0x%x\n",
63 1.1 riastrad mode->base.id, mode->name, mode->vrefresh, mode->clock,
64 1.1 riastrad mode->hdisplay, mode->hsync_start,
65 1.1 riastrad mode->hsync_end, mode->htotal,
66 1.1 riastrad mode->vdisplay, mode->vsync_start,
67 1.1 riastrad mode->vsync_end, mode->vtotal, mode->type, mode->flags);
68 1.1 riastrad }
69 1.1 riastrad EXPORT_SYMBOL(drm_mode_debug_printmodeline);
70 1.1 riastrad
71 1.1 riastrad /**
72 1.4 riastrad * drm_mode_create - create a new display mode
73 1.4 riastrad * @dev: DRM device
74 1.4 riastrad *
75 1.4 riastrad * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
76 1.4 riastrad * and return it.
77 1.4 riastrad *
78 1.4 riastrad * Returns:
79 1.4 riastrad * Pointer to new mode on success, NULL on error.
80 1.4 riastrad */
81 1.4 riastrad struct drm_display_mode *drm_mode_create(struct drm_device *dev)
82 1.4 riastrad {
83 1.4 riastrad struct drm_display_mode *nmode;
84 1.4 riastrad
85 1.4 riastrad nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
86 1.4 riastrad if (!nmode)
87 1.4 riastrad return NULL;
88 1.4 riastrad
89 1.4 riastrad if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
90 1.4 riastrad kfree(nmode);
91 1.4 riastrad return NULL;
92 1.4 riastrad }
93 1.4 riastrad
94 1.4 riastrad return nmode;
95 1.4 riastrad }
96 1.4 riastrad EXPORT_SYMBOL(drm_mode_create);
97 1.4 riastrad
98 1.4 riastrad /**
99 1.4 riastrad * drm_mode_destroy - remove a mode
100 1.1 riastrad * @dev: DRM device
101 1.4 riastrad * @mode: mode to remove
102 1.4 riastrad *
103 1.4 riastrad * Release @mode's unique ID, then free it @mode structure itself using kfree.
104 1.4 riastrad */
105 1.4 riastrad void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
106 1.4 riastrad {
107 1.4 riastrad if (!mode)
108 1.4 riastrad return;
109 1.4 riastrad
110 1.4 riastrad drm_mode_object_put(dev, &mode->base);
111 1.4 riastrad
112 1.4 riastrad kfree(mode);
113 1.4 riastrad }
114 1.4 riastrad EXPORT_SYMBOL(drm_mode_destroy);
115 1.4 riastrad
116 1.4 riastrad /**
117 1.4 riastrad * drm_mode_probed_add - add a mode to a connector's probed_mode list
118 1.4 riastrad * @connector: connector the new mode
119 1.4 riastrad * @mode: mode data
120 1.4 riastrad *
121 1.4 riastrad * Add @mode to @connector's probed_mode list for later use. This list should
122 1.4 riastrad * then in a second step get filtered and all the modes actually supported by
123 1.4 riastrad * the hardware moved to the @connector's modes list.
124 1.4 riastrad */
125 1.4 riastrad void drm_mode_probed_add(struct drm_connector *connector,
126 1.4 riastrad struct drm_display_mode *mode)
127 1.4 riastrad {
128 1.4 riastrad WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
129 1.4 riastrad
130 1.4 riastrad list_add_tail(&mode->head, &connector->probed_modes);
131 1.4 riastrad }
132 1.4 riastrad EXPORT_SYMBOL(drm_mode_probed_add);
133 1.4 riastrad
134 1.4 riastrad /**
135 1.4 riastrad * drm_cvt_mode -create a modeline based on the CVT algorithm
136 1.4 riastrad * @dev: drm device
137 1.1 riastrad * @hdisplay: hdisplay size
138 1.1 riastrad * @vdisplay: vdisplay size
139 1.4 riastrad * @vrefresh: vrefresh rate
140 1.4 riastrad * @reduced: whether to use reduced blanking
141 1.4 riastrad * @interlaced: whether to compute an interlaced mode
142 1.4 riastrad * @margins: whether to add margins (borders)
143 1.1 riastrad *
144 1.1 riastrad * This function is called to generate the modeline based on CVT algorithm
145 1.1 riastrad * according to the hdisplay, vdisplay, vrefresh.
146 1.1 riastrad * It is based from the VESA(TM) Coordinated Video Timing Generator by
147 1.1 riastrad * Graham Loveridge April 9, 2003 available at
148 1.1 riastrad * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
149 1.1 riastrad *
150 1.1 riastrad * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
151 1.1 riastrad * What I have done is to translate it by using integer calculation.
152 1.4 riastrad *
153 1.4 riastrad * Returns:
154 1.4 riastrad * The modeline based on the CVT algorithm stored in a drm_display_mode object.
155 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
156 1.4 riastrad * when no mode could be allocated.
157 1.1 riastrad */
158 1.1 riastrad struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
159 1.1 riastrad int vdisplay, int vrefresh,
160 1.1 riastrad bool reduced, bool interlaced, bool margins)
161 1.1 riastrad {
162 1.4 riastrad #define HV_FACTOR 1000
163 1.1 riastrad /* 1) top/bottom margin size (% of height) - default: 1.8, */
164 1.1 riastrad #define CVT_MARGIN_PERCENTAGE 18
165 1.1 riastrad /* 2) character cell horizontal granularity (pixels) - default 8 */
166 1.1 riastrad #define CVT_H_GRANULARITY 8
167 1.1 riastrad /* 3) Minimum vertical porch (lines) - default 3 */
168 1.1 riastrad #define CVT_MIN_V_PORCH 3
169 1.1 riastrad /* 4) Minimum number of vertical back porch lines - default 6 */
170 1.1 riastrad #define CVT_MIN_V_BPORCH 6
171 1.1 riastrad /* Pixel Clock step (kHz) */
172 1.1 riastrad #define CVT_CLOCK_STEP 250
173 1.1 riastrad struct drm_display_mode *drm_mode;
174 1.1 riastrad unsigned int vfieldrate, hperiod;
175 1.1 riastrad int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
176 1.1 riastrad int interlace;
177 1.1 riastrad
178 1.1 riastrad /* allocate the drm_display_mode structure. If failure, we will
179 1.1 riastrad * return directly
180 1.1 riastrad */
181 1.1 riastrad drm_mode = drm_mode_create(dev);
182 1.1 riastrad if (!drm_mode)
183 1.1 riastrad return NULL;
184 1.1 riastrad
185 1.1 riastrad /* the CVT default refresh rate is 60Hz */
186 1.1 riastrad if (!vrefresh)
187 1.1 riastrad vrefresh = 60;
188 1.1 riastrad
189 1.1 riastrad /* the required field fresh rate */
190 1.1 riastrad if (interlaced)
191 1.1 riastrad vfieldrate = vrefresh * 2;
192 1.1 riastrad else
193 1.1 riastrad vfieldrate = vrefresh;
194 1.1 riastrad
195 1.1 riastrad /* horizontal pixels */
196 1.1 riastrad hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
197 1.1 riastrad
198 1.1 riastrad /* determine the left&right borders */
199 1.1 riastrad hmargin = 0;
200 1.1 riastrad if (margins) {
201 1.1 riastrad hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
202 1.1 riastrad hmargin -= hmargin % CVT_H_GRANULARITY;
203 1.1 riastrad }
204 1.1 riastrad /* find the total active pixels */
205 1.1 riastrad drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
206 1.1 riastrad
207 1.1 riastrad /* find the number of lines per field */
208 1.1 riastrad if (interlaced)
209 1.1 riastrad vdisplay_rnd = vdisplay / 2;
210 1.1 riastrad else
211 1.1 riastrad vdisplay_rnd = vdisplay;
212 1.1 riastrad
213 1.1 riastrad /* find the top & bottom borders */
214 1.1 riastrad vmargin = 0;
215 1.1 riastrad if (margins)
216 1.1 riastrad vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
217 1.1 riastrad
218 1.1 riastrad drm_mode->vdisplay = vdisplay + 2 * vmargin;
219 1.1 riastrad
220 1.1 riastrad /* Interlaced */
221 1.1 riastrad if (interlaced)
222 1.1 riastrad interlace = 1;
223 1.1 riastrad else
224 1.1 riastrad interlace = 0;
225 1.1 riastrad
226 1.1 riastrad /* Determine VSync Width from aspect ratio */
227 1.1 riastrad if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
228 1.1 riastrad vsync = 4;
229 1.1 riastrad else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
230 1.1 riastrad vsync = 5;
231 1.1 riastrad else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
232 1.1 riastrad vsync = 6;
233 1.1 riastrad else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
234 1.1 riastrad vsync = 7;
235 1.1 riastrad else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
236 1.1 riastrad vsync = 7;
237 1.1 riastrad else /* custom */
238 1.1 riastrad vsync = 10;
239 1.1 riastrad
240 1.1 riastrad if (!reduced) {
241 1.1 riastrad /* simplify the GTF calculation */
242 1.1 riastrad /* 4) Minimum time of vertical sync + back porch interval (s)
243 1.1 riastrad * default 550.0
244 1.1 riastrad */
245 1.1 riastrad int tmp1, tmp2;
246 1.1 riastrad #define CVT_MIN_VSYNC_BP 550
247 1.1 riastrad /* 3) Nominal HSync width (% of line period) - default 8 */
248 1.1 riastrad #define CVT_HSYNC_PERCENTAGE 8
249 1.1 riastrad unsigned int hblank_percentage;
250 1.3 riastrad int vsyncandback_porch, vback_porch __unused, hblank;
251 1.1 riastrad
252 1.1 riastrad /* estimated the horizontal period */
253 1.1 riastrad tmp1 = HV_FACTOR * 1000000 -
254 1.1 riastrad CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
255 1.1 riastrad tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
256 1.1 riastrad interlace;
257 1.1 riastrad hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
258 1.1 riastrad
259 1.1 riastrad tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
260 1.1 riastrad /* 9. Find number of lines in sync + backporch */
261 1.1 riastrad if (tmp1 < (vsync + CVT_MIN_V_PORCH))
262 1.1 riastrad vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
263 1.1 riastrad else
264 1.1 riastrad vsyncandback_porch = tmp1;
265 1.1 riastrad /* 10. Find number of lines in back porch */
266 1.1 riastrad vback_porch = vsyncandback_porch - vsync;
267 1.1 riastrad drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
268 1.1 riastrad vsyncandback_porch + CVT_MIN_V_PORCH;
269 1.1 riastrad /* 5) Definition of Horizontal blanking time limitation */
270 1.1 riastrad /* Gradient (%/kHz) - default 600 */
271 1.1 riastrad #define CVT_M_FACTOR 600
272 1.1 riastrad /* Offset (%) - default 40 */
273 1.1 riastrad #define CVT_C_FACTOR 40
274 1.1 riastrad /* Blanking time scaling factor - default 128 */
275 1.1 riastrad #define CVT_K_FACTOR 128
276 1.1 riastrad /* Scaling factor weighting - default 20 */
277 1.1 riastrad #define CVT_J_FACTOR 20
278 1.1 riastrad #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
279 1.1 riastrad #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
280 1.1 riastrad CVT_J_FACTOR)
281 1.1 riastrad /* 12. Find ideal blanking duty cycle from formula */
282 1.1 riastrad hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
283 1.1 riastrad hperiod / 1000;
284 1.1 riastrad /* 13. Blanking time */
285 1.1 riastrad if (hblank_percentage < 20 * HV_FACTOR)
286 1.1 riastrad hblank_percentage = 20 * HV_FACTOR;
287 1.1 riastrad hblank = drm_mode->hdisplay * hblank_percentage /
288 1.1 riastrad (100 * HV_FACTOR - hblank_percentage);
289 1.1 riastrad hblank -= hblank % (2 * CVT_H_GRANULARITY);
290 1.7 riastrad /* 14. find the total pixels per line */
291 1.1 riastrad drm_mode->htotal = drm_mode->hdisplay + hblank;
292 1.1 riastrad drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
293 1.1 riastrad drm_mode->hsync_start = drm_mode->hsync_end -
294 1.1 riastrad (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
295 1.1 riastrad drm_mode->hsync_start += CVT_H_GRANULARITY -
296 1.1 riastrad drm_mode->hsync_start % CVT_H_GRANULARITY;
297 1.1 riastrad /* fill the Vsync values */
298 1.1 riastrad drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
299 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + vsync;
300 1.1 riastrad } else {
301 1.1 riastrad /* Reduced blanking */
302 1.1 riastrad /* Minimum vertical blanking interval time (s)- default 460 */
303 1.1 riastrad #define CVT_RB_MIN_VBLANK 460
304 1.1 riastrad /* Fixed number of clocks for horizontal sync */
305 1.1 riastrad #define CVT_RB_H_SYNC 32
306 1.1 riastrad /* Fixed number of clocks for horizontal blanking */
307 1.1 riastrad #define CVT_RB_H_BLANK 160
308 1.1 riastrad /* Fixed number of lines for vertical front porch - default 3*/
309 1.1 riastrad #define CVT_RB_VFPORCH 3
310 1.1 riastrad int vbilines;
311 1.1 riastrad int tmp1, tmp2;
312 1.1 riastrad /* 8. Estimate Horizontal period. */
313 1.1 riastrad tmp1 = HV_FACTOR * 1000000 -
314 1.1 riastrad CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
315 1.1 riastrad tmp2 = vdisplay_rnd + 2 * vmargin;
316 1.1 riastrad hperiod = tmp1 / (tmp2 * vfieldrate);
317 1.1 riastrad /* 9. Find number of lines in vertical blanking */
318 1.1 riastrad vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
319 1.1 riastrad /* 10. Check if vertical blanking is sufficient */
320 1.1 riastrad if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
321 1.1 riastrad vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
322 1.1 riastrad /* 11. Find total number of lines in vertical field */
323 1.1 riastrad drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
324 1.1 riastrad /* 12. Find total number of pixels in a line */
325 1.1 riastrad drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
326 1.1 riastrad /* Fill in HSync values */
327 1.1 riastrad drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
328 1.1 riastrad drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
329 1.1 riastrad /* Fill in VSync values */
330 1.1 riastrad drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
331 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + vsync;
332 1.1 riastrad }
333 1.1 riastrad /* 15/13. Find pixel clock frequency (kHz for xf86) */
334 1.1 riastrad drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
335 1.1 riastrad drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
336 1.1 riastrad /* 18/16. Find actual vertical frame frequency */
337 1.1 riastrad /* ignore - just set the mode flag for interlaced */
338 1.1 riastrad if (interlaced) {
339 1.1 riastrad drm_mode->vtotal *= 2;
340 1.1 riastrad drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
341 1.1 riastrad }
342 1.1 riastrad /* Fill the mode line name */
343 1.1 riastrad drm_mode_set_name(drm_mode);
344 1.1 riastrad if (reduced)
345 1.1 riastrad drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
346 1.1 riastrad DRM_MODE_FLAG_NVSYNC);
347 1.1 riastrad else
348 1.1 riastrad drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
349 1.1 riastrad DRM_MODE_FLAG_NHSYNC);
350 1.1 riastrad
351 1.1 riastrad return drm_mode;
352 1.1 riastrad }
353 1.1 riastrad EXPORT_SYMBOL(drm_cvt_mode);
354 1.1 riastrad
355 1.1 riastrad /**
356 1.4 riastrad * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
357 1.4 riastrad * @dev: drm device
358 1.4 riastrad * @hdisplay: hdisplay size
359 1.4 riastrad * @vdisplay: vdisplay size
360 1.4 riastrad * @vrefresh: vrefresh rate.
361 1.4 riastrad * @interlaced: whether to compute an interlaced mode
362 1.4 riastrad * @margins: desired margin (borders) size
363 1.4 riastrad * @GTF_M: extended GTF formula parameters
364 1.4 riastrad * @GTF_2C: extended GTF formula parameters
365 1.4 riastrad * @GTF_K: extended GTF formula parameters
366 1.4 riastrad * @GTF_2J: extended GTF formula parameters
367 1.1 riastrad *
368 1.1 riastrad * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
369 1.1 riastrad * in here multiplied by two. For a C of 40, pass in 80.
370 1.4 riastrad *
371 1.4 riastrad * Returns:
372 1.4 riastrad * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
373 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
374 1.4 riastrad * when no mode could be allocated.
375 1.1 riastrad */
376 1.1 riastrad struct drm_display_mode *
377 1.1 riastrad drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
378 1.1 riastrad int vrefresh, bool interlaced, int margins,
379 1.1 riastrad int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
380 1.1 riastrad { /* 1) top/bottom margin size (% of height) - default: 1.8, */
381 1.1 riastrad #define GTF_MARGIN_PERCENTAGE 18
382 1.1 riastrad /* 2) character cell horizontal granularity (pixels) - default 8 */
383 1.1 riastrad #define GTF_CELL_GRAN 8
384 1.1 riastrad /* 3) Minimum vertical porch (lines) - default 3 */
385 1.1 riastrad #define GTF_MIN_V_PORCH 1
386 1.1 riastrad /* width of vsync in lines */
387 1.1 riastrad #define V_SYNC_RQD 3
388 1.1 riastrad /* width of hsync as % of total line */
389 1.1 riastrad #define H_SYNC_PERCENT 8
390 1.1 riastrad /* min time of vsync + back porch (microsec) */
391 1.1 riastrad #define MIN_VSYNC_PLUS_BP 550
392 1.1 riastrad /* C' and M' are part of the Blanking Duty Cycle computation */
393 1.1 riastrad #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
394 1.1 riastrad #define GTF_M_PRIME (GTF_K * GTF_M / 256)
395 1.1 riastrad struct drm_display_mode *drm_mode;
396 1.1 riastrad unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
397 1.1 riastrad int top_margin, bottom_margin;
398 1.1 riastrad int interlace;
399 1.1 riastrad unsigned int hfreq_est;
400 1.3 riastrad int vsync_plus_bp, vback_porch __unused;
401 1.3 riastrad unsigned int vtotal_lines, vfieldrate_est __unused, hperiod __unused;
402 1.3 riastrad unsigned int vfield_rate, vframe_rate __unused;
403 1.1 riastrad int left_margin, right_margin;
404 1.1 riastrad unsigned int total_active_pixels, ideal_duty_cycle;
405 1.1 riastrad unsigned int hblank, total_pixels, pixel_freq;
406 1.1 riastrad int hsync, hfront_porch, vodd_front_porch_lines;
407 1.1 riastrad unsigned int tmp1, tmp2;
408 1.1 riastrad
409 1.1 riastrad drm_mode = drm_mode_create(dev);
410 1.1 riastrad if (!drm_mode)
411 1.1 riastrad return NULL;
412 1.1 riastrad
413 1.1 riastrad /* 1. In order to give correct results, the number of horizontal
414 1.1 riastrad * pixels requested is first processed to ensure that it is divisible
415 1.1 riastrad * by the character size, by rounding it to the nearest character
416 1.1 riastrad * cell boundary:
417 1.1 riastrad */
418 1.1 riastrad hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
419 1.1 riastrad hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
420 1.1 riastrad
421 1.1 riastrad /* 2. If interlace is requested, the number of vertical lines assumed
422 1.1 riastrad * by the calculation must be halved, as the computation calculates
423 1.1 riastrad * the number of vertical lines per field.
424 1.1 riastrad */
425 1.1 riastrad if (interlaced)
426 1.1 riastrad vdisplay_rnd = vdisplay / 2;
427 1.1 riastrad else
428 1.1 riastrad vdisplay_rnd = vdisplay;
429 1.1 riastrad
430 1.1 riastrad /* 3. Find the frame rate required: */
431 1.1 riastrad if (interlaced)
432 1.1 riastrad vfieldrate_rqd = vrefresh * 2;
433 1.1 riastrad else
434 1.1 riastrad vfieldrate_rqd = vrefresh;
435 1.1 riastrad
436 1.1 riastrad /* 4. Find number of lines in Top margin: */
437 1.1 riastrad top_margin = 0;
438 1.1 riastrad if (margins)
439 1.1 riastrad top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
440 1.1 riastrad 1000;
441 1.1 riastrad /* 5. Find number of lines in bottom margin: */
442 1.1 riastrad bottom_margin = top_margin;
443 1.1 riastrad
444 1.1 riastrad /* 6. If interlace is required, then set variable interlace: */
445 1.1 riastrad if (interlaced)
446 1.1 riastrad interlace = 1;
447 1.1 riastrad else
448 1.1 riastrad interlace = 0;
449 1.1 riastrad
450 1.1 riastrad /* 7. Estimate the Horizontal frequency */
451 1.1 riastrad {
452 1.1 riastrad tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
453 1.1 riastrad tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
454 1.1 riastrad 2 + interlace;
455 1.1 riastrad hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
456 1.1 riastrad }
457 1.1 riastrad
458 1.1 riastrad /* 8. Find the number of lines in V sync + back porch */
459 1.1 riastrad /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
460 1.1 riastrad vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
461 1.1 riastrad vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
462 1.1 riastrad /* 9. Find the number of lines in V back porch alone: */
463 1.1 riastrad vback_porch = vsync_plus_bp - V_SYNC_RQD;
464 1.1 riastrad /* 10. Find the total number of lines in Vertical field period: */
465 1.1 riastrad vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
466 1.1 riastrad vsync_plus_bp + GTF_MIN_V_PORCH;
467 1.1 riastrad /* 11. Estimate the Vertical field frequency: */
468 1.1 riastrad vfieldrate_est = hfreq_est / vtotal_lines;
469 1.1 riastrad /* 12. Find the actual horizontal period: */
470 1.1 riastrad hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
471 1.1 riastrad
472 1.1 riastrad /* 13. Find the actual Vertical field frequency: */
473 1.1 riastrad vfield_rate = hfreq_est / vtotal_lines;
474 1.1 riastrad /* 14. Find the Vertical frame frequency: */
475 1.1 riastrad if (interlaced)
476 1.1 riastrad vframe_rate = vfield_rate / 2;
477 1.1 riastrad else
478 1.1 riastrad vframe_rate = vfield_rate;
479 1.1 riastrad /* 15. Find number of pixels in left margin: */
480 1.1 riastrad if (margins)
481 1.1 riastrad left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
482 1.1 riastrad 1000;
483 1.1 riastrad else
484 1.1 riastrad left_margin = 0;
485 1.1 riastrad
486 1.1 riastrad /* 16.Find number of pixels in right margin: */
487 1.1 riastrad right_margin = left_margin;
488 1.1 riastrad /* 17.Find total number of active pixels in image and left and right */
489 1.1 riastrad total_active_pixels = hdisplay_rnd + left_margin + right_margin;
490 1.1 riastrad /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
491 1.1 riastrad ideal_duty_cycle = GTF_C_PRIME * 1000 -
492 1.1 riastrad (GTF_M_PRIME * 1000000 / hfreq_est);
493 1.1 riastrad /* 19.Find the number of pixels in the blanking time to the nearest
494 1.1 riastrad * double character cell: */
495 1.1 riastrad hblank = total_active_pixels * ideal_duty_cycle /
496 1.1 riastrad (100000 - ideal_duty_cycle);
497 1.1 riastrad hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
498 1.1 riastrad hblank = hblank * 2 * GTF_CELL_GRAN;
499 1.1 riastrad /* 20.Find total number of pixels: */
500 1.1 riastrad total_pixels = total_active_pixels + hblank;
501 1.1 riastrad /* 21.Find pixel clock frequency: */
502 1.1 riastrad pixel_freq = total_pixels * hfreq_est / 1000;
503 1.1 riastrad /* Stage 1 computations are now complete; I should really pass
504 1.1 riastrad * the results to another function and do the Stage 2 computations,
505 1.1 riastrad * but I only need a few more values so I'll just append the
506 1.1 riastrad * computations here for now */
507 1.1 riastrad /* 17. Find the number of pixels in the horizontal sync period: */
508 1.1 riastrad hsync = H_SYNC_PERCENT * total_pixels / 100;
509 1.1 riastrad hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
510 1.1 riastrad hsync = hsync * GTF_CELL_GRAN;
511 1.1 riastrad /* 18. Find the number of pixels in horizontal front porch period */
512 1.1 riastrad hfront_porch = hblank / 2 - hsync;
513 1.1 riastrad /* 36. Find the number of lines in the odd front porch period: */
514 1.1 riastrad vodd_front_porch_lines = GTF_MIN_V_PORCH ;
515 1.1 riastrad
516 1.1 riastrad /* finally, pack the results in the mode struct */
517 1.1 riastrad drm_mode->hdisplay = hdisplay_rnd;
518 1.1 riastrad drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
519 1.1 riastrad drm_mode->hsync_end = drm_mode->hsync_start + hsync;
520 1.1 riastrad drm_mode->htotal = total_pixels;
521 1.1 riastrad drm_mode->vdisplay = vdisplay_rnd;
522 1.1 riastrad drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
523 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
524 1.1 riastrad drm_mode->vtotal = vtotal_lines;
525 1.1 riastrad
526 1.1 riastrad drm_mode->clock = pixel_freq;
527 1.1 riastrad
528 1.1 riastrad if (interlaced) {
529 1.1 riastrad drm_mode->vtotal *= 2;
530 1.1 riastrad drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
531 1.1 riastrad }
532 1.1 riastrad
533 1.1 riastrad drm_mode_set_name(drm_mode);
534 1.1 riastrad if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
535 1.1 riastrad drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
536 1.1 riastrad else
537 1.1 riastrad drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
538 1.1 riastrad
539 1.1 riastrad return drm_mode;
540 1.1 riastrad }
541 1.1 riastrad EXPORT_SYMBOL(drm_gtf_mode_complex);
542 1.1 riastrad
543 1.1 riastrad /**
544 1.4 riastrad * drm_gtf_mode - create the modeline based on the GTF algorithm
545 1.4 riastrad * @dev: drm device
546 1.4 riastrad * @hdisplay: hdisplay size
547 1.4 riastrad * @vdisplay: vdisplay size
548 1.4 riastrad * @vrefresh: vrefresh rate.
549 1.4 riastrad * @interlaced: whether to compute an interlaced mode
550 1.4 riastrad * @margins: desired margin (borders) size
551 1.1 riastrad *
552 1.1 riastrad * return the modeline based on GTF algorithm
553 1.1 riastrad *
554 1.1 riastrad * This function is to create the modeline based on the GTF algorithm.
555 1.1 riastrad * Generalized Timing Formula is derived from:
556 1.1 riastrad * GTF Spreadsheet by Andy Morrish (1/5/97)
557 1.1 riastrad * available at http://www.vesa.org
558 1.1 riastrad *
559 1.1 riastrad * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
560 1.1 riastrad * What I have done is to translate it by using integer calculation.
561 1.1 riastrad * I also refer to the function of fb_get_mode in the file of
562 1.1 riastrad * drivers/video/fbmon.c
563 1.1 riastrad *
564 1.1 riastrad * Standard GTF parameters:
565 1.1 riastrad * M = 600
566 1.1 riastrad * C = 40
567 1.1 riastrad * K = 128
568 1.1 riastrad * J = 20
569 1.4 riastrad *
570 1.4 riastrad * Returns:
571 1.4 riastrad * The modeline based on the GTF algorithm stored in a drm_display_mode object.
572 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
573 1.4 riastrad * when no mode could be allocated.
574 1.1 riastrad */
575 1.1 riastrad struct drm_display_mode *
576 1.1 riastrad drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
577 1.4 riastrad bool interlaced, int margins)
578 1.1 riastrad {
579 1.4 riastrad return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
580 1.4 riastrad interlaced, margins,
581 1.4 riastrad 600, 40 * 2, 128, 20 * 2);
582 1.1 riastrad }
583 1.1 riastrad EXPORT_SYMBOL(drm_gtf_mode);
584 1.1 riastrad
585 1.4 riastrad #ifdef CONFIG_VIDEOMODE_HELPERS
586 1.1 riastrad /**
587 1.4 riastrad * drm_display_mode_from_videomode - fill in @dmode using @vm,
588 1.4 riastrad * @vm: videomode structure to use as source
589 1.4 riastrad * @dmode: drm_display_mode structure to use as destination
590 1.4 riastrad *
591 1.4 riastrad * Fills out @dmode using the display mode specified in @vm.
592 1.4 riastrad */
593 1.4 riastrad void drm_display_mode_from_videomode(const struct videomode *vm,
594 1.4 riastrad struct drm_display_mode *dmode)
595 1.4 riastrad {
596 1.4 riastrad dmode->hdisplay = vm->hactive;
597 1.4 riastrad dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
598 1.4 riastrad dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
599 1.4 riastrad dmode->htotal = dmode->hsync_end + vm->hback_porch;
600 1.4 riastrad
601 1.4 riastrad dmode->vdisplay = vm->vactive;
602 1.4 riastrad dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
603 1.4 riastrad dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
604 1.4 riastrad dmode->vtotal = dmode->vsync_end + vm->vback_porch;
605 1.4 riastrad
606 1.4 riastrad dmode->clock = vm->pixelclock / 1000;
607 1.4 riastrad
608 1.4 riastrad dmode->flags = 0;
609 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
610 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_PHSYNC;
611 1.4 riastrad else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
612 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_NHSYNC;
613 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
614 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_PVSYNC;
615 1.4 riastrad else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
616 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_NVSYNC;
617 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_INTERLACED)
618 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_INTERLACE;
619 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
620 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
621 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
622 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_DBLCLK;
623 1.4 riastrad drm_mode_set_name(dmode);
624 1.1 riastrad }
625 1.4 riastrad EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
626 1.1 riastrad
627 1.7 riastrad /**
628 1.7 riastrad * drm_display_mode_to_videomode - fill in @vm using @dmode,
629 1.7 riastrad * @dmode: drm_display_mode structure to use as source
630 1.7 riastrad * @vm: videomode structure to use as destination
631 1.7 riastrad *
632 1.7 riastrad * Fills out @vm using the display mode specified in @dmode.
633 1.7 riastrad */
634 1.7 riastrad void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
635 1.7 riastrad struct videomode *vm)
636 1.7 riastrad {
637 1.7 riastrad vm->hactive = dmode->hdisplay;
638 1.7 riastrad vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
639 1.7 riastrad vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
640 1.7 riastrad vm->hback_porch = dmode->htotal - dmode->hsync_end;
641 1.7 riastrad
642 1.7 riastrad vm->vactive = dmode->vdisplay;
643 1.7 riastrad vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
644 1.7 riastrad vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
645 1.7 riastrad vm->vback_porch = dmode->vtotal - dmode->vsync_end;
646 1.7 riastrad
647 1.7 riastrad vm->pixelclock = dmode->clock * 1000;
648 1.7 riastrad
649 1.7 riastrad vm->flags = 0;
650 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
651 1.7 riastrad vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
652 1.7 riastrad else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
653 1.7 riastrad vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
654 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
655 1.7 riastrad vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
656 1.7 riastrad else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
657 1.7 riastrad vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
658 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
659 1.7 riastrad vm->flags |= DISPLAY_FLAGS_INTERLACED;
660 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
661 1.7 riastrad vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
662 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
663 1.7 riastrad vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
664 1.7 riastrad }
665 1.7 riastrad EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
666 1.7 riastrad
667 1.4 riastrad #ifdef CONFIG_OF
668 1.1 riastrad /**
669 1.4 riastrad * of_get_drm_display_mode - get a drm_display_mode from devicetree
670 1.4 riastrad * @np: device_node with the timing specification
671 1.4 riastrad * @dmode: will be set to the return value
672 1.4 riastrad * @index: index into the list of display timings in devicetree
673 1.1 riastrad *
674 1.4 riastrad * This function is expensive and should only be used, if only one mode is to be
675 1.4 riastrad * read from DT. To get multiple modes start with of_get_display_timings and
676 1.4 riastrad * work with that instead.
677 1.1 riastrad *
678 1.4 riastrad * Returns:
679 1.4 riastrad * 0 on success, a negative errno code when no of videomode node was found.
680 1.1 riastrad */
681 1.4 riastrad int of_get_drm_display_mode(struct device_node *np,
682 1.4 riastrad struct drm_display_mode *dmode, int index)
683 1.1 riastrad {
684 1.4 riastrad struct videomode vm;
685 1.4 riastrad int ret;
686 1.1 riastrad
687 1.4 riastrad ret = of_get_videomode(np, &vm, index);
688 1.4 riastrad if (ret)
689 1.4 riastrad return ret;
690 1.1 riastrad
691 1.4 riastrad drm_display_mode_from_videomode(&vm, dmode);
692 1.1 riastrad
693 1.4 riastrad pr_debug("%s: got %dx%d display mode from %s\n",
694 1.4 riastrad of_node_full_name(np), vm.hactive, vm.vactive, np->name);
695 1.4 riastrad drm_mode_debug_printmodeline(dmode);
696 1.1 riastrad
697 1.4 riastrad return 0;
698 1.1 riastrad }
699 1.4 riastrad EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
700 1.4 riastrad #endif /* CONFIG_OF */
701 1.4 riastrad #endif /* CONFIG_VIDEOMODE_HELPERS */
702 1.1 riastrad
703 1.1 riastrad /**
704 1.4 riastrad * drm_mode_set_name - set the name on a mode
705 1.4 riastrad * @mode: name will be set in this mode
706 1.1 riastrad *
707 1.4 riastrad * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
708 1.4 riastrad * with an optional 'i' suffix for interlaced modes.
709 1.1 riastrad */
710 1.4 riastrad void drm_mode_set_name(struct drm_display_mode *mode)
711 1.1 riastrad {
712 1.4 riastrad bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
713 1.4 riastrad
714 1.4 riastrad snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
715 1.4 riastrad mode->hdisplay, mode->vdisplay,
716 1.4 riastrad interlaced ? "i" : "");
717 1.1 riastrad }
718 1.4 riastrad EXPORT_SYMBOL(drm_mode_set_name);
719 1.1 riastrad
720 1.1 riastrad /** drm_mode_hsync - get the hsync of a mode
721 1.1 riastrad * @mode: mode
722 1.1 riastrad *
723 1.4 riastrad * Returns:
724 1.4 riastrad * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
725 1.4 riastrad * value first if it is not yet set.
726 1.1 riastrad */
727 1.1 riastrad int drm_mode_hsync(const struct drm_display_mode *mode)
728 1.1 riastrad {
729 1.1 riastrad unsigned int calc_val;
730 1.1 riastrad
731 1.1 riastrad if (mode->hsync)
732 1.1 riastrad return mode->hsync;
733 1.1 riastrad
734 1.1 riastrad if (mode->htotal < 0)
735 1.1 riastrad return 0;
736 1.1 riastrad
737 1.1 riastrad calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
738 1.1 riastrad calc_val += 500; /* round to 1000Hz */
739 1.1 riastrad calc_val /= 1000; /* truncate to kHz */
740 1.1 riastrad
741 1.1 riastrad return calc_val;
742 1.1 riastrad }
743 1.1 riastrad EXPORT_SYMBOL(drm_mode_hsync);
744 1.1 riastrad
745 1.1 riastrad /**
746 1.1 riastrad * drm_mode_vrefresh - get the vrefresh of a mode
747 1.1 riastrad * @mode: mode
748 1.1 riastrad *
749 1.4 riastrad * Returns:
750 1.4 riastrad * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
751 1.4 riastrad * value first if it is not yet set.
752 1.1 riastrad */
753 1.1 riastrad int drm_mode_vrefresh(const struct drm_display_mode *mode)
754 1.1 riastrad {
755 1.1 riastrad int refresh = 0;
756 1.1 riastrad unsigned int calc_val;
757 1.1 riastrad
758 1.1 riastrad if (mode->vrefresh > 0)
759 1.1 riastrad refresh = mode->vrefresh;
760 1.1 riastrad else if (mode->htotal > 0 && mode->vtotal > 0) {
761 1.1 riastrad int vtotal;
762 1.1 riastrad vtotal = mode->vtotal;
763 1.1 riastrad /* work out vrefresh the value will be x1000 */
764 1.1 riastrad calc_val = (mode->clock * 1000);
765 1.1 riastrad calc_val /= mode->htotal;
766 1.1 riastrad refresh = (calc_val + vtotal / 2) / vtotal;
767 1.1 riastrad
768 1.1 riastrad if (mode->flags & DRM_MODE_FLAG_INTERLACE)
769 1.1 riastrad refresh *= 2;
770 1.1 riastrad if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
771 1.1 riastrad refresh /= 2;
772 1.1 riastrad if (mode->vscan > 1)
773 1.1 riastrad refresh /= mode->vscan;
774 1.1 riastrad }
775 1.1 riastrad return refresh;
776 1.1 riastrad }
777 1.1 riastrad EXPORT_SYMBOL(drm_mode_vrefresh);
778 1.1 riastrad
779 1.1 riastrad /**
780 1.4 riastrad * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
781 1.1 riastrad * @p: mode
782 1.4 riastrad * @adjust_flags: a combination of adjustment flags
783 1.1 riastrad *
784 1.4 riastrad * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
785 1.1 riastrad *
786 1.4 riastrad * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
787 1.4 riastrad * interlaced modes.
788 1.4 riastrad * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
789 1.4 riastrad * buffers containing two eyes (only adjust the timings when needed, eg. for
790 1.4 riastrad * "frame packing" or "side by side full").
791 1.7 riastrad * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
792 1.7 riastrad * be performed for doublescan and vscan > 1 modes respectively.
793 1.1 riastrad */
794 1.1 riastrad void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
795 1.1 riastrad {
796 1.1 riastrad if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
797 1.1 riastrad return;
798 1.1 riastrad
799 1.4 riastrad p->crtc_clock = p->clock;
800 1.1 riastrad p->crtc_hdisplay = p->hdisplay;
801 1.1 riastrad p->crtc_hsync_start = p->hsync_start;
802 1.1 riastrad p->crtc_hsync_end = p->hsync_end;
803 1.1 riastrad p->crtc_htotal = p->htotal;
804 1.1 riastrad p->crtc_hskew = p->hskew;
805 1.1 riastrad p->crtc_vdisplay = p->vdisplay;
806 1.1 riastrad p->crtc_vsync_start = p->vsync_start;
807 1.1 riastrad p->crtc_vsync_end = p->vsync_end;
808 1.1 riastrad p->crtc_vtotal = p->vtotal;
809 1.1 riastrad
810 1.1 riastrad if (p->flags & DRM_MODE_FLAG_INTERLACE) {
811 1.1 riastrad if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
812 1.1 riastrad p->crtc_vdisplay /= 2;
813 1.1 riastrad p->crtc_vsync_start /= 2;
814 1.1 riastrad p->crtc_vsync_end /= 2;
815 1.1 riastrad p->crtc_vtotal /= 2;
816 1.1 riastrad }
817 1.1 riastrad }
818 1.1 riastrad
819 1.7 riastrad if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
820 1.7 riastrad if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
821 1.7 riastrad p->crtc_vdisplay *= 2;
822 1.7 riastrad p->crtc_vsync_start *= 2;
823 1.7 riastrad p->crtc_vsync_end *= 2;
824 1.7 riastrad p->crtc_vtotal *= 2;
825 1.7 riastrad }
826 1.1 riastrad }
827 1.1 riastrad
828 1.7 riastrad if (!(adjust_flags & CRTC_NO_VSCAN)) {
829 1.7 riastrad if (p->vscan > 1) {
830 1.7 riastrad p->crtc_vdisplay *= p->vscan;
831 1.7 riastrad p->crtc_vsync_start *= p->vscan;
832 1.7 riastrad p->crtc_vsync_end *= p->vscan;
833 1.7 riastrad p->crtc_vtotal *= p->vscan;
834 1.7 riastrad }
835 1.1 riastrad }
836 1.1 riastrad
837 1.4 riastrad if (adjust_flags & CRTC_STEREO_DOUBLE) {
838 1.4 riastrad unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
839 1.4 riastrad
840 1.4 riastrad switch (layout) {
841 1.4 riastrad case DRM_MODE_FLAG_3D_FRAME_PACKING:
842 1.4 riastrad p->crtc_clock *= 2;
843 1.4 riastrad p->crtc_vdisplay += p->crtc_vtotal;
844 1.4 riastrad p->crtc_vsync_start += p->crtc_vtotal;
845 1.4 riastrad p->crtc_vsync_end += p->crtc_vtotal;
846 1.4 riastrad p->crtc_vtotal += p->crtc_vtotal;
847 1.4 riastrad break;
848 1.4 riastrad }
849 1.4 riastrad }
850 1.4 riastrad
851 1.1 riastrad p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
852 1.1 riastrad p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
853 1.1 riastrad p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
854 1.1 riastrad p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
855 1.1 riastrad }
856 1.1 riastrad EXPORT_SYMBOL(drm_mode_set_crtcinfo);
857 1.1 riastrad
858 1.1 riastrad /**
859 1.1 riastrad * drm_mode_copy - copy the mode
860 1.1 riastrad * @dst: mode to overwrite
861 1.1 riastrad * @src: mode to copy
862 1.1 riastrad *
863 1.4 riastrad * Copy an existing mode into another mode, preserving the object id and
864 1.4 riastrad * list head of the destination mode.
865 1.1 riastrad */
866 1.1 riastrad void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
867 1.1 riastrad {
868 1.1 riastrad int id = dst->base.id;
869 1.4 riastrad struct list_head head = dst->head;
870 1.1 riastrad
871 1.1 riastrad *dst = *src;
872 1.1 riastrad dst->base.id = id;
873 1.4 riastrad dst->head = head;
874 1.1 riastrad }
875 1.1 riastrad EXPORT_SYMBOL(drm_mode_copy);
876 1.1 riastrad
877 1.1 riastrad /**
878 1.1 riastrad * drm_mode_duplicate - allocate and duplicate an existing mode
879 1.4 riastrad * @dev: drm_device to allocate the duplicated mode for
880 1.4 riastrad * @mode: mode to duplicate
881 1.1 riastrad *
882 1.1 riastrad * Just allocate a new mode, copy the existing mode into it, and return
883 1.1 riastrad * a pointer to it. Used to create new instances of established modes.
884 1.4 riastrad *
885 1.4 riastrad * Returns:
886 1.4 riastrad * Pointer to duplicated mode on success, NULL on error.
887 1.1 riastrad */
888 1.1 riastrad struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
889 1.1 riastrad const struct drm_display_mode *mode)
890 1.1 riastrad {
891 1.1 riastrad struct drm_display_mode *nmode;
892 1.1 riastrad
893 1.1 riastrad nmode = drm_mode_create(dev);
894 1.1 riastrad if (!nmode)
895 1.1 riastrad return NULL;
896 1.1 riastrad
897 1.1 riastrad drm_mode_copy(nmode, mode);
898 1.1 riastrad
899 1.1 riastrad return nmode;
900 1.1 riastrad }
901 1.1 riastrad EXPORT_SYMBOL(drm_mode_duplicate);
902 1.1 riastrad
903 1.1 riastrad /**
904 1.1 riastrad * drm_mode_equal - test modes for equality
905 1.1 riastrad * @mode1: first mode
906 1.1 riastrad * @mode2: second mode
907 1.1 riastrad *
908 1.1 riastrad * Check to see if @mode1 and @mode2 are equivalent.
909 1.1 riastrad *
910 1.4 riastrad * Returns:
911 1.1 riastrad * True if the modes are equal, false otherwise.
912 1.1 riastrad */
913 1.1 riastrad bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
914 1.1 riastrad {
915 1.7 riastrad if (!mode1 && !mode2)
916 1.7 riastrad return true;
917 1.7 riastrad
918 1.7 riastrad if (!mode1 || !mode2)
919 1.7 riastrad return false;
920 1.7 riastrad
921 1.1 riastrad /* do clock check convert to PICOS so fb modes get matched
922 1.1 riastrad * the same */
923 1.1 riastrad if (mode1->clock && mode2->clock) {
924 1.1 riastrad if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
925 1.1 riastrad return false;
926 1.1 riastrad } else if (mode1->clock != mode2->clock)
927 1.1 riastrad return false;
928 1.1 riastrad
929 1.4 riastrad if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
930 1.4 riastrad (mode2->flags & DRM_MODE_FLAG_3D_MASK))
931 1.4 riastrad return false;
932 1.4 riastrad
933 1.4 riastrad return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
934 1.4 riastrad }
935 1.4 riastrad EXPORT_SYMBOL(drm_mode_equal);
936 1.4 riastrad
937 1.4 riastrad /**
938 1.4 riastrad * drm_mode_equal_no_clocks_no_stereo - test modes for equality
939 1.4 riastrad * @mode1: first mode
940 1.4 riastrad * @mode2: second mode
941 1.4 riastrad *
942 1.4 riastrad * Check to see if @mode1 and @mode2 are equivalent, but
943 1.4 riastrad * don't check the pixel clocks nor the stereo layout.
944 1.4 riastrad *
945 1.4 riastrad * Returns:
946 1.4 riastrad * True if the modes are equal, false otherwise.
947 1.4 riastrad */
948 1.4 riastrad bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
949 1.4 riastrad const struct drm_display_mode *mode2)
950 1.4 riastrad {
951 1.1 riastrad if (mode1->hdisplay == mode2->hdisplay &&
952 1.1 riastrad mode1->hsync_start == mode2->hsync_start &&
953 1.1 riastrad mode1->hsync_end == mode2->hsync_end &&
954 1.1 riastrad mode1->htotal == mode2->htotal &&
955 1.1 riastrad mode1->hskew == mode2->hskew &&
956 1.1 riastrad mode1->vdisplay == mode2->vdisplay &&
957 1.1 riastrad mode1->vsync_start == mode2->vsync_start &&
958 1.1 riastrad mode1->vsync_end == mode2->vsync_end &&
959 1.1 riastrad mode1->vtotal == mode2->vtotal &&
960 1.1 riastrad mode1->vscan == mode2->vscan &&
961 1.4 riastrad (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
962 1.4 riastrad (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
963 1.1 riastrad return true;
964 1.1 riastrad
965 1.1 riastrad return false;
966 1.1 riastrad }
967 1.4 riastrad EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
968 1.1 riastrad
969 1.1 riastrad /**
970 1.7 riastrad * drm_mode_validate_basic - make sure the mode is somewhat sane
971 1.7 riastrad * @mode: mode to check
972 1.7 riastrad *
973 1.7 riastrad * Check that the mode timings are at least somewhat reasonable.
974 1.7 riastrad * Any hardware specific limits are left up for each driver to check.
975 1.7 riastrad *
976 1.7 riastrad * Returns:
977 1.7 riastrad * The mode status
978 1.7 riastrad */
979 1.7 riastrad enum drm_mode_status
980 1.7 riastrad drm_mode_validate_basic(const struct drm_display_mode *mode)
981 1.7 riastrad {
982 1.7 riastrad if (mode->clock == 0)
983 1.7 riastrad return MODE_CLOCK_LOW;
984 1.7 riastrad
985 1.7 riastrad if (mode->hdisplay == 0 ||
986 1.7 riastrad mode->hsync_start < mode->hdisplay ||
987 1.7 riastrad mode->hsync_end < mode->hsync_start ||
988 1.7 riastrad mode->htotal < mode->hsync_end)
989 1.7 riastrad return MODE_H_ILLEGAL;
990 1.7 riastrad
991 1.7 riastrad if (mode->vdisplay == 0 ||
992 1.7 riastrad mode->vsync_start < mode->vdisplay ||
993 1.7 riastrad mode->vsync_end < mode->vsync_start ||
994 1.7 riastrad mode->vtotal < mode->vsync_end)
995 1.7 riastrad return MODE_V_ILLEGAL;
996 1.7 riastrad
997 1.7 riastrad return MODE_OK;
998 1.7 riastrad }
999 1.7 riastrad EXPORT_SYMBOL(drm_mode_validate_basic);
1000 1.7 riastrad
1001 1.7 riastrad /**
1002 1.1 riastrad * drm_mode_validate_size - make sure modes adhere to size constraints
1003 1.7 riastrad * @mode: mode to check
1004 1.1 riastrad * @maxX: maximum width
1005 1.1 riastrad * @maxY: maximum height
1006 1.1 riastrad *
1007 1.4 riastrad * This function is a helper which can be used to validate modes against size
1008 1.4 riastrad * limitations of the DRM device/connector. If a mode is too big its status
1009 1.7 riastrad * member is updated with the appropriate validation failure code. The list
1010 1.4 riastrad * itself is not changed.
1011 1.7 riastrad *
1012 1.7 riastrad * Returns:
1013 1.7 riastrad * The mode status
1014 1.1 riastrad */
1015 1.7 riastrad enum drm_mode_status
1016 1.7 riastrad drm_mode_validate_size(const struct drm_display_mode *mode,
1017 1.7 riastrad int maxX, int maxY)
1018 1.1 riastrad {
1019 1.7 riastrad if (maxX > 0 && mode->hdisplay > maxX)
1020 1.7 riastrad return MODE_VIRTUAL_X;
1021 1.1 riastrad
1022 1.7 riastrad if (maxY > 0 && mode->vdisplay > maxY)
1023 1.7 riastrad return MODE_VIRTUAL_Y;
1024 1.1 riastrad
1025 1.7 riastrad return MODE_OK;
1026 1.1 riastrad }
1027 1.1 riastrad EXPORT_SYMBOL(drm_mode_validate_size);
1028 1.1 riastrad
1029 1.7 riastrad #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1030 1.7 riastrad
1031 1.7 riastrad static const char * const drm_mode_status_names[] = {
1032 1.7 riastrad MODE_STATUS(OK),
1033 1.7 riastrad MODE_STATUS(HSYNC),
1034 1.7 riastrad MODE_STATUS(VSYNC),
1035 1.7 riastrad MODE_STATUS(H_ILLEGAL),
1036 1.7 riastrad MODE_STATUS(V_ILLEGAL),
1037 1.7 riastrad MODE_STATUS(BAD_WIDTH),
1038 1.7 riastrad MODE_STATUS(NOMODE),
1039 1.7 riastrad MODE_STATUS(NO_INTERLACE),
1040 1.7 riastrad MODE_STATUS(NO_DBLESCAN),
1041 1.7 riastrad MODE_STATUS(NO_VSCAN),
1042 1.7 riastrad MODE_STATUS(MEM),
1043 1.7 riastrad MODE_STATUS(VIRTUAL_X),
1044 1.7 riastrad MODE_STATUS(VIRTUAL_Y),
1045 1.7 riastrad MODE_STATUS(MEM_VIRT),
1046 1.7 riastrad MODE_STATUS(NOCLOCK),
1047 1.7 riastrad MODE_STATUS(CLOCK_HIGH),
1048 1.7 riastrad MODE_STATUS(CLOCK_LOW),
1049 1.7 riastrad MODE_STATUS(CLOCK_RANGE),
1050 1.7 riastrad MODE_STATUS(BAD_HVALUE),
1051 1.7 riastrad MODE_STATUS(BAD_VVALUE),
1052 1.7 riastrad MODE_STATUS(BAD_VSCAN),
1053 1.7 riastrad MODE_STATUS(HSYNC_NARROW),
1054 1.7 riastrad MODE_STATUS(HSYNC_WIDE),
1055 1.7 riastrad MODE_STATUS(HBLANK_NARROW),
1056 1.7 riastrad MODE_STATUS(HBLANK_WIDE),
1057 1.7 riastrad MODE_STATUS(VSYNC_NARROW),
1058 1.7 riastrad MODE_STATUS(VSYNC_WIDE),
1059 1.7 riastrad MODE_STATUS(VBLANK_NARROW),
1060 1.7 riastrad MODE_STATUS(VBLANK_WIDE),
1061 1.7 riastrad MODE_STATUS(PANEL),
1062 1.7 riastrad MODE_STATUS(INTERLACE_WIDTH),
1063 1.7 riastrad MODE_STATUS(ONE_WIDTH),
1064 1.7 riastrad MODE_STATUS(ONE_HEIGHT),
1065 1.7 riastrad MODE_STATUS(ONE_SIZE),
1066 1.7 riastrad MODE_STATUS(NO_REDUCED),
1067 1.7 riastrad MODE_STATUS(NO_STEREO),
1068 1.7 riastrad MODE_STATUS(UNVERIFIED),
1069 1.7 riastrad MODE_STATUS(BAD),
1070 1.7 riastrad MODE_STATUS(ERROR),
1071 1.7 riastrad };
1072 1.7 riastrad
1073 1.7 riastrad #undef MODE_STATUS
1074 1.7 riastrad
1075 1.7 riastrad static const char *drm_get_mode_status_name(enum drm_mode_status status)
1076 1.7 riastrad {
1077 1.7 riastrad int index = status + 3;
1078 1.7 riastrad
1079 1.7 riastrad if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1080 1.7 riastrad return "";
1081 1.7 riastrad
1082 1.7 riastrad return drm_mode_status_names[index];
1083 1.7 riastrad }
1084 1.7 riastrad
1085 1.1 riastrad /**
1086 1.1 riastrad * drm_mode_prune_invalid - remove invalid modes from mode list
1087 1.1 riastrad * @dev: DRM device
1088 1.1 riastrad * @mode_list: list of modes to check
1089 1.1 riastrad * @verbose: be verbose about it
1090 1.1 riastrad *
1091 1.4 riastrad * This helper function can be used to prune a display mode list after
1092 1.4 riastrad * validation has been completed. All modes who's status is not MODE_OK will be
1093 1.4 riastrad * removed from the list, and if @verbose the status code and mode name is also
1094 1.4 riastrad * printed to dmesg.
1095 1.1 riastrad */
1096 1.1 riastrad void drm_mode_prune_invalid(struct drm_device *dev,
1097 1.1 riastrad struct list_head *mode_list, bool verbose)
1098 1.1 riastrad {
1099 1.1 riastrad struct drm_display_mode *mode, *t;
1100 1.1 riastrad
1101 1.1 riastrad list_for_each_entry_safe(mode, t, mode_list, head) {
1102 1.1 riastrad if (mode->status != MODE_OK) {
1103 1.1 riastrad list_del(&mode->head);
1104 1.1 riastrad if (verbose) {
1105 1.1 riastrad drm_mode_debug_printmodeline(mode);
1106 1.7 riastrad DRM_DEBUG_KMS("Not using %s mode: %s\n",
1107 1.7 riastrad mode->name,
1108 1.7 riastrad drm_get_mode_status_name(mode->status));
1109 1.1 riastrad }
1110 1.1 riastrad drm_mode_destroy(dev, mode);
1111 1.1 riastrad }
1112 1.1 riastrad }
1113 1.1 riastrad }
1114 1.1 riastrad EXPORT_SYMBOL(drm_mode_prune_invalid);
1115 1.1 riastrad
1116 1.1 riastrad /**
1117 1.1 riastrad * drm_mode_compare - compare modes for favorability
1118 1.1 riastrad * @priv: unused
1119 1.1 riastrad * @lh_a: list_head for first mode
1120 1.1 riastrad * @lh_b: list_head for second mode
1121 1.1 riastrad *
1122 1.1 riastrad * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1123 1.1 riastrad * which is better.
1124 1.1 riastrad *
1125 1.4 riastrad * Returns:
1126 1.1 riastrad * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1127 1.1 riastrad * positive if @lh_b is better than @lh_a.
1128 1.1 riastrad */
1129 1.1 riastrad static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
1130 1.1 riastrad {
1131 1.1 riastrad struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1132 1.1 riastrad struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1133 1.1 riastrad int diff;
1134 1.1 riastrad
1135 1.1 riastrad diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1136 1.1 riastrad ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1137 1.1 riastrad if (diff)
1138 1.1 riastrad return diff;
1139 1.1 riastrad diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1140 1.1 riastrad if (diff)
1141 1.1 riastrad return diff;
1142 1.4 riastrad
1143 1.4 riastrad diff = b->vrefresh - a->vrefresh;
1144 1.4 riastrad if (diff)
1145 1.4 riastrad return diff;
1146 1.4 riastrad
1147 1.1 riastrad diff = b->clock - a->clock;
1148 1.1 riastrad return diff;
1149 1.1 riastrad }
1150 1.1 riastrad
1151 1.1 riastrad /**
1152 1.1 riastrad * drm_mode_sort - sort mode list
1153 1.4 riastrad * @mode_list: list of drm_display_mode structures to sort
1154 1.1 riastrad *
1155 1.4 riastrad * Sort @mode_list by favorability, moving good modes to the head of the list.
1156 1.1 riastrad */
1157 1.1 riastrad void drm_mode_sort(struct list_head *mode_list)
1158 1.1 riastrad {
1159 1.1 riastrad list_sort(NULL, mode_list, drm_mode_compare);
1160 1.1 riastrad }
1161 1.1 riastrad EXPORT_SYMBOL(drm_mode_sort);
1162 1.1 riastrad
1163 1.1 riastrad /**
1164 1.1 riastrad * drm_mode_connector_list_update - update the mode list for the connector
1165 1.1 riastrad * @connector: the connector to update
1166 1.7 riastrad * @merge_type_bits: whether to merge or overwrite type bits
1167 1.1 riastrad *
1168 1.1 riastrad * This moves the modes from the @connector probed_modes list
1169 1.1 riastrad * to the actual mode list. It compares the probed mode against the current
1170 1.4 riastrad * list and only adds different/new modes.
1171 1.4 riastrad *
1172 1.4 riastrad * This is just a helper functions doesn't validate any modes itself and also
1173 1.4 riastrad * doesn't prune any invalid modes. Callers need to do that themselves.
1174 1.1 riastrad */
1175 1.7 riastrad void drm_mode_connector_list_update(struct drm_connector *connector,
1176 1.7 riastrad bool merge_type_bits)
1177 1.1 riastrad {
1178 1.1 riastrad struct drm_display_mode *mode;
1179 1.1 riastrad struct drm_display_mode *pmode, *pt;
1180 1.1 riastrad int found_it;
1181 1.1 riastrad
1182 1.4 riastrad WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1183 1.4 riastrad
1184 1.1 riastrad list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
1185 1.1 riastrad head) {
1186 1.1 riastrad found_it = 0;
1187 1.1 riastrad /* go through current modes checking for the new probed mode */
1188 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) {
1189 1.1 riastrad if (drm_mode_equal(pmode, mode)) {
1190 1.1 riastrad found_it = 1;
1191 1.1 riastrad /* if equal delete the probed mode */
1192 1.1 riastrad mode->status = pmode->status;
1193 1.1 riastrad /* Merge type bits together */
1194 1.7 riastrad if (merge_type_bits)
1195 1.7 riastrad mode->type |= pmode->type;
1196 1.7 riastrad else
1197 1.7 riastrad mode->type = pmode->type;
1198 1.1 riastrad list_del(&pmode->head);
1199 1.1 riastrad drm_mode_destroy(connector->dev, pmode);
1200 1.1 riastrad break;
1201 1.1 riastrad }
1202 1.1 riastrad }
1203 1.1 riastrad
1204 1.1 riastrad if (!found_it) {
1205 1.1 riastrad list_move_tail(&pmode->head, &connector->modes);
1206 1.1 riastrad }
1207 1.1 riastrad }
1208 1.1 riastrad }
1209 1.1 riastrad EXPORT_SYMBOL(drm_mode_connector_list_update);
1210 1.1 riastrad
1211 1.1 riastrad /**
1212 1.4 riastrad * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1213 1.4 riastrad * @mode_option: optional per connector mode option
1214 1.4 riastrad * @connector: connector to parse modeline for
1215 1.4 riastrad * @mode: preallocated drm_cmdline_mode structure to fill out
1216 1.4 riastrad *
1217 1.4 riastrad * This parses @mode_option command line modeline for modes and options to
1218 1.4 riastrad * configure the connector. If @mode_option is NULL the default command line
1219 1.4 riastrad * modeline in fb_mode_option will be parsed instead.
1220 1.1 riastrad *
1221 1.4 riastrad * This uses the same parameters as the fb modedb.c, except for an extra
1222 1.4 riastrad * force-enable, force-enable-digital and force-disable bit at the end:
1223 1.1 riastrad *
1224 1.1 riastrad * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1225 1.1 riastrad *
1226 1.4 riastrad * The intermediate drm_cmdline_mode structure is required to store additional
1227 1.7 riastrad * options from the command line modline like the force-enable/disable flag.
1228 1.4 riastrad *
1229 1.4 riastrad * Returns:
1230 1.4 riastrad * True if a valid modeline has been parsed, false otherwise.
1231 1.1 riastrad */
1232 1.1 riastrad bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1233 1.1 riastrad struct drm_connector *connector,
1234 1.1 riastrad struct drm_cmdline_mode *mode)
1235 1.1 riastrad {
1236 1.1 riastrad const char *name;
1237 1.1 riastrad unsigned int namelen;
1238 1.1 riastrad bool res_specified = false, bpp_specified = false, refresh_specified = false;
1239 1.6 jmcneill long xres = 0, yres = 0, bpp = 32, refresh = 0;
1240 1.1 riastrad bool yres_specified = false, cvt = false, rb = false;
1241 1.1 riastrad bool interlace = false, margins = false, was_digit = false;
1242 1.1 riastrad int i;
1243 1.1 riastrad enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
1244 1.1 riastrad
1245 1.6 jmcneill #if !defined(__NetBSD__)
1246 1.1 riastrad #ifdef CONFIG_FB
1247 1.1 riastrad if (!mode_option)
1248 1.1 riastrad mode_option = fb_mode_option;
1249 1.1 riastrad #endif
1250 1.6 jmcneill #endif
1251 1.1 riastrad
1252 1.1 riastrad if (!mode_option) {
1253 1.1 riastrad mode->specified = false;
1254 1.1 riastrad return false;
1255 1.1 riastrad }
1256 1.1 riastrad
1257 1.1 riastrad name = mode_option;
1258 1.1 riastrad namelen = strlen(name);
1259 1.1 riastrad for (i = namelen-1; i >= 0; i--) {
1260 1.1 riastrad switch (name[i]) {
1261 1.1 riastrad case '@':
1262 1.1 riastrad if (!refresh_specified && !bpp_specified &&
1263 1.1 riastrad !yres_specified && !cvt && !rb && was_digit) {
1264 1.6 jmcneill if (kstrtol(&name[i+1], 10, &refresh) == 0) {
1265 1.6 jmcneill refresh_specified = true;
1266 1.6 jmcneill was_digit = false;
1267 1.6 jmcneill } else {
1268 1.6 jmcneill goto done;
1269 1.6 jmcneill }
1270 1.1 riastrad } else
1271 1.1 riastrad goto done;
1272 1.1 riastrad break;
1273 1.1 riastrad case '-':
1274 1.1 riastrad if (!bpp_specified && !yres_specified && !cvt &&
1275 1.1 riastrad !rb && was_digit) {
1276 1.6 jmcneill if (kstrtol(&name[i+1], 10, &bpp) == 0) {
1277 1.6 jmcneill bpp_specified = true;
1278 1.6 jmcneill was_digit = false;
1279 1.6 jmcneill } else {
1280 1.6 jmcneill goto done;
1281 1.6 jmcneill }
1282 1.1 riastrad } else
1283 1.1 riastrad goto done;
1284 1.1 riastrad break;
1285 1.1 riastrad case 'x':
1286 1.1 riastrad if (!yres_specified && was_digit) {
1287 1.6 jmcneill if (kstrtol(&name[i+1], 10, &yres) == 0) {
1288 1.6 jmcneill yres_specified = true;
1289 1.6 jmcneill was_digit = false;
1290 1.6 jmcneill } else {
1291 1.6 jmcneill goto done;
1292 1.6 jmcneill }
1293 1.1 riastrad } else
1294 1.1 riastrad goto done;
1295 1.4 riastrad break;
1296 1.1 riastrad case '0' ... '9':
1297 1.1 riastrad was_digit = true;
1298 1.1 riastrad break;
1299 1.1 riastrad case 'M':
1300 1.1 riastrad if (yres_specified || cvt || was_digit)
1301 1.1 riastrad goto done;
1302 1.1 riastrad cvt = true;
1303 1.1 riastrad break;
1304 1.1 riastrad case 'R':
1305 1.1 riastrad if (yres_specified || cvt || rb || was_digit)
1306 1.1 riastrad goto done;
1307 1.1 riastrad rb = true;
1308 1.1 riastrad break;
1309 1.1 riastrad case 'm':
1310 1.1 riastrad if (cvt || yres_specified || was_digit)
1311 1.1 riastrad goto done;
1312 1.1 riastrad margins = true;
1313 1.1 riastrad break;
1314 1.1 riastrad case 'i':
1315 1.1 riastrad if (cvt || yres_specified || was_digit)
1316 1.1 riastrad goto done;
1317 1.1 riastrad interlace = true;
1318 1.1 riastrad break;
1319 1.1 riastrad case 'e':
1320 1.1 riastrad if (yres_specified || bpp_specified || refresh_specified ||
1321 1.1 riastrad was_digit || (force != DRM_FORCE_UNSPECIFIED))
1322 1.1 riastrad goto done;
1323 1.1 riastrad
1324 1.1 riastrad force = DRM_FORCE_ON;
1325 1.1 riastrad break;
1326 1.1 riastrad case 'D':
1327 1.1 riastrad if (yres_specified || bpp_specified || refresh_specified ||
1328 1.1 riastrad was_digit || (force != DRM_FORCE_UNSPECIFIED))
1329 1.1 riastrad goto done;
1330 1.1 riastrad
1331 1.1 riastrad if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1332 1.1 riastrad (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1333 1.1 riastrad force = DRM_FORCE_ON;
1334 1.1 riastrad else
1335 1.1 riastrad force = DRM_FORCE_ON_DIGITAL;
1336 1.1 riastrad break;
1337 1.1 riastrad case 'd':
1338 1.1 riastrad if (yres_specified || bpp_specified || refresh_specified ||
1339 1.1 riastrad was_digit || (force != DRM_FORCE_UNSPECIFIED))
1340 1.1 riastrad goto done;
1341 1.1 riastrad
1342 1.1 riastrad force = DRM_FORCE_OFF;
1343 1.1 riastrad break;
1344 1.1 riastrad default:
1345 1.1 riastrad goto done;
1346 1.1 riastrad }
1347 1.1 riastrad }
1348 1.1 riastrad
1349 1.1 riastrad if (i < 0 && yres_specified) {
1350 1.6 jmcneill char *ch = NULL;
1351 1.6 jmcneill xres = strtoll(name, &ch, 10);
1352 1.1 riastrad if ((ch != NULL) && (*ch == 'x'))
1353 1.1 riastrad res_specified = true;
1354 1.1 riastrad else
1355 1.1 riastrad i = ch - name;
1356 1.1 riastrad } else if (!yres_specified && was_digit) {
1357 1.1 riastrad /* catch mode that begins with digits but has no 'x' */
1358 1.1 riastrad i = 0;
1359 1.1 riastrad }
1360 1.1 riastrad done:
1361 1.1 riastrad if (i >= 0) {
1362 1.6 jmcneill DRM_ERROR(
1363 1.1 riastrad "parse error at position %i in video mode '%s'\n",
1364 1.1 riastrad i, name);
1365 1.1 riastrad mode->specified = false;
1366 1.1 riastrad return false;
1367 1.1 riastrad }
1368 1.1 riastrad
1369 1.1 riastrad if (res_specified) {
1370 1.1 riastrad mode->specified = true;
1371 1.1 riastrad mode->xres = xres;
1372 1.1 riastrad mode->yres = yres;
1373 1.1 riastrad }
1374 1.1 riastrad
1375 1.1 riastrad if (refresh_specified) {
1376 1.1 riastrad mode->refresh_specified = true;
1377 1.1 riastrad mode->refresh = refresh;
1378 1.1 riastrad }
1379 1.1 riastrad
1380 1.1 riastrad if (bpp_specified) {
1381 1.1 riastrad mode->bpp_specified = true;
1382 1.1 riastrad mode->bpp = bpp;
1383 1.1 riastrad }
1384 1.1 riastrad mode->rb = rb;
1385 1.1 riastrad mode->cvt = cvt;
1386 1.1 riastrad mode->interlace = interlace;
1387 1.1 riastrad mode->margins = margins;
1388 1.1 riastrad mode->force = force;
1389 1.1 riastrad
1390 1.1 riastrad return true;
1391 1.1 riastrad }
1392 1.1 riastrad EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1393 1.1 riastrad
1394 1.4 riastrad /**
1395 1.4 riastrad * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1396 1.4 riastrad * @dev: DRM device to create the new mode for
1397 1.4 riastrad * @cmd: input command line modeline
1398 1.4 riastrad *
1399 1.4 riastrad * Returns:
1400 1.4 riastrad * Pointer to converted mode on success, NULL on error.
1401 1.4 riastrad */
1402 1.1 riastrad struct drm_display_mode *
1403 1.1 riastrad drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1404 1.1 riastrad struct drm_cmdline_mode *cmd)
1405 1.1 riastrad {
1406 1.1 riastrad struct drm_display_mode *mode;
1407 1.1 riastrad
1408 1.1 riastrad if (cmd->cvt)
1409 1.1 riastrad mode = drm_cvt_mode(dev,
1410 1.1 riastrad cmd->xres, cmd->yres,
1411 1.1 riastrad cmd->refresh_specified ? cmd->refresh : 60,
1412 1.1 riastrad cmd->rb, cmd->interlace,
1413 1.1 riastrad cmd->margins);
1414 1.1 riastrad else
1415 1.1 riastrad mode = drm_gtf_mode(dev,
1416 1.1 riastrad cmd->xres, cmd->yres,
1417 1.1 riastrad cmd->refresh_specified ? cmd->refresh : 60,
1418 1.1 riastrad cmd->interlace,
1419 1.1 riastrad cmd->margins);
1420 1.1 riastrad if (!mode)
1421 1.1 riastrad return NULL;
1422 1.1 riastrad
1423 1.7 riastrad mode->type |= DRM_MODE_TYPE_USERDEF;
1424 1.7 riastrad /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
1425 1.7 riastrad if (cmd->xres == 1366 && mode->hdisplay == 1368) {
1426 1.7 riastrad mode->hdisplay = 1366;
1427 1.7 riastrad mode->hsync_start--;
1428 1.7 riastrad mode->hsync_end--;
1429 1.7 riastrad drm_mode_set_name(mode);
1430 1.7 riastrad }
1431 1.1 riastrad drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1432 1.1 riastrad return mode;
1433 1.1 riastrad }
1434 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1435 1.7 riastrad
1436 1.7 riastrad /**
1437 1.7 riastrad * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1438 1.7 riastrad * @out: drm_mode_modeinfo struct to return to the user
1439 1.7 riastrad * @in: drm_display_mode to use
1440 1.7 riastrad *
1441 1.7 riastrad * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1442 1.7 riastrad * the user.
1443 1.7 riastrad */
1444 1.7 riastrad void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1445 1.7 riastrad const struct drm_display_mode *in)
1446 1.7 riastrad {
1447 1.7 riastrad WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1448 1.7 riastrad in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1449 1.7 riastrad in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1450 1.7 riastrad in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1451 1.7 riastrad in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1452 1.7 riastrad "timing values too large for mode info\n");
1453 1.7 riastrad
1454 1.7 riastrad out->clock = in->clock;
1455 1.7 riastrad out->hdisplay = in->hdisplay;
1456 1.7 riastrad out->hsync_start = in->hsync_start;
1457 1.7 riastrad out->hsync_end = in->hsync_end;
1458 1.7 riastrad out->htotal = in->htotal;
1459 1.7 riastrad out->hskew = in->hskew;
1460 1.7 riastrad out->vdisplay = in->vdisplay;
1461 1.7 riastrad out->vsync_start = in->vsync_start;
1462 1.7 riastrad out->vsync_end = in->vsync_end;
1463 1.7 riastrad out->vtotal = in->vtotal;
1464 1.7 riastrad out->vscan = in->vscan;
1465 1.7 riastrad out->vrefresh = in->vrefresh;
1466 1.7 riastrad out->flags = in->flags;
1467 1.7 riastrad out->type = in->type;
1468 1.7 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1469 1.7 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1470 1.7 riastrad }
1471 1.7 riastrad
1472 1.7 riastrad /**
1473 1.7 riastrad * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1474 1.7 riastrad * @out: drm_display_mode to return to the user
1475 1.7 riastrad * @in: drm_mode_modeinfo to use
1476 1.7 riastrad *
1477 1.7 riastrad * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1478 1.7 riastrad * the caller.
1479 1.7 riastrad *
1480 1.7 riastrad * Returns:
1481 1.7 riastrad * Zero on success, negative errno on failure.
1482 1.7 riastrad */
1483 1.7 riastrad int drm_mode_convert_umode(struct drm_display_mode *out,
1484 1.7 riastrad const struct drm_mode_modeinfo *in)
1485 1.7 riastrad {
1486 1.7 riastrad int ret = -EINVAL;
1487 1.7 riastrad
1488 1.7 riastrad if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
1489 1.7 riastrad ret = -ERANGE;
1490 1.7 riastrad goto out;
1491 1.7 riastrad }
1492 1.7 riastrad
1493 1.7 riastrad if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1494 1.7 riastrad goto out;
1495 1.7 riastrad
1496 1.7 riastrad out->clock = in->clock;
1497 1.7 riastrad out->hdisplay = in->hdisplay;
1498 1.7 riastrad out->hsync_start = in->hsync_start;
1499 1.7 riastrad out->hsync_end = in->hsync_end;
1500 1.7 riastrad out->htotal = in->htotal;
1501 1.7 riastrad out->hskew = in->hskew;
1502 1.7 riastrad out->vdisplay = in->vdisplay;
1503 1.7 riastrad out->vsync_start = in->vsync_start;
1504 1.7 riastrad out->vsync_end = in->vsync_end;
1505 1.7 riastrad out->vtotal = in->vtotal;
1506 1.7 riastrad out->vscan = in->vscan;
1507 1.7 riastrad out->vrefresh = in->vrefresh;
1508 1.7 riastrad out->flags = in->flags;
1509 1.7 riastrad out->type = in->type;
1510 1.7 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1511 1.7 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1512 1.7 riastrad
1513 1.7 riastrad out->status = drm_mode_validate_basic(out);
1514 1.7 riastrad if (out->status != MODE_OK)
1515 1.7 riastrad goto out;
1516 1.7 riastrad
1517 1.7 riastrad drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
1518 1.7 riastrad
1519 1.7 riastrad ret = 0;
1520 1.7 riastrad
1521 1.7 riastrad out:
1522 1.7 riastrad return ret;
1523 1.7 riastrad }
1524