blob: f64ac86deb84aac6a93f8756121bb9d6910bacda [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
Dave Airlief453ba02008-11-07 14:05:41 -08002 * Copyright © 1997-2003 by The XFree86 Project, Inc.
3 * Copyright © 2007 Dave Airlie
4 * Copyright © 2007-2008 Intel Corporation
5 * Jesse Barnes <jesse.barnes@intel.com>
Zhao Yakuid782c3f2009-06-22 13:17:08 +08006 * Copyright 2005-2006 Luc Verhaegen
Zhao Yakui26bbdad2009-06-22 13:17:09 +08007 * Copyright (c) 2001, Andy Ritger aritger@nvidia.com
Dave Airlief453ba02008-11-07 14:05:41 -08008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the copyright holder(s)
28 * and author(s) shall not be used in advertising or otherwise to promote
29 * the sale, use or other dealings in this Software without prior written
30 * authorization from the copyright holder(s) and author(s).
31 */
32
33#include <linux/list.h>
Dave Chinner2c761272010-01-12 17:39:16 +110034#include <linux/list_sort.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040035#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
Steffen Trumtraredb37a92012-10-28 18:28:06 +010038#include <video/of_videomode.h>
Steffen Trumtrarebc64e42012-11-14 11:22:52 +010039#include <video/videomode.h>
Daniel Vetter55310002014-01-23 15:52:20 +010040#include <drm/drm_modes.h>
Dave Airlief453ba02008-11-07 14:05:41 -080041
Daniel Vetter8bd441b2014-01-23 15:35:24 +010042#include "drm_crtc_internal.h"
43
Dave Airlief453ba02008-11-07 14:05:41 -080044/**
Daniel Vetter3ec0db82014-01-23 15:06:15 +010045 * drm_mode_debug_printmodeline - print a mode to dmesg
Dave Airlief453ba02008-11-07 14:05:41 -080046 * @mode: mode to print
47 *
Dave Airlief453ba02008-11-07 14:05:41 -080048 * Describe @mode using DRM_DEBUG.
49 */
Ville Syrjälä0b3904a2012-10-25 18:05:05 +000050void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -080051{
Zhao Yakuif940f372009-07-20 13:48:05 +080052 DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
Zhao Yakui8a4c47f2009-07-20 13:48:04 +080053 "0x%x 0x%x\n",
yakui_zhaof0531852009-06-02 14:12:47 +080054 mode->base.id, mode->name, mode->vrefresh, mode->clock,
55 mode->hdisplay, mode->hsync_start,
56 mode->hsync_end, mode->htotal,
57 mode->vdisplay, mode->vsync_start,
58 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
Dave Airlief453ba02008-11-07 14:05:41 -080059}
60EXPORT_SYMBOL(drm_mode_debug_printmodeline);
61
62/**
Daniel Vetter8bd441b2014-01-23 15:35:24 +010063 * drm_mode_create - create a new display mode
64 * @dev: DRM device
65 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010066 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
67 * and return it.
Daniel Vetter8bd441b2014-01-23 15:35:24 +010068 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010069 * Returns:
Daniel Vetter8bd441b2014-01-23 15:35:24 +010070 * Pointer to new mode on success, NULL on error.
71 */
72struct drm_display_mode *drm_mode_create(struct drm_device *dev)
73{
74 struct drm_display_mode *nmode;
75
76 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
77 if (!nmode)
78 return NULL;
79
80 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
81 kfree(nmode);
82 return NULL;
83 }
84
85 return nmode;
86}
87EXPORT_SYMBOL(drm_mode_create);
88
89/**
90 * drm_mode_destroy - remove a mode
91 * @dev: DRM device
92 * @mode: mode to remove
93 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010094 * Release @mode's unique ID, then free it @mode structure itself using kfree.
Daniel Vetter8bd441b2014-01-23 15:35:24 +010095 */
96void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
97{
98 if (!mode)
99 return;
100
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000101 drm_mode_object_unregister(dev, &mode->base);
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100102
103 kfree(mode);
104}
105EXPORT_SYMBOL(drm_mode_destroy);
106
107/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100108 * drm_mode_probed_add - add a mode to a connector's probed_mode list
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100109 * @connector: connector the new mode
110 * @mode: mode data
111 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100112 * Add @mode to @connector's probed_mode list for later use. This list should
113 * then in a second step get filtered and all the modes actually supported by
114 * the hardware moved to the @connector's modes list.
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100115 */
116void drm_mode_probed_add(struct drm_connector *connector,
117 struct drm_display_mode *mode)
118{
119 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
120
121 list_add_tail(&mode->head, &connector->probed_modes);
122}
123EXPORT_SYMBOL(drm_mode_probed_add);
124
125/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100126 * drm_cvt_mode -create a modeline based on the CVT algorithm
127 * @dev: drm device
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800128 * @hdisplay: hdisplay size
129 * @vdisplay: vdisplay size
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100130 * @vrefresh: vrefresh rate
131 * @reduced: whether to use reduced blanking
132 * @interlaced: whether to compute an interlaced mode
133 * @margins: whether to add margins (borders)
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800134 *
135 * This function is called to generate the modeline based on CVT algorithm
136 * according to the hdisplay, vdisplay, vrefresh.
137 * It is based from the VESA(TM) Coordinated Video Timing Generator by
138 * Graham Loveridge April 9, 2003 available at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200139 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800140 *
141 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
142 * What I have done is to translate it by using integer calculation.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100143 *
144 * Returns:
145 * The modeline based on the CVT algorithm stored in a drm_display_mode object.
146 * The display mode object is allocated with drm_mode_create(). Returns NULL
147 * when no mode could be allocated.
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800148 */
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800149struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
150 int vdisplay, int vrefresh,
Dave Airlied50ba252009-09-23 14:44:08 +1000151 bool reduced, bool interlaced, bool margins)
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800152{
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100153#define HV_FACTOR 1000
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800154 /* 1) top/bottom margin size (% of height) - default: 1.8, */
155#define CVT_MARGIN_PERCENTAGE 18
156 /* 2) character cell horizontal granularity (pixels) - default 8 */
157#define CVT_H_GRANULARITY 8
158 /* 3) Minimum vertical porch (lines) - default 3 */
159#define CVT_MIN_V_PORCH 3
160 /* 4) Minimum number of vertical back porch lines - default 6 */
161#define CVT_MIN_V_BPORCH 6
162 /* Pixel Clock step (kHz) */
163#define CVT_CLOCK_STEP 250
164 struct drm_display_mode *drm_mode;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800165 unsigned int vfieldrate, hperiod;
166 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
167 int interlace;
Chris Wilson8a5bbf32016-10-21 15:15:40 +0100168 u64 tmp;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800169
170 /* allocate the drm_display_mode structure. If failure, we will
171 * return directly
172 */
173 drm_mode = drm_mode_create(dev);
174 if (!drm_mode)
175 return NULL;
176
177 /* the CVT default refresh rate is 60Hz */
178 if (!vrefresh)
179 vrefresh = 60;
180
181 /* the required field fresh rate */
182 if (interlaced)
183 vfieldrate = vrefresh * 2;
184 else
185 vfieldrate = vrefresh;
186
187 /* horizontal pixels */
188 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
189
190 /* determine the left&right borders */
191 hmargin = 0;
192 if (margins) {
193 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
194 hmargin -= hmargin % CVT_H_GRANULARITY;
195 }
196 /* find the total active pixels */
197 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
198
199 /* find the number of lines per field */
200 if (interlaced)
201 vdisplay_rnd = vdisplay / 2;
202 else
203 vdisplay_rnd = vdisplay;
204
205 /* find the top & bottom borders */
206 vmargin = 0;
207 if (margins)
208 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
209
Francisco Jerez841b4112009-08-12 02:30:09 +0200210 drm_mode->vdisplay = vdisplay + 2 * vmargin;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800211
212 /* Interlaced */
213 if (interlaced)
214 interlace = 1;
215 else
216 interlace = 0;
217
218 /* Determine VSync Width from aspect ratio */
219 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
220 vsync = 4;
221 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
222 vsync = 5;
223 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
224 vsync = 6;
225 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
226 vsync = 7;
227 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
228 vsync = 7;
229 else /* custom */
230 vsync = 10;
231
232 if (!reduced) {
233 /* simplify the GTF calculation */
234 /* 4) Minimum time of vertical sync + back porch interval (µs)
235 * default 550.0
236 */
237 int tmp1, tmp2;
238#define CVT_MIN_VSYNC_BP 550
239 /* 3) Nominal HSync width (% of line period) - default 8 */
240#define CVT_HSYNC_PERCENTAGE 8
241 unsigned int hblank_percentage;
242 int vsyncandback_porch, vback_porch, hblank;
243
244 /* estimated the horizontal period */
245 tmp1 = HV_FACTOR * 1000000 -
246 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
247 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
248 interlace;
249 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
250
251 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
252 /* 9. Find number of lines in sync + backporch */
253 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
254 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
255 else
256 vsyncandback_porch = tmp1;
257 /* 10. Find number of lines in back porch */
258 vback_porch = vsyncandback_porch - vsync;
259 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
260 vsyncandback_porch + CVT_MIN_V_PORCH;
261 /* 5) Definition of Horizontal blanking time limitation */
262 /* Gradient (%/kHz) - default 600 */
263#define CVT_M_FACTOR 600
264 /* Offset (%) - default 40 */
265#define CVT_C_FACTOR 40
266 /* Blanking time scaling factor - default 128 */
267#define CVT_K_FACTOR 128
268 /* Scaling factor weighting - default 20 */
269#define CVT_J_FACTOR 20
270#define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
271#define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
272 CVT_J_FACTOR)
273 /* 12. Find ideal blanking duty cycle from formula */
274 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
275 hperiod / 1000;
276 /* 13. Blanking time */
277 if (hblank_percentage < 20 * HV_FACTOR)
278 hblank_percentage = 20 * HV_FACTOR;
279 hblank = drm_mode->hdisplay * hblank_percentage /
280 (100 * HV_FACTOR - hblank_percentage);
281 hblank -= hblank % (2 * CVT_H_GRANULARITY);
Yannick Guerrini2a97acd2015-03-04 09:30:09 +0100282 /* 14. find the total pixels per line */
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800283 drm_mode->htotal = drm_mode->hdisplay + hblank;
284 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
285 drm_mode->hsync_start = drm_mode->hsync_end -
286 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
287 drm_mode->hsync_start += CVT_H_GRANULARITY -
288 drm_mode->hsync_start % CVT_H_GRANULARITY;
289 /* fill the Vsync values */
290 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
291 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
292 } else {
293 /* Reduced blanking */
294 /* Minimum vertical blanking interval time (µs)- default 460 */
295#define CVT_RB_MIN_VBLANK 460
296 /* Fixed number of clocks for horizontal sync */
297#define CVT_RB_H_SYNC 32
298 /* Fixed number of clocks for horizontal blanking */
299#define CVT_RB_H_BLANK 160
300 /* Fixed number of lines for vertical front porch - default 3*/
301#define CVT_RB_VFPORCH 3
302 int vbilines;
303 int tmp1, tmp2;
304 /* 8. Estimate Horizontal period. */
305 tmp1 = HV_FACTOR * 1000000 -
306 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
307 tmp2 = vdisplay_rnd + 2 * vmargin;
308 hperiod = tmp1 / (tmp2 * vfieldrate);
309 /* 9. Find number of lines in vertical blanking */
310 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
311 /* 10. Check if vertical blanking is sufficient */
312 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
313 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
314 /* 11. Find total number of lines in vertical field */
315 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
316 /* 12. Find total number of pixels in a line */
317 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
318 /* Fill in HSync values */
319 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
Adam Jacksonadde0f22010-08-23 10:19:14 -0400320 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
321 /* Fill in VSync values */
322 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
323 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800324 }
325 /* 15/13. Find pixel clock frequency (kHz for xf86) */
Chris Wilson8a5bbf32016-10-21 15:15:40 +0100326 tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
327 tmp *= HV_FACTOR * 1000;
328 do_div(tmp, hperiod);
329 tmp -= drm_mode->clock % CVT_CLOCK_STEP;
330 drm_mode->clock = tmp;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800331 /* 18/16. Find actual vertical frame frequency */
332 /* ignore - just set the mode flag for interlaced */
Adam Jackson171fdd82010-03-29 21:43:31 +0000333 if (interlaced) {
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800334 drm_mode->vtotal *= 2;
Adam Jackson171fdd82010-03-29 21:43:31 +0000335 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
336 }
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800337 /* Fill the mode line name */
338 drm_mode_set_name(drm_mode);
339 if (reduced)
340 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
341 DRM_MODE_FLAG_NVSYNC);
342 else
343 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
344 DRM_MODE_FLAG_NHSYNC);
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800345
Adam Jackson171fdd82010-03-29 21:43:31 +0000346 return drm_mode;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800347}
348EXPORT_SYMBOL(drm_cvt_mode);
349
350/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100351 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
352 * @dev: drm device
353 * @hdisplay: hdisplay size
354 * @vdisplay: vdisplay size
355 * @vrefresh: vrefresh rate.
356 * @interlaced: whether to compute an interlaced mode
357 * @margins: desired margin (borders) size
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100358 * @GTF_M: extended GTF formula parameters
359 * @GTF_2C: extended GTF formula parameters
360 * @GTF_K: extended GTF formula parameters
361 * @GTF_2J: extended GTF formula parameters
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800362 *
Adam Jackson7a374352010-03-29 21:43:30 +0000363 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
364 * in here multiplied by two. For a C of 40, pass in 80.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100365 *
366 * Returns:
367 * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
368 * The display mode object is allocated with drm_mode_create(). Returns NULL
369 * when no mode could be allocated.
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800370 */
Adam Jackson7a374352010-03-29 21:43:30 +0000371struct drm_display_mode *
372drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
373 int vrefresh, bool interlaced, int margins,
374 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
375{ /* 1) top/bottom margin size (% of height) - default: 1.8, */
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800376#define GTF_MARGIN_PERCENTAGE 18
377 /* 2) character cell horizontal granularity (pixels) - default 8 */
378#define GTF_CELL_GRAN 8
379 /* 3) Minimum vertical porch (lines) - default 3 */
380#define GTF_MIN_V_PORCH 1
381 /* width of vsync in lines */
382#define V_SYNC_RQD 3
383 /* width of hsync as % of total line */
384#define H_SYNC_PERCENT 8
385 /* min time of vsync + back porch (microsec) */
386#define MIN_VSYNC_PLUS_BP 550
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800387 /* C' and M' are part of the Blanking Duty Cycle computation */
Adam Jackson7a374352010-03-29 21:43:30 +0000388#define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
389#define GTF_M_PRIME (GTF_K * GTF_M / 256)
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800390 struct drm_display_mode *drm_mode;
391 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
392 int top_margin, bottom_margin;
393 int interlace;
394 unsigned int hfreq_est;
395 int vsync_plus_bp, vback_porch;
396 unsigned int vtotal_lines, vfieldrate_est, hperiod;
397 unsigned int vfield_rate, vframe_rate;
398 int left_margin, right_margin;
399 unsigned int total_active_pixels, ideal_duty_cycle;
400 unsigned int hblank, total_pixels, pixel_freq;
401 int hsync, hfront_porch, vodd_front_porch_lines;
402 unsigned int tmp1, tmp2;
403
404 drm_mode = drm_mode_create(dev);
405 if (!drm_mode)
406 return NULL;
407
408 /* 1. In order to give correct results, the number of horizontal
409 * pixels requested is first processed to ensure that it is divisible
410 * by the character size, by rounding it to the nearest character
411 * cell boundary:
412 */
413 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
414 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
415
416 /* 2. If interlace is requested, the number of vertical lines assumed
417 * by the calculation must be halved, as the computation calculates
418 * the number of vertical lines per field.
419 */
420 if (interlaced)
421 vdisplay_rnd = vdisplay / 2;
422 else
423 vdisplay_rnd = vdisplay;
424
425 /* 3. Find the frame rate required: */
426 if (interlaced)
427 vfieldrate_rqd = vrefresh * 2;
428 else
429 vfieldrate_rqd = vrefresh;
430
431 /* 4. Find number of lines in Top margin: */
432 top_margin = 0;
433 if (margins)
434 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
435 1000;
436 /* 5. Find number of lines in bottom margin: */
437 bottom_margin = top_margin;
438
439 /* 6. If interlace is required, then set variable interlace: */
440 if (interlaced)
441 interlace = 1;
442 else
443 interlace = 0;
444
445 /* 7. Estimate the Horizontal frequency */
446 {
447 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
448 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
449 2 + interlace;
450 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
451 }
452
453 /* 8. Find the number of lines in V sync + back porch */
454 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
455 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
456 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
457 /* 9. Find the number of lines in V back porch alone: */
458 vback_porch = vsync_plus_bp - V_SYNC_RQD;
459 /* 10. Find the total number of lines in Vertical field period: */
460 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
461 vsync_plus_bp + GTF_MIN_V_PORCH;
462 /* 11. Estimate the Vertical field frequency: */
463 vfieldrate_est = hfreq_est / vtotal_lines;
464 /* 12. Find the actual horizontal period: */
465 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
466
467 /* 13. Find the actual Vertical field frequency: */
468 vfield_rate = hfreq_est / vtotal_lines;
469 /* 14. Find the Vertical frame frequency: */
470 if (interlaced)
471 vframe_rate = vfield_rate / 2;
472 else
473 vframe_rate = vfield_rate;
474 /* 15. Find number of pixels in left margin: */
475 if (margins)
476 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
477 1000;
478 else
479 left_margin = 0;
480
481 /* 16.Find number of pixels in right margin: */
482 right_margin = left_margin;
483 /* 17.Find total number of active pixels in image and left and right */
484 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
485 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
486 ideal_duty_cycle = GTF_C_PRIME * 1000 -
487 (GTF_M_PRIME * 1000000 / hfreq_est);
488 /* 19.Find the number of pixels in the blanking time to the nearest
489 * double character cell: */
490 hblank = total_active_pixels * ideal_duty_cycle /
491 (100000 - ideal_duty_cycle);
492 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
493 hblank = hblank * 2 * GTF_CELL_GRAN;
494 /* 20.Find total number of pixels: */
495 total_pixels = total_active_pixels + hblank;
496 /* 21.Find pixel clock frequency: */
497 pixel_freq = total_pixels * hfreq_est / 1000;
498 /* Stage 1 computations are now complete; I should really pass
499 * the results to another function and do the Stage 2 computations,
500 * but I only need a few more values so I'll just append the
501 * computations here for now */
502 /* 17. Find the number of pixels in the horizontal sync period: */
503 hsync = H_SYNC_PERCENT * total_pixels / 100;
504 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
505 hsync = hsync * GTF_CELL_GRAN;
506 /* 18. Find the number of pixels in horizontal front porch period */
507 hfront_porch = hblank / 2 - hsync;
508 /* 36. Find the number of lines in the odd front porch period: */
509 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
510
511 /* finally, pack the results in the mode struct */
512 drm_mode->hdisplay = hdisplay_rnd;
513 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
514 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
515 drm_mode->htotal = total_pixels;
516 drm_mode->vdisplay = vdisplay_rnd;
517 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
518 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
519 drm_mode->vtotal = vtotal_lines;
520
521 drm_mode->clock = pixel_freq;
522
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800523 if (interlaced) {
524 drm_mode->vtotal *= 2;
525 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
526 }
527
Adam Jackson171fdd82010-03-29 21:43:31 +0000528 drm_mode_set_name(drm_mode);
Adam Jacksonc385e50c2010-04-08 19:00:30 +0000529 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
530 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
531 else
532 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
Adam Jackson171fdd82010-03-29 21:43:31 +0000533
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800534 return drm_mode;
535}
Adam Jackson7a374352010-03-29 21:43:30 +0000536EXPORT_SYMBOL(drm_gtf_mode_complex);
537
538/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100539 * drm_gtf_mode - create the modeline based on the GTF algorithm
540 * @dev: drm device
541 * @hdisplay: hdisplay size
542 * @vdisplay: vdisplay size
543 * @vrefresh: vrefresh rate.
544 * @interlaced: whether to compute an interlaced mode
545 * @margins: desired margin (borders) size
Adam Jackson7a374352010-03-29 21:43:30 +0000546 *
Adam Jackson7a374352010-03-29 21:43:30 +0000547 * return the modeline based on GTF algorithm
548 *
549 * This function is to create the modeline based on the GTF algorithm.
550 * Generalized Timing Formula is derived from:
Daniel Vetter2e7a5702016-06-01 23:40:36 +0200551 *
Adam Jackson7a374352010-03-29 21:43:30 +0000552 * GTF Spreadsheet by Andy Morrish (1/5/97)
553 * available at http://www.vesa.org
554 *
555 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
556 * What I have done is to translate it by using integer calculation.
557 * I also refer to the function of fb_get_mode in the file of
558 * drivers/video/fbmon.c
559 *
Daniel Vetterda5335b2016-05-31 22:55:13 +0200560 * Standard GTF parameters::
561 *
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +0100562 * M = 600
563 * C = 40
564 * K = 128
565 * J = 20
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100566 *
567 * Returns:
568 * The modeline based on the GTF algorithm stored in a drm_display_mode object.
569 * The display mode object is allocated with drm_mode_create(). Returns NULL
570 * when no mode could be allocated.
Adam Jackson7a374352010-03-29 21:43:30 +0000571 */
572struct drm_display_mode *
573drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100574 bool interlaced, int margins)
Adam Jackson7a374352010-03-29 21:43:30 +0000575{
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100576 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
577 interlaced, margins,
578 600, 40 * 2, 128, 20 * 2);
Adam Jackson7a374352010-03-29 21:43:30 +0000579}
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800580EXPORT_SYMBOL(drm_gtf_mode);
Adam Jackson7a374352010-03-29 21:43:30 +0000581
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200582#ifdef CONFIG_VIDEOMODE_HELPERS
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100583/**
584 * drm_display_mode_from_videomode - fill in @dmode using @vm,
585 * @vm: videomode structure to use as source
586 * @dmode: drm_display_mode structure to use as destination
587 *
588 * Fills out @dmode using the display mode specified in @vm.
589 */
Daniel Vetterba0c2422014-01-23 16:28:50 +0100590void drm_display_mode_from_videomode(const struct videomode *vm,
591 struct drm_display_mode *dmode)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100592{
593 dmode->hdisplay = vm->hactive;
594 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
595 dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
596 dmode->htotal = dmode->hsync_end + vm->hback_porch;
597
598 dmode->vdisplay = vm->vactive;
599 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
600 dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
601 dmode->vtotal = dmode->vsync_end + vm->vback_porch;
602
603 dmode->clock = vm->pixelclock / 1000;
604
605 dmode->flags = 0;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200606 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100607 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200608 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100609 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200610 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100611 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200612 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100613 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200614 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100615 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200616 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100617 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
Steffen Trumtrar328a4712013-05-27 12:33:35 +0000618 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
619 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100620 drm_mode_set_name(dmode);
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100621}
622EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100623
Steve Longerbeamd490f452014-12-18 18:00:22 -0800624/**
625 * drm_display_mode_to_videomode - fill in @vm using @dmode,
626 * @dmode: drm_display_mode structure to use as source
627 * @vm: videomode structure to use as destination
628 *
629 * Fills out @vm using the display mode specified in @dmode.
630 */
631void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
632 struct videomode *vm)
633{
634 vm->hactive = dmode->hdisplay;
635 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
636 vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
637 vm->hback_porch = dmode->htotal - dmode->hsync_end;
638
639 vm->vactive = dmode->vdisplay;
640 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
641 vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
642 vm->vback_porch = dmode->vtotal - dmode->vsync_end;
643
644 vm->pixelclock = dmode->clock * 1000;
645
646 vm->flags = 0;
647 if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
648 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
649 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
650 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
651 if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
652 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
653 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
654 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
655 if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
656 vm->flags |= DISPLAY_FLAGS_INTERLACED;
657 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
658 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
659 if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
660 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
661}
662EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
663
Lothar Waßmanncb34d7f2016-08-16 15:34:37 +0200664/**
665 * drm_bus_flags_from_videomode - extract information about pixelclk and
666 * DE polarity from videomode and store it in a separate variable
667 * @vm: videomode structure to use
668 * @bus_flags: information about pixelclk and DE polarity will be stored here
669 *
670 * Sets DRM_BUS_FLAG_DE_(LOW|HIGH) and DRM_BUS_FLAG_PIXDATA_(POS|NEG)EDGE
671 * in @bus_flags according to DISPLAY_FLAGS found in @vm
672 */
Lothar Waßmannd72daa02016-07-12 15:30:02 +0200673void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
674{
675 *bus_flags = 0;
676 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
677 *bus_flags |= DRM_BUS_FLAG_PIXDATA_POSEDGE;
678 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
679 *bus_flags |= DRM_BUS_FLAG_PIXDATA_NEGEDGE;
680
681 if (vm->flags & DISPLAY_FLAGS_DE_LOW)
682 *bus_flags |= DRM_BUS_FLAG_DE_LOW;
683 if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
684 *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
685}
686EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
687
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200688#ifdef CONFIG_OF
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100689/**
690 * of_get_drm_display_mode - get a drm_display_mode from devicetree
691 * @np: device_node with the timing specification
692 * @dmode: will be set to the return value
Lothar Waßmanncb34d7f2016-08-16 15:34:37 +0200693 * @bus_flags: information about pixelclk and DE polarity
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100694 * @index: index into the list of display timings in devicetree
695 *
696 * This function is expensive and should only be used, if only one mode is to be
697 * read from DT. To get multiple modes start with of_get_display_timings and
698 * work with that instead.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100699 *
700 * Returns:
701 * 0 on success, a negative errno code when no of videomode node was found.
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100702 */
703int of_get_drm_display_mode(struct device_node *np,
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200704 struct drm_display_mode *dmode, u32 *bus_flags,
705 int index)
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100706{
707 struct videomode vm;
708 int ret;
709
710 ret = of_get_videomode(np, &vm, index);
711 if (ret)
712 return ret;
713
714 drm_display_mode_from_videomode(&vm, dmode);
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200715 if (bus_flags)
716 drm_bus_flags_from_videomode(&vm, bus_flags);
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100717
718 pr_debug("%s: got %dx%d display mode from %s\n",
719 of_node_full_name(np), vm.hactive, vm.vactive, np->name);
720 drm_mode_debug_printmodeline(dmode);
721
722 return 0;
723}
724EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200725#endif /* CONFIG_OF */
726#endif /* CONFIG_VIDEOMODE_HELPERS */
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100727
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800728/**
Dave Airlief453ba02008-11-07 14:05:41 -0800729 * drm_mode_set_name - set the name on a mode
730 * @mode: name will be set in this mode
731 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100732 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
733 * with an optional 'i' suffix for interlaced modes.
Dave Airlief453ba02008-11-07 14:05:41 -0800734 */
735void drm_mode_set_name(struct drm_display_mode *mode)
736{
Adam Jackson171fdd82010-03-29 21:43:31 +0000737 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
738
739 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
740 mode->hdisplay, mode->vdisplay,
741 interlaced ? "i" : "");
Dave Airlief453ba02008-11-07 14:05:41 -0800742}
743EXPORT_SYMBOL(drm_mode_set_name);
744
Daniel Vetter30ecad72015-12-09 09:29:36 +0100745/**
746 * drm_mode_hsync - get the hsync of a mode
Adam Jackson7ac96a92009-12-03 17:44:37 -0500747 * @mode: mode
748 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100749 * Returns:
750 * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
751 * value first if it is not yet set.
Adam Jackson7ac96a92009-12-03 17:44:37 -0500752 */
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000753int drm_mode_hsync(const struct drm_display_mode *mode)
Adam Jackson7ac96a92009-12-03 17:44:37 -0500754{
755 unsigned int calc_val;
756
757 if (mode->hsync)
758 return mode->hsync;
759
760 if (mode->htotal < 0)
761 return 0;
762
763 calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
764 calc_val += 500; /* round to 1000Hz */
765 calc_val /= 1000; /* truncate to kHz */
766
767 return calc_val;
768}
769EXPORT_SYMBOL(drm_mode_hsync);
770
Dave Airlief453ba02008-11-07 14:05:41 -0800771/**
772 * drm_mode_vrefresh - get the vrefresh of a mode
773 * @mode: mode
774 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100775 * Returns:
776 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
777 * value first if it is not yet set.
Dave Airlief453ba02008-11-07 14:05:41 -0800778 */
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000779int drm_mode_vrefresh(const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800780{
781 int refresh = 0;
782 unsigned int calc_val;
783
784 if (mode->vrefresh > 0)
785 refresh = mode->vrefresh;
786 else if (mode->htotal > 0 && mode->vtotal > 0) {
Zhao Yakui559ee212009-09-03 09:33:47 +0800787 int vtotal;
788 vtotal = mode->vtotal;
Dave Airlief453ba02008-11-07 14:05:41 -0800789 /* work out vrefresh the value will be x1000 */
790 calc_val = (mode->clock * 1000);
Dave Airlief453ba02008-11-07 14:05:41 -0800791 calc_val /= mode->htotal;
Zhao Yakui559ee212009-09-03 09:33:47 +0800792 refresh = (calc_val + vtotal / 2) / vtotal;
Dave Airlief453ba02008-11-07 14:05:41 -0800793
Dave Airlief453ba02008-11-07 14:05:41 -0800794 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
795 refresh *= 2;
796 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
797 refresh /= 2;
798 if (mode->vscan > 1)
799 refresh /= mode->vscan;
800 }
801 return refresh;
802}
803EXPORT_SYMBOL(drm_mode_vrefresh);
804
805/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100806 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
Dave Airlief453ba02008-11-07 14:05:41 -0800807 * @p: mode
Damien Lespiau448cce22013-09-25 16:45:35 +0100808 * @adjust_flags: a combination of adjustment flags
Dave Airlief453ba02008-11-07 14:05:41 -0800809 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100810 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
Damien Lespiau448cce22013-09-25 16:45:35 +0100811 *
812 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
813 * interlaced modes.
814 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
815 * buffers containing two eyes (only adjust the timings when needed, eg. for
816 * "frame packing" or "side by side full").
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800817 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
818 * be performed for doublescan and vscan > 1 modes respectively.
Dave Airlief453ba02008-11-07 14:05:41 -0800819 */
820void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
821{
822 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
823 return;
824
Damien Lespiaubde2dcf2013-09-25 16:45:34 +0100825 p->crtc_clock = p->clock;
Dave Airlief453ba02008-11-07 14:05:41 -0800826 p->crtc_hdisplay = p->hdisplay;
827 p->crtc_hsync_start = p->hsync_start;
828 p->crtc_hsync_end = p->hsync_end;
829 p->crtc_htotal = p->htotal;
830 p->crtc_hskew = p->hskew;
831 p->crtc_vdisplay = p->vdisplay;
832 p->crtc_vsync_start = p->vsync_start;
833 p->crtc_vsync_end = p->vsync_end;
834 p->crtc_vtotal = p->vtotal;
835
836 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
837 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
838 p->crtc_vdisplay /= 2;
839 p->crtc_vsync_start /= 2;
840 p->crtc_vsync_end /= 2;
841 p->crtc_vtotal /= 2;
842 }
Dave Airlief453ba02008-11-07 14:05:41 -0800843 }
844
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800845 if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
846 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
847 p->crtc_vdisplay *= 2;
848 p->crtc_vsync_start *= 2;
849 p->crtc_vsync_end *= 2;
850 p->crtc_vtotal *= 2;
851 }
Dave Airlief453ba02008-11-07 14:05:41 -0800852 }
853
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800854 if (!(adjust_flags & CRTC_NO_VSCAN)) {
855 if (p->vscan > 1) {
856 p->crtc_vdisplay *= p->vscan;
857 p->crtc_vsync_start *= p->vscan;
858 p->crtc_vsync_end *= p->vscan;
859 p->crtc_vtotal *= p->vscan;
860 }
Dave Airlief453ba02008-11-07 14:05:41 -0800861 }
862
Damien Lespiau448cce22013-09-25 16:45:35 +0100863 if (adjust_flags & CRTC_STEREO_DOUBLE) {
864 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
865
866 switch (layout) {
867 case DRM_MODE_FLAG_3D_FRAME_PACKING:
868 p->crtc_clock *= 2;
869 p->crtc_vdisplay += p->crtc_vtotal;
870 p->crtc_vsync_start += p->crtc_vtotal;
871 p->crtc_vsync_end += p->crtc_vtotal;
872 p->crtc_vtotal += p->crtc_vtotal;
873 break;
874 }
875 }
876
Dave Airlief453ba02008-11-07 14:05:41 -0800877 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
878 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
879 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
880 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
Dave Airlief453ba02008-11-07 14:05:41 -0800881}
882EXPORT_SYMBOL(drm_mode_set_crtcinfo);
883
Dave Airlief453ba02008-11-07 14:05:41 -0800884/**
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200885 * drm_mode_copy - copy the mode
886 * @dst: mode to overwrite
887 * @src: mode to copy
888 *
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000889 * Copy an existing mode into another mode, preserving the object id and
890 * list head of the destination mode.
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200891 */
892void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
893{
894 int id = dst->base.id;
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000895 struct list_head head = dst->head;
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200896
897 *dst = *src;
898 dst->base.id = id;
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000899 dst->head = head;
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200900}
901EXPORT_SYMBOL(drm_mode_copy);
902
903/**
Dave Airlief453ba02008-11-07 14:05:41 -0800904 * drm_mode_duplicate - allocate and duplicate an existing mode
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100905 * @dev: drm_device to allocate the duplicated mode for
906 * @mode: mode to duplicate
Dave Airlief453ba02008-11-07 14:05:41 -0800907 *
Dave Airlief453ba02008-11-07 14:05:41 -0800908 * Just allocate a new mode, copy the existing mode into it, and return
909 * a pointer to it. Used to create new instances of established modes.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100910 *
911 * Returns:
912 * Pointer to duplicated mode on success, NULL on error.
Dave Airlief453ba02008-11-07 14:05:41 -0800913 */
914struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000915 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800916{
917 struct drm_display_mode *nmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800918
919 nmode = drm_mode_create(dev);
920 if (!nmode)
921 return NULL;
922
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200923 drm_mode_copy(nmode, mode);
924
Dave Airlief453ba02008-11-07 14:05:41 -0800925 return nmode;
926}
927EXPORT_SYMBOL(drm_mode_duplicate);
928
929/**
930 * drm_mode_equal - test modes for equality
931 * @mode1: first mode
932 * @mode2: second mode
933 *
Dave Airlief453ba02008-11-07 14:05:41 -0800934 * Check to see if @mode1 and @mode2 are equivalent.
935 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100936 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -0800937 * True if the modes are equal, false otherwise.
938 */
Ville Syrjälä0b3904a2012-10-25 18:05:05 +0000939bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
Dave Airlief453ba02008-11-07 14:05:41 -0800940{
Daniel Stone54270952015-03-19 04:33:02 +0000941 if (!mode1 && !mode2)
942 return true;
943
944 if (!mode1 || !mode2)
945 return false;
946
Dave Airlief453ba02008-11-07 14:05:41 -0800947 /* do clock check convert to PICOS so fb modes get matched
948 * the same */
949 if (mode1->clock && mode2->clock) {
950 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
951 return false;
952 } else if (mode1->clock != mode2->clock)
953 return false;
954
Ville Syrjälä4c6bcf42015-11-16 21:05:12 +0200955 return drm_mode_equal_no_clocks(mode1, mode2);
956}
957EXPORT_SYMBOL(drm_mode_equal);
958
959/**
960 * drm_mode_equal_no_clocks - test modes for equality
961 * @mode1: first mode
962 * @mode2: second mode
963 *
964 * Check to see if @mode1 and @mode2 are equivalent, but
965 * don't check the pixel clocks.
966 *
967 * Returns:
968 * True if the modes are equal, false otherwise.
969 */
970bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
971{
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +0100972 if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
973 (mode2->flags & DRM_MODE_FLAG_3D_MASK))
974 return false;
975
976 return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300977}
Ville Syrjälä4c6bcf42015-11-16 21:05:12 +0200978EXPORT_SYMBOL(drm_mode_equal_no_clocks);
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300979
980/**
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +0100981 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300982 * @mode1: first mode
983 * @mode2: second mode
984 *
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300985 * Check to see if @mode1 and @mode2 are equivalent, but
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +0100986 * don't check the pixel clocks nor the stereo layout.
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300987 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100988 * Returns:
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300989 * True if the modes are equal, false otherwise.
990 */
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +0100991bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
992 const struct drm_display_mode *mode2)
Ville Syrjälä8cc3f232013-04-24 19:07:16 +0300993{
Dave Airlief453ba02008-11-07 14:05:41 -0800994 if (mode1->hdisplay == mode2->hdisplay &&
995 mode1->hsync_start == mode2->hsync_start &&
996 mode1->hsync_end == mode2->hsync_end &&
997 mode1->htotal == mode2->htotal &&
998 mode1->hskew == mode2->hskew &&
999 mode1->vdisplay == mode2->vdisplay &&
1000 mode1->vsync_start == mode2->vsync_start &&
1001 mode1->vsync_end == mode2->vsync_end &&
1002 mode1->vtotal == mode2->vtotal &&
1003 mode1->vscan == mode2->vscan &&
Shashank Sharma6dffd432016-10-17 17:34:38 +05301004 mode1->picture_aspect_ratio == mode2->picture_aspect_ratio &&
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001005 (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
1006 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
Dave Airlief453ba02008-11-07 14:05:41 -08001007 return true;
1008
1009 return false;
1010}
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001011EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
Dave Airlief453ba02008-11-07 14:05:41 -08001012
1013/**
Ville Syrjäläabc0b142014-12-17 13:56:23 +02001014 * drm_mode_validate_basic - make sure the mode is somewhat sane
1015 * @mode: mode to check
1016 *
1017 * Check that the mode timings are at least somewhat reasonable.
1018 * Any hardware specific limits are left up for each driver to check.
1019 *
1020 * Returns:
1021 * The mode status
1022 */
1023enum drm_mode_status
1024drm_mode_validate_basic(const struct drm_display_mode *mode)
1025{
1026 if (mode->clock == 0)
1027 return MODE_CLOCK_LOW;
1028
1029 if (mode->hdisplay == 0 ||
1030 mode->hsync_start < mode->hdisplay ||
1031 mode->hsync_end < mode->hsync_start ||
1032 mode->htotal < mode->hsync_end)
1033 return MODE_H_ILLEGAL;
1034
1035 if (mode->vdisplay == 0 ||
1036 mode->vsync_start < mode->vdisplay ||
1037 mode->vsync_end < mode->vsync_start ||
1038 mode->vtotal < mode->vsync_end)
1039 return MODE_V_ILLEGAL;
1040
1041 return MODE_OK;
1042}
1043EXPORT_SYMBOL(drm_mode_validate_basic);
1044
1045/**
Dave Airlief453ba02008-11-07 14:05:41 -08001046 * drm_mode_validate_size - make sure modes adhere to size constraints
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001047 * @mode: mode to check
Dave Airlief453ba02008-11-07 14:05:41 -08001048 * @maxX: maximum width
1049 * @maxY: maximum height
Dave Airlief453ba02008-11-07 14:05:41 -08001050 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001051 * This function is a helper which can be used to validate modes against size
1052 * limitations of the DRM device/connector. If a mode is too big its status
Masanari Iida32197aa2014-10-20 23:53:13 +09001053 * member is updated with the appropriate validation failure code. The list
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001054 * itself is not changed.
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001055 *
1056 * Returns:
1057 * The mode status
Dave Airlief453ba02008-11-07 14:05:41 -08001058 */
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001059enum drm_mode_status
1060drm_mode_validate_size(const struct drm_display_mode *mode,
1061 int maxX, int maxY)
Dave Airlief453ba02008-11-07 14:05:41 -08001062{
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001063 if (maxX > 0 && mode->hdisplay > maxX)
1064 return MODE_VIRTUAL_X;
Dave Airlief453ba02008-11-07 14:05:41 -08001065
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001066 if (maxY > 0 && mode->vdisplay > maxY)
1067 return MODE_VIRTUAL_Y;
Dave Airlief453ba02008-11-07 14:05:41 -08001068
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001069 return MODE_OK;
Dave Airlief453ba02008-11-07 14:05:41 -08001070}
1071EXPORT_SYMBOL(drm_mode_validate_size);
1072
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001073#define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1074
1075static const char * const drm_mode_status_names[] = {
1076 MODE_STATUS(OK),
1077 MODE_STATUS(HSYNC),
1078 MODE_STATUS(VSYNC),
1079 MODE_STATUS(H_ILLEGAL),
1080 MODE_STATUS(V_ILLEGAL),
1081 MODE_STATUS(BAD_WIDTH),
1082 MODE_STATUS(NOMODE),
1083 MODE_STATUS(NO_INTERLACE),
1084 MODE_STATUS(NO_DBLESCAN),
1085 MODE_STATUS(NO_VSCAN),
1086 MODE_STATUS(MEM),
1087 MODE_STATUS(VIRTUAL_X),
1088 MODE_STATUS(VIRTUAL_Y),
1089 MODE_STATUS(MEM_VIRT),
1090 MODE_STATUS(NOCLOCK),
1091 MODE_STATUS(CLOCK_HIGH),
1092 MODE_STATUS(CLOCK_LOW),
1093 MODE_STATUS(CLOCK_RANGE),
1094 MODE_STATUS(BAD_HVALUE),
1095 MODE_STATUS(BAD_VVALUE),
1096 MODE_STATUS(BAD_VSCAN),
1097 MODE_STATUS(HSYNC_NARROW),
1098 MODE_STATUS(HSYNC_WIDE),
1099 MODE_STATUS(HBLANK_NARROW),
1100 MODE_STATUS(HBLANK_WIDE),
1101 MODE_STATUS(VSYNC_NARROW),
1102 MODE_STATUS(VSYNC_WIDE),
1103 MODE_STATUS(VBLANK_NARROW),
1104 MODE_STATUS(VBLANK_WIDE),
1105 MODE_STATUS(PANEL),
1106 MODE_STATUS(INTERLACE_WIDTH),
1107 MODE_STATUS(ONE_WIDTH),
1108 MODE_STATUS(ONE_HEIGHT),
1109 MODE_STATUS(ONE_SIZE),
1110 MODE_STATUS(NO_REDUCED),
1111 MODE_STATUS(NO_STEREO),
Ville Syrjälä5ba89402015-12-10 22:39:08 +02001112 MODE_STATUS(STALE),
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001113 MODE_STATUS(BAD),
1114 MODE_STATUS(ERROR),
1115};
1116
1117#undef MODE_STATUS
1118
1119static const char *drm_get_mode_status_name(enum drm_mode_status status)
1120{
1121 int index = status + 3;
1122
1123 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1124 return "";
1125
1126 return drm_mode_status_names[index];
1127}
1128
Dave Airlief453ba02008-11-07 14:05:41 -08001129/**
Dave Airlief453ba02008-11-07 14:05:41 -08001130 * drm_mode_prune_invalid - remove invalid modes from mode list
1131 * @dev: DRM device
1132 * @mode_list: list of modes to check
1133 * @verbose: be verbose about it
1134 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001135 * This helper function can be used to prune a display mode list after
1136 * validation has been completed. All modes who's status is not MODE_OK will be
1137 * removed from the list, and if @verbose the status code and mode name is also
1138 * printed to dmesg.
Dave Airlief453ba02008-11-07 14:05:41 -08001139 */
1140void drm_mode_prune_invalid(struct drm_device *dev,
1141 struct list_head *mode_list, bool verbose)
1142{
1143 struct drm_display_mode *mode, *t;
1144
1145 list_for_each_entry_safe(mode, t, mode_list, head) {
1146 if (mode->status != MODE_OK) {
1147 list_del(&mode->head);
1148 if (verbose) {
1149 drm_mode_debug_printmodeline(mode);
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001150 DRM_DEBUG_KMS("Not using %s mode: %s\n",
1151 mode->name,
1152 drm_get_mode_status_name(mode->status));
Dave Airlief453ba02008-11-07 14:05:41 -08001153 }
1154 drm_mode_destroy(dev, mode);
1155 }
1156 }
1157}
1158EXPORT_SYMBOL(drm_mode_prune_invalid);
1159
1160/**
1161 * drm_mode_compare - compare modes for favorability
Dave Chinner2c761272010-01-12 17:39:16 +11001162 * @priv: unused
Dave Airlief453ba02008-11-07 14:05:41 -08001163 * @lh_a: list_head for first mode
1164 * @lh_b: list_head for second mode
1165 *
Dave Airlief453ba02008-11-07 14:05:41 -08001166 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1167 * which is better.
1168 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001169 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001170 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1171 * positive if @lh_b is better than @lh_a.
1172 */
Dave Chinner2c761272010-01-12 17:39:16 +11001173static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
Dave Airlief453ba02008-11-07 14:05:41 -08001174{
1175 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1176 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1177 int diff;
1178
1179 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1180 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1181 if (diff)
1182 return diff;
1183 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1184 if (diff)
1185 return diff;
Ville Syrjälä9bc3cd52013-05-31 12:17:08 +00001186
1187 diff = b->vrefresh - a->vrefresh;
1188 if (diff)
1189 return diff;
1190
Dave Airlief453ba02008-11-07 14:05:41 -08001191 diff = b->clock - a->clock;
1192 return diff;
1193}
1194
Dave Airlief453ba02008-11-07 14:05:41 -08001195/**
1196 * drm_mode_sort - sort mode list
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001197 * @mode_list: list of drm_display_mode structures to sort
Dave Airlief453ba02008-11-07 14:05:41 -08001198 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001199 * Sort @mode_list by favorability, moving good modes to the head of the list.
Dave Airlief453ba02008-11-07 14:05:41 -08001200 */
1201void drm_mode_sort(struct list_head *mode_list)
1202{
Dave Chinner2c761272010-01-12 17:39:16 +11001203 list_sort(NULL, mode_list, drm_mode_compare);
Dave Airlief453ba02008-11-07 14:05:41 -08001204}
1205EXPORT_SYMBOL(drm_mode_sort);
1206
1207/**
1208 * drm_mode_connector_list_update - update the mode list for the connector
1209 * @connector: the connector to update
1210 *
Dave Airlief453ba02008-11-07 14:05:41 -08001211 * This moves the modes from the @connector probed_modes list
1212 * to the actual mode list. It compares the probed mode against the current
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001213 * list and only adds different/new modes.
1214 *
1215 * This is just a helper functions doesn't validate any modes itself and also
1216 * doesn't prune any invalid modes. Callers need to do that themselves.
Dave Airlief453ba02008-11-07 14:05:41 -08001217 */
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001218void drm_mode_connector_list_update(struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -08001219{
Dave Airlief453ba02008-11-07 14:05:41 -08001220 struct drm_display_mode *pmode, *pt;
Dave Airlief453ba02008-11-07 14:05:41 -08001221
Daniel Vetter63951382014-01-23 15:14:15 +01001222 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1223
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001224 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1225 struct drm_display_mode *mode;
1226 bool found_it = false;
1227
Dave Airlief453ba02008-11-07 14:05:41 -08001228 /* go through current modes checking for the new probed mode */
1229 list_for_each_entry(mode, &connector->modes, head) {
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001230 if (!drm_mode_equal(pmode, mode))
1231 continue;
1232
1233 found_it = true;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001234
1235 /*
1236 * If the old matching mode is stale (ie. left over
1237 * from a previous probe) just replace it outright.
1238 * Otherwise just merge the type bits between all
1239 * equal probed modes.
1240 *
1241 * If two probed modes are considered equal, pick the
1242 * actual timings from the one that's marked as
1243 * preferred (in case the match isn't 100%). If
1244 * multiple or zero preferred modes are present, favor
1245 * the mode added to the probed_modes list first.
1246 */
1247 if (mode->status == MODE_STALE) {
1248 drm_mode_copy(mode, pmode);
1249 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1250 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001251 pmode->type |= mode->type;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001252 drm_mode_copy(mode, pmode);
1253 } else {
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001254 mode->type |= pmode->type;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001255 }
1256
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001257 list_del(&pmode->head);
1258 drm_mode_destroy(connector->dev, pmode);
1259 break;
Dave Airlief453ba02008-11-07 14:05:41 -08001260 }
1261
1262 if (!found_it) {
1263 list_move_tail(&pmode->head, &connector->modes);
1264 }
1265 }
1266}
1267EXPORT_SYMBOL(drm_mode_connector_list_update);
Chris Wilson1794d252011-04-17 07:43:32 +01001268
1269/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001270 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1271 * @mode_option: optional per connector mode option
1272 * @connector: connector to parse modeline for
1273 * @mode: preallocated drm_cmdline_mode structure to fill out
Chris Wilson1794d252011-04-17 07:43:32 +01001274 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001275 * This parses @mode_option command line modeline for modes and options to
1276 * configure the connector. If @mode_option is NULL the default command line
1277 * modeline in fb_mode_option will be parsed instead.
Chris Wilson1794d252011-04-17 07:43:32 +01001278 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001279 * This uses the same parameters as the fb modedb.c, except for an extra
1280 * force-enable, force-enable-digital and force-disable bit at the end:
1281 *
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +01001282 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
Chris Wilson1794d252011-04-17 07:43:32 +01001283 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001284 * The intermediate drm_cmdline_mode structure is required to store additional
Yannick Guerrini2a97acd2015-03-04 09:30:09 +01001285 * options from the command line modline like the force-enable/disable flag.
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001286 *
1287 * Returns:
1288 * True if a valid modeline has been parsed, false otherwise.
Chris Wilson1794d252011-04-17 07:43:32 +01001289 */
1290bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1291 struct drm_connector *connector,
1292 struct drm_cmdline_mode *mode)
1293{
1294 const char *name;
1295 unsigned int namelen;
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001296 bool res_specified = false, bpp_specified = false, refresh_specified = false;
Chris Wilson1794d252011-04-17 07:43:32 +01001297 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001298 bool yres_specified = false, cvt = false, rb = false;
1299 bool interlace = false, margins = false, was_digit = false;
LABBE Corentind6e6e142015-12-11 13:58:59 +01001300 int i;
Chris Wilson1794d252011-04-17 07:43:32 +01001301 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
1302
Dave Airliecb3c4382011-05-04 13:08:58 +10001303#ifdef CONFIG_FB
Chris Wilson1794d252011-04-17 07:43:32 +01001304 if (!mode_option)
1305 mode_option = fb_mode_option;
Dave Airliecb3c4382011-05-04 13:08:58 +10001306#endif
Chris Wilson1794d252011-04-17 07:43:32 +01001307
1308 if (!mode_option) {
1309 mode->specified = false;
1310 return false;
1311 }
1312
1313 name = mode_option;
1314 namelen = strlen(name);
1315 for (i = namelen-1; i >= 0; i--) {
1316 switch (name[i]) {
1317 case '@':
Chris Wilson1794d252011-04-17 07:43:32 +01001318 if (!refresh_specified && !bpp_specified &&
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001319 !yres_specified && !cvt && !rb && was_digit) {
LABBE Corentind6e6e142015-12-11 13:58:59 +01001320 refresh = simple_strtol(&name[i+1], NULL, 10);
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001321 refresh_specified = true;
1322 was_digit = false;
Chris Wilson1794d252011-04-17 07:43:32 +01001323 } else
1324 goto done;
1325 break;
1326 case '-':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001327 if (!bpp_specified && !yres_specified && !cvt &&
1328 !rb && was_digit) {
LABBE Corentind6e6e142015-12-11 13:58:59 +01001329 bpp = simple_strtol(&name[i+1], NULL, 10);
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001330 bpp_specified = true;
1331 was_digit = false;
Chris Wilson1794d252011-04-17 07:43:32 +01001332 } else
1333 goto done;
1334 break;
1335 case 'x':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001336 if (!yres_specified && was_digit) {
LABBE Corentind6e6e142015-12-11 13:58:59 +01001337 yres = simple_strtol(&name[i+1], NULL, 10);
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001338 yres_specified = true;
1339 was_digit = false;
Chris Wilson1794d252011-04-17 07:43:32 +01001340 } else
1341 goto done;
Damien Lespiau97fbfbf2013-05-08 17:03:30 +01001342 break;
Chris Wilson1794d252011-04-17 07:43:32 +01001343 case '0' ... '9':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001344 was_digit = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001345 break;
1346 case 'M':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001347 if (yres_specified || cvt || was_digit)
1348 goto done;
1349 cvt = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001350 break;
1351 case 'R':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001352 if (yres_specified || cvt || rb || was_digit)
1353 goto done;
1354 rb = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001355 break;
1356 case 'm':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001357 if (cvt || yres_specified || was_digit)
1358 goto done;
1359 margins = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001360 break;
1361 case 'i':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001362 if (cvt || yres_specified || was_digit)
1363 goto done;
1364 interlace = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001365 break;
1366 case 'e':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001367 if (yres_specified || bpp_specified || refresh_specified ||
1368 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1369 goto done;
1370
Chris Wilson1794d252011-04-17 07:43:32 +01001371 force = DRM_FORCE_ON;
1372 break;
1373 case 'D':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001374 if (yres_specified || bpp_specified || refresh_specified ||
1375 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1376 goto done;
1377
Chris Wilson1794d252011-04-17 07:43:32 +01001378 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1379 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1380 force = DRM_FORCE_ON;
1381 else
1382 force = DRM_FORCE_ON_DIGITAL;
1383 break;
1384 case 'd':
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001385 if (yres_specified || bpp_specified || refresh_specified ||
1386 was_digit || (force != DRM_FORCE_UNSPECIFIED))
1387 goto done;
1388
Chris Wilson1794d252011-04-17 07:43:32 +01001389 force = DRM_FORCE_OFF;
1390 break;
1391 default:
1392 goto done;
1393 }
1394 }
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001395
Chris Wilson1794d252011-04-17 07:43:32 +01001396 if (i < 0 && yres_specified) {
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001397 char *ch;
1398 xres = simple_strtol(name, &ch, 10);
1399 if ((ch != NULL) && (*ch == 'x'))
1400 res_specified = true;
1401 else
1402 i = ch - name;
1403 } else if (!yres_specified && was_digit) {
1404 /* catch mode that begins with digits but has no 'x' */
1405 i = 0;
Chris Wilson1794d252011-04-17 07:43:32 +01001406 }
1407done:
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001408 if (i >= 0) {
LABBE Corentin67fe85d2016-02-04 15:03:52 +01001409 pr_warn("[drm] parse error at position %i in video mode '%s'\n",
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001410 i, name);
1411 mode->specified = false;
1412 return false;
1413 }
1414
Chris Wilson1794d252011-04-17 07:43:32 +01001415 if (res_specified) {
1416 mode->specified = true;
1417 mode->xres = xres;
1418 mode->yres = yres;
1419 }
1420
1421 if (refresh_specified) {
1422 mode->refresh_specified = true;
1423 mode->refresh = refresh;
1424 }
1425
1426 if (bpp_specified) {
1427 mode->bpp_specified = true;
1428 mode->bpp = bpp;
1429 }
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001430 mode->rb = rb;
1431 mode->cvt = cvt;
1432 mode->interlace = interlace;
1433 mode->margins = margins;
Chris Wilson1794d252011-04-17 07:43:32 +01001434 mode->force = force;
1435
1436 return true;
1437}
1438EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1439
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001440/**
1441 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1442 * @dev: DRM device to create the new mode for
1443 * @cmd: input command line modeline
1444 *
1445 * Returns:
1446 * Pointer to converted mode on success, NULL on error.
1447 */
Chris Wilson1794d252011-04-17 07:43:32 +01001448struct drm_display_mode *
1449drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1450 struct drm_cmdline_mode *cmd)
1451{
1452 struct drm_display_mode *mode;
1453
1454 if (cmd->cvt)
1455 mode = drm_cvt_mode(dev,
1456 cmd->xres, cmd->yres,
1457 cmd->refresh_specified ? cmd->refresh : 60,
1458 cmd->rb, cmd->interlace,
1459 cmd->margins);
1460 else
1461 mode = drm_gtf_mode(dev,
1462 cmd->xres, cmd->yres,
1463 cmd->refresh_specified ? cmd->refresh : 60,
1464 cmd->interlace,
1465 cmd->margins);
1466 if (!mode)
1467 return NULL;
1468
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001469 mode->type |= DRM_MODE_TYPE_USERDEF;
Chris Wilson1794d252011-04-17 07:43:32 +01001470 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1471 return mode;
1472}
1473EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
Daniel Stone934a8a82015-05-22 13:34:48 +01001474
1475/**
1476 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1477 * @out: drm_mode_modeinfo struct to return to the user
1478 * @in: drm_display_mode to use
1479 *
1480 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1481 * the user.
1482 */
1483void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1484 const struct drm_display_mode *in)
1485{
1486 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1487 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1488 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1489 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1490 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1491 "timing values too large for mode info\n");
1492
1493 out->clock = in->clock;
1494 out->hdisplay = in->hdisplay;
1495 out->hsync_start = in->hsync_start;
1496 out->hsync_end = in->hsync_end;
1497 out->htotal = in->htotal;
1498 out->hskew = in->hskew;
1499 out->vdisplay = in->vdisplay;
1500 out->vsync_start = in->vsync_start;
1501 out->vsync_end = in->vsync_end;
1502 out->vtotal = in->vtotal;
1503 out->vscan = in->vscan;
1504 out->vrefresh = in->vrefresh;
1505 out->flags = in->flags;
1506 out->type = in->type;
Shashank Sharma6dffd432016-10-17 17:34:38 +05301507 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
1508
1509 switch (in->picture_aspect_ratio) {
1510 case HDMI_PICTURE_ASPECT_4_3:
1511 out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
1512 break;
1513 case HDMI_PICTURE_ASPECT_16_9:
1514 out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
1515 break;
Shashank Sharmaa68362f2016-10-17 17:34:40 +05301516 case HDMI_PICTURE_ASPECT_64_27:
1517 out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
1518 break;
1519 case DRM_MODE_PICTURE_ASPECT_256_135:
1520 out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
1521 break;
Shashank Sharma6dffd432016-10-17 17:34:38 +05301522 case HDMI_PICTURE_ASPECT_RESERVED:
1523 default:
1524 out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
1525 break;
1526 }
1527
Daniel Stone934a8a82015-05-22 13:34:48 +01001528 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1529 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1530}
1531
1532/**
1533 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1534 * @out: drm_display_mode to return to the user
1535 * @in: drm_mode_modeinfo to use
1536 *
1537 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1538 * the caller.
1539 *
1540 * Returns:
1541 * Zero on success, negative errno on failure.
1542 */
1543int drm_mode_convert_umode(struct drm_display_mode *out,
1544 const struct drm_mode_modeinfo *in)
1545{
1546 int ret = -EINVAL;
1547
1548 if (in->clock > INT_MAX || in->vrefresh > INT_MAX) {
1549 ret = -ERANGE;
1550 goto out;
1551 }
1552
1553 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1554 goto out;
1555
1556 out->clock = in->clock;
1557 out->hdisplay = in->hdisplay;
1558 out->hsync_start = in->hsync_start;
1559 out->hsync_end = in->hsync_end;
1560 out->htotal = in->htotal;
1561 out->hskew = in->hskew;
1562 out->vdisplay = in->vdisplay;
1563 out->vsync_start = in->vsync_start;
1564 out->vsync_end = in->vsync_end;
1565 out->vtotal = in->vtotal;
1566 out->vscan = in->vscan;
1567 out->vrefresh = in->vrefresh;
1568 out->flags = in->flags;
1569 out->type = in->type;
1570 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1571 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1572
Shashank Sharma6dffd432016-10-17 17:34:38 +05301573 /* Clearing picture aspect ratio bits from out flags */
1574 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
1575
1576 switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
1577 case DRM_MODE_FLAG_PIC_AR_4_3:
1578 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_4_3;
1579 break;
1580 case DRM_MODE_FLAG_PIC_AR_16_9:
1581 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_16_9;
1582 break;
Shashank Sharmaa68362f2016-10-17 17:34:40 +05301583 case DRM_MODE_FLAG_PIC_AR_64_27:
1584 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_64_27;
1585 break;
1586 case DRM_MODE_FLAG_PIC_AR_256_135:
1587 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_256_135;
1588 break;
Shashank Sharma6dffd432016-10-17 17:34:38 +05301589 default:
1590 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
1591 break;
1592 }
1593
Daniel Stone934a8a82015-05-22 13:34:48 +01001594 out->status = drm_mode_validate_basic(out);
1595 if (out->status != MODE_OK)
1596 goto out;
1597
Tomi Valkeinenb201e742016-05-31 15:03:15 +03001598 drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
1599
Daniel Stone934a8a82015-05-22 13:34:48 +01001600 ret = 0;
1601
1602out:
1603 return ret;
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +01001604}