drm_modes.c revision 1.10 1 1.10 riastrad /* $NetBSD: drm_modes.c,v 1.10 2021/12/18 23:44:57 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.10 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_modes.c,v 1.10 2021/12/18 23:44:57 riastradh Exp $");
37 1.7 riastrad
38 1.10 riastrad #include <linux/ctype.h>
39 1.1 riastrad #include <linux/list.h>
40 1.1 riastrad #include <linux/list_sort.h>
41 1.1 riastrad #include <linux/export.h>
42 1.10 riastrad
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.10 riastrad
50 1.10 riastrad #include <drm/drm_crtc.h>
51 1.10 riastrad #include <drm/drm_device.h>
52 1.4 riastrad #include <drm/drm_modes.h>
53 1.10 riastrad #include <drm/drm_print.h>
54 1.4 riastrad
55 1.4 riastrad #include "drm_crtc_internal.h"
56 1.1 riastrad
57 1.1 riastrad /**
58 1.4 riastrad * drm_mode_debug_printmodeline - print a mode to dmesg
59 1.1 riastrad * @mode: mode to print
60 1.1 riastrad *
61 1.1 riastrad * Describe @mode using DRM_DEBUG.
62 1.1 riastrad */
63 1.1 riastrad void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
64 1.1 riastrad {
65 1.10 riastrad DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
66 1.1 riastrad }
67 1.1 riastrad EXPORT_SYMBOL(drm_mode_debug_printmodeline);
68 1.1 riastrad
69 1.1 riastrad /**
70 1.4 riastrad * drm_mode_create - create a new display mode
71 1.4 riastrad * @dev: DRM device
72 1.4 riastrad *
73 1.4 riastrad * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
74 1.4 riastrad * and return it.
75 1.4 riastrad *
76 1.4 riastrad * Returns:
77 1.4 riastrad * Pointer to new mode on success, NULL on error.
78 1.4 riastrad */
79 1.4 riastrad struct drm_display_mode *drm_mode_create(struct drm_device *dev)
80 1.4 riastrad {
81 1.4 riastrad struct drm_display_mode *nmode;
82 1.4 riastrad
83 1.4 riastrad nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
84 1.4 riastrad if (!nmode)
85 1.4 riastrad return NULL;
86 1.4 riastrad
87 1.4 riastrad return nmode;
88 1.4 riastrad }
89 1.4 riastrad EXPORT_SYMBOL(drm_mode_create);
90 1.4 riastrad
91 1.4 riastrad /**
92 1.4 riastrad * drm_mode_destroy - remove a mode
93 1.1 riastrad * @dev: DRM device
94 1.4 riastrad * @mode: mode to remove
95 1.4 riastrad *
96 1.4 riastrad * Release @mode's unique ID, then free it @mode structure itself using kfree.
97 1.4 riastrad */
98 1.4 riastrad void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
99 1.4 riastrad {
100 1.4 riastrad if (!mode)
101 1.4 riastrad return;
102 1.4 riastrad
103 1.4 riastrad kfree(mode);
104 1.4 riastrad }
105 1.4 riastrad EXPORT_SYMBOL(drm_mode_destroy);
106 1.4 riastrad
107 1.4 riastrad /**
108 1.4 riastrad * drm_mode_probed_add - add a mode to a connector's probed_mode list
109 1.4 riastrad * @connector: connector the new mode
110 1.4 riastrad * @mode: mode data
111 1.4 riastrad *
112 1.4 riastrad * Add @mode to @connector's probed_mode list for later use. This list should
113 1.4 riastrad * then in a second step get filtered and all the modes actually supported by
114 1.4 riastrad * the hardware moved to the @connector's modes list.
115 1.4 riastrad */
116 1.4 riastrad void drm_mode_probed_add(struct drm_connector *connector,
117 1.4 riastrad struct drm_display_mode *mode)
118 1.4 riastrad {
119 1.4 riastrad WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
120 1.4 riastrad
121 1.4 riastrad list_add_tail(&mode->head, &connector->probed_modes);
122 1.4 riastrad }
123 1.4 riastrad EXPORT_SYMBOL(drm_mode_probed_add);
124 1.4 riastrad
125 1.4 riastrad /**
126 1.4 riastrad * drm_cvt_mode -create a modeline based on the CVT algorithm
127 1.4 riastrad * @dev: drm device
128 1.1 riastrad * @hdisplay: hdisplay size
129 1.1 riastrad * @vdisplay: vdisplay size
130 1.4 riastrad * @vrefresh: vrefresh rate
131 1.4 riastrad * @reduced: whether to use reduced blanking
132 1.4 riastrad * @interlaced: whether to compute an interlaced mode
133 1.4 riastrad * @margins: whether to add margins (borders)
134 1.1 riastrad *
135 1.1 riastrad * This function is called to generate the modeline based on CVT algorithm
136 1.1 riastrad * according to the hdisplay, vdisplay, vrefresh.
137 1.1 riastrad * It is based from the VESA(TM) Coordinated Video Timing Generator by
138 1.1 riastrad * Graham Loveridge April 9, 2003 available at
139 1.1 riastrad * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
140 1.1 riastrad *
141 1.1 riastrad * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
142 1.1 riastrad * What I have done is to translate it by using integer calculation.
143 1.4 riastrad *
144 1.4 riastrad * Returns:
145 1.4 riastrad * The modeline based on the CVT algorithm stored in a drm_display_mode object.
146 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
147 1.4 riastrad * when no mode could be allocated.
148 1.1 riastrad */
149 1.1 riastrad struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
150 1.1 riastrad int vdisplay, int vrefresh,
151 1.1 riastrad bool reduced, bool interlaced, bool margins)
152 1.1 riastrad {
153 1.4 riastrad #define HV_FACTOR 1000
154 1.1 riastrad /* 1) top/bottom margin size (% of height) - default: 1.8, */
155 1.1 riastrad #define CVT_MARGIN_PERCENTAGE 18
156 1.1 riastrad /* 2) character cell horizontal granularity (pixels) - default 8 */
157 1.1 riastrad #define CVT_H_GRANULARITY 8
158 1.1 riastrad /* 3) Minimum vertical porch (lines) - default 3 */
159 1.1 riastrad #define CVT_MIN_V_PORCH 3
160 1.1 riastrad /* 4) Minimum number of vertical back porch lines - default 6 */
161 1.1 riastrad #define CVT_MIN_V_BPORCH 6
162 1.1 riastrad /* Pixel Clock step (kHz) */
163 1.1 riastrad #define CVT_CLOCK_STEP 250
164 1.1 riastrad struct drm_display_mode *drm_mode;
165 1.1 riastrad unsigned int vfieldrate, hperiod;
166 1.1 riastrad int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
167 1.1 riastrad int interlace;
168 1.10 riastrad u64 tmp;
169 1.10 riastrad
170 1.10 riastrad if (!hdisplay || !vdisplay)
171 1.10 riastrad return NULL;
172 1.1 riastrad
173 1.1 riastrad /* allocate the drm_display_mode structure. If failure, we will
174 1.1 riastrad * return directly
175 1.1 riastrad */
176 1.1 riastrad drm_mode = drm_mode_create(dev);
177 1.1 riastrad if (!drm_mode)
178 1.1 riastrad return NULL;
179 1.1 riastrad
180 1.1 riastrad /* the CVT default refresh rate is 60Hz */
181 1.1 riastrad if (!vrefresh)
182 1.1 riastrad vrefresh = 60;
183 1.1 riastrad
184 1.1 riastrad /* the required field fresh rate */
185 1.1 riastrad if (interlaced)
186 1.1 riastrad vfieldrate = vrefresh * 2;
187 1.1 riastrad else
188 1.1 riastrad vfieldrate = vrefresh;
189 1.1 riastrad
190 1.1 riastrad /* horizontal pixels */
191 1.1 riastrad hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
192 1.1 riastrad
193 1.1 riastrad /* determine the left&right borders */
194 1.1 riastrad hmargin = 0;
195 1.1 riastrad if (margins) {
196 1.1 riastrad hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
197 1.1 riastrad hmargin -= hmargin % CVT_H_GRANULARITY;
198 1.1 riastrad }
199 1.1 riastrad /* find the total active pixels */
200 1.1 riastrad drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
201 1.1 riastrad
202 1.1 riastrad /* find the number of lines per field */
203 1.1 riastrad if (interlaced)
204 1.1 riastrad vdisplay_rnd = vdisplay / 2;
205 1.1 riastrad else
206 1.1 riastrad vdisplay_rnd = vdisplay;
207 1.1 riastrad
208 1.1 riastrad /* find the top & bottom borders */
209 1.1 riastrad vmargin = 0;
210 1.1 riastrad if (margins)
211 1.1 riastrad vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
212 1.1 riastrad
213 1.1 riastrad drm_mode->vdisplay = vdisplay + 2 * vmargin;
214 1.1 riastrad
215 1.1 riastrad /* Interlaced */
216 1.1 riastrad if (interlaced)
217 1.1 riastrad interlace = 1;
218 1.1 riastrad else
219 1.1 riastrad interlace = 0;
220 1.1 riastrad
221 1.1 riastrad /* Determine VSync Width from aspect ratio */
222 1.1 riastrad if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
223 1.1 riastrad vsync = 4;
224 1.1 riastrad else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
225 1.1 riastrad vsync = 5;
226 1.1 riastrad else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
227 1.1 riastrad vsync = 6;
228 1.1 riastrad else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
229 1.1 riastrad vsync = 7;
230 1.1 riastrad else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
231 1.1 riastrad vsync = 7;
232 1.1 riastrad else /* custom */
233 1.1 riastrad vsync = 10;
234 1.1 riastrad
235 1.1 riastrad if (!reduced) {
236 1.1 riastrad /* simplify the GTF calculation */
237 1.1 riastrad /* 4) Minimum time of vertical sync + back porch interval (s)
238 1.1 riastrad * default 550.0
239 1.1 riastrad */
240 1.1 riastrad int tmp1, tmp2;
241 1.1 riastrad #define CVT_MIN_VSYNC_BP 550
242 1.1 riastrad /* 3) Nominal HSync width (% of line period) - default 8 */
243 1.1 riastrad #define CVT_HSYNC_PERCENTAGE 8
244 1.1 riastrad unsigned int hblank_percentage;
245 1.10 riastrad int vsyncandback_porch, __maybe_unused vback_porch, hblank;
246 1.1 riastrad
247 1.1 riastrad /* estimated the horizontal period */
248 1.1 riastrad tmp1 = HV_FACTOR * 1000000 -
249 1.1 riastrad CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
250 1.1 riastrad tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
251 1.1 riastrad interlace;
252 1.1 riastrad hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
253 1.1 riastrad
254 1.1 riastrad tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
255 1.1 riastrad /* 9. Find number of lines in sync + backporch */
256 1.1 riastrad if (tmp1 < (vsync + CVT_MIN_V_PORCH))
257 1.1 riastrad vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
258 1.1 riastrad else
259 1.1 riastrad vsyncandback_porch = tmp1;
260 1.1 riastrad /* 10. Find number of lines in back porch */
261 1.1 riastrad vback_porch = vsyncandback_porch - vsync;
262 1.1 riastrad drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
263 1.1 riastrad vsyncandback_porch + CVT_MIN_V_PORCH;
264 1.1 riastrad /* 5) Definition of Horizontal blanking time limitation */
265 1.1 riastrad /* Gradient (%/kHz) - default 600 */
266 1.1 riastrad #define CVT_M_FACTOR 600
267 1.1 riastrad /* Offset (%) - default 40 */
268 1.1 riastrad #define CVT_C_FACTOR 40
269 1.1 riastrad /* Blanking time scaling factor - default 128 */
270 1.1 riastrad #define CVT_K_FACTOR 128
271 1.1 riastrad /* Scaling factor weighting - default 20 */
272 1.1 riastrad #define CVT_J_FACTOR 20
273 1.1 riastrad #define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
274 1.1 riastrad #define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
275 1.1 riastrad CVT_J_FACTOR)
276 1.1 riastrad /* 12. Find ideal blanking duty cycle from formula */
277 1.1 riastrad hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
278 1.1 riastrad hperiod / 1000;
279 1.1 riastrad /* 13. Blanking time */
280 1.1 riastrad if (hblank_percentage < 20 * HV_FACTOR)
281 1.1 riastrad hblank_percentage = 20 * HV_FACTOR;
282 1.1 riastrad hblank = drm_mode->hdisplay * hblank_percentage /
283 1.1 riastrad (100 * HV_FACTOR - hblank_percentage);
284 1.1 riastrad hblank -= hblank % (2 * CVT_H_GRANULARITY);
285 1.7 riastrad /* 14. find the total pixels per line */
286 1.1 riastrad drm_mode->htotal = drm_mode->hdisplay + hblank;
287 1.1 riastrad drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
288 1.1 riastrad drm_mode->hsync_start = drm_mode->hsync_end -
289 1.1 riastrad (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
290 1.1 riastrad drm_mode->hsync_start += CVT_H_GRANULARITY -
291 1.1 riastrad drm_mode->hsync_start % CVT_H_GRANULARITY;
292 1.1 riastrad /* fill the Vsync values */
293 1.1 riastrad drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
294 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + vsync;
295 1.1 riastrad } else {
296 1.1 riastrad /* Reduced blanking */
297 1.1 riastrad /* Minimum vertical blanking interval time (s)- default 460 */
298 1.1 riastrad #define CVT_RB_MIN_VBLANK 460
299 1.1 riastrad /* Fixed number of clocks for horizontal sync */
300 1.1 riastrad #define CVT_RB_H_SYNC 32
301 1.1 riastrad /* Fixed number of clocks for horizontal blanking */
302 1.1 riastrad #define CVT_RB_H_BLANK 160
303 1.1 riastrad /* Fixed number of lines for vertical front porch - default 3*/
304 1.1 riastrad #define CVT_RB_VFPORCH 3
305 1.1 riastrad int vbilines;
306 1.1 riastrad int tmp1, tmp2;
307 1.1 riastrad /* 8. Estimate Horizontal period. */
308 1.1 riastrad tmp1 = HV_FACTOR * 1000000 -
309 1.1 riastrad CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
310 1.1 riastrad tmp2 = vdisplay_rnd + 2 * vmargin;
311 1.1 riastrad hperiod = tmp1 / (tmp2 * vfieldrate);
312 1.1 riastrad /* 9. Find number of lines in vertical blanking */
313 1.1 riastrad vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
314 1.1 riastrad /* 10. Check if vertical blanking is sufficient */
315 1.1 riastrad if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
316 1.1 riastrad vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
317 1.1 riastrad /* 11. Find total number of lines in vertical field */
318 1.1 riastrad drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
319 1.1 riastrad /* 12. Find total number of pixels in a line */
320 1.1 riastrad drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
321 1.1 riastrad /* Fill in HSync values */
322 1.1 riastrad drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
323 1.1 riastrad drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
324 1.1 riastrad /* Fill in VSync values */
325 1.1 riastrad drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
326 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + vsync;
327 1.1 riastrad }
328 1.1 riastrad /* 15/13. Find pixel clock frequency (kHz for xf86) */
329 1.10 riastrad tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
330 1.10 riastrad tmp *= HV_FACTOR * 1000;
331 1.10 riastrad do_div(tmp, hperiod);
332 1.10 riastrad tmp -= drm_mode->clock % CVT_CLOCK_STEP;
333 1.10 riastrad drm_mode->clock = tmp;
334 1.1 riastrad /* 18/16. Find actual vertical frame frequency */
335 1.1 riastrad /* ignore - just set the mode flag for interlaced */
336 1.1 riastrad if (interlaced) {
337 1.1 riastrad drm_mode->vtotal *= 2;
338 1.1 riastrad drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
339 1.1 riastrad }
340 1.1 riastrad /* Fill the mode line name */
341 1.1 riastrad drm_mode_set_name(drm_mode);
342 1.1 riastrad if (reduced)
343 1.1 riastrad drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
344 1.1 riastrad DRM_MODE_FLAG_NVSYNC);
345 1.1 riastrad else
346 1.1 riastrad drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
347 1.1 riastrad DRM_MODE_FLAG_NHSYNC);
348 1.1 riastrad
349 1.1 riastrad return drm_mode;
350 1.1 riastrad }
351 1.1 riastrad EXPORT_SYMBOL(drm_cvt_mode);
352 1.1 riastrad
353 1.1 riastrad /**
354 1.4 riastrad * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
355 1.4 riastrad * @dev: drm device
356 1.4 riastrad * @hdisplay: hdisplay size
357 1.4 riastrad * @vdisplay: vdisplay size
358 1.4 riastrad * @vrefresh: vrefresh rate.
359 1.4 riastrad * @interlaced: whether to compute an interlaced mode
360 1.4 riastrad * @margins: desired margin (borders) size
361 1.4 riastrad * @GTF_M: extended GTF formula parameters
362 1.4 riastrad * @GTF_2C: extended GTF formula parameters
363 1.4 riastrad * @GTF_K: extended GTF formula parameters
364 1.4 riastrad * @GTF_2J: extended GTF formula parameters
365 1.1 riastrad *
366 1.1 riastrad * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
367 1.1 riastrad * in here multiplied by two. For a C of 40, pass in 80.
368 1.4 riastrad *
369 1.4 riastrad * Returns:
370 1.4 riastrad * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
371 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
372 1.4 riastrad * when no mode could be allocated.
373 1.1 riastrad */
374 1.1 riastrad struct drm_display_mode *
375 1.1 riastrad drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
376 1.1 riastrad int vrefresh, bool interlaced, int margins,
377 1.1 riastrad int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
378 1.1 riastrad { /* 1) top/bottom margin size (% of height) - default: 1.8, */
379 1.1 riastrad #define GTF_MARGIN_PERCENTAGE 18
380 1.1 riastrad /* 2) character cell horizontal granularity (pixels) - default 8 */
381 1.1 riastrad #define GTF_CELL_GRAN 8
382 1.1 riastrad /* 3) Minimum vertical porch (lines) - default 3 */
383 1.1 riastrad #define GTF_MIN_V_PORCH 1
384 1.1 riastrad /* width of vsync in lines */
385 1.1 riastrad #define V_SYNC_RQD 3
386 1.1 riastrad /* width of hsync as % of total line */
387 1.1 riastrad #define H_SYNC_PERCENT 8
388 1.1 riastrad /* min time of vsync + back porch (microsec) */
389 1.1 riastrad #define MIN_VSYNC_PLUS_BP 550
390 1.1 riastrad /* C' and M' are part of the Blanking Duty Cycle computation */
391 1.1 riastrad #define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
392 1.1 riastrad #define GTF_M_PRIME (GTF_K * GTF_M / 256)
393 1.1 riastrad struct drm_display_mode *drm_mode;
394 1.1 riastrad unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
395 1.1 riastrad int top_margin, bottom_margin;
396 1.1 riastrad int interlace;
397 1.1 riastrad unsigned int hfreq_est;
398 1.10 riastrad int vsync_plus_bp, __maybe_unused vback_porch;
399 1.10 riastrad unsigned int vtotal_lines, __maybe_unused vfieldrate_est;
400 1.10 riastrad unsigned int __maybe_unused hperiod;
401 1.10 riastrad unsigned int vfield_rate, __maybe_unused vframe_rate;
402 1.1 riastrad int left_margin, right_margin;
403 1.1 riastrad unsigned int total_active_pixels, ideal_duty_cycle;
404 1.1 riastrad unsigned int hblank, total_pixels, pixel_freq;
405 1.1 riastrad int hsync, hfront_porch, vodd_front_porch_lines;
406 1.1 riastrad unsigned int tmp1, tmp2;
407 1.1 riastrad
408 1.10 riastrad if (!hdisplay || !vdisplay)
409 1.10 riastrad return NULL;
410 1.10 riastrad
411 1.1 riastrad drm_mode = drm_mode_create(dev);
412 1.1 riastrad if (!drm_mode)
413 1.1 riastrad return NULL;
414 1.1 riastrad
415 1.1 riastrad /* 1. In order to give correct results, the number of horizontal
416 1.1 riastrad * pixels requested is first processed to ensure that it is divisible
417 1.1 riastrad * by the character size, by rounding it to the nearest character
418 1.1 riastrad * cell boundary:
419 1.1 riastrad */
420 1.1 riastrad hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
421 1.1 riastrad hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
422 1.1 riastrad
423 1.1 riastrad /* 2. If interlace is requested, the number of vertical lines assumed
424 1.1 riastrad * by the calculation must be halved, as the computation calculates
425 1.1 riastrad * the number of vertical lines per field.
426 1.1 riastrad */
427 1.1 riastrad if (interlaced)
428 1.1 riastrad vdisplay_rnd = vdisplay / 2;
429 1.1 riastrad else
430 1.1 riastrad vdisplay_rnd = vdisplay;
431 1.1 riastrad
432 1.1 riastrad /* 3. Find the frame rate required: */
433 1.1 riastrad if (interlaced)
434 1.1 riastrad vfieldrate_rqd = vrefresh * 2;
435 1.1 riastrad else
436 1.1 riastrad vfieldrate_rqd = vrefresh;
437 1.1 riastrad
438 1.1 riastrad /* 4. Find number of lines in Top margin: */
439 1.1 riastrad top_margin = 0;
440 1.1 riastrad if (margins)
441 1.1 riastrad top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
442 1.1 riastrad 1000;
443 1.1 riastrad /* 5. Find number of lines in bottom margin: */
444 1.1 riastrad bottom_margin = top_margin;
445 1.1 riastrad
446 1.1 riastrad /* 6. If interlace is required, then set variable interlace: */
447 1.1 riastrad if (interlaced)
448 1.1 riastrad interlace = 1;
449 1.1 riastrad else
450 1.1 riastrad interlace = 0;
451 1.1 riastrad
452 1.1 riastrad /* 7. Estimate the Horizontal frequency */
453 1.1 riastrad {
454 1.1 riastrad tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
455 1.1 riastrad tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
456 1.1 riastrad 2 + interlace;
457 1.1 riastrad hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
458 1.1 riastrad }
459 1.1 riastrad
460 1.1 riastrad /* 8. Find the number of lines in V sync + back porch */
461 1.1 riastrad /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
462 1.1 riastrad vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
463 1.1 riastrad vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
464 1.1 riastrad /* 9. Find the number of lines in V back porch alone: */
465 1.1 riastrad vback_porch = vsync_plus_bp - V_SYNC_RQD;
466 1.1 riastrad /* 10. Find the total number of lines in Vertical field period: */
467 1.1 riastrad vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
468 1.1 riastrad vsync_plus_bp + GTF_MIN_V_PORCH;
469 1.1 riastrad /* 11. Estimate the Vertical field frequency: */
470 1.1 riastrad vfieldrate_est = hfreq_est / vtotal_lines;
471 1.1 riastrad /* 12. Find the actual horizontal period: */
472 1.1 riastrad hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
473 1.1 riastrad
474 1.1 riastrad /* 13. Find the actual Vertical field frequency: */
475 1.1 riastrad vfield_rate = hfreq_est / vtotal_lines;
476 1.1 riastrad /* 14. Find the Vertical frame frequency: */
477 1.1 riastrad if (interlaced)
478 1.1 riastrad vframe_rate = vfield_rate / 2;
479 1.1 riastrad else
480 1.1 riastrad vframe_rate = vfield_rate;
481 1.1 riastrad /* 15. Find number of pixels in left margin: */
482 1.1 riastrad if (margins)
483 1.1 riastrad left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
484 1.1 riastrad 1000;
485 1.1 riastrad else
486 1.1 riastrad left_margin = 0;
487 1.1 riastrad
488 1.1 riastrad /* 16.Find number of pixels in right margin: */
489 1.1 riastrad right_margin = left_margin;
490 1.1 riastrad /* 17.Find total number of active pixels in image and left and right */
491 1.1 riastrad total_active_pixels = hdisplay_rnd + left_margin + right_margin;
492 1.1 riastrad /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
493 1.1 riastrad ideal_duty_cycle = GTF_C_PRIME * 1000 -
494 1.1 riastrad (GTF_M_PRIME * 1000000 / hfreq_est);
495 1.1 riastrad /* 19.Find the number of pixels in the blanking time to the nearest
496 1.1 riastrad * double character cell: */
497 1.1 riastrad hblank = total_active_pixels * ideal_duty_cycle /
498 1.1 riastrad (100000 - ideal_duty_cycle);
499 1.1 riastrad hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
500 1.1 riastrad hblank = hblank * 2 * GTF_CELL_GRAN;
501 1.1 riastrad /* 20.Find total number of pixels: */
502 1.1 riastrad total_pixels = total_active_pixels + hblank;
503 1.1 riastrad /* 21.Find pixel clock frequency: */
504 1.1 riastrad pixel_freq = total_pixels * hfreq_est / 1000;
505 1.1 riastrad /* Stage 1 computations are now complete; I should really pass
506 1.1 riastrad * the results to another function and do the Stage 2 computations,
507 1.1 riastrad * but I only need a few more values so I'll just append the
508 1.1 riastrad * computations here for now */
509 1.1 riastrad /* 17. Find the number of pixels in the horizontal sync period: */
510 1.1 riastrad hsync = H_SYNC_PERCENT * total_pixels / 100;
511 1.1 riastrad hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
512 1.1 riastrad hsync = hsync * GTF_CELL_GRAN;
513 1.1 riastrad /* 18. Find the number of pixels in horizontal front porch period */
514 1.1 riastrad hfront_porch = hblank / 2 - hsync;
515 1.1 riastrad /* 36. Find the number of lines in the odd front porch period: */
516 1.1 riastrad vodd_front_porch_lines = GTF_MIN_V_PORCH ;
517 1.1 riastrad
518 1.1 riastrad /* finally, pack the results in the mode struct */
519 1.1 riastrad drm_mode->hdisplay = hdisplay_rnd;
520 1.1 riastrad drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
521 1.1 riastrad drm_mode->hsync_end = drm_mode->hsync_start + hsync;
522 1.1 riastrad drm_mode->htotal = total_pixels;
523 1.1 riastrad drm_mode->vdisplay = vdisplay_rnd;
524 1.1 riastrad drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
525 1.1 riastrad drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
526 1.1 riastrad drm_mode->vtotal = vtotal_lines;
527 1.1 riastrad
528 1.1 riastrad drm_mode->clock = pixel_freq;
529 1.1 riastrad
530 1.1 riastrad if (interlaced) {
531 1.1 riastrad drm_mode->vtotal *= 2;
532 1.1 riastrad drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
533 1.1 riastrad }
534 1.1 riastrad
535 1.1 riastrad drm_mode_set_name(drm_mode);
536 1.1 riastrad if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
537 1.1 riastrad drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
538 1.1 riastrad else
539 1.1 riastrad drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
540 1.1 riastrad
541 1.1 riastrad return drm_mode;
542 1.1 riastrad }
543 1.1 riastrad EXPORT_SYMBOL(drm_gtf_mode_complex);
544 1.1 riastrad
545 1.1 riastrad /**
546 1.4 riastrad * drm_gtf_mode - create the modeline based on the GTF algorithm
547 1.4 riastrad * @dev: drm device
548 1.4 riastrad * @hdisplay: hdisplay size
549 1.4 riastrad * @vdisplay: vdisplay size
550 1.4 riastrad * @vrefresh: vrefresh rate.
551 1.4 riastrad * @interlaced: whether to compute an interlaced mode
552 1.4 riastrad * @margins: desired margin (borders) size
553 1.1 riastrad *
554 1.1 riastrad * return the modeline based on GTF algorithm
555 1.1 riastrad *
556 1.1 riastrad * This function is to create the modeline based on the GTF algorithm.
557 1.1 riastrad * Generalized Timing Formula is derived from:
558 1.10 riastrad *
559 1.1 riastrad * GTF Spreadsheet by Andy Morrish (1/5/97)
560 1.1 riastrad * available at http://www.vesa.org
561 1.1 riastrad *
562 1.1 riastrad * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
563 1.1 riastrad * What I have done is to translate it by using integer calculation.
564 1.1 riastrad * I also refer to the function of fb_get_mode in the file of
565 1.1 riastrad * drivers/video/fbmon.c
566 1.1 riastrad *
567 1.10 riastrad * Standard GTF parameters::
568 1.10 riastrad *
569 1.10 riastrad * M = 600
570 1.10 riastrad * C = 40
571 1.10 riastrad * K = 128
572 1.10 riastrad * J = 20
573 1.4 riastrad *
574 1.4 riastrad * Returns:
575 1.4 riastrad * The modeline based on the GTF algorithm stored in a drm_display_mode object.
576 1.4 riastrad * The display mode object is allocated with drm_mode_create(). Returns NULL
577 1.4 riastrad * when no mode could be allocated.
578 1.1 riastrad */
579 1.1 riastrad struct drm_display_mode *
580 1.1 riastrad drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
581 1.4 riastrad bool interlaced, int margins)
582 1.1 riastrad {
583 1.4 riastrad return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
584 1.4 riastrad interlaced, margins,
585 1.4 riastrad 600, 40 * 2, 128, 20 * 2);
586 1.1 riastrad }
587 1.1 riastrad EXPORT_SYMBOL(drm_gtf_mode);
588 1.1 riastrad
589 1.4 riastrad #ifdef CONFIG_VIDEOMODE_HELPERS
590 1.1 riastrad /**
591 1.4 riastrad * drm_display_mode_from_videomode - fill in @dmode using @vm,
592 1.4 riastrad * @vm: videomode structure to use as source
593 1.4 riastrad * @dmode: drm_display_mode structure to use as destination
594 1.4 riastrad *
595 1.4 riastrad * Fills out @dmode using the display mode specified in @vm.
596 1.4 riastrad */
597 1.4 riastrad void drm_display_mode_from_videomode(const struct videomode *vm,
598 1.4 riastrad struct drm_display_mode *dmode)
599 1.4 riastrad {
600 1.4 riastrad dmode->hdisplay = vm->hactive;
601 1.4 riastrad dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
602 1.4 riastrad dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
603 1.4 riastrad dmode->htotal = dmode->hsync_end + vm->hback_porch;
604 1.4 riastrad
605 1.4 riastrad dmode->vdisplay = vm->vactive;
606 1.4 riastrad dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
607 1.4 riastrad dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
608 1.4 riastrad dmode->vtotal = dmode->vsync_end + vm->vback_porch;
609 1.4 riastrad
610 1.4 riastrad dmode->clock = vm->pixelclock / 1000;
611 1.4 riastrad
612 1.4 riastrad dmode->flags = 0;
613 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
614 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_PHSYNC;
615 1.4 riastrad else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
616 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_NHSYNC;
617 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
618 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_PVSYNC;
619 1.4 riastrad else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
620 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_NVSYNC;
621 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_INTERLACED)
622 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_INTERLACE;
623 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
624 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
625 1.4 riastrad if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
626 1.4 riastrad dmode->flags |= DRM_MODE_FLAG_DBLCLK;
627 1.4 riastrad drm_mode_set_name(dmode);
628 1.1 riastrad }
629 1.4 riastrad EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
630 1.1 riastrad
631 1.7 riastrad /**
632 1.7 riastrad * drm_display_mode_to_videomode - fill in @vm using @dmode,
633 1.7 riastrad * @dmode: drm_display_mode structure to use as source
634 1.7 riastrad * @vm: videomode structure to use as destination
635 1.7 riastrad *
636 1.7 riastrad * Fills out @vm using the display mode specified in @dmode.
637 1.7 riastrad */
638 1.7 riastrad void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
639 1.7 riastrad struct videomode *vm)
640 1.7 riastrad {
641 1.7 riastrad vm->hactive = dmode->hdisplay;
642 1.7 riastrad vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
643 1.7 riastrad vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
644 1.7 riastrad vm->hback_porch = dmode->htotal - dmode->hsync_end;
645 1.7 riastrad
646 1.7 riastrad vm->vactive = dmode->vdisplay;
647 1.7 riastrad vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
648 1.7 riastrad vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
649 1.7 riastrad vm->vback_porch = dmode->vtotal - dmode->vsync_end;
650 1.7 riastrad
651 1.7 riastrad vm->pixelclock = dmode->clock * 1000;
652 1.7 riastrad
653 1.7 riastrad vm->flags = 0;
654 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
655 1.7 riastrad vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
656 1.7 riastrad else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
657 1.7 riastrad vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
658 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
659 1.7 riastrad vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
660 1.7 riastrad else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
661 1.7 riastrad vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
662 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
663 1.7 riastrad vm->flags |= DISPLAY_FLAGS_INTERLACED;
664 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
665 1.7 riastrad vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
666 1.7 riastrad if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
667 1.7 riastrad vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
668 1.7 riastrad }
669 1.7 riastrad EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
670 1.7 riastrad
671 1.10 riastrad /**
672 1.10 riastrad * drm_bus_flags_from_videomode - extract information about pixelclk and
673 1.10 riastrad * DE polarity from videomode and store it in a separate variable
674 1.10 riastrad * @vm: videomode structure to use
675 1.10 riastrad * @bus_flags: information about pixelclk, sync and DE polarity will be stored
676 1.10 riastrad * here
677 1.10 riastrad *
678 1.10 riastrad * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
679 1.10 riastrad * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
680 1.10 riastrad * found in @vm
681 1.10 riastrad */
682 1.10 riastrad void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
683 1.10 riastrad {
684 1.10 riastrad *bus_flags = 0;
685 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
686 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
687 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
688 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
689 1.10 riastrad
690 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
691 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
692 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
693 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
694 1.10 riastrad
695 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_DE_LOW)
696 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_DE_LOW;
697 1.10 riastrad if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
698 1.10 riastrad *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
699 1.10 riastrad }
700 1.10 riastrad EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
701 1.10 riastrad
702 1.4 riastrad #ifdef CONFIG_OF
703 1.1 riastrad /**
704 1.4 riastrad * of_get_drm_display_mode - get a drm_display_mode from devicetree
705 1.4 riastrad * @np: device_node with the timing specification
706 1.4 riastrad * @dmode: will be set to the return value
707 1.10 riastrad * @bus_flags: information about pixelclk, sync and DE polarity
708 1.4 riastrad * @index: index into the list of display timings in devicetree
709 1.1 riastrad *
710 1.4 riastrad * This function is expensive and should only be used, if only one mode is to be
711 1.4 riastrad * read from DT. To get multiple modes start with of_get_display_timings and
712 1.4 riastrad * work with that instead.
713 1.1 riastrad *
714 1.4 riastrad * Returns:
715 1.4 riastrad * 0 on success, a negative errno code when no of videomode node was found.
716 1.1 riastrad */
717 1.4 riastrad int of_get_drm_display_mode(struct device_node *np,
718 1.10 riastrad struct drm_display_mode *dmode, u32 *bus_flags,
719 1.10 riastrad int index)
720 1.1 riastrad {
721 1.4 riastrad struct videomode vm;
722 1.4 riastrad int ret;
723 1.1 riastrad
724 1.4 riastrad ret = of_get_videomode(np, &vm, index);
725 1.4 riastrad if (ret)
726 1.4 riastrad return ret;
727 1.1 riastrad
728 1.4 riastrad drm_display_mode_from_videomode(&vm, dmode);
729 1.10 riastrad if (bus_flags)
730 1.10 riastrad drm_bus_flags_from_videomode(&vm, bus_flags);
731 1.1 riastrad
732 1.10 riastrad pr_debug("%pOF: got %dx%d display mode\n",
733 1.10 riastrad np, vm.hactive, vm.vactive);
734 1.4 riastrad drm_mode_debug_printmodeline(dmode);
735 1.1 riastrad
736 1.4 riastrad return 0;
737 1.1 riastrad }
738 1.4 riastrad EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
739 1.4 riastrad #endif /* CONFIG_OF */
740 1.4 riastrad #endif /* CONFIG_VIDEOMODE_HELPERS */
741 1.1 riastrad
742 1.1 riastrad /**
743 1.4 riastrad * drm_mode_set_name - set the name on a mode
744 1.4 riastrad * @mode: name will be set in this mode
745 1.1 riastrad *
746 1.4 riastrad * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
747 1.4 riastrad * with an optional 'i' suffix for interlaced modes.
748 1.1 riastrad */
749 1.4 riastrad void drm_mode_set_name(struct drm_display_mode *mode)
750 1.1 riastrad {
751 1.4 riastrad bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
752 1.4 riastrad
753 1.4 riastrad snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
754 1.4 riastrad mode->hdisplay, mode->vdisplay,
755 1.4 riastrad interlaced ? "i" : "");
756 1.1 riastrad }
757 1.4 riastrad EXPORT_SYMBOL(drm_mode_set_name);
758 1.1 riastrad
759 1.10 riastrad /**
760 1.10 riastrad * drm_mode_hsync - get the hsync of a mode
761 1.1 riastrad * @mode: mode
762 1.1 riastrad *
763 1.4 riastrad * Returns:
764 1.4 riastrad * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
765 1.4 riastrad * value first if it is not yet set.
766 1.1 riastrad */
767 1.1 riastrad int drm_mode_hsync(const struct drm_display_mode *mode)
768 1.1 riastrad {
769 1.1 riastrad unsigned int calc_val;
770 1.1 riastrad
771 1.1 riastrad if (mode->hsync)
772 1.1 riastrad return mode->hsync;
773 1.1 riastrad
774 1.10 riastrad if (mode->htotal <= 0)
775 1.1 riastrad return 0;
776 1.1 riastrad
777 1.1 riastrad calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
778 1.1 riastrad calc_val += 500; /* round to 1000Hz */
779 1.1 riastrad calc_val /= 1000; /* truncate to kHz */
780 1.1 riastrad
781 1.1 riastrad return calc_val;
782 1.1 riastrad }
783 1.1 riastrad EXPORT_SYMBOL(drm_mode_hsync);
784 1.1 riastrad
785 1.1 riastrad /**
786 1.1 riastrad * drm_mode_vrefresh - get the vrefresh of a mode
787 1.1 riastrad * @mode: mode
788 1.1 riastrad *
789 1.4 riastrad * Returns:
790 1.4 riastrad * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
791 1.4 riastrad * value first if it is not yet set.
792 1.1 riastrad */
793 1.1 riastrad int drm_mode_vrefresh(const struct drm_display_mode *mode)
794 1.1 riastrad {
795 1.1 riastrad int refresh = 0;
796 1.1 riastrad
797 1.1 riastrad if (mode->vrefresh > 0)
798 1.1 riastrad refresh = mode->vrefresh;
799 1.1 riastrad else if (mode->htotal > 0 && mode->vtotal > 0) {
800 1.10 riastrad unsigned int num, den;
801 1.10 riastrad
802 1.10 riastrad num = mode->clock * 1000;
803 1.10 riastrad den = mode->htotal * mode->vtotal;
804 1.1 riastrad
805 1.1 riastrad if (mode->flags & DRM_MODE_FLAG_INTERLACE)
806 1.10 riastrad num *= 2;
807 1.1 riastrad if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
808 1.10 riastrad den *= 2;
809 1.1 riastrad if (mode->vscan > 1)
810 1.10 riastrad den *= mode->vscan;
811 1.10 riastrad
812 1.10 riastrad refresh = DIV_ROUND_CLOSEST(num, den);
813 1.1 riastrad }
814 1.1 riastrad return refresh;
815 1.1 riastrad }
816 1.1 riastrad EXPORT_SYMBOL(drm_mode_vrefresh);
817 1.1 riastrad
818 1.1 riastrad /**
819 1.10 riastrad * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
820 1.10 riastrad * @mode: mode to query
821 1.10 riastrad * @hdisplay: hdisplay value to fill in
822 1.10 riastrad * @vdisplay: vdisplay value to fill in
823 1.10 riastrad *
824 1.10 riastrad * The vdisplay value will be doubled if the specified mode is a stereo mode of
825 1.10 riastrad * the appropriate layout.
826 1.10 riastrad */
827 1.10 riastrad void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
828 1.10 riastrad int *hdisplay, int *vdisplay)
829 1.10 riastrad {
830 1.10 riastrad struct drm_display_mode adjusted = *mode;
831 1.10 riastrad
832 1.10 riastrad drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
833 1.10 riastrad *hdisplay = adjusted.crtc_hdisplay;
834 1.10 riastrad *vdisplay = adjusted.crtc_vdisplay;
835 1.10 riastrad }
836 1.10 riastrad EXPORT_SYMBOL(drm_mode_get_hv_timing);
837 1.10 riastrad
838 1.10 riastrad /**
839 1.4 riastrad * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
840 1.1 riastrad * @p: mode
841 1.4 riastrad * @adjust_flags: a combination of adjustment flags
842 1.1 riastrad *
843 1.4 riastrad * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
844 1.1 riastrad *
845 1.4 riastrad * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
846 1.4 riastrad * interlaced modes.
847 1.4 riastrad * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
848 1.4 riastrad * buffers containing two eyes (only adjust the timings when needed, eg. for
849 1.4 riastrad * "frame packing" or "side by side full").
850 1.7 riastrad * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
851 1.7 riastrad * be performed for doublescan and vscan > 1 modes respectively.
852 1.1 riastrad */
853 1.1 riastrad void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
854 1.1 riastrad {
855 1.10 riastrad if (!p)
856 1.1 riastrad return;
857 1.1 riastrad
858 1.4 riastrad p->crtc_clock = p->clock;
859 1.1 riastrad p->crtc_hdisplay = p->hdisplay;
860 1.1 riastrad p->crtc_hsync_start = p->hsync_start;
861 1.1 riastrad p->crtc_hsync_end = p->hsync_end;
862 1.1 riastrad p->crtc_htotal = p->htotal;
863 1.1 riastrad p->crtc_hskew = p->hskew;
864 1.1 riastrad p->crtc_vdisplay = p->vdisplay;
865 1.1 riastrad p->crtc_vsync_start = p->vsync_start;
866 1.1 riastrad p->crtc_vsync_end = p->vsync_end;
867 1.1 riastrad p->crtc_vtotal = p->vtotal;
868 1.1 riastrad
869 1.1 riastrad if (p->flags & DRM_MODE_FLAG_INTERLACE) {
870 1.1 riastrad if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
871 1.1 riastrad p->crtc_vdisplay /= 2;
872 1.1 riastrad p->crtc_vsync_start /= 2;
873 1.1 riastrad p->crtc_vsync_end /= 2;
874 1.1 riastrad p->crtc_vtotal /= 2;
875 1.1 riastrad }
876 1.1 riastrad }
877 1.1 riastrad
878 1.7 riastrad if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
879 1.7 riastrad if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
880 1.7 riastrad p->crtc_vdisplay *= 2;
881 1.7 riastrad p->crtc_vsync_start *= 2;
882 1.7 riastrad p->crtc_vsync_end *= 2;
883 1.7 riastrad p->crtc_vtotal *= 2;
884 1.7 riastrad }
885 1.1 riastrad }
886 1.1 riastrad
887 1.7 riastrad if (!(adjust_flags & CRTC_NO_VSCAN)) {
888 1.7 riastrad if (p->vscan > 1) {
889 1.7 riastrad p->crtc_vdisplay *= p->vscan;
890 1.7 riastrad p->crtc_vsync_start *= p->vscan;
891 1.7 riastrad p->crtc_vsync_end *= p->vscan;
892 1.7 riastrad p->crtc_vtotal *= p->vscan;
893 1.7 riastrad }
894 1.1 riastrad }
895 1.1 riastrad
896 1.4 riastrad if (adjust_flags & CRTC_STEREO_DOUBLE) {
897 1.4 riastrad unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
898 1.4 riastrad
899 1.4 riastrad switch (layout) {
900 1.4 riastrad case DRM_MODE_FLAG_3D_FRAME_PACKING:
901 1.4 riastrad p->crtc_clock *= 2;
902 1.4 riastrad p->crtc_vdisplay += p->crtc_vtotal;
903 1.4 riastrad p->crtc_vsync_start += p->crtc_vtotal;
904 1.4 riastrad p->crtc_vsync_end += p->crtc_vtotal;
905 1.4 riastrad p->crtc_vtotal += p->crtc_vtotal;
906 1.4 riastrad break;
907 1.4 riastrad }
908 1.4 riastrad }
909 1.4 riastrad
910 1.1 riastrad p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
911 1.1 riastrad p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
912 1.1 riastrad p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
913 1.1 riastrad p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
914 1.1 riastrad }
915 1.1 riastrad EXPORT_SYMBOL(drm_mode_set_crtcinfo);
916 1.1 riastrad
917 1.1 riastrad /**
918 1.1 riastrad * drm_mode_copy - copy the mode
919 1.1 riastrad * @dst: mode to overwrite
920 1.1 riastrad * @src: mode to copy
921 1.1 riastrad *
922 1.4 riastrad * Copy an existing mode into another mode, preserving the object id and
923 1.4 riastrad * list head of the destination mode.
924 1.1 riastrad */
925 1.1 riastrad void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
926 1.1 riastrad {
927 1.4 riastrad struct list_head head = dst->head;
928 1.1 riastrad
929 1.1 riastrad *dst = *src;
930 1.4 riastrad dst->head = head;
931 1.1 riastrad }
932 1.1 riastrad EXPORT_SYMBOL(drm_mode_copy);
933 1.1 riastrad
934 1.1 riastrad /**
935 1.1 riastrad * drm_mode_duplicate - allocate and duplicate an existing mode
936 1.4 riastrad * @dev: drm_device to allocate the duplicated mode for
937 1.4 riastrad * @mode: mode to duplicate
938 1.1 riastrad *
939 1.1 riastrad * Just allocate a new mode, copy the existing mode into it, and return
940 1.1 riastrad * a pointer to it. Used to create new instances of established modes.
941 1.4 riastrad *
942 1.4 riastrad * Returns:
943 1.4 riastrad * Pointer to duplicated mode on success, NULL on error.
944 1.1 riastrad */
945 1.1 riastrad struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
946 1.1 riastrad const struct drm_display_mode *mode)
947 1.1 riastrad {
948 1.1 riastrad struct drm_display_mode *nmode;
949 1.1 riastrad
950 1.1 riastrad nmode = drm_mode_create(dev);
951 1.1 riastrad if (!nmode)
952 1.1 riastrad return NULL;
953 1.1 riastrad
954 1.1 riastrad drm_mode_copy(nmode, mode);
955 1.1 riastrad
956 1.1 riastrad return nmode;
957 1.1 riastrad }
958 1.1 riastrad EXPORT_SYMBOL(drm_mode_duplicate);
959 1.1 riastrad
960 1.10 riastrad static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
961 1.10 riastrad const struct drm_display_mode *mode2)
962 1.10 riastrad {
963 1.10 riastrad return mode1->hdisplay == mode2->hdisplay &&
964 1.10 riastrad mode1->hsync_start == mode2->hsync_start &&
965 1.10 riastrad mode1->hsync_end == mode2->hsync_end &&
966 1.10 riastrad mode1->htotal == mode2->htotal &&
967 1.10 riastrad mode1->hskew == mode2->hskew &&
968 1.10 riastrad mode1->vdisplay == mode2->vdisplay &&
969 1.10 riastrad mode1->vsync_start == mode2->vsync_start &&
970 1.10 riastrad mode1->vsync_end == mode2->vsync_end &&
971 1.10 riastrad mode1->vtotal == mode2->vtotal &&
972 1.10 riastrad mode1->vscan == mode2->vscan;
973 1.10 riastrad }
974 1.10 riastrad
975 1.10 riastrad static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
976 1.10 riastrad const struct drm_display_mode *mode2)
977 1.10 riastrad {
978 1.10 riastrad /*
979 1.10 riastrad * do clock check convert to PICOS
980 1.10 riastrad * so fb modes get matched the same
981 1.10 riastrad */
982 1.10 riastrad if (mode1->clock && mode2->clock)
983 1.10 riastrad return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
984 1.10 riastrad else
985 1.10 riastrad return mode1->clock == mode2->clock;
986 1.10 riastrad }
987 1.10 riastrad
988 1.10 riastrad static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
989 1.10 riastrad const struct drm_display_mode *mode2)
990 1.10 riastrad {
991 1.10 riastrad return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
992 1.10 riastrad (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
993 1.10 riastrad }
994 1.10 riastrad
995 1.10 riastrad static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
996 1.10 riastrad const struct drm_display_mode *mode2)
997 1.10 riastrad {
998 1.10 riastrad return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
999 1.10 riastrad (mode2->flags & DRM_MODE_FLAG_3D_MASK);
1000 1.10 riastrad }
1001 1.10 riastrad
1002 1.10 riastrad static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
1003 1.10 riastrad const struct drm_display_mode *mode2)
1004 1.10 riastrad {
1005 1.10 riastrad return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
1006 1.10 riastrad }
1007 1.10 riastrad
1008 1.1 riastrad /**
1009 1.10 riastrad * drm_mode_match - test modes for (partial) equality
1010 1.1 riastrad * @mode1: first mode
1011 1.1 riastrad * @mode2: second mode
1012 1.10 riastrad * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
1013 1.1 riastrad *
1014 1.1 riastrad * Check to see if @mode1 and @mode2 are equivalent.
1015 1.1 riastrad *
1016 1.4 riastrad * Returns:
1017 1.10 riastrad * True if the modes are (partially) equal, false otherwise.
1018 1.1 riastrad */
1019 1.10 riastrad bool drm_mode_match(const struct drm_display_mode *mode1,
1020 1.10 riastrad const struct drm_display_mode *mode2,
1021 1.10 riastrad unsigned int match_flags)
1022 1.1 riastrad {
1023 1.7 riastrad if (!mode1 && !mode2)
1024 1.7 riastrad return true;
1025 1.7 riastrad
1026 1.7 riastrad if (!mode1 || !mode2)
1027 1.7 riastrad return false;
1028 1.7 riastrad
1029 1.10 riastrad if (match_flags & DRM_MODE_MATCH_TIMINGS &&
1030 1.10 riastrad !drm_mode_match_timings(mode1, mode2))
1031 1.10 riastrad return false;
1032 1.10 riastrad
1033 1.10 riastrad if (match_flags & DRM_MODE_MATCH_CLOCK &&
1034 1.10 riastrad !drm_mode_match_clock(mode1, mode2))
1035 1.10 riastrad return false;
1036 1.10 riastrad
1037 1.10 riastrad if (match_flags & DRM_MODE_MATCH_FLAGS &&
1038 1.10 riastrad !drm_mode_match_flags(mode1, mode2))
1039 1.10 riastrad return false;
1040 1.10 riastrad
1041 1.10 riastrad if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
1042 1.10 riastrad !drm_mode_match_3d_flags(mode1, mode2))
1043 1.1 riastrad return false;
1044 1.1 riastrad
1045 1.10 riastrad if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
1046 1.10 riastrad !drm_mode_match_aspect_ratio(mode1, mode2))
1047 1.4 riastrad return false;
1048 1.4 riastrad
1049 1.10 riastrad return true;
1050 1.10 riastrad }
1051 1.10 riastrad EXPORT_SYMBOL(drm_mode_match);
1052 1.10 riastrad
1053 1.10 riastrad /**
1054 1.10 riastrad * drm_mode_equal - test modes for equality
1055 1.10 riastrad * @mode1: first mode
1056 1.10 riastrad * @mode2: second mode
1057 1.10 riastrad *
1058 1.10 riastrad * Check to see if @mode1 and @mode2 are equivalent.
1059 1.10 riastrad *
1060 1.10 riastrad * Returns:
1061 1.10 riastrad * True if the modes are equal, false otherwise.
1062 1.10 riastrad */
1063 1.10 riastrad bool drm_mode_equal(const struct drm_display_mode *mode1,
1064 1.10 riastrad const struct drm_display_mode *mode2)
1065 1.10 riastrad {
1066 1.10 riastrad return drm_mode_match(mode1, mode2,
1067 1.10 riastrad DRM_MODE_MATCH_TIMINGS |
1068 1.10 riastrad DRM_MODE_MATCH_CLOCK |
1069 1.10 riastrad DRM_MODE_MATCH_FLAGS |
1070 1.10 riastrad DRM_MODE_MATCH_3D_FLAGS|
1071 1.10 riastrad DRM_MODE_MATCH_ASPECT_RATIO);
1072 1.4 riastrad }
1073 1.4 riastrad EXPORT_SYMBOL(drm_mode_equal);
1074 1.4 riastrad
1075 1.4 riastrad /**
1076 1.10 riastrad * drm_mode_equal_no_clocks - test modes for equality
1077 1.10 riastrad * @mode1: first mode
1078 1.10 riastrad * @mode2: second mode
1079 1.10 riastrad *
1080 1.10 riastrad * Check to see if @mode1 and @mode2 are equivalent, but
1081 1.10 riastrad * don't check the pixel clocks.
1082 1.10 riastrad *
1083 1.10 riastrad * Returns:
1084 1.10 riastrad * True if the modes are equal, false otherwise.
1085 1.10 riastrad */
1086 1.10 riastrad bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
1087 1.10 riastrad const struct drm_display_mode *mode2)
1088 1.10 riastrad {
1089 1.10 riastrad return drm_mode_match(mode1, mode2,
1090 1.10 riastrad DRM_MODE_MATCH_TIMINGS |
1091 1.10 riastrad DRM_MODE_MATCH_FLAGS |
1092 1.10 riastrad DRM_MODE_MATCH_3D_FLAGS);
1093 1.10 riastrad }
1094 1.10 riastrad EXPORT_SYMBOL(drm_mode_equal_no_clocks);
1095 1.10 riastrad
1096 1.10 riastrad /**
1097 1.4 riastrad * drm_mode_equal_no_clocks_no_stereo - test modes for equality
1098 1.4 riastrad * @mode1: first mode
1099 1.4 riastrad * @mode2: second mode
1100 1.4 riastrad *
1101 1.4 riastrad * Check to see if @mode1 and @mode2 are equivalent, but
1102 1.4 riastrad * don't check the pixel clocks nor the stereo layout.
1103 1.4 riastrad *
1104 1.4 riastrad * Returns:
1105 1.4 riastrad * True if the modes are equal, false otherwise.
1106 1.4 riastrad */
1107 1.4 riastrad bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
1108 1.4 riastrad const struct drm_display_mode *mode2)
1109 1.4 riastrad {
1110 1.10 riastrad return drm_mode_match(mode1, mode2,
1111 1.10 riastrad DRM_MODE_MATCH_TIMINGS |
1112 1.10 riastrad DRM_MODE_MATCH_FLAGS);
1113 1.1 riastrad }
1114 1.4 riastrad EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
1115 1.1 riastrad
1116 1.10 riastrad static enum drm_mode_status
1117 1.7 riastrad drm_mode_validate_basic(const struct drm_display_mode *mode)
1118 1.7 riastrad {
1119 1.10 riastrad if (mode->type & ~DRM_MODE_TYPE_ALL)
1120 1.10 riastrad return MODE_BAD;
1121 1.10 riastrad
1122 1.10 riastrad if (mode->flags & ~DRM_MODE_FLAG_ALL)
1123 1.10 riastrad return MODE_BAD;
1124 1.10 riastrad
1125 1.10 riastrad if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1126 1.10 riastrad return MODE_BAD;
1127 1.10 riastrad
1128 1.7 riastrad if (mode->clock == 0)
1129 1.7 riastrad return MODE_CLOCK_LOW;
1130 1.7 riastrad
1131 1.7 riastrad if (mode->hdisplay == 0 ||
1132 1.7 riastrad mode->hsync_start < mode->hdisplay ||
1133 1.7 riastrad mode->hsync_end < mode->hsync_start ||
1134 1.7 riastrad mode->htotal < mode->hsync_end)
1135 1.7 riastrad return MODE_H_ILLEGAL;
1136 1.7 riastrad
1137 1.7 riastrad if (mode->vdisplay == 0 ||
1138 1.7 riastrad mode->vsync_start < mode->vdisplay ||
1139 1.7 riastrad mode->vsync_end < mode->vsync_start ||
1140 1.7 riastrad mode->vtotal < mode->vsync_end)
1141 1.7 riastrad return MODE_V_ILLEGAL;
1142 1.7 riastrad
1143 1.7 riastrad return MODE_OK;
1144 1.7 riastrad }
1145 1.10 riastrad
1146 1.10 riastrad /**
1147 1.10 riastrad * drm_mode_validate_driver - make sure the mode is somewhat sane
1148 1.10 riastrad * @dev: drm device
1149 1.10 riastrad * @mode: mode to check
1150 1.10 riastrad *
1151 1.10 riastrad * First do basic validation on the mode, and then allow the driver
1152 1.10 riastrad * to check for device/driver specific limitations via the optional
1153 1.10 riastrad * &drm_mode_config_helper_funcs.mode_valid hook.
1154 1.10 riastrad *
1155 1.10 riastrad * Returns:
1156 1.10 riastrad * The mode status
1157 1.10 riastrad */
1158 1.10 riastrad enum drm_mode_status
1159 1.10 riastrad drm_mode_validate_driver(struct drm_device *dev,
1160 1.10 riastrad const struct drm_display_mode *mode)
1161 1.10 riastrad {
1162 1.10 riastrad enum drm_mode_status status;
1163 1.10 riastrad
1164 1.10 riastrad status = drm_mode_validate_basic(mode);
1165 1.10 riastrad if (status != MODE_OK)
1166 1.10 riastrad return status;
1167 1.10 riastrad
1168 1.10 riastrad if (dev->mode_config.funcs->mode_valid)
1169 1.10 riastrad return dev->mode_config.funcs->mode_valid(dev, mode);
1170 1.10 riastrad else
1171 1.10 riastrad return MODE_OK;
1172 1.10 riastrad }
1173 1.10 riastrad EXPORT_SYMBOL(drm_mode_validate_driver);
1174 1.7 riastrad
1175 1.7 riastrad /**
1176 1.1 riastrad * drm_mode_validate_size - make sure modes adhere to size constraints
1177 1.7 riastrad * @mode: mode to check
1178 1.1 riastrad * @maxX: maximum width
1179 1.1 riastrad * @maxY: maximum height
1180 1.1 riastrad *
1181 1.4 riastrad * This function is a helper which can be used to validate modes against size
1182 1.4 riastrad * limitations of the DRM device/connector. If a mode is too big its status
1183 1.7 riastrad * member is updated with the appropriate validation failure code. The list
1184 1.4 riastrad * itself is not changed.
1185 1.7 riastrad *
1186 1.7 riastrad * Returns:
1187 1.7 riastrad * The mode status
1188 1.1 riastrad */
1189 1.7 riastrad enum drm_mode_status
1190 1.7 riastrad drm_mode_validate_size(const struct drm_display_mode *mode,
1191 1.7 riastrad int maxX, int maxY)
1192 1.1 riastrad {
1193 1.7 riastrad if (maxX > 0 && mode->hdisplay > maxX)
1194 1.7 riastrad return MODE_VIRTUAL_X;
1195 1.1 riastrad
1196 1.9 wiz #if defined(DRM_MAX_RESOLUTION_HORIZONTAL)
1197 1.9 wiz if (mode->hdisplay > DRM_MAX_RESOLUTION_HORIZONTAL)
1198 1.9 wiz return MODE_VIRTUAL_X;
1199 1.9 wiz #endif
1200 1.9 wiz
1201 1.7 riastrad if (maxY > 0 && mode->vdisplay > maxY)
1202 1.7 riastrad return MODE_VIRTUAL_Y;
1203 1.1 riastrad
1204 1.9 wiz #if defined(DRM_MAX_RESOLUTION_VERTICAL)
1205 1.9 wiz if (mode->vdisplay > DRM_MAX_RESOLUTION_VERTICAL)
1206 1.9 wiz return MODE_VIRTUAL_Y;
1207 1.9 wiz #endif
1208 1.9 wiz
1209 1.7 riastrad return MODE_OK;
1210 1.1 riastrad }
1211 1.1 riastrad EXPORT_SYMBOL(drm_mode_validate_size);
1212 1.1 riastrad
1213 1.10 riastrad /**
1214 1.10 riastrad * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1215 1.10 riastrad * @mode: mode to check
1216 1.10 riastrad * @connector: drm connector under action
1217 1.10 riastrad *
1218 1.10 riastrad * This function is a helper which can be used to filter out any YCBCR420
1219 1.10 riastrad * only mode, when the source doesn't support it.
1220 1.10 riastrad *
1221 1.10 riastrad * Returns:
1222 1.10 riastrad * The mode status
1223 1.10 riastrad */
1224 1.10 riastrad enum drm_mode_status
1225 1.10 riastrad drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1226 1.10 riastrad struct drm_connector *connector)
1227 1.10 riastrad {
1228 1.10 riastrad u8 vic = drm_match_cea_mode(mode);
1229 1.10 riastrad enum drm_mode_status status = MODE_OK;
1230 1.10 riastrad struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
1231 1.10 riastrad
1232 1.10 riastrad if (test_bit(vic, hdmi->y420_vdb_modes)) {
1233 1.10 riastrad if (!connector->ycbcr_420_allowed)
1234 1.10 riastrad status = MODE_NO_420;
1235 1.10 riastrad }
1236 1.10 riastrad
1237 1.10 riastrad return status;
1238 1.10 riastrad }
1239 1.10 riastrad EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1240 1.10 riastrad
1241 1.7 riastrad #define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1242 1.7 riastrad
1243 1.7 riastrad static const char * const drm_mode_status_names[] = {
1244 1.7 riastrad MODE_STATUS(OK),
1245 1.7 riastrad MODE_STATUS(HSYNC),
1246 1.7 riastrad MODE_STATUS(VSYNC),
1247 1.7 riastrad MODE_STATUS(H_ILLEGAL),
1248 1.7 riastrad MODE_STATUS(V_ILLEGAL),
1249 1.7 riastrad MODE_STATUS(BAD_WIDTH),
1250 1.7 riastrad MODE_STATUS(NOMODE),
1251 1.7 riastrad MODE_STATUS(NO_INTERLACE),
1252 1.7 riastrad MODE_STATUS(NO_DBLESCAN),
1253 1.7 riastrad MODE_STATUS(NO_VSCAN),
1254 1.7 riastrad MODE_STATUS(MEM),
1255 1.7 riastrad MODE_STATUS(VIRTUAL_X),
1256 1.7 riastrad MODE_STATUS(VIRTUAL_Y),
1257 1.7 riastrad MODE_STATUS(MEM_VIRT),
1258 1.7 riastrad MODE_STATUS(NOCLOCK),
1259 1.7 riastrad MODE_STATUS(CLOCK_HIGH),
1260 1.7 riastrad MODE_STATUS(CLOCK_LOW),
1261 1.7 riastrad MODE_STATUS(CLOCK_RANGE),
1262 1.7 riastrad MODE_STATUS(BAD_HVALUE),
1263 1.7 riastrad MODE_STATUS(BAD_VVALUE),
1264 1.7 riastrad MODE_STATUS(BAD_VSCAN),
1265 1.7 riastrad MODE_STATUS(HSYNC_NARROW),
1266 1.7 riastrad MODE_STATUS(HSYNC_WIDE),
1267 1.7 riastrad MODE_STATUS(HBLANK_NARROW),
1268 1.7 riastrad MODE_STATUS(HBLANK_WIDE),
1269 1.7 riastrad MODE_STATUS(VSYNC_NARROW),
1270 1.7 riastrad MODE_STATUS(VSYNC_WIDE),
1271 1.7 riastrad MODE_STATUS(VBLANK_NARROW),
1272 1.7 riastrad MODE_STATUS(VBLANK_WIDE),
1273 1.7 riastrad MODE_STATUS(PANEL),
1274 1.7 riastrad MODE_STATUS(INTERLACE_WIDTH),
1275 1.7 riastrad MODE_STATUS(ONE_WIDTH),
1276 1.7 riastrad MODE_STATUS(ONE_HEIGHT),
1277 1.7 riastrad MODE_STATUS(ONE_SIZE),
1278 1.7 riastrad MODE_STATUS(NO_REDUCED),
1279 1.7 riastrad MODE_STATUS(NO_STEREO),
1280 1.10 riastrad MODE_STATUS(NO_420),
1281 1.10 riastrad MODE_STATUS(STALE),
1282 1.7 riastrad MODE_STATUS(BAD),
1283 1.7 riastrad MODE_STATUS(ERROR),
1284 1.7 riastrad };
1285 1.7 riastrad
1286 1.7 riastrad #undef MODE_STATUS
1287 1.7 riastrad
1288 1.10 riastrad const char *drm_get_mode_status_name(enum drm_mode_status status)
1289 1.7 riastrad {
1290 1.7 riastrad int index = status + 3;
1291 1.7 riastrad
1292 1.7 riastrad if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1293 1.7 riastrad return "";
1294 1.7 riastrad
1295 1.7 riastrad return drm_mode_status_names[index];
1296 1.7 riastrad }
1297 1.7 riastrad
1298 1.1 riastrad /**
1299 1.1 riastrad * drm_mode_prune_invalid - remove invalid modes from mode list
1300 1.1 riastrad * @dev: DRM device
1301 1.1 riastrad * @mode_list: list of modes to check
1302 1.1 riastrad * @verbose: be verbose about it
1303 1.1 riastrad *
1304 1.4 riastrad * This helper function can be used to prune a display mode list after
1305 1.10 riastrad * validation has been completed. All modes whose status is not MODE_OK will be
1306 1.4 riastrad * removed from the list, and if @verbose the status code and mode name is also
1307 1.4 riastrad * printed to dmesg.
1308 1.1 riastrad */
1309 1.1 riastrad void drm_mode_prune_invalid(struct drm_device *dev,
1310 1.1 riastrad struct list_head *mode_list, bool verbose)
1311 1.1 riastrad {
1312 1.1 riastrad struct drm_display_mode *mode, *t;
1313 1.1 riastrad
1314 1.1 riastrad list_for_each_entry_safe(mode, t, mode_list, head) {
1315 1.1 riastrad if (mode->status != MODE_OK) {
1316 1.1 riastrad list_del(&mode->head);
1317 1.1 riastrad if (verbose) {
1318 1.1 riastrad drm_mode_debug_printmodeline(mode);
1319 1.7 riastrad DRM_DEBUG_KMS("Not using %s mode: %s\n",
1320 1.7 riastrad mode->name,
1321 1.7 riastrad drm_get_mode_status_name(mode->status));
1322 1.1 riastrad }
1323 1.1 riastrad drm_mode_destroy(dev, mode);
1324 1.1 riastrad }
1325 1.1 riastrad }
1326 1.1 riastrad }
1327 1.1 riastrad EXPORT_SYMBOL(drm_mode_prune_invalid);
1328 1.1 riastrad
1329 1.1 riastrad /**
1330 1.1 riastrad * drm_mode_compare - compare modes for favorability
1331 1.1 riastrad * @priv: unused
1332 1.1 riastrad * @lh_a: list_head for first mode
1333 1.1 riastrad * @lh_b: list_head for second mode
1334 1.1 riastrad *
1335 1.1 riastrad * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1336 1.1 riastrad * which is better.
1337 1.1 riastrad *
1338 1.4 riastrad * Returns:
1339 1.1 riastrad * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1340 1.1 riastrad * positive if @lh_b is better than @lh_a.
1341 1.1 riastrad */
1342 1.1 riastrad static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
1343 1.1 riastrad {
1344 1.1 riastrad struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1345 1.1 riastrad struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1346 1.1 riastrad int diff;
1347 1.1 riastrad
1348 1.1 riastrad diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1349 1.1 riastrad ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1350 1.1 riastrad if (diff)
1351 1.1 riastrad return diff;
1352 1.1 riastrad diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1353 1.1 riastrad if (diff)
1354 1.1 riastrad return diff;
1355 1.4 riastrad
1356 1.4 riastrad diff = b->vrefresh - a->vrefresh;
1357 1.4 riastrad if (diff)
1358 1.4 riastrad return diff;
1359 1.4 riastrad
1360 1.1 riastrad diff = b->clock - a->clock;
1361 1.1 riastrad return diff;
1362 1.1 riastrad }
1363 1.1 riastrad
1364 1.1 riastrad /**
1365 1.1 riastrad * drm_mode_sort - sort mode list
1366 1.4 riastrad * @mode_list: list of drm_display_mode structures to sort
1367 1.1 riastrad *
1368 1.4 riastrad * Sort @mode_list by favorability, moving good modes to the head of the list.
1369 1.1 riastrad */
1370 1.1 riastrad void drm_mode_sort(struct list_head *mode_list)
1371 1.1 riastrad {
1372 1.1 riastrad list_sort(NULL, mode_list, drm_mode_compare);
1373 1.1 riastrad }
1374 1.1 riastrad EXPORT_SYMBOL(drm_mode_sort);
1375 1.1 riastrad
1376 1.1 riastrad /**
1377 1.10 riastrad * drm_connector_list_update - update the mode list for the connector
1378 1.1 riastrad * @connector: the connector to update
1379 1.1 riastrad *
1380 1.1 riastrad * This moves the modes from the @connector probed_modes list
1381 1.1 riastrad * to the actual mode list. It compares the probed mode against the current
1382 1.4 riastrad * list and only adds different/new modes.
1383 1.4 riastrad *
1384 1.4 riastrad * This is just a helper functions doesn't validate any modes itself and also
1385 1.4 riastrad * doesn't prune any invalid modes. Callers need to do that themselves.
1386 1.1 riastrad */
1387 1.10 riastrad void drm_connector_list_update(struct drm_connector *connector)
1388 1.1 riastrad {
1389 1.1 riastrad struct drm_display_mode *pmode, *pt;
1390 1.1 riastrad
1391 1.4 riastrad WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1392 1.4 riastrad
1393 1.10 riastrad list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1394 1.10 riastrad struct drm_display_mode *mode;
1395 1.10 riastrad bool found_it = false;
1396 1.10 riastrad
1397 1.1 riastrad /* go through current modes checking for the new probed mode */
1398 1.1 riastrad list_for_each_entry(mode, &connector->modes, head) {
1399 1.10 riastrad if (!drm_mode_equal(pmode, mode))
1400 1.10 riastrad continue;
1401 1.10 riastrad
1402 1.10 riastrad found_it = true;
1403 1.10 riastrad
1404 1.10 riastrad /*
1405 1.10 riastrad * If the old matching mode is stale (ie. left over
1406 1.10 riastrad * from a previous probe) just replace it outright.
1407 1.10 riastrad * Otherwise just merge the type bits between all
1408 1.10 riastrad * equal probed modes.
1409 1.10 riastrad *
1410 1.10 riastrad * If two probed modes are considered equal, pick the
1411 1.10 riastrad * actual timings from the one that's marked as
1412 1.10 riastrad * preferred (in case the match isn't 100%). If
1413 1.10 riastrad * multiple or zero preferred modes are present, favor
1414 1.10 riastrad * the mode added to the probed_modes list first.
1415 1.10 riastrad */
1416 1.10 riastrad if (mode->status == MODE_STALE) {
1417 1.10 riastrad drm_mode_copy(mode, pmode);
1418 1.10 riastrad } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1419 1.10 riastrad (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
1420 1.10 riastrad pmode->type |= mode->type;
1421 1.10 riastrad drm_mode_copy(mode, pmode);
1422 1.10 riastrad } else {
1423 1.10 riastrad mode->type |= pmode->type;
1424 1.1 riastrad }
1425 1.10 riastrad
1426 1.10 riastrad list_del(&pmode->head);
1427 1.10 riastrad drm_mode_destroy(connector->dev, pmode);
1428 1.10 riastrad break;
1429 1.1 riastrad }
1430 1.1 riastrad
1431 1.1 riastrad if (!found_it) {
1432 1.1 riastrad list_move_tail(&pmode->head, &connector->modes);
1433 1.1 riastrad }
1434 1.1 riastrad }
1435 1.1 riastrad }
1436 1.10 riastrad EXPORT_SYMBOL(drm_connector_list_update);
1437 1.10 riastrad
1438 1.10 riastrad static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
1439 1.10 riastrad struct drm_cmdline_mode *mode)
1440 1.10 riastrad {
1441 1.10 riastrad unsigned int bpp;
1442 1.10 riastrad
1443 1.10 riastrad if (str[0] != '-')
1444 1.10 riastrad return -EINVAL;
1445 1.10 riastrad
1446 1.10 riastrad str++;
1447 1.10 riastrad bpp = simple_strtol(str, end_ptr, 10);
1448 1.10 riastrad if (*end_ptr == str)
1449 1.10 riastrad return -EINVAL;
1450 1.10 riastrad
1451 1.10 riastrad mode->bpp = bpp;
1452 1.10 riastrad mode->bpp_specified = true;
1453 1.10 riastrad
1454 1.10 riastrad return 0;
1455 1.10 riastrad }
1456 1.10 riastrad
1457 1.10 riastrad static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
1458 1.10 riastrad struct drm_cmdline_mode *mode)
1459 1.10 riastrad {
1460 1.10 riastrad unsigned int refresh;
1461 1.10 riastrad
1462 1.10 riastrad if (str[0] != '@')
1463 1.10 riastrad return -EINVAL;
1464 1.10 riastrad
1465 1.10 riastrad str++;
1466 1.10 riastrad refresh = simple_strtol(str, end_ptr, 10);
1467 1.10 riastrad if (*end_ptr == str)
1468 1.10 riastrad return -EINVAL;
1469 1.10 riastrad
1470 1.10 riastrad mode->refresh = refresh;
1471 1.10 riastrad mode->refresh_specified = true;
1472 1.10 riastrad
1473 1.10 riastrad return 0;
1474 1.10 riastrad }
1475 1.10 riastrad
1476 1.10 riastrad static int drm_mode_parse_cmdline_extra(const char *str, int length,
1477 1.10 riastrad bool freestanding,
1478 1.10 riastrad const struct drm_connector *connector,
1479 1.10 riastrad struct drm_cmdline_mode *mode)
1480 1.10 riastrad {
1481 1.10 riastrad int i;
1482 1.10 riastrad
1483 1.10 riastrad for (i = 0; i < length; i++) {
1484 1.10 riastrad switch (str[i]) {
1485 1.10 riastrad case 'i':
1486 1.10 riastrad if (freestanding)
1487 1.10 riastrad return -EINVAL;
1488 1.10 riastrad
1489 1.10 riastrad mode->interlace = true;
1490 1.10 riastrad break;
1491 1.10 riastrad case 'm':
1492 1.10 riastrad if (freestanding)
1493 1.10 riastrad return -EINVAL;
1494 1.10 riastrad
1495 1.10 riastrad mode->margins = true;
1496 1.10 riastrad break;
1497 1.10 riastrad case 'D':
1498 1.10 riastrad if (mode->force != DRM_FORCE_UNSPECIFIED)
1499 1.10 riastrad return -EINVAL;
1500 1.10 riastrad
1501 1.10 riastrad if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1502 1.10 riastrad (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1503 1.10 riastrad mode->force = DRM_FORCE_ON;
1504 1.10 riastrad else
1505 1.10 riastrad mode->force = DRM_FORCE_ON_DIGITAL;
1506 1.10 riastrad break;
1507 1.10 riastrad case 'd':
1508 1.10 riastrad if (mode->force != DRM_FORCE_UNSPECIFIED)
1509 1.10 riastrad return -EINVAL;
1510 1.10 riastrad
1511 1.10 riastrad mode->force = DRM_FORCE_OFF;
1512 1.10 riastrad break;
1513 1.10 riastrad case 'e':
1514 1.10 riastrad if (mode->force != DRM_FORCE_UNSPECIFIED)
1515 1.10 riastrad return -EINVAL;
1516 1.10 riastrad
1517 1.10 riastrad mode->force = DRM_FORCE_ON;
1518 1.10 riastrad break;
1519 1.10 riastrad default:
1520 1.10 riastrad return -EINVAL;
1521 1.10 riastrad }
1522 1.10 riastrad }
1523 1.10 riastrad
1524 1.10 riastrad return 0;
1525 1.10 riastrad }
1526 1.10 riastrad
1527 1.10 riastrad static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
1528 1.10 riastrad bool extras,
1529 1.10 riastrad const struct drm_connector *connector,
1530 1.10 riastrad struct drm_cmdline_mode *mode)
1531 1.10 riastrad {
1532 1.10 riastrad const char *str_start = str;
1533 1.10 riastrad bool rb = false, cvt = false;
1534 1.10 riastrad int xres = 0, yres = 0;
1535 1.10 riastrad int remaining, i;
1536 1.10 riastrad char *end_ptr;
1537 1.10 riastrad
1538 1.10 riastrad xres = simple_strtol(str, &end_ptr, 10);
1539 1.10 riastrad if (end_ptr == str)
1540 1.10 riastrad return -EINVAL;
1541 1.10 riastrad
1542 1.10 riastrad if (end_ptr[0] != 'x')
1543 1.10 riastrad return -EINVAL;
1544 1.10 riastrad end_ptr++;
1545 1.10 riastrad
1546 1.10 riastrad str = end_ptr;
1547 1.10 riastrad yres = simple_strtol(str, &end_ptr, 10);
1548 1.10 riastrad if (end_ptr == str)
1549 1.10 riastrad return -EINVAL;
1550 1.10 riastrad
1551 1.10 riastrad remaining = length - (end_ptr - str_start);
1552 1.10 riastrad if (remaining < 0)
1553 1.10 riastrad return -EINVAL;
1554 1.10 riastrad
1555 1.10 riastrad for (i = 0; i < remaining; i++) {
1556 1.10 riastrad switch (end_ptr[i]) {
1557 1.10 riastrad case 'M':
1558 1.10 riastrad cvt = true;
1559 1.10 riastrad break;
1560 1.10 riastrad case 'R':
1561 1.10 riastrad rb = true;
1562 1.10 riastrad break;
1563 1.10 riastrad default:
1564 1.10 riastrad /*
1565 1.10 riastrad * Try to pass that to our extras parsing
1566 1.10 riastrad * function to handle the case where the
1567 1.10 riastrad * extras are directly after the resolution
1568 1.10 riastrad */
1569 1.10 riastrad if (extras) {
1570 1.10 riastrad int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
1571 1.10 riastrad 1,
1572 1.10 riastrad false,
1573 1.10 riastrad connector,
1574 1.10 riastrad mode);
1575 1.10 riastrad if (ret)
1576 1.10 riastrad return ret;
1577 1.10 riastrad } else {
1578 1.10 riastrad return -EINVAL;
1579 1.10 riastrad }
1580 1.10 riastrad }
1581 1.10 riastrad }
1582 1.10 riastrad
1583 1.10 riastrad mode->xres = xres;
1584 1.10 riastrad mode->yres = yres;
1585 1.10 riastrad mode->cvt = cvt;
1586 1.10 riastrad mode->rb = rb;
1587 1.10 riastrad
1588 1.10 riastrad return 0;
1589 1.10 riastrad }
1590 1.10 riastrad
1591 1.10 riastrad static int drm_mode_parse_cmdline_int(const char *delim, unsigned int *int_ret)
1592 1.10 riastrad {
1593 1.10 riastrad const char *value;
1594 1.10 riastrad char *endp;
1595 1.10 riastrad
1596 1.10 riastrad /*
1597 1.10 riastrad * delim must point to the '=', otherwise it is a syntax error and
1598 1.10 riastrad * if delim points to the terminating zero, then delim + 1 wil point
1599 1.10 riastrad * past the end of the string.
1600 1.10 riastrad */
1601 1.10 riastrad if (*delim != '=')
1602 1.10 riastrad return -EINVAL;
1603 1.10 riastrad
1604 1.10 riastrad value = delim + 1;
1605 1.10 riastrad *int_ret = simple_strtol(value, &endp, 10);
1606 1.10 riastrad
1607 1.10 riastrad /* Make sure we have parsed something */
1608 1.10 riastrad if (endp == value)
1609 1.10 riastrad return -EINVAL;
1610 1.10 riastrad
1611 1.10 riastrad return 0;
1612 1.10 riastrad }
1613 1.10 riastrad
1614 1.10 riastrad static int drm_mode_parse_panel_orientation(const char *delim,
1615 1.10 riastrad struct drm_cmdline_mode *mode)
1616 1.10 riastrad {
1617 1.10 riastrad const char *value;
1618 1.10 riastrad
1619 1.10 riastrad if (*delim != '=')
1620 1.10 riastrad return -EINVAL;
1621 1.10 riastrad
1622 1.10 riastrad value = delim + 1;
1623 1.10 riastrad delim = strchr(value, ',');
1624 1.10 riastrad if (!delim)
1625 1.10 riastrad delim = value + strlen(value);
1626 1.10 riastrad
1627 1.10 riastrad if (!strncmp(value, "normal", delim - value))
1628 1.10 riastrad mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
1629 1.10 riastrad else if (!strncmp(value, "upside_down", delim - value))
1630 1.10 riastrad mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1631 1.10 riastrad else if (!strncmp(value, "left_side_up", delim - value))
1632 1.10 riastrad mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1633 1.10 riastrad else if (!strncmp(value, "right_side_up", delim - value))
1634 1.10 riastrad mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1635 1.10 riastrad else
1636 1.10 riastrad return -EINVAL;
1637 1.10 riastrad
1638 1.10 riastrad return 0;
1639 1.10 riastrad }
1640 1.10 riastrad
1641 1.10 riastrad static int drm_mode_parse_cmdline_options(const char *str,
1642 1.10 riastrad bool freestanding,
1643 1.10 riastrad const struct drm_connector *connector,
1644 1.10 riastrad struct drm_cmdline_mode *mode)
1645 1.10 riastrad {
1646 1.10 riastrad unsigned int deg, margin, rotation = 0;
1647 1.10 riastrad const char *delim, *option, *sep;
1648 1.10 riastrad
1649 1.10 riastrad option = str;
1650 1.10 riastrad do {
1651 1.10 riastrad delim = strchr(option, '=');
1652 1.10 riastrad if (!delim) {
1653 1.10 riastrad delim = strchr(option, ',');
1654 1.10 riastrad
1655 1.10 riastrad if (!delim)
1656 1.10 riastrad delim = option + strlen(option);
1657 1.10 riastrad }
1658 1.10 riastrad
1659 1.10 riastrad if (!strncmp(option, "rotate", delim - option)) {
1660 1.10 riastrad if (drm_mode_parse_cmdline_int(delim, °))
1661 1.10 riastrad return -EINVAL;
1662 1.10 riastrad
1663 1.10 riastrad switch (deg) {
1664 1.10 riastrad case 0:
1665 1.10 riastrad rotation |= DRM_MODE_ROTATE_0;
1666 1.10 riastrad break;
1667 1.10 riastrad
1668 1.10 riastrad case 90:
1669 1.10 riastrad rotation |= DRM_MODE_ROTATE_90;
1670 1.10 riastrad break;
1671 1.10 riastrad
1672 1.10 riastrad case 180:
1673 1.10 riastrad rotation |= DRM_MODE_ROTATE_180;
1674 1.10 riastrad break;
1675 1.10 riastrad
1676 1.10 riastrad case 270:
1677 1.10 riastrad rotation |= DRM_MODE_ROTATE_270;
1678 1.10 riastrad break;
1679 1.10 riastrad
1680 1.10 riastrad default:
1681 1.10 riastrad return -EINVAL;
1682 1.10 riastrad }
1683 1.10 riastrad } else if (!strncmp(option, "reflect_x", delim - option)) {
1684 1.10 riastrad rotation |= DRM_MODE_REFLECT_X;
1685 1.10 riastrad } else if (!strncmp(option, "reflect_y", delim - option)) {
1686 1.10 riastrad rotation |= DRM_MODE_REFLECT_Y;
1687 1.10 riastrad } else if (!strncmp(option, "margin_right", delim - option)) {
1688 1.10 riastrad if (drm_mode_parse_cmdline_int(delim, &margin))
1689 1.10 riastrad return -EINVAL;
1690 1.10 riastrad
1691 1.10 riastrad mode->tv_margins.right = margin;
1692 1.10 riastrad } else if (!strncmp(option, "margin_left", delim - option)) {
1693 1.10 riastrad if (drm_mode_parse_cmdline_int(delim, &margin))
1694 1.10 riastrad return -EINVAL;
1695 1.10 riastrad
1696 1.10 riastrad mode->tv_margins.left = margin;
1697 1.10 riastrad } else if (!strncmp(option, "margin_top", delim - option)) {
1698 1.10 riastrad if (drm_mode_parse_cmdline_int(delim, &margin))
1699 1.10 riastrad return -EINVAL;
1700 1.10 riastrad
1701 1.10 riastrad mode->tv_margins.top = margin;
1702 1.10 riastrad } else if (!strncmp(option, "margin_bottom", delim - option)) {
1703 1.10 riastrad if (drm_mode_parse_cmdline_int(delim, &margin))
1704 1.10 riastrad return -EINVAL;
1705 1.10 riastrad
1706 1.10 riastrad mode->tv_margins.bottom = margin;
1707 1.10 riastrad } else if (!strncmp(option, "panel_orientation", delim - option)) {
1708 1.10 riastrad if (drm_mode_parse_panel_orientation(delim, mode))
1709 1.10 riastrad return -EINVAL;
1710 1.10 riastrad } else {
1711 1.10 riastrad return -EINVAL;
1712 1.10 riastrad }
1713 1.10 riastrad sep = strchr(delim, ',');
1714 1.10 riastrad option = sep + 1;
1715 1.10 riastrad } while (sep);
1716 1.10 riastrad
1717 1.10 riastrad if (rotation && freestanding)
1718 1.10 riastrad return -EINVAL;
1719 1.10 riastrad
1720 1.10 riastrad if (!(rotation & DRM_MODE_ROTATE_MASK))
1721 1.10 riastrad rotation |= DRM_MODE_ROTATE_0;
1722 1.10 riastrad
1723 1.10 riastrad /* Make sure there is exactly one rotation defined */
1724 1.10 riastrad if (!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK))
1725 1.10 riastrad return -EINVAL;
1726 1.10 riastrad
1727 1.10 riastrad mode->rotation_reflection = rotation;
1728 1.10 riastrad
1729 1.10 riastrad return 0;
1730 1.10 riastrad }
1731 1.10 riastrad
1732 1.10 riastrad static const char * const drm_named_modes_whitelist[] = {
1733 1.10 riastrad "NTSC",
1734 1.10 riastrad "PAL",
1735 1.10 riastrad };
1736 1.1 riastrad
1737 1.1 riastrad /**
1738 1.4 riastrad * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1739 1.4 riastrad * @mode_option: optional per connector mode option
1740 1.4 riastrad * @connector: connector to parse modeline for
1741 1.4 riastrad * @mode: preallocated drm_cmdline_mode structure to fill out
1742 1.4 riastrad *
1743 1.4 riastrad * This parses @mode_option command line modeline for modes and options to
1744 1.4 riastrad * configure the connector. If @mode_option is NULL the default command line
1745 1.4 riastrad * modeline in fb_mode_option will be parsed instead.
1746 1.1 riastrad *
1747 1.4 riastrad * This uses the same parameters as the fb modedb.c, except for an extra
1748 1.10 riastrad * force-enable, force-enable-digital and force-disable bit at the end::
1749 1.1 riastrad *
1750 1.1 riastrad * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1751 1.1 riastrad *
1752 1.10 riastrad * Additionals options can be provided following the mode, using a comma to
1753 1.10 riastrad * separate each option. Valid options can be found in
1754 1.10 riastrad * Documentation/fb/modedb.rst.
1755 1.10 riastrad *
1756 1.4 riastrad * The intermediate drm_cmdline_mode structure is required to store additional
1757 1.7 riastrad * options from the command line modline like the force-enable/disable flag.
1758 1.4 riastrad *
1759 1.4 riastrad * Returns:
1760 1.4 riastrad * True if a valid modeline has been parsed, false otherwise.
1761 1.1 riastrad */
1762 1.1 riastrad bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1763 1.10 riastrad const struct drm_connector *connector,
1764 1.1 riastrad struct drm_cmdline_mode *mode)
1765 1.1 riastrad {
1766 1.1 riastrad const char *name;
1767 1.10 riastrad bool freestanding = false, parse_extras = false;
1768 1.10 riastrad unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
1769 1.10 riastrad unsigned int mode_end = 0;
1770 1.10 riastrad const char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
1771 1.10 riastrad const char *options_ptr = NULL;
1772 1.10 riastrad char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
1773 1.10 riastrad int i, len, ret;
1774 1.10 riastrad
1775 1.10 riastrad memset(mode, 0, sizeof(*mode));
1776 1.10 riastrad mode->panel_orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1777 1.1 riastrad
1778 1.1 riastrad if (!mode_option)
1779 1.1 riastrad return false;
1780 1.1 riastrad
1781 1.1 riastrad name = mode_option;
1782 1.1 riastrad
1783 1.10 riastrad /* Try to locate the bpp and refresh specifiers, if any */
1784 1.10 riastrad bpp_ptr = strchr(name, '-');
1785 1.10 riastrad if (bpp_ptr)
1786 1.10 riastrad bpp_off = bpp_ptr - name;
1787 1.10 riastrad
1788 1.10 riastrad refresh_ptr = strchr(name, '@');
1789 1.10 riastrad if (refresh_ptr)
1790 1.10 riastrad refresh_off = refresh_ptr - name;
1791 1.10 riastrad
1792 1.10 riastrad /* Locate the start of named options */
1793 1.10 riastrad options_ptr = strchr(name, ',');
1794 1.10 riastrad if (options_ptr)
1795 1.10 riastrad options_off = options_ptr - name;
1796 1.10 riastrad
1797 1.10 riastrad /* Locate the end of the name / resolution, and parse it */
1798 1.10 riastrad if (bpp_ptr) {
1799 1.10 riastrad mode_end = bpp_off;
1800 1.10 riastrad } else if (refresh_ptr) {
1801 1.10 riastrad mode_end = refresh_off;
1802 1.10 riastrad } else if (options_ptr) {
1803 1.10 riastrad mode_end = options_off;
1804 1.10 riastrad parse_extras = true;
1805 1.10 riastrad } else {
1806 1.10 riastrad mode_end = strlen(name);
1807 1.10 riastrad parse_extras = true;
1808 1.10 riastrad }
1809 1.1 riastrad
1810 1.10 riastrad /* First check for a named mode */
1811 1.10 riastrad for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
1812 1.10 riastrad ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
1813 1.10 riastrad if (ret == mode_end) {
1814 1.10 riastrad if (refresh_ptr)
1815 1.10 riastrad return false; /* named + refresh is invalid */
1816 1.1 riastrad
1817 1.10 riastrad strcpy(mode->name, drm_named_modes_whitelist[i]);
1818 1.10 riastrad mode->specified = true;
1819 1.1 riastrad break;
1820 1.1 riastrad }
1821 1.1 riastrad }
1822 1.1 riastrad
1823 1.10 riastrad /* No named mode? Check for a normal mode argument, e.g. 1024x768 */
1824 1.10 riastrad if (!mode->specified && isdigit(name[0])) {
1825 1.10 riastrad ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
1826 1.10 riastrad parse_extras,
1827 1.10 riastrad connector,
1828 1.10 riastrad mode);
1829 1.10 riastrad if (ret)
1830 1.10 riastrad return false;
1831 1.10 riastrad
1832 1.10 riastrad mode->specified = true;
1833 1.10 riastrad }
1834 1.10 riastrad
1835 1.10 riastrad /* No mode? Check for freestanding extras and/or options */
1836 1.10 riastrad if (!mode->specified) {
1837 1.10 riastrad unsigned int len = strlen(mode_option);
1838 1.10 riastrad
1839 1.10 riastrad if (bpp_ptr || refresh_ptr)
1840 1.10 riastrad return false; /* syntax error */
1841 1.10 riastrad
1842 1.10 riastrad if (len == 1 || (len >= 2 && mode_option[1] == ','))
1843 1.10 riastrad extra_ptr = mode_option;
1844 1.1 riastrad else
1845 1.10 riastrad options_ptr = mode_option - 1;
1846 1.10 riastrad
1847 1.10 riastrad freestanding = true;
1848 1.1 riastrad }
1849 1.1 riastrad
1850 1.10 riastrad if (bpp_ptr) {
1851 1.10 riastrad ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
1852 1.10 riastrad if (ret)
1853 1.10 riastrad return false;
1854 1.10 riastrad
1855 1.10 riastrad mode->bpp_specified = true;
1856 1.1 riastrad }
1857 1.1 riastrad
1858 1.10 riastrad if (refresh_ptr) {
1859 1.10 riastrad ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
1860 1.10 riastrad &refresh_end_ptr, mode);
1861 1.10 riastrad if (ret)
1862 1.10 riastrad return false;
1863 1.10 riastrad
1864 1.1 riastrad mode->refresh_specified = true;
1865 1.1 riastrad }
1866 1.1 riastrad
1867 1.10 riastrad /*
1868 1.10 riastrad * Locate the end of the bpp / refresh, and parse the extras
1869 1.10 riastrad * if relevant
1870 1.10 riastrad */
1871 1.10 riastrad if (bpp_ptr && refresh_ptr)
1872 1.10 riastrad extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
1873 1.10 riastrad else if (bpp_ptr)
1874 1.10 riastrad extra_ptr = bpp_end_ptr;
1875 1.10 riastrad else if (refresh_ptr)
1876 1.10 riastrad extra_ptr = refresh_end_ptr;
1877 1.10 riastrad
1878 1.10 riastrad if (extra_ptr) {
1879 1.10 riastrad if (options_ptr)
1880 1.10 riastrad len = options_ptr - extra_ptr;
1881 1.10 riastrad else
1882 1.10 riastrad len = strlen(extra_ptr);
1883 1.10 riastrad
1884 1.10 riastrad ret = drm_mode_parse_cmdline_extra(extra_ptr, len, freestanding,
1885 1.10 riastrad connector, mode);
1886 1.10 riastrad if (ret)
1887 1.10 riastrad return false;
1888 1.10 riastrad }
1889 1.10 riastrad
1890 1.10 riastrad if (options_ptr) {
1891 1.10 riastrad ret = drm_mode_parse_cmdline_options(options_ptr + 1,
1892 1.10 riastrad freestanding,
1893 1.10 riastrad connector, mode);
1894 1.10 riastrad if (ret)
1895 1.10 riastrad return false;
1896 1.1 riastrad }
1897 1.1 riastrad
1898 1.1 riastrad return true;
1899 1.1 riastrad }
1900 1.1 riastrad EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1901 1.1 riastrad
1902 1.4 riastrad /**
1903 1.4 riastrad * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1904 1.4 riastrad * @dev: DRM device to create the new mode for
1905 1.4 riastrad * @cmd: input command line modeline
1906 1.4 riastrad *
1907 1.4 riastrad * Returns:
1908 1.4 riastrad * Pointer to converted mode on success, NULL on error.
1909 1.4 riastrad */
1910 1.1 riastrad struct drm_display_mode *
1911 1.1 riastrad drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1912 1.1 riastrad struct drm_cmdline_mode *cmd)
1913 1.1 riastrad {
1914 1.1 riastrad struct drm_display_mode *mode;
1915 1.1 riastrad
1916 1.1 riastrad if (cmd->cvt)
1917 1.1 riastrad mode = drm_cvt_mode(dev,
1918 1.1 riastrad cmd->xres, cmd->yres,
1919 1.1 riastrad cmd->refresh_specified ? cmd->refresh : 60,
1920 1.1 riastrad cmd->rb, cmd->interlace,
1921 1.1 riastrad cmd->margins);
1922 1.1 riastrad else
1923 1.1 riastrad mode = drm_gtf_mode(dev,
1924 1.1 riastrad cmd->xres, cmd->yres,
1925 1.1 riastrad cmd->refresh_specified ? cmd->refresh : 60,
1926 1.1 riastrad cmd->interlace,
1927 1.1 riastrad cmd->margins);
1928 1.1 riastrad if (!mode)
1929 1.1 riastrad return NULL;
1930 1.1 riastrad
1931 1.7 riastrad mode->type |= DRM_MODE_TYPE_USERDEF;
1932 1.7 riastrad /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
1933 1.10 riastrad if (cmd->xres == 1366)
1934 1.10 riastrad drm_mode_fixup_1366x768(mode);
1935 1.1 riastrad drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1936 1.1 riastrad return mode;
1937 1.1 riastrad }
1938 1.1 riastrad EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
1939 1.7 riastrad
1940 1.7 riastrad /**
1941 1.7 riastrad * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1942 1.7 riastrad * @out: drm_mode_modeinfo struct to return to the user
1943 1.7 riastrad * @in: drm_display_mode to use
1944 1.7 riastrad *
1945 1.7 riastrad * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1946 1.7 riastrad * the user.
1947 1.7 riastrad */
1948 1.7 riastrad void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1949 1.7 riastrad const struct drm_display_mode *in)
1950 1.7 riastrad {
1951 1.7 riastrad WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1952 1.7 riastrad in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1953 1.7 riastrad in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1954 1.7 riastrad in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1955 1.7 riastrad in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1956 1.7 riastrad "timing values too large for mode info\n");
1957 1.7 riastrad
1958 1.7 riastrad out->clock = in->clock;
1959 1.7 riastrad out->hdisplay = in->hdisplay;
1960 1.7 riastrad out->hsync_start = in->hsync_start;
1961 1.7 riastrad out->hsync_end = in->hsync_end;
1962 1.7 riastrad out->htotal = in->htotal;
1963 1.7 riastrad out->hskew = in->hskew;
1964 1.7 riastrad out->vdisplay = in->vdisplay;
1965 1.7 riastrad out->vsync_start = in->vsync_start;
1966 1.7 riastrad out->vsync_end = in->vsync_end;
1967 1.7 riastrad out->vtotal = in->vtotal;
1968 1.7 riastrad out->vscan = in->vscan;
1969 1.7 riastrad out->vrefresh = in->vrefresh;
1970 1.7 riastrad out->flags = in->flags;
1971 1.7 riastrad out->type = in->type;
1972 1.10 riastrad
1973 1.10 riastrad switch (in->picture_aspect_ratio) {
1974 1.10 riastrad case HDMI_PICTURE_ASPECT_4_3:
1975 1.10 riastrad out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
1976 1.10 riastrad break;
1977 1.10 riastrad case HDMI_PICTURE_ASPECT_16_9:
1978 1.10 riastrad out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
1979 1.10 riastrad break;
1980 1.10 riastrad case HDMI_PICTURE_ASPECT_64_27:
1981 1.10 riastrad out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
1982 1.10 riastrad break;
1983 1.10 riastrad case HDMI_PICTURE_ASPECT_256_135:
1984 1.10 riastrad out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
1985 1.10 riastrad break;
1986 1.10 riastrad default:
1987 1.10 riastrad WARN(1, "Invalid aspect ratio (0%x) on mode\n",
1988 1.10 riastrad in->picture_aspect_ratio);
1989 1.10 riastrad /* fall through */
1990 1.10 riastrad case HDMI_PICTURE_ASPECT_NONE:
1991 1.10 riastrad out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
1992 1.10 riastrad break;
1993 1.10 riastrad }
1994 1.10 riastrad
1995 1.7 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1996 1.7 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1997 1.7 riastrad }
1998 1.7 riastrad
1999 1.7 riastrad /**
2000 1.7 riastrad * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
2001 1.10 riastrad * @dev: drm device
2002 1.7 riastrad * @out: drm_display_mode to return to the user
2003 1.7 riastrad * @in: drm_mode_modeinfo to use
2004 1.7 riastrad *
2005 1.7 riastrad * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
2006 1.7 riastrad * the caller.
2007 1.7 riastrad *
2008 1.7 riastrad * Returns:
2009 1.7 riastrad * Zero on success, negative errno on failure.
2010 1.7 riastrad */
2011 1.10 riastrad int drm_mode_convert_umode(struct drm_device *dev,
2012 1.10 riastrad struct drm_display_mode *out,
2013 1.7 riastrad const struct drm_mode_modeinfo *in)
2014 1.7 riastrad {
2015 1.10 riastrad if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
2016 1.10 riastrad return -ERANGE;
2017 1.7 riastrad
2018 1.7 riastrad out->clock = in->clock;
2019 1.7 riastrad out->hdisplay = in->hdisplay;
2020 1.7 riastrad out->hsync_start = in->hsync_start;
2021 1.7 riastrad out->hsync_end = in->hsync_end;
2022 1.7 riastrad out->htotal = in->htotal;
2023 1.7 riastrad out->hskew = in->hskew;
2024 1.7 riastrad out->vdisplay = in->vdisplay;
2025 1.7 riastrad out->vsync_start = in->vsync_start;
2026 1.7 riastrad out->vsync_end = in->vsync_end;
2027 1.7 riastrad out->vtotal = in->vtotal;
2028 1.7 riastrad out->vscan = in->vscan;
2029 1.7 riastrad out->vrefresh = in->vrefresh;
2030 1.7 riastrad out->flags = in->flags;
2031 1.10 riastrad /*
2032 1.10 riastrad * Old xf86-video-vmware (possibly others too) used to
2033 1.10 riastrad * leave 'type' unititialized. Just ignore any bits we
2034 1.10 riastrad * don't like. It's a just hint after all, and more
2035 1.10 riastrad * useful for the kernel->userspace direction anyway.
2036 1.10 riastrad */
2037 1.10 riastrad out->type = in->type & DRM_MODE_TYPE_ALL;
2038 1.7 riastrad strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
2039 1.7 riastrad out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
2040 1.7 riastrad
2041 1.10 riastrad /* Clearing picture aspect ratio bits from out flags,
2042 1.10 riastrad * as the aspect-ratio information is not stored in
2043 1.10 riastrad * flags for kernel-mode, but in picture_aspect_ratio.
2044 1.10 riastrad */
2045 1.10 riastrad out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
2046 1.10 riastrad
2047 1.10 riastrad switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
2048 1.10 riastrad case DRM_MODE_FLAG_PIC_AR_4_3:
2049 1.10 riastrad out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3;
2050 1.10 riastrad break;
2051 1.10 riastrad case DRM_MODE_FLAG_PIC_AR_16_9:
2052 1.10 riastrad out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
2053 1.10 riastrad break;
2054 1.10 riastrad case DRM_MODE_FLAG_PIC_AR_64_27:
2055 1.10 riastrad out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_64_27;
2056 1.10 riastrad break;
2057 1.10 riastrad case DRM_MODE_FLAG_PIC_AR_256_135:
2058 1.10 riastrad out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135;
2059 1.10 riastrad break;
2060 1.10 riastrad case DRM_MODE_FLAG_PIC_AR_NONE:
2061 1.10 riastrad out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
2062 1.10 riastrad break;
2063 1.10 riastrad default:
2064 1.10 riastrad return -EINVAL;
2065 1.10 riastrad }
2066 1.10 riastrad
2067 1.10 riastrad out->status = drm_mode_validate_driver(dev, out);
2068 1.7 riastrad if (out->status != MODE_OK)
2069 1.10 riastrad return -EINVAL;
2070 1.7 riastrad
2071 1.7 riastrad drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
2072 1.7 riastrad
2073 1.10 riastrad return 0;
2074 1.10 riastrad }
2075 1.10 riastrad
2076 1.10 riastrad /**
2077 1.10 riastrad * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
2078 1.10 riastrad * output format
2079 1.10 riastrad *
2080 1.10 riastrad * @display: display under action
2081 1.10 riastrad * @mode: video mode to be tested.
2082 1.10 riastrad *
2083 1.10 riastrad * Returns:
2084 1.10 riastrad * true if the mode can be supported in YCBCR420 format
2085 1.10 riastrad * false if not.
2086 1.10 riastrad */
2087 1.10 riastrad bool drm_mode_is_420_only(const struct drm_display_info *display,
2088 1.10 riastrad const struct drm_display_mode *mode)
2089 1.10 riastrad {
2090 1.10 riastrad u8 vic = drm_match_cea_mode(mode);
2091 1.10 riastrad
2092 1.10 riastrad return test_bit(vic, display->hdmi.y420_vdb_modes);
2093 1.10 riastrad }
2094 1.10 riastrad EXPORT_SYMBOL(drm_mode_is_420_only);
2095 1.10 riastrad
2096 1.10 riastrad /**
2097 1.10 riastrad * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
2098 1.10 riastrad * output format also (along with RGB/YCBCR444/422)
2099 1.10 riastrad *
2100 1.10 riastrad * @display: display under action.
2101 1.10 riastrad * @mode: video mode to be tested.
2102 1.10 riastrad *
2103 1.10 riastrad * Returns:
2104 1.10 riastrad * true if the mode can be support YCBCR420 format
2105 1.10 riastrad * false if not.
2106 1.10 riastrad */
2107 1.10 riastrad bool drm_mode_is_420_also(const struct drm_display_info *display,
2108 1.10 riastrad const struct drm_display_mode *mode)
2109 1.10 riastrad {
2110 1.10 riastrad u8 vic = drm_match_cea_mode(mode);
2111 1.7 riastrad
2112 1.10 riastrad return test_bit(vic, display->hdmi.y420_cmdb_modes);
2113 1.10 riastrad }
2114 1.10 riastrad EXPORT_SYMBOL(drm_mode_is_420_also);
2115 1.10 riastrad /**
2116 1.10 riastrad * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
2117 1.10 riastrad * output format
2118 1.10 riastrad *
2119 1.10 riastrad * @display: display under action.
2120 1.10 riastrad * @mode: video mode to be tested.
2121 1.10 riastrad *
2122 1.10 riastrad * Returns:
2123 1.10 riastrad * true if the mode can be supported in YCBCR420 format
2124 1.10 riastrad * false if not.
2125 1.10 riastrad */
2126 1.10 riastrad bool drm_mode_is_420(const struct drm_display_info *display,
2127 1.10 riastrad const struct drm_display_mode *mode)
2128 1.10 riastrad {
2129 1.10 riastrad return drm_mode_is_420_only(display, mode) ||
2130 1.10 riastrad drm_mode_is_420_also(display, mode);
2131 1.7 riastrad }
2132 1.10 riastrad EXPORT_SYMBOL(drm_mode_is_420);
2133