blob: 57e6408288c80f6adb86c2b29b23a1b0e1829627 [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
Maxime Riparde08ab742019-06-19 12:17:49 +020033#include <linux/ctype.h>
Dave Airlief453ba02008-11-07 14:05:41 -080034#include <linux/list.h>
Dave Chinner2c761272010-01-12 17:39:16 +110035#include <linux/list_sort.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040036#include <linux/export.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020037
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>
Sam Ravnborg0500c042019-05-26 19:35:35 +020040
41#include <drm/drm_crtc.h>
42#include <drm/drm_device.h>
Daniel Vetter55310002014-01-23 15:52:20 +010043#include <drm/drm_modes.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020044#include <drm/drm_print.h>
Dave Airlief453ba02008-11-07 14:05:41 -080045
Daniel Vetter8bd441b2014-01-23 15:35:24 +010046#include "drm_crtc_internal.h"
47
Dave Airlief453ba02008-11-07 14:05:41 -080048/**
Daniel Vetter3ec0db82014-01-23 15:06:15 +010049 * drm_mode_debug_printmodeline - print a mode to dmesg
Dave Airlief453ba02008-11-07 14:05:41 -080050 * @mode: mode to print
51 *
Dave Airlief453ba02008-11-07 14:05:41 -080052 * Describe @mode using DRM_DEBUG.
53 */
Ville Syrjälä0b3904a2012-10-25 18:05:05 +000054void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -080055{
Rob Clark65c7dc12016-11-05 11:08:06 -040056 DRM_DEBUG_KMS("Modeline " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
Dave Airlief453ba02008-11-07 14:05:41 -080057}
58EXPORT_SYMBOL(drm_mode_debug_printmodeline);
59
60/**
Daniel Vetter8bd441b2014-01-23 15:35:24 +010061 * drm_mode_create - create a new display mode
62 * @dev: DRM device
63 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010064 * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
65 * and return it.
Daniel Vetter8bd441b2014-01-23 15:35:24 +010066 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010067 * Returns:
Daniel Vetter8bd441b2014-01-23 15:35:24 +010068 * Pointer to new mode on success, NULL on error.
69 */
70struct drm_display_mode *drm_mode_create(struct drm_device *dev)
71{
72 struct drm_display_mode *nmode;
73
74 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
75 if (!nmode)
76 return NULL;
77
Daniel Vetter8bd441b2014-01-23 15:35:24 +010078 return nmode;
79}
80EXPORT_SYMBOL(drm_mode_create);
81
82/**
83 * drm_mode_destroy - remove a mode
84 * @dev: DRM device
85 * @mode: mode to remove
86 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +010087 * Release @mode's unique ID, then free it @mode structure itself using kfree.
Daniel Vetter8bd441b2014-01-23 15:35:24 +010088 */
89void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
90{
91 if (!mode)
92 return;
93
Daniel Vetter8bd441b2014-01-23 15:35:24 +010094 kfree(mode);
95}
96EXPORT_SYMBOL(drm_mode_destroy);
97
98/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +010099 * drm_mode_probed_add - add a mode to a connector's probed_mode list
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100100 * @connector: connector the new mode
101 * @mode: mode data
102 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100103 * Add @mode to @connector's probed_mode list for later use. This list should
104 * then in a second step get filtered and all the modes actually supported by
105 * the hardware moved to the @connector's modes list.
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100106 */
107void drm_mode_probed_add(struct drm_connector *connector,
108 struct drm_display_mode *mode)
109{
110 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
111
112 list_add_tail(&mode->head, &connector->probed_modes);
113}
114EXPORT_SYMBOL(drm_mode_probed_add);
115
116/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100117 * drm_cvt_mode -create a modeline based on the CVT algorithm
118 * @dev: drm device
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800119 * @hdisplay: hdisplay size
120 * @vdisplay: vdisplay size
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100121 * @vrefresh: vrefresh rate
122 * @reduced: whether to use reduced blanking
123 * @interlaced: whether to compute an interlaced mode
124 * @margins: whether to add margins (borders)
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800125 *
126 * This function is called to generate the modeline based on CVT algorithm
127 * according to the hdisplay, vdisplay, vrefresh.
128 * It is based from the VESA(TM) Coordinated Video Timing Generator by
129 * Graham Loveridge April 9, 2003 available at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200130 * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800131 *
132 * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
133 * What I have done is to translate it by using integer calculation.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100134 *
135 * Returns:
136 * The modeline based on the CVT algorithm stored in a drm_display_mode object.
137 * The display mode object is allocated with drm_mode_create(). Returns NULL
138 * when no mode could be allocated.
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800139 */
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800140struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
141 int vdisplay, int vrefresh,
Dave Airlied50ba252009-09-23 14:44:08 +1000142 bool reduced, bool interlaced, bool margins)
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800143{
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100144#define HV_FACTOR 1000
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800145 /* 1) top/bottom margin size (% of height) - default: 1.8, */
146#define CVT_MARGIN_PERCENTAGE 18
147 /* 2) character cell horizontal granularity (pixels) - default 8 */
148#define CVT_H_GRANULARITY 8
149 /* 3) Minimum vertical porch (lines) - default 3 */
150#define CVT_MIN_V_PORCH 3
151 /* 4) Minimum number of vertical back porch lines - default 6 */
152#define CVT_MIN_V_BPORCH 6
153 /* Pixel Clock step (kHz) */
154#define CVT_CLOCK_STEP 250
155 struct drm_display_mode *drm_mode;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800156 unsigned int vfieldrate, hperiod;
157 int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
158 int interlace;
Chris Wilson8a5bbf32016-10-21 15:15:40 +0100159 u64 tmp;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800160
161 /* allocate the drm_display_mode structure. If failure, we will
162 * return directly
163 */
164 drm_mode = drm_mode_create(dev);
165 if (!drm_mode)
166 return NULL;
167
168 /* the CVT default refresh rate is 60Hz */
169 if (!vrefresh)
170 vrefresh = 60;
171
172 /* the required field fresh rate */
173 if (interlaced)
174 vfieldrate = vrefresh * 2;
175 else
176 vfieldrate = vrefresh;
177
178 /* horizontal pixels */
179 hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
180
181 /* determine the left&right borders */
182 hmargin = 0;
183 if (margins) {
184 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
185 hmargin -= hmargin % CVT_H_GRANULARITY;
186 }
187 /* find the total active pixels */
188 drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
189
190 /* find the number of lines per field */
191 if (interlaced)
192 vdisplay_rnd = vdisplay / 2;
193 else
194 vdisplay_rnd = vdisplay;
195
196 /* find the top & bottom borders */
197 vmargin = 0;
198 if (margins)
199 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
200
Francisco Jerez841b4112009-08-12 02:30:09 +0200201 drm_mode->vdisplay = vdisplay + 2 * vmargin;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800202
203 /* Interlaced */
204 if (interlaced)
205 interlace = 1;
206 else
207 interlace = 0;
208
209 /* Determine VSync Width from aspect ratio */
210 if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
211 vsync = 4;
212 else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
213 vsync = 5;
214 else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
215 vsync = 6;
216 else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
217 vsync = 7;
218 else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
219 vsync = 7;
220 else /* custom */
221 vsync = 10;
222
223 if (!reduced) {
224 /* simplify the GTF calculation */
225 /* 4) Minimum time of vertical sync + back porch interval (µs)
226 * default 550.0
227 */
228 int tmp1, tmp2;
229#define CVT_MIN_VSYNC_BP 550
230 /* 3) Nominal HSync width (% of line period) - default 8 */
231#define CVT_HSYNC_PERCENTAGE 8
232 unsigned int hblank_percentage;
233 int vsyncandback_porch, vback_porch, hblank;
234
235 /* estimated the horizontal period */
236 tmp1 = HV_FACTOR * 1000000 -
237 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
238 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
239 interlace;
240 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
241
242 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
243 /* 9. Find number of lines in sync + backporch */
244 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
245 vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
246 else
247 vsyncandback_porch = tmp1;
248 /* 10. Find number of lines in back porch */
249 vback_porch = vsyncandback_porch - vsync;
250 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
251 vsyncandback_porch + CVT_MIN_V_PORCH;
252 /* 5) Definition of Horizontal blanking time limitation */
253 /* Gradient (%/kHz) - default 600 */
254#define CVT_M_FACTOR 600
255 /* Offset (%) - default 40 */
256#define CVT_C_FACTOR 40
257 /* Blanking time scaling factor - default 128 */
258#define CVT_K_FACTOR 128
259 /* Scaling factor weighting - default 20 */
260#define CVT_J_FACTOR 20
261#define CVT_M_PRIME (CVT_M_FACTOR * CVT_K_FACTOR / 256)
262#define CVT_C_PRIME ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
263 CVT_J_FACTOR)
264 /* 12. Find ideal blanking duty cycle from formula */
265 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
266 hperiod / 1000;
267 /* 13. Blanking time */
268 if (hblank_percentage < 20 * HV_FACTOR)
269 hblank_percentage = 20 * HV_FACTOR;
270 hblank = drm_mode->hdisplay * hblank_percentage /
271 (100 * HV_FACTOR - hblank_percentage);
272 hblank -= hblank % (2 * CVT_H_GRANULARITY);
Yannick Guerrini2a97acd2015-03-04 09:30:09 +0100273 /* 14. find the total pixels per line */
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800274 drm_mode->htotal = drm_mode->hdisplay + hblank;
275 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
276 drm_mode->hsync_start = drm_mode->hsync_end -
277 (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
278 drm_mode->hsync_start += CVT_H_GRANULARITY -
279 drm_mode->hsync_start % CVT_H_GRANULARITY;
280 /* fill the Vsync values */
281 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
282 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
283 } else {
284 /* Reduced blanking */
285 /* Minimum vertical blanking interval time (µs)- default 460 */
286#define CVT_RB_MIN_VBLANK 460
287 /* Fixed number of clocks for horizontal sync */
288#define CVT_RB_H_SYNC 32
289 /* Fixed number of clocks for horizontal blanking */
290#define CVT_RB_H_BLANK 160
291 /* Fixed number of lines for vertical front porch - default 3*/
292#define CVT_RB_VFPORCH 3
293 int vbilines;
294 int tmp1, tmp2;
295 /* 8. Estimate Horizontal period. */
296 tmp1 = HV_FACTOR * 1000000 -
297 CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
298 tmp2 = vdisplay_rnd + 2 * vmargin;
299 hperiod = tmp1 / (tmp2 * vfieldrate);
300 /* 9. Find number of lines in vertical blanking */
301 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
302 /* 10. Check if vertical blanking is sufficient */
303 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
304 vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
305 /* 11. Find total number of lines in vertical field */
306 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
307 /* 12. Find total number of pixels in a line */
308 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
309 /* Fill in HSync values */
310 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
Adam Jacksonadde0f22010-08-23 10:19:14 -0400311 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
312 /* Fill in VSync values */
313 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
314 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800315 }
316 /* 15/13. Find pixel clock frequency (kHz for xf86) */
Chris Wilson8a5bbf32016-10-21 15:15:40 +0100317 tmp = drm_mode->htotal; /* perform intermediate calcs in u64 */
318 tmp *= HV_FACTOR * 1000;
319 do_div(tmp, hperiod);
320 tmp -= drm_mode->clock % CVT_CLOCK_STEP;
321 drm_mode->clock = tmp;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800322 /* 18/16. Find actual vertical frame frequency */
323 /* ignore - just set the mode flag for interlaced */
Adam Jackson171fdd82010-03-29 21:43:31 +0000324 if (interlaced) {
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800325 drm_mode->vtotal *= 2;
Adam Jackson171fdd82010-03-29 21:43:31 +0000326 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
327 }
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800328 /* Fill the mode line name */
329 drm_mode_set_name(drm_mode);
330 if (reduced)
331 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
332 DRM_MODE_FLAG_NVSYNC);
333 else
334 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
335 DRM_MODE_FLAG_NHSYNC);
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800336
Adam Jackson171fdd82010-03-29 21:43:31 +0000337 return drm_mode;
Zhao Yakuid782c3f2009-06-22 13:17:08 +0800338}
339EXPORT_SYMBOL(drm_cvt_mode);
340
341/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100342 * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
343 * @dev: drm device
344 * @hdisplay: hdisplay size
345 * @vdisplay: vdisplay size
346 * @vrefresh: vrefresh rate.
347 * @interlaced: whether to compute an interlaced mode
348 * @margins: desired margin (borders) size
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100349 * @GTF_M: extended GTF formula parameters
350 * @GTF_2C: extended GTF formula parameters
351 * @GTF_K: extended GTF formula parameters
352 * @GTF_2J: extended GTF formula parameters
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800353 *
Adam Jackson7a374352010-03-29 21:43:30 +0000354 * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
355 * in here multiplied by two. For a C of 40, pass in 80.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100356 *
357 * Returns:
358 * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
359 * The display mode object is allocated with drm_mode_create(). Returns NULL
360 * when no mode could be allocated.
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800361 */
Adam Jackson7a374352010-03-29 21:43:30 +0000362struct drm_display_mode *
363drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
364 int vrefresh, bool interlaced, int margins,
365 int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
366{ /* 1) top/bottom margin size (% of height) - default: 1.8, */
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800367#define GTF_MARGIN_PERCENTAGE 18
368 /* 2) character cell horizontal granularity (pixels) - default 8 */
369#define GTF_CELL_GRAN 8
370 /* 3) Minimum vertical porch (lines) - default 3 */
371#define GTF_MIN_V_PORCH 1
372 /* width of vsync in lines */
373#define V_SYNC_RQD 3
374 /* width of hsync as % of total line */
375#define H_SYNC_PERCENT 8
376 /* min time of vsync + back porch (microsec) */
377#define MIN_VSYNC_PLUS_BP 550
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800378 /* C' and M' are part of the Blanking Duty Cycle computation */
Adam Jackson7a374352010-03-29 21:43:30 +0000379#define GTF_C_PRIME ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
380#define GTF_M_PRIME (GTF_K * GTF_M / 256)
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800381 struct drm_display_mode *drm_mode;
382 unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
383 int top_margin, bottom_margin;
384 int interlace;
385 unsigned int hfreq_est;
386 int vsync_plus_bp, vback_porch;
387 unsigned int vtotal_lines, vfieldrate_est, hperiod;
388 unsigned int vfield_rate, vframe_rate;
389 int left_margin, right_margin;
390 unsigned int total_active_pixels, ideal_duty_cycle;
391 unsigned int hblank, total_pixels, pixel_freq;
392 int hsync, hfront_porch, vodd_front_porch_lines;
393 unsigned int tmp1, tmp2;
394
395 drm_mode = drm_mode_create(dev);
396 if (!drm_mode)
397 return NULL;
398
399 /* 1. In order to give correct results, the number of horizontal
400 * pixels requested is first processed to ensure that it is divisible
401 * by the character size, by rounding it to the nearest character
402 * cell boundary:
403 */
404 hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
405 hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
406
407 /* 2. If interlace is requested, the number of vertical lines assumed
408 * by the calculation must be halved, as the computation calculates
409 * the number of vertical lines per field.
410 */
411 if (interlaced)
412 vdisplay_rnd = vdisplay / 2;
413 else
414 vdisplay_rnd = vdisplay;
415
416 /* 3. Find the frame rate required: */
417 if (interlaced)
418 vfieldrate_rqd = vrefresh * 2;
419 else
420 vfieldrate_rqd = vrefresh;
421
422 /* 4. Find number of lines in Top margin: */
423 top_margin = 0;
424 if (margins)
425 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
426 1000;
427 /* 5. Find number of lines in bottom margin: */
428 bottom_margin = top_margin;
429
430 /* 6. If interlace is required, then set variable interlace: */
431 if (interlaced)
432 interlace = 1;
433 else
434 interlace = 0;
435
436 /* 7. Estimate the Horizontal frequency */
437 {
438 tmp1 = (1000000 - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
439 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
440 2 + interlace;
441 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
442 }
443
444 /* 8. Find the number of lines in V sync + back porch */
445 /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
446 vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
447 vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
448 /* 9. Find the number of lines in V back porch alone: */
449 vback_porch = vsync_plus_bp - V_SYNC_RQD;
450 /* 10. Find the total number of lines in Vertical field period: */
451 vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
452 vsync_plus_bp + GTF_MIN_V_PORCH;
453 /* 11. Estimate the Vertical field frequency: */
454 vfieldrate_est = hfreq_est / vtotal_lines;
455 /* 12. Find the actual horizontal period: */
456 hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
457
458 /* 13. Find the actual Vertical field frequency: */
459 vfield_rate = hfreq_est / vtotal_lines;
460 /* 14. Find the Vertical frame frequency: */
461 if (interlaced)
462 vframe_rate = vfield_rate / 2;
463 else
464 vframe_rate = vfield_rate;
465 /* 15. Find number of pixels in left margin: */
466 if (margins)
467 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
468 1000;
469 else
470 left_margin = 0;
471
472 /* 16.Find number of pixels in right margin: */
473 right_margin = left_margin;
474 /* 17.Find total number of active pixels in image and left and right */
475 total_active_pixels = hdisplay_rnd + left_margin + right_margin;
476 /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
477 ideal_duty_cycle = GTF_C_PRIME * 1000 -
478 (GTF_M_PRIME * 1000000 / hfreq_est);
479 /* 19.Find the number of pixels in the blanking time to the nearest
480 * double character cell: */
481 hblank = total_active_pixels * ideal_duty_cycle /
482 (100000 - ideal_duty_cycle);
483 hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
484 hblank = hblank * 2 * GTF_CELL_GRAN;
485 /* 20.Find total number of pixels: */
486 total_pixels = total_active_pixels + hblank;
487 /* 21.Find pixel clock frequency: */
488 pixel_freq = total_pixels * hfreq_est / 1000;
489 /* Stage 1 computations are now complete; I should really pass
490 * the results to another function and do the Stage 2 computations,
491 * but I only need a few more values so I'll just append the
492 * computations here for now */
493 /* 17. Find the number of pixels in the horizontal sync period: */
494 hsync = H_SYNC_PERCENT * total_pixels / 100;
495 hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
496 hsync = hsync * GTF_CELL_GRAN;
497 /* 18. Find the number of pixels in horizontal front porch period */
498 hfront_porch = hblank / 2 - hsync;
499 /* 36. Find the number of lines in the odd front porch period: */
500 vodd_front_porch_lines = GTF_MIN_V_PORCH ;
501
502 /* finally, pack the results in the mode struct */
503 drm_mode->hdisplay = hdisplay_rnd;
504 drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
505 drm_mode->hsync_end = drm_mode->hsync_start + hsync;
506 drm_mode->htotal = total_pixels;
507 drm_mode->vdisplay = vdisplay_rnd;
508 drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
509 drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
510 drm_mode->vtotal = vtotal_lines;
511
512 drm_mode->clock = pixel_freq;
513
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800514 if (interlaced) {
515 drm_mode->vtotal *= 2;
516 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
517 }
518
Adam Jackson171fdd82010-03-29 21:43:31 +0000519 drm_mode_set_name(drm_mode);
Adam Jacksonc385e50c2010-04-08 19:00:30 +0000520 if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
521 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
522 else
523 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
Adam Jackson171fdd82010-03-29 21:43:31 +0000524
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800525 return drm_mode;
526}
Adam Jackson7a374352010-03-29 21:43:30 +0000527EXPORT_SYMBOL(drm_gtf_mode_complex);
528
529/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100530 * drm_gtf_mode - create the modeline based on the GTF algorithm
531 * @dev: drm device
532 * @hdisplay: hdisplay size
533 * @vdisplay: vdisplay size
534 * @vrefresh: vrefresh rate.
535 * @interlaced: whether to compute an interlaced mode
536 * @margins: desired margin (borders) size
Adam Jackson7a374352010-03-29 21:43:30 +0000537 *
Adam Jackson7a374352010-03-29 21:43:30 +0000538 * return the modeline based on GTF algorithm
539 *
540 * This function is to create the modeline based on the GTF algorithm.
541 * Generalized Timing Formula is derived from:
Daniel Vetter2e7a5702016-06-01 23:40:36 +0200542 *
Adam Jackson7a374352010-03-29 21:43:30 +0000543 * GTF Spreadsheet by Andy Morrish (1/5/97)
544 * available at http://www.vesa.org
545 *
546 * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
547 * What I have done is to translate it by using integer calculation.
548 * I also refer to the function of fb_get_mode in the file of
549 * drivers/video/fbmon.c
550 *
Daniel Vetterda5335b2016-05-31 22:55:13 +0200551 * Standard GTF parameters::
552 *
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +0100553 * M = 600
554 * C = 40
555 * K = 128
556 * J = 20
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100557 *
558 * Returns:
559 * The modeline based on the GTF algorithm stored in a drm_display_mode object.
560 * The display mode object is allocated with drm_mode_create(). Returns NULL
561 * when no mode could be allocated.
Adam Jackson7a374352010-03-29 21:43:30 +0000562 */
563struct drm_display_mode *
564drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100565 bool interlaced, int margins)
Adam Jackson7a374352010-03-29 21:43:30 +0000566{
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100567 return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
568 interlaced, margins,
569 600, 40 * 2, 128, 20 * 2);
Adam Jackson7a374352010-03-29 21:43:30 +0000570}
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800571EXPORT_SYMBOL(drm_gtf_mode);
Adam Jackson7a374352010-03-29 21:43:30 +0000572
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200573#ifdef CONFIG_VIDEOMODE_HELPERS
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100574/**
575 * drm_display_mode_from_videomode - fill in @dmode using @vm,
576 * @vm: videomode structure to use as source
577 * @dmode: drm_display_mode structure to use as destination
578 *
579 * Fills out @dmode using the display mode specified in @vm.
580 */
Daniel Vetterba0c2422014-01-23 16:28:50 +0100581void drm_display_mode_from_videomode(const struct videomode *vm,
582 struct drm_display_mode *dmode)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100583{
584 dmode->hdisplay = vm->hactive;
585 dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
586 dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
587 dmode->htotal = dmode->hsync_end + vm->hback_porch;
588
589 dmode->vdisplay = vm->vactive;
590 dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
591 dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
592 dmode->vtotal = dmode->vsync_end + vm->vback_porch;
593
594 dmode->clock = vm->pixelclock / 1000;
595
596 dmode->flags = 0;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200597 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100598 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200599 else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100600 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200601 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100602 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200603 else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100604 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200605 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100606 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
Tomi Valkeinen06a33072013-03-12 10:26:45 +0200607 if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100608 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
Steffen Trumtrar328a4712013-05-27 12:33:35 +0000609 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
610 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100611 drm_mode_set_name(dmode);
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100612}
613EXPORT_SYMBOL_GPL(drm_display_mode_from_videomode);
Steffen Trumtrarebc64e42012-11-14 11:22:52 +0100614
Steve Longerbeamd490f452014-12-18 18:00:22 -0800615/**
616 * drm_display_mode_to_videomode - fill in @vm using @dmode,
617 * @dmode: drm_display_mode structure to use as source
618 * @vm: videomode structure to use as destination
619 *
620 * Fills out @vm using the display mode specified in @dmode.
621 */
622void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
623 struct videomode *vm)
624{
625 vm->hactive = dmode->hdisplay;
626 vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
627 vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
628 vm->hback_porch = dmode->htotal - dmode->hsync_end;
629
630 vm->vactive = dmode->vdisplay;
631 vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
632 vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
633 vm->vback_porch = dmode->vtotal - dmode->vsync_end;
634
635 vm->pixelclock = dmode->clock * 1000;
636
637 vm->flags = 0;
638 if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
639 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
640 else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
641 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
642 if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
643 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
644 else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
645 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
646 if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
647 vm->flags |= DISPLAY_FLAGS_INTERLACED;
648 if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
649 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
650 if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
651 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
652}
653EXPORT_SYMBOL_GPL(drm_display_mode_to_videomode);
654
Lothar Waßmanncb34d7f2016-08-16 15:34:37 +0200655/**
656 * drm_bus_flags_from_videomode - extract information about pixelclk and
657 * DE polarity from videomode and store it in a separate variable
658 * @vm: videomode structure to use
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300659 * @bus_flags: information about pixelclk, sync and DE polarity will be stored
660 * here
Lothar Waßmanncb34d7f2016-08-16 15:34:37 +0200661 *
Laurent Pinchart88bc4172018-09-22 15:02:42 +0300662 * Sets DRM_BUS_FLAG_DE_(LOW|HIGH), DRM_BUS_FLAG_PIXDATA_DRIVE_(POS|NEG)EDGE
663 * and DISPLAY_FLAGS_SYNC_(POS|NEG)EDGE in @bus_flags according to DISPLAY_FLAGS
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300664 * found in @vm
Lothar Waßmanncb34d7f2016-08-16 15:34:37 +0200665 */
Lothar Waßmannd72daa02016-07-12 15:30:02 +0200666void drm_bus_flags_from_videomode(const struct videomode *vm, u32 *bus_flags)
667{
668 *bus_flags = 0;
669 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
Laurent Pinchart88bc4172018-09-22 15:02:42 +0300670 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_POSEDGE;
Lothar Waßmannd72daa02016-07-12 15:30:02 +0200671 if (vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
Laurent Pinchart88bc4172018-09-22 15:02:42 +0300672 *bus_flags |= DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
Lothar Waßmannd72daa02016-07-12 15:30:02 +0200673
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300674 if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
Laurent Pinchart88bc4172018-09-22 15:02:42 +0300675 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE;
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300676 if (vm->flags & DISPLAY_FLAGS_SYNC_NEGEDGE)
Laurent Pinchart88bc4172018-09-22 15:02:42 +0300677 *bus_flags |= DRM_BUS_FLAG_SYNC_DRIVE_NEGEDGE;
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300678
Lothar Waßmannd72daa02016-07-12 15:30:02 +0200679 if (vm->flags & DISPLAY_FLAGS_DE_LOW)
680 *bus_flags |= DRM_BUS_FLAG_DE_LOW;
681 if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
682 *bus_flags |= DRM_BUS_FLAG_DE_HIGH;
683}
684EXPORT_SYMBOL_GPL(drm_bus_flags_from_videomode);
685
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200686#ifdef CONFIG_OF
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100687/**
688 * of_get_drm_display_mode - get a drm_display_mode from devicetree
689 * @np: device_node with the timing specification
690 * @dmode: will be set to the return value
Peter Ujfalusi955f60d2018-06-18 16:22:34 +0300691 * @bus_flags: information about pixelclk, sync and DE polarity
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100692 * @index: index into the list of display timings in devicetree
693 *
694 * This function is expensive and should only be used, if only one mode is to be
695 * read from DT. To get multiple modes start with of_get_display_timings and
696 * work with that instead.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100697 *
698 * Returns:
699 * 0 on success, a negative errno code when no of videomode node was found.
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100700 */
701int of_get_drm_display_mode(struct device_node *np,
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200702 struct drm_display_mode *dmode, u32 *bus_flags,
703 int index)
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100704{
705 struct videomode vm;
706 int ret;
707
708 ret = of_get_videomode(np, &vm, index);
709 if (ret)
710 return ret;
711
712 drm_display_mode_from_videomode(&vm, dmode);
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200713 if (bus_flags)
714 drm_bus_flags_from_videomode(&vm, bus_flags);
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100715
Rob Herringf384d7d2018-09-28 17:50:44 -0500716 pr_debug("%pOF: got %dx%d display mode\n",
717 np, vm.hactive, vm.vactive);
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100718 drm_mode_debug_printmodeline(dmode);
719
720 return 0;
721}
722EXPORT_SYMBOL_GPL(of_get_drm_display_mode);
Tomi Valkeinena38884f2013-03-12 10:15:43 +0200723#endif /* CONFIG_OF */
724#endif /* CONFIG_VIDEOMODE_HELPERS */
Steffen Trumtraredb37a92012-10-28 18:28:06 +0100725
Zhao Yakui26bbdad2009-06-22 13:17:09 +0800726/**
Dave Airlief453ba02008-11-07 14:05:41 -0800727 * drm_mode_set_name - set the name on a mode
728 * @mode: name will be set in this mode
729 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100730 * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
731 * with an optional 'i' suffix for interlaced modes.
Dave Airlief453ba02008-11-07 14:05:41 -0800732 */
733void drm_mode_set_name(struct drm_display_mode *mode)
734{
Adam Jackson171fdd82010-03-29 21:43:31 +0000735 bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
736
737 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
738 mode->hdisplay, mode->vdisplay,
739 interlaced ? "i" : "");
Dave Airlief453ba02008-11-07 14:05:41 -0800740}
741EXPORT_SYMBOL(drm_mode_set_name);
742
Daniel Vetter30ecad72015-12-09 09:29:36 +0100743/**
744 * drm_mode_hsync - get the hsync of a mode
Adam Jackson7ac96a92009-12-03 17:44:37 -0500745 * @mode: mode
746 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100747 * Returns:
748 * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
749 * value first if it is not yet set.
Adam Jackson7ac96a92009-12-03 17:44:37 -0500750 */
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000751int drm_mode_hsync(const struct drm_display_mode *mode)
Adam Jackson7ac96a92009-12-03 17:44:37 -0500752{
753 unsigned int calc_val;
754
755 if (mode->hsync)
756 return mode->hsync;
757
Tina Zhanga2fcd5c2019-01-23 15:28:59 +0800758 if (mode->htotal <= 0)
Adam Jackson7ac96a92009-12-03 17:44:37 -0500759 return 0;
760
761 calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
762 calc_val += 500; /* round to 1000Hz */
763 calc_val /= 1000; /* truncate to kHz */
764
765 return calc_val;
766}
767EXPORT_SYMBOL(drm_mode_hsync);
768
Dave Airlief453ba02008-11-07 14:05:41 -0800769/**
770 * drm_mode_vrefresh - get the vrefresh of a mode
771 * @mode: mode
772 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100773 * Returns:
774 * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
775 * value first if it is not yet set.
Dave Airlief453ba02008-11-07 14:05:41 -0800776 */
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000777int drm_mode_vrefresh(const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800778{
779 int refresh = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800780
781 if (mode->vrefresh > 0)
782 refresh = mode->vrefresh;
783 else if (mode->htotal > 0 && mode->vtotal > 0) {
Ville Syrjälä2f0e9d82018-03-13 17:07:58 +0200784 unsigned int num, den;
785
786 num = mode->clock * 1000;
787 den = mode->htotal * mode->vtotal;
Dave Airlief453ba02008-11-07 14:05:41 -0800788
Dave Airlief453ba02008-11-07 14:05:41 -0800789 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
Ville Syrjälä2f0e9d82018-03-13 17:07:58 +0200790 num *= 2;
Dave Airlief453ba02008-11-07 14:05:41 -0800791 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
Ville Syrjälä2f0e9d82018-03-13 17:07:58 +0200792 den *= 2;
Dave Airlief453ba02008-11-07 14:05:41 -0800793 if (mode->vscan > 1)
Ville Syrjälä2f0e9d82018-03-13 17:07:58 +0200794 den *= mode->vscan;
795
796 refresh = DIV_ROUND_CLOSEST(num, den);
Dave Airlief453ba02008-11-07 14:05:41 -0800797 }
798 return refresh;
799}
800EXPORT_SYMBOL(drm_mode_vrefresh);
801
802/**
Daniel Vetter196cd5d2017-01-25 07:26:56 +0100803 * drm_mode_get_hv_timing - Fetches hdisplay/vdisplay for given mode
804 * @mode: mode to query
805 * @hdisplay: hdisplay value to fill in
806 * @vdisplay: vdisplay value to fill in
807 *
808 * The vdisplay value will be doubled if the specified mode is a stereo mode of
809 * the appropriate layout.
810 */
811void drm_mode_get_hv_timing(const struct drm_display_mode *mode,
812 int *hdisplay, int *vdisplay)
813{
Chris Wilson6f15f842017-01-26 11:44:09 +0000814 struct drm_display_mode adjusted = *mode;
Daniel Vetter196cd5d2017-01-25 07:26:56 +0100815
Daniel Vetter196cd5d2017-01-25 07:26:56 +0100816 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
817 *hdisplay = adjusted.crtc_hdisplay;
818 *vdisplay = adjusted.crtc_vdisplay;
819}
820EXPORT_SYMBOL(drm_mode_get_hv_timing);
821
822/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100823 * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
Dave Airlief453ba02008-11-07 14:05:41 -0800824 * @p: mode
Damien Lespiau448cce22013-09-25 16:45:35 +0100825 * @adjust_flags: a combination of adjustment flags
Dave Airlief453ba02008-11-07 14:05:41 -0800826 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100827 * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
Damien Lespiau448cce22013-09-25 16:45:35 +0100828 *
829 * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
830 * interlaced modes.
831 * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
832 * buffers containing two eyes (only adjust the timings when needed, eg. for
833 * "frame packing" or "side by side full").
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800834 * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
835 * be performed for doublescan and vscan > 1 modes respectively.
Dave Airlief453ba02008-11-07 14:05:41 -0800836 */
837void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
838{
Ville Syrjälä4f09c772017-11-14 20:32:54 +0200839 if (!p)
Dave Airlief453ba02008-11-07 14:05:41 -0800840 return;
841
Damien Lespiaubde2dcf2013-09-25 16:45:34 +0100842 p->crtc_clock = p->clock;
Dave Airlief453ba02008-11-07 14:05:41 -0800843 p->crtc_hdisplay = p->hdisplay;
844 p->crtc_hsync_start = p->hsync_start;
845 p->crtc_hsync_end = p->hsync_end;
846 p->crtc_htotal = p->htotal;
847 p->crtc_hskew = p->hskew;
848 p->crtc_vdisplay = p->vdisplay;
849 p->crtc_vsync_start = p->vsync_start;
850 p->crtc_vsync_end = p->vsync_end;
851 p->crtc_vtotal = p->vtotal;
852
853 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
854 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
855 p->crtc_vdisplay /= 2;
856 p->crtc_vsync_start /= 2;
857 p->crtc_vsync_end /= 2;
858 p->crtc_vtotal /= 2;
859 }
Dave Airlief453ba02008-11-07 14:05:41 -0800860 }
861
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800862 if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
863 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
864 p->crtc_vdisplay *= 2;
865 p->crtc_vsync_start *= 2;
866 p->crtc_vsync_end *= 2;
867 p->crtc_vtotal *= 2;
868 }
Dave Airlief453ba02008-11-07 14:05:41 -0800869 }
870
Gustavo Padovanecb7e162014-12-01 15:40:09 -0800871 if (!(adjust_flags & CRTC_NO_VSCAN)) {
872 if (p->vscan > 1) {
873 p->crtc_vdisplay *= p->vscan;
874 p->crtc_vsync_start *= p->vscan;
875 p->crtc_vsync_end *= p->vscan;
876 p->crtc_vtotal *= p->vscan;
877 }
Dave Airlief453ba02008-11-07 14:05:41 -0800878 }
879
Damien Lespiau448cce22013-09-25 16:45:35 +0100880 if (adjust_flags & CRTC_STEREO_DOUBLE) {
881 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
882
883 switch (layout) {
884 case DRM_MODE_FLAG_3D_FRAME_PACKING:
885 p->crtc_clock *= 2;
886 p->crtc_vdisplay += p->crtc_vtotal;
887 p->crtc_vsync_start += p->crtc_vtotal;
888 p->crtc_vsync_end += p->crtc_vtotal;
889 p->crtc_vtotal += p->crtc_vtotal;
890 break;
891 }
892 }
893
Dave Airlief453ba02008-11-07 14:05:41 -0800894 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
895 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
896 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
897 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
Dave Airlief453ba02008-11-07 14:05:41 -0800898}
899EXPORT_SYMBOL(drm_mode_set_crtcinfo);
900
Dave Airlief453ba02008-11-07 14:05:41 -0800901/**
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200902 * drm_mode_copy - copy the mode
903 * @dst: mode to overwrite
904 * @src: mode to copy
905 *
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000906 * Copy an existing mode into another mode, preserving the object id and
907 * list head of the destination mode.
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200908 */
909void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
910{
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000911 struct list_head head = dst->head;
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200912
913 *dst = *src;
Ville Syrjälä72e45e92013-05-31 12:17:06 +0000914 dst->head = head;
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200915}
916EXPORT_SYMBOL(drm_mode_copy);
917
918/**
Dave Airlief453ba02008-11-07 14:05:41 -0800919 * drm_mode_duplicate - allocate and duplicate an existing mode
Daniel Vetter3ec0db82014-01-23 15:06:15 +0100920 * @dev: drm_device to allocate the duplicated mode for
921 * @mode: mode to duplicate
Dave Airlief453ba02008-11-07 14:05:41 -0800922 *
Dave Airlief453ba02008-11-07 14:05:41 -0800923 * Just allocate a new mode, copy the existing mode into it, and return
924 * a pointer to it. Used to create new instances of established modes.
Daniel Vetterf5aabb972014-01-23 20:05:00 +0100925 *
926 * Returns:
927 * Pointer to duplicated mode on success, NULL on error.
Dave Airlief453ba02008-11-07 14:05:41 -0800928 */
929struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
Chris Wilsonb1f559e2011-01-26 09:49:47 +0000930 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800931{
932 struct drm_display_mode *nmode;
Dave Airlief453ba02008-11-07 14:05:41 -0800933
934 nmode = drm_mode_create(dev);
935 if (!nmode)
936 return NULL;
937
Ville Syrjäläc3c50e82012-03-13 12:35:51 +0200938 drm_mode_copy(nmode, mode);
939
Dave Airlief453ba02008-11-07 14:05:41 -0800940 return nmode;
941}
942EXPORT_SYMBOL(drm_mode_duplicate);
943
Ville Syrjälädd7c2622018-05-08 16:39:36 +0530944static bool drm_mode_match_timings(const struct drm_display_mode *mode1,
945 const struct drm_display_mode *mode2)
946{
947 return mode1->hdisplay == mode2->hdisplay &&
948 mode1->hsync_start == mode2->hsync_start &&
949 mode1->hsync_end == mode2->hsync_end &&
950 mode1->htotal == mode2->htotal &&
951 mode1->hskew == mode2->hskew &&
952 mode1->vdisplay == mode2->vdisplay &&
953 mode1->vsync_start == mode2->vsync_start &&
954 mode1->vsync_end == mode2->vsync_end &&
955 mode1->vtotal == mode2->vtotal &&
956 mode1->vscan == mode2->vscan;
957}
958
959static bool drm_mode_match_clock(const struct drm_display_mode *mode1,
960 const struct drm_display_mode *mode2)
961{
962 /*
963 * do clock check convert to PICOS
964 * so fb modes get matched the same
965 */
966 if (mode1->clock && mode2->clock)
967 return KHZ2PICOS(mode1->clock) == KHZ2PICOS(mode2->clock);
968 else
969 return mode1->clock == mode2->clock;
970}
971
972static bool drm_mode_match_flags(const struct drm_display_mode *mode1,
973 const struct drm_display_mode *mode2)
974{
975 return (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
976 (mode2->flags & ~DRM_MODE_FLAG_3D_MASK);
977}
978
979static bool drm_mode_match_3d_flags(const struct drm_display_mode *mode1,
980 const struct drm_display_mode *mode2)
981{
982 return (mode1->flags & DRM_MODE_FLAG_3D_MASK) ==
983 (mode2->flags & DRM_MODE_FLAG_3D_MASK);
984}
985
986static bool drm_mode_match_aspect_ratio(const struct drm_display_mode *mode1,
987 const struct drm_display_mode *mode2)
988{
989 return mode1->picture_aspect_ratio == mode2->picture_aspect_ratio;
990}
991
992/**
993 * drm_mode_match - test modes for (partial) equality
994 * @mode1: first mode
995 * @mode2: second mode
996 * @match_flags: which parts need to match (DRM_MODE_MATCH_*)
997 *
998 * Check to see if @mode1 and @mode2 are equivalent.
999 *
1000 * Returns:
1001 * True if the modes are (partially) equal, false otherwise.
1002 */
1003bool drm_mode_match(const struct drm_display_mode *mode1,
1004 const struct drm_display_mode *mode2,
1005 unsigned int match_flags)
1006{
1007 if (!mode1 && !mode2)
1008 return true;
1009
1010 if (!mode1 || !mode2)
1011 return false;
1012
1013 if (match_flags & DRM_MODE_MATCH_TIMINGS &&
1014 !drm_mode_match_timings(mode1, mode2))
1015 return false;
1016
1017 if (match_flags & DRM_MODE_MATCH_CLOCK &&
1018 !drm_mode_match_clock(mode1, mode2))
1019 return false;
1020
1021 if (match_flags & DRM_MODE_MATCH_FLAGS &&
1022 !drm_mode_match_flags(mode1, mode2))
1023 return false;
1024
1025 if (match_flags & DRM_MODE_MATCH_3D_FLAGS &&
1026 !drm_mode_match_3d_flags(mode1, mode2))
1027 return false;
1028
1029 if (match_flags & DRM_MODE_MATCH_ASPECT_RATIO &&
1030 !drm_mode_match_aspect_ratio(mode1, mode2))
1031 return false;
1032
1033 return true;
1034}
1035EXPORT_SYMBOL(drm_mode_match);
1036
Dave Airlief453ba02008-11-07 14:05:41 -08001037/**
1038 * drm_mode_equal - test modes for equality
1039 * @mode1: first mode
1040 * @mode2: second mode
1041 *
Dave Airlief453ba02008-11-07 14:05:41 -08001042 * Check to see if @mode1 and @mode2 are equivalent.
1043 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001044 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001045 * True if the modes are equal, false otherwise.
1046 */
Ville Syrjälädd7c2622018-05-08 16:39:36 +05301047bool drm_mode_equal(const struct drm_display_mode *mode1,
1048 const struct drm_display_mode *mode2)
Dave Airlief453ba02008-11-07 14:05:41 -08001049{
Ville Syrjälädd7c2622018-05-08 16:39:36 +05301050 return drm_mode_match(mode1, mode2,
1051 DRM_MODE_MATCH_TIMINGS |
1052 DRM_MODE_MATCH_CLOCK |
1053 DRM_MODE_MATCH_FLAGS |
Shashank Sharma222ec162018-05-08 16:39:44 +05301054 DRM_MODE_MATCH_3D_FLAGS|
1055 DRM_MODE_MATCH_ASPECT_RATIO);
Ville Syrjälä4c6bcf42015-11-16 21:05:12 +02001056}
1057EXPORT_SYMBOL(drm_mode_equal);
1058
1059/**
1060 * drm_mode_equal_no_clocks - test modes for equality
1061 * @mode1: first mode
1062 * @mode2: second mode
1063 *
1064 * Check to see if @mode1 and @mode2 are equivalent, but
1065 * don't check the pixel clocks.
1066 *
1067 * Returns:
1068 * True if the modes are equal, false otherwise.
1069 */
Ville Syrjälädd7c2622018-05-08 16:39:36 +05301070bool drm_mode_equal_no_clocks(const struct drm_display_mode *mode1,
1071 const struct drm_display_mode *mode2)
Ville Syrjälä4c6bcf42015-11-16 21:05:12 +02001072{
Ville Syrjälädd7c2622018-05-08 16:39:36 +05301073 return drm_mode_match(mode1, mode2,
1074 DRM_MODE_MATCH_TIMINGS |
1075 DRM_MODE_MATCH_FLAGS |
1076 DRM_MODE_MATCH_3D_FLAGS);
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001077}
Ville Syrjälä4c6bcf42015-11-16 21:05:12 +02001078EXPORT_SYMBOL(drm_mode_equal_no_clocks);
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001079
1080/**
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001081 * drm_mode_equal_no_clocks_no_stereo - test modes for equality
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001082 * @mode1: first mode
1083 * @mode2: second mode
1084 *
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001085 * Check to see if @mode1 and @mode2 are equivalent, but
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001086 * don't check the pixel clocks nor the stereo layout.
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001087 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001088 * Returns:
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001089 * True if the modes are equal, false otherwise.
1090 */
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001091bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
1092 const struct drm_display_mode *mode2)
Ville Syrjälä8cc3f232013-04-24 19:07:16 +03001093{
Ville Syrjälädd7c2622018-05-08 16:39:36 +05301094 return drm_mode_match(mode1, mode2,
1095 DRM_MODE_MATCH_TIMINGS |
1096 DRM_MODE_MATCH_FLAGS);
Dave Airlief453ba02008-11-07 14:05:41 -08001097}
Damien Lespiauf2ecf2e32013-09-25 16:45:27 +01001098EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
Dave Airlief453ba02008-11-07 14:05:41 -08001099
Ville Syrjälä75a655e2017-11-14 20:32:57 +02001100static enum drm_mode_status
Ville Syrjäläabc0b142014-12-17 13:56:23 +02001101drm_mode_validate_basic(const struct drm_display_mode *mode)
1102{
Ville Syrjäläc6ed6da2017-11-15 17:49:13 +02001103 if (mode->type & ~DRM_MODE_TYPE_ALL)
1104 return MODE_BAD;
1105
1106 if (mode->flags & ~DRM_MODE_FLAG_ALL)
1107 return MODE_BAD;
1108
Ville Syrjälä064a3e62017-11-14 20:32:49 +02001109 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1110 return MODE_BAD;
1111
Ville Syrjäläabc0b142014-12-17 13:56:23 +02001112 if (mode->clock == 0)
1113 return MODE_CLOCK_LOW;
1114
1115 if (mode->hdisplay == 0 ||
1116 mode->hsync_start < mode->hdisplay ||
1117 mode->hsync_end < mode->hsync_start ||
1118 mode->htotal < mode->hsync_end)
1119 return MODE_H_ILLEGAL;
1120
1121 if (mode->vdisplay == 0 ||
1122 mode->vsync_start < mode->vdisplay ||
1123 mode->vsync_end < mode->vsync_start ||
1124 mode->vtotal < mode->vsync_end)
1125 return MODE_V_ILLEGAL;
1126
1127 return MODE_OK;
1128}
Ville Syrjälä75a655e2017-11-14 20:32:57 +02001129
1130/**
1131 * drm_mode_validate_driver - make sure the mode is somewhat sane
1132 * @dev: drm device
1133 * @mode: mode to check
1134 *
1135 * First do basic validation on the mode, and then allow the driver
1136 * to check for device/driver specific limitations via the optional
1137 * &drm_mode_config_helper_funcs.mode_valid hook.
1138 *
1139 * Returns:
1140 * The mode status
1141 */
1142enum drm_mode_status
1143drm_mode_validate_driver(struct drm_device *dev,
1144 const struct drm_display_mode *mode)
1145{
1146 enum drm_mode_status status;
1147
1148 status = drm_mode_validate_basic(mode);
1149 if (status != MODE_OK)
1150 return status;
1151
1152 if (dev->mode_config.funcs->mode_valid)
1153 return dev->mode_config.funcs->mode_valid(dev, mode);
1154 else
1155 return MODE_OK;
1156}
1157EXPORT_SYMBOL(drm_mode_validate_driver);
Ville Syrjäläabc0b142014-12-17 13:56:23 +02001158
1159/**
Dave Airlief453ba02008-11-07 14:05:41 -08001160 * drm_mode_validate_size - make sure modes adhere to size constraints
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001161 * @mode: mode to check
Dave Airlief453ba02008-11-07 14:05:41 -08001162 * @maxX: maximum width
1163 * @maxY: maximum height
Dave Airlief453ba02008-11-07 14:05:41 -08001164 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001165 * This function is a helper which can be used to validate modes against size
1166 * limitations of the DRM device/connector. If a mode is too big its status
Masanari Iida32197aa2014-10-20 23:53:13 +09001167 * member is updated with the appropriate validation failure code. The list
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001168 * itself is not changed.
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001169 *
1170 * Returns:
1171 * The mode status
Dave Airlief453ba02008-11-07 14:05:41 -08001172 */
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001173enum drm_mode_status
1174drm_mode_validate_size(const struct drm_display_mode *mode,
1175 int maxX, int maxY)
Dave Airlief453ba02008-11-07 14:05:41 -08001176{
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001177 if (maxX > 0 && mode->hdisplay > maxX)
1178 return MODE_VIRTUAL_X;
Dave Airlief453ba02008-11-07 14:05:41 -08001179
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001180 if (maxY > 0 && mode->vdisplay > maxY)
1181 return MODE_VIRTUAL_Y;
Dave Airlief453ba02008-11-07 14:05:41 -08001182
Ville Syrjälä05acaec2014-12-17 13:56:22 +02001183 return MODE_OK;
Dave Airlief453ba02008-11-07 14:05:41 -08001184}
1185EXPORT_SYMBOL(drm_mode_validate_size);
1186
Shashank Sharmad8523152017-07-13 21:03:11 +05301187/**
1188 * drm_mode_validate_ycbcr420 - add 'ycbcr420-only' modes only when allowed
1189 * @mode: mode to check
1190 * @connector: drm connector under action
1191 *
1192 * This function is a helper which can be used to filter out any YCBCR420
1193 * only mode, when the source doesn't support it.
1194 *
1195 * Returns:
1196 * The mode status
1197 */
1198enum drm_mode_status
1199drm_mode_validate_ycbcr420(const struct drm_display_mode *mode,
1200 struct drm_connector *connector)
1201{
1202 u8 vic = drm_match_cea_mode(mode);
1203 enum drm_mode_status status = MODE_OK;
1204 struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;
1205
1206 if (test_bit(vic, hdmi->y420_vdb_modes)) {
1207 if (!connector->ycbcr_420_allowed)
1208 status = MODE_NO_420;
1209 }
1210
1211 return status;
1212}
1213EXPORT_SYMBOL(drm_mode_validate_ycbcr420);
1214
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001215#define MODE_STATUS(status) [MODE_ ## status + 3] = #status
1216
1217static const char * const drm_mode_status_names[] = {
1218 MODE_STATUS(OK),
1219 MODE_STATUS(HSYNC),
1220 MODE_STATUS(VSYNC),
1221 MODE_STATUS(H_ILLEGAL),
1222 MODE_STATUS(V_ILLEGAL),
1223 MODE_STATUS(BAD_WIDTH),
1224 MODE_STATUS(NOMODE),
1225 MODE_STATUS(NO_INTERLACE),
1226 MODE_STATUS(NO_DBLESCAN),
1227 MODE_STATUS(NO_VSCAN),
1228 MODE_STATUS(MEM),
1229 MODE_STATUS(VIRTUAL_X),
1230 MODE_STATUS(VIRTUAL_Y),
1231 MODE_STATUS(MEM_VIRT),
1232 MODE_STATUS(NOCLOCK),
1233 MODE_STATUS(CLOCK_HIGH),
1234 MODE_STATUS(CLOCK_LOW),
1235 MODE_STATUS(CLOCK_RANGE),
1236 MODE_STATUS(BAD_HVALUE),
1237 MODE_STATUS(BAD_VVALUE),
1238 MODE_STATUS(BAD_VSCAN),
1239 MODE_STATUS(HSYNC_NARROW),
1240 MODE_STATUS(HSYNC_WIDE),
1241 MODE_STATUS(HBLANK_NARROW),
1242 MODE_STATUS(HBLANK_WIDE),
1243 MODE_STATUS(VSYNC_NARROW),
1244 MODE_STATUS(VSYNC_WIDE),
1245 MODE_STATUS(VBLANK_NARROW),
1246 MODE_STATUS(VBLANK_WIDE),
1247 MODE_STATUS(PANEL),
1248 MODE_STATUS(INTERLACE_WIDTH),
1249 MODE_STATUS(ONE_WIDTH),
1250 MODE_STATUS(ONE_HEIGHT),
1251 MODE_STATUS(ONE_SIZE),
1252 MODE_STATUS(NO_REDUCED),
1253 MODE_STATUS(NO_STEREO),
Shashank Sharmad8523152017-07-13 21:03:11 +05301254 MODE_STATUS(NO_420),
Ville Syrjälä5ba89402015-12-10 22:39:08 +02001255 MODE_STATUS(STALE),
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001256 MODE_STATUS(BAD),
1257 MODE_STATUS(ERROR),
1258};
1259
1260#undef MODE_STATUS
1261
Ville Syrjälä6ab0edf2018-06-11 22:34:02 +03001262const char *drm_get_mode_status_name(enum drm_mode_status status)
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001263{
1264 int index = status + 3;
1265
1266 if (WARN_ON(index < 0 || index >= ARRAY_SIZE(drm_mode_status_names)))
1267 return "";
1268
1269 return drm_mode_status_names[index];
1270}
1271
Dave Airlief453ba02008-11-07 14:05:41 -08001272/**
Dave Airlief453ba02008-11-07 14:05:41 -08001273 * drm_mode_prune_invalid - remove invalid modes from mode list
1274 * @dev: DRM device
1275 * @mode_list: list of modes to check
1276 * @verbose: be verbose about it
1277 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001278 * This helper function can be used to prune a display mode list after
Matt Roper1e55a532019-02-01 17:23:26 -08001279 * validation has been completed. All modes whose status is not MODE_OK will be
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001280 * removed from the list, and if @verbose the status code and mode name is also
1281 * printed to dmesg.
Dave Airlief453ba02008-11-07 14:05:41 -08001282 */
1283void drm_mode_prune_invalid(struct drm_device *dev,
1284 struct list_head *mode_list, bool verbose)
1285{
1286 struct drm_display_mode *mode, *t;
1287
1288 list_for_each_entry_safe(mode, t, mode_list, head) {
1289 if (mode->status != MODE_OK) {
1290 list_del(&mode->head);
1291 if (verbose) {
1292 drm_mode_debug_printmodeline(mode);
Ville Syrjäläe4bf44b2015-02-02 19:13:57 +02001293 DRM_DEBUG_KMS("Not using %s mode: %s\n",
1294 mode->name,
1295 drm_get_mode_status_name(mode->status));
Dave Airlief453ba02008-11-07 14:05:41 -08001296 }
1297 drm_mode_destroy(dev, mode);
1298 }
1299 }
1300}
1301EXPORT_SYMBOL(drm_mode_prune_invalid);
1302
1303/**
1304 * drm_mode_compare - compare modes for favorability
Dave Chinner2c761272010-01-12 17:39:16 +11001305 * @priv: unused
Dave Airlief453ba02008-11-07 14:05:41 -08001306 * @lh_a: list_head for first mode
1307 * @lh_b: list_head for second mode
1308 *
Dave Airlief453ba02008-11-07 14:05:41 -08001309 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1310 * which is better.
1311 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001312 * Returns:
Dave Airlief453ba02008-11-07 14:05:41 -08001313 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1314 * positive if @lh_b is better than @lh_a.
1315 */
Dave Chinner2c761272010-01-12 17:39:16 +11001316static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
Dave Airlief453ba02008-11-07 14:05:41 -08001317{
1318 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1319 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1320 int diff;
1321
1322 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1323 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1324 if (diff)
1325 return diff;
1326 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1327 if (diff)
1328 return diff;
Ville Syrjälä9bc3cd52013-05-31 12:17:08 +00001329
1330 diff = b->vrefresh - a->vrefresh;
1331 if (diff)
1332 return diff;
1333
Dave Airlief453ba02008-11-07 14:05:41 -08001334 diff = b->clock - a->clock;
1335 return diff;
1336}
1337
Dave Airlief453ba02008-11-07 14:05:41 -08001338/**
1339 * drm_mode_sort - sort mode list
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001340 * @mode_list: list of drm_display_mode structures to sort
Dave Airlief453ba02008-11-07 14:05:41 -08001341 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001342 * Sort @mode_list by favorability, moving good modes to the head of the list.
Dave Airlief453ba02008-11-07 14:05:41 -08001343 */
1344void drm_mode_sort(struct list_head *mode_list)
1345{
Dave Chinner2c761272010-01-12 17:39:16 +11001346 list_sort(NULL, mode_list, drm_mode_compare);
Dave Airlief453ba02008-11-07 14:05:41 -08001347}
1348EXPORT_SYMBOL(drm_mode_sort);
1349
1350/**
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001351 * drm_connector_list_update - update the mode list for the connector
Dave Airlief453ba02008-11-07 14:05:41 -08001352 * @connector: the connector to update
1353 *
Dave Airlief453ba02008-11-07 14:05:41 -08001354 * This moves the modes from the @connector probed_modes list
1355 * to the actual mode list. It compares the probed mode against the current
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001356 * list and only adds different/new modes.
1357 *
1358 * This is just a helper functions doesn't validate any modes itself and also
1359 * doesn't prune any invalid modes. Callers need to do that themselves.
Dave Airlief453ba02008-11-07 14:05:41 -08001360 */
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001361void drm_connector_list_update(struct drm_connector *connector)
Dave Airlief453ba02008-11-07 14:05:41 -08001362{
Dave Airlief453ba02008-11-07 14:05:41 -08001363 struct drm_display_mode *pmode, *pt;
Dave Airlief453ba02008-11-07 14:05:41 -08001364
Daniel Vetter63951382014-01-23 15:14:15 +01001365 WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1366
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001367 list_for_each_entry_safe(pmode, pt, &connector->probed_modes, head) {
1368 struct drm_display_mode *mode;
1369 bool found_it = false;
1370
Dave Airlief453ba02008-11-07 14:05:41 -08001371 /* go through current modes checking for the new probed mode */
1372 list_for_each_entry(mode, &connector->modes, head) {
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001373 if (!drm_mode_equal(pmode, mode))
1374 continue;
1375
1376 found_it = true;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001377
1378 /*
1379 * If the old matching mode is stale (ie. left over
1380 * from a previous probe) just replace it outright.
1381 * Otherwise just merge the type bits between all
1382 * equal probed modes.
1383 *
1384 * If two probed modes are considered equal, pick the
1385 * actual timings from the one that's marked as
1386 * preferred (in case the match isn't 100%). If
1387 * multiple or zero preferred modes are present, favor
1388 * the mode added to the probed_modes list first.
1389 */
1390 if (mode->status == MODE_STALE) {
1391 drm_mode_copy(mode, pmode);
1392 } else if ((mode->type & DRM_MODE_TYPE_PREFERRED) == 0 &&
1393 (pmode->type & DRM_MODE_TYPE_PREFERRED) != 0) {
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001394 pmode->type |= mode->type;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001395 drm_mode_copy(mode, pmode);
1396 } else {
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001397 mode->type |= pmode->type;
Ville Syrjäläfc245f82015-12-04 15:13:01 +02001398 }
1399
Ville Syrjälä2f8c19e2015-12-03 23:14:12 +02001400 list_del(&pmode->head);
1401 drm_mode_destroy(connector->dev, pmode);
1402 break;
Dave Airlief453ba02008-11-07 14:05:41 -08001403 }
1404
1405 if (!found_it) {
1406 list_move_tail(&pmode->head, &connector->modes);
1407 }
1408 }
1409}
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001410EXPORT_SYMBOL(drm_connector_list_update);
Chris Wilson1794d252011-04-17 07:43:32 +01001411
Maxime Riparde08ab742019-06-19 12:17:49 +02001412static int drm_mode_parse_cmdline_bpp(const char *str, char **end_ptr,
1413 struct drm_cmdline_mode *mode)
1414{
1415 unsigned int bpp;
1416
1417 if (str[0] != '-')
1418 return -EINVAL;
1419
1420 str++;
1421 bpp = simple_strtol(str, end_ptr, 10);
1422 if (*end_ptr == str)
1423 return -EINVAL;
1424
1425 mode->bpp = bpp;
1426 mode->bpp_specified = true;
1427
1428 return 0;
1429}
1430
1431static int drm_mode_parse_cmdline_refresh(const char *str, char **end_ptr,
1432 struct drm_cmdline_mode *mode)
1433{
1434 unsigned int refresh;
1435
1436 if (str[0] != '@')
1437 return -EINVAL;
1438
1439 str++;
1440 refresh = simple_strtol(str, end_ptr, 10);
1441 if (*end_ptr == str)
1442 return -EINVAL;
1443
1444 mode->refresh = refresh;
1445 mode->refresh_specified = true;
1446
1447 return 0;
1448}
1449
1450static int drm_mode_parse_cmdline_extra(const char *str, int length,
1451 struct drm_connector *connector,
1452 struct drm_cmdline_mode *mode)
1453{
1454 int i;
1455
1456 for (i = 0; i < length; i++) {
1457 switch (str[i]) {
1458 case 'i':
1459 mode->interlace = true;
1460 break;
1461 case 'm':
1462 mode->margins = true;
1463 break;
1464 case 'D':
1465 if (mode->force != DRM_FORCE_UNSPECIFIED)
1466 return -EINVAL;
1467
1468 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1469 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1470 mode->force = DRM_FORCE_ON;
1471 else
1472 mode->force = DRM_FORCE_ON_DIGITAL;
1473 break;
1474 case 'd':
1475 if (mode->force != DRM_FORCE_UNSPECIFIED)
1476 return -EINVAL;
1477
1478 mode->force = DRM_FORCE_OFF;
1479 break;
1480 case 'e':
1481 if (mode->force != DRM_FORCE_UNSPECIFIED)
1482 return -EINVAL;
1483
1484 mode->force = DRM_FORCE_ON;
1485 break;
1486 default:
1487 return -EINVAL;
1488 }
1489 }
1490
1491 return 0;
1492}
1493
1494static int drm_mode_parse_cmdline_res_mode(const char *str, unsigned int length,
1495 bool extras,
1496 struct drm_connector *connector,
1497 struct drm_cmdline_mode *mode)
1498{
1499 const char *str_start = str;
1500 bool rb = false, cvt = false;
1501 int xres = 0, yres = 0;
1502 int remaining, i;
1503 char *end_ptr;
1504
1505 xres = simple_strtol(str, &end_ptr, 10);
1506 if (end_ptr == str)
1507 return -EINVAL;
1508
1509 if (end_ptr[0] != 'x')
1510 return -EINVAL;
1511 end_ptr++;
1512
1513 str = end_ptr;
1514 yres = simple_strtol(str, &end_ptr, 10);
1515 if (end_ptr == str)
1516 return -EINVAL;
1517
1518 remaining = length - (end_ptr - str_start);
1519 if (remaining < 0)
1520 return -EINVAL;
1521
1522 for (i = 0; i < remaining; i++) {
1523 switch (end_ptr[i]) {
1524 case 'M':
1525 cvt = true;
1526 break;
1527 case 'R':
1528 rb = true;
1529 break;
1530 default:
1531 /*
1532 * Try to pass that to our extras parsing
1533 * function to handle the case where the
1534 * extras are directly after the resolution
1535 */
1536 if (extras) {
1537 int ret = drm_mode_parse_cmdline_extra(end_ptr + i,
1538 1,
1539 connector,
1540 mode);
1541 if (ret)
1542 return ret;
1543 } else {
1544 return -EINVAL;
1545 }
1546 }
1547 }
1548
1549 mode->xres = xres;
1550 mode->yres = yres;
1551 mode->cvt = cvt;
1552 mode->rb = rb;
1553
1554 return 0;
1555}
1556
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001557static int drm_mode_parse_cmdline_options(char *str, size_t len,
1558 struct drm_connector *connector,
1559 struct drm_cmdline_mode *mode)
1560{
1561 unsigned int rotation = 0;
1562 char *sep = str;
1563
1564 while ((sep = strchr(sep, ','))) {
1565 char *delim, *option;
1566
1567 option = sep + 1;
1568 delim = strchr(option, '=');
1569 if (!delim) {
1570 delim = strchr(option, ',');
1571
1572 if (!delim)
1573 delim = str + len;
1574 }
1575
1576 if (!strncmp(option, "rotate", delim - option)) {
1577 const char *value = delim + 1;
1578 unsigned int deg;
1579
1580 deg = simple_strtol(value, &sep, 10);
1581
1582 /* Make sure we have parsed something */
1583 if (sep == value)
1584 return -EINVAL;
1585
1586 switch (deg) {
1587 case 0:
1588 rotation |= DRM_MODE_ROTATE_0;
1589 break;
1590
1591 case 90:
1592 rotation |= DRM_MODE_ROTATE_90;
1593 break;
1594
1595 case 180:
1596 rotation |= DRM_MODE_ROTATE_180;
1597 break;
1598
1599 case 270:
1600 rotation |= DRM_MODE_ROTATE_270;
1601 break;
1602
1603 default:
1604 return -EINVAL;
1605 }
1606 } else if (!strncmp(option, "reflect_x", delim - option)) {
1607 rotation |= DRM_MODE_REFLECT_X;
1608 sep = delim;
1609 } else if (!strncmp(option, "reflect_y", delim - option)) {
1610 rotation |= DRM_MODE_REFLECT_Y;
1611 sep = delim;
Maxime Ripard3d46a302019-06-19 12:17:51 +02001612 } else if (!strncmp(option, "margin_right", delim - option)) {
1613 const char *value = delim + 1;
1614 unsigned int margin;
1615
1616 margin = simple_strtol(value, &sep, 10);
1617
1618 /* Make sure we have parsed something */
1619 if (sep == value)
1620 return -EINVAL;
1621
1622 mode->tv_margins.right = margin;
1623 } else if (!strncmp(option, "margin_left", delim - option)) {
1624 const char *value = delim + 1;
1625 unsigned int margin;
1626
1627 margin = simple_strtol(value, &sep, 10);
1628
1629 /* Make sure we have parsed something */
1630 if (sep == value)
1631 return -EINVAL;
1632
1633 mode->tv_margins.left = margin;
1634 } else if (!strncmp(option, "margin_top", delim - option)) {
1635 const char *value = delim + 1;
1636 unsigned int margin;
1637
1638 margin = simple_strtol(value, &sep, 10);
1639
1640 /* Make sure we have parsed something */
1641 if (sep == value)
1642 return -EINVAL;
1643
1644 mode->tv_margins.top = margin;
1645 } else if (!strncmp(option, "margin_bottom", delim - option)) {
1646 const char *value = delim + 1;
1647 unsigned int margin;
1648
1649 margin = simple_strtol(value, &sep, 10);
1650
1651 /* Make sure we have parsed something */
1652 if (sep == value)
1653 return -EINVAL;
1654
1655 mode->tv_margins.bottom = margin;
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001656 } else {
1657 return -EINVAL;
1658 }
1659 }
1660
1661 mode->rotation_reflection = rotation;
1662
1663 return 0;
1664}
1665
Chris Wilson1794d252011-04-17 07:43:32 +01001666/**
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001667 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1668 * @mode_option: optional per connector mode option
1669 * @connector: connector to parse modeline for
1670 * @mode: preallocated drm_cmdline_mode structure to fill out
Chris Wilson1794d252011-04-17 07:43:32 +01001671 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001672 * This parses @mode_option command line modeline for modes and options to
1673 * configure the connector. If @mode_option is NULL the default command line
1674 * modeline in fb_mode_option will be parsed instead.
Chris Wilson1794d252011-04-17 07:43:32 +01001675 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001676 * This uses the same parameters as the fb modedb.c, except for an extra
Daniel Vetterdbd124f2018-02-19 23:53:55 +01001677 * force-enable, force-enable-digital and force-disable bit at the end::
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001678 *
Daniel Vetterdbd124f2018-02-19 23:53:55 +01001679 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
Chris Wilson1794d252011-04-17 07:43:32 +01001680 *
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001681 * Additionals options can be provided following the mode, using a comma to
1682 * separate each option. Valid options can be found in
1683 * Documentation/fb/modedb.txt.
1684 *
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001685 * The intermediate drm_cmdline_mode structure is required to store additional
Yannick Guerrini2a97acd2015-03-04 09:30:09 +01001686 * options from the command line modline like the force-enable/disable flag.
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001687 *
1688 * Returns:
1689 * True if a valid modeline has been parsed, false otherwise.
Chris Wilson1794d252011-04-17 07:43:32 +01001690 */
1691bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1692 struct drm_connector *connector,
1693 struct drm_cmdline_mode *mode)
1694{
1695 const char *name;
Maxime Ripard3aeeb132019-06-19 12:17:50 +02001696 bool named_mode = false, parse_extras = false;
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001697 unsigned int bpp_off = 0, refresh_off = 0, options_off = 0;
Maxime Riparde08ab742019-06-19 12:17:49 +02001698 unsigned int mode_end = 0;
1699 char *bpp_ptr = NULL, *refresh_ptr = NULL, *extra_ptr = NULL;
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001700 char *options_ptr = NULL;
Maxime Riparde08ab742019-06-19 12:17:49 +02001701 char *bpp_end_ptr = NULL, *refresh_end_ptr = NULL;
1702 int ret;
Chris Wilson1794d252011-04-17 07:43:32 +01001703
Dave Airliecb3c4382011-05-04 13:08:58 +10001704#ifdef CONFIG_FB
Chris Wilson1794d252011-04-17 07:43:32 +01001705 if (!mode_option)
1706 mode_option = fb_mode_option;
Dave Airliecb3c4382011-05-04 13:08:58 +10001707#endif
Chris Wilson1794d252011-04-17 07:43:32 +01001708
1709 if (!mode_option) {
1710 mode->specified = false;
1711 return false;
1712 }
1713
1714 name = mode_option;
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001715
Maxime Ripard3aeeb132019-06-19 12:17:50 +02001716 /*
1717 * This is a bit convoluted. To differentiate between the
1718 * named modes and poorly formatted resolutions, we need a
1719 * bunch of things:
1720 * - We need to make sure that the first character (which
1721 * would be our resolution in X) is a digit.
1722 * - However, if the X resolution is missing, then we end up
1723 * with something like x<yres>, with our first character
1724 * being an alpha-numerical character, which would be
1725 * considered a named mode.
1726 *
1727 * If this isn't enough, we should add more heuristics here,
1728 * and matching unit-tests.
1729 */
1730 if (!isdigit(name[0]) && name[0] != 'x')
1731 named_mode = true;
Rolf Eike Beer04fee892011-06-15 11:27:02 +02001732
Maxime Riparde08ab742019-06-19 12:17:49 +02001733 /* Try to locate the bpp and refresh specifiers, if any */
1734 bpp_ptr = strchr(name, '-');
1735 if (bpp_ptr) {
1736 bpp_off = bpp_ptr - name;
Chris Wilson1794d252011-04-17 07:43:32 +01001737 mode->bpp_specified = true;
Chris Wilson1794d252011-04-17 07:43:32 +01001738 }
Maxime Riparde08ab742019-06-19 12:17:49 +02001739
1740 refresh_ptr = strchr(name, '@');
1741 if (refresh_ptr) {
Maxime Ripard3aeeb132019-06-19 12:17:50 +02001742 if (named_mode)
1743 return false;
1744
Maxime Riparde08ab742019-06-19 12:17:49 +02001745 refresh_off = refresh_ptr - name;
1746 mode->refresh_specified = true;
1747 }
1748
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001749 /* Locate the start of named options */
1750 options_ptr = strchr(name, ',');
1751 if (options_ptr)
1752 options_off = options_ptr - name;
1753
Maxime Riparde08ab742019-06-19 12:17:49 +02001754 /* Locate the end of the name / resolution, and parse it */
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001755 if (bpp_ptr) {
Maxime Riparde08ab742019-06-19 12:17:49 +02001756 mode_end = bpp_off;
1757 } else if (refresh_ptr) {
1758 mode_end = refresh_off;
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001759 } else if (options_ptr) {
1760 mode_end = options_off;
Maxime Riparde08ab742019-06-19 12:17:49 +02001761 } else {
1762 mode_end = strlen(name);
1763 parse_extras = true;
1764 }
1765
Maxime Ripard3aeeb132019-06-19 12:17:50 +02001766 if (named_mode) {
1767 strncpy(mode->name, name, mode_end);
1768 } else {
1769 ret = drm_mode_parse_cmdline_res_mode(name, mode_end,
1770 parse_extras,
1771 connector,
1772 mode);
1773 if (ret)
1774 return false;
1775 }
Maxime Riparde08ab742019-06-19 12:17:49 +02001776 mode->specified = true;
1777
1778 if (bpp_ptr) {
1779 ret = drm_mode_parse_cmdline_bpp(bpp_ptr, &bpp_end_ptr, mode);
1780 if (ret)
1781 return false;
1782 }
1783
1784 if (refresh_ptr) {
1785 ret = drm_mode_parse_cmdline_refresh(refresh_ptr,
1786 &refresh_end_ptr, mode);
1787 if (ret)
1788 return false;
1789 }
1790
1791 /*
1792 * Locate the end of the bpp / refresh, and parse the extras
1793 * if relevant
1794 */
1795 if (bpp_ptr && refresh_ptr)
1796 extra_ptr = max(bpp_end_ptr, refresh_end_ptr);
1797 else if (bpp_ptr)
1798 extra_ptr = bpp_end_ptr;
1799 else if (refresh_ptr)
1800 extra_ptr = refresh_end_ptr;
1801
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001802 if (extra_ptr &&
1803 extra_ptr != options_ptr) {
1804 int len = strlen(name) - (extra_ptr - name);
Maxime Riparde08ab742019-06-19 12:17:49 +02001805
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001806 ret = drm_mode_parse_cmdline_extra(extra_ptr, len,
1807 connector, mode);
1808 if (ret)
1809 return false;
1810 }
Maxime Ripard3aeeb132019-06-19 12:17:50 +02001811
Maxime Ripard1bf4e092019-06-19 12:17:51 +02001812 if (options_ptr) {
1813 int len = strlen(name) - (options_ptr - name);
1814
1815 ret = drm_mode_parse_cmdline_options(options_ptr, len,
1816 connector, mode);
1817 if (ret)
1818 return false;
Maxime Riparde08ab742019-06-19 12:17:49 +02001819 }
Chris Wilson1794d252011-04-17 07:43:32 +01001820
1821 return true;
1822}
1823EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1824
Daniel Vetterf5aabb972014-01-23 20:05:00 +01001825/**
1826 * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1827 * @dev: DRM device to create the new mode for
1828 * @cmd: input command line modeline
1829 *
1830 * Returns:
1831 * Pointer to converted mode on success, NULL on error.
1832 */
Chris Wilson1794d252011-04-17 07:43:32 +01001833struct drm_display_mode *
1834drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1835 struct drm_cmdline_mode *cmd)
1836{
1837 struct drm_display_mode *mode;
1838
1839 if (cmd->cvt)
1840 mode = drm_cvt_mode(dev,
1841 cmd->xres, cmd->yres,
1842 cmd->refresh_specified ? cmd->refresh : 60,
1843 cmd->rb, cmd->interlace,
1844 cmd->margins);
1845 else
1846 mode = drm_gtf_mode(dev,
1847 cmd->xres, cmd->yres,
1848 cmd->refresh_specified ? cmd->refresh : 60,
1849 cmd->interlace,
1850 cmd->margins);
1851 if (!mode)
1852 return NULL;
1853
Chris Wilsoneaf99c72014-08-06 10:08:32 +02001854 mode->type |= DRM_MODE_TYPE_USERDEF;
Takashi Iwaifdf35a62017-01-09 15:56:14 +01001855 /* fix up 1368x768: GFT/CVT can't express 1366 width due to alignment */
Takashi Iwai969218f2017-01-17 17:43:29 +01001856 if (cmd->xres == 1366)
1857 drm_mode_fixup_1366x768(mode);
Chris Wilson1794d252011-04-17 07:43:32 +01001858 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1859 return mode;
1860}
1861EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);
Daniel Stone934a8a82015-05-22 13:34:48 +01001862
1863/**
1864 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1865 * @out: drm_mode_modeinfo struct to return to the user
1866 * @in: drm_display_mode to use
1867 *
1868 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1869 * the user.
1870 */
1871void drm_mode_convert_to_umode(struct drm_mode_modeinfo *out,
1872 const struct drm_display_mode *in)
1873{
1874 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1875 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1876 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1877 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1878 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1879 "timing values too large for mode info\n");
1880
1881 out->clock = in->clock;
1882 out->hdisplay = in->hdisplay;
1883 out->hsync_start = in->hsync_start;
1884 out->hsync_end = in->hsync_end;
1885 out->htotal = in->htotal;
1886 out->hskew = in->hskew;
1887 out->vdisplay = in->vdisplay;
1888 out->vsync_start = in->vsync_start;
1889 out->vsync_end = in->vsync_end;
1890 out->vtotal = in->vtotal;
1891 out->vscan = in->vscan;
1892 out->vrefresh = in->vrefresh;
1893 out->flags = in->flags;
1894 out->type = in->type;
Shashank Sharma222ec162018-05-08 16:39:44 +05301895
1896 switch (in->picture_aspect_ratio) {
1897 case HDMI_PICTURE_ASPECT_4_3:
1898 out->flags |= DRM_MODE_FLAG_PIC_AR_4_3;
1899 break;
1900 case HDMI_PICTURE_ASPECT_16_9:
1901 out->flags |= DRM_MODE_FLAG_PIC_AR_16_9;
1902 break;
Shashank Sharma900aa8a2018-05-08 16:39:45 +05301903 case HDMI_PICTURE_ASPECT_64_27:
1904 out->flags |= DRM_MODE_FLAG_PIC_AR_64_27;
1905 break;
1906 case HDMI_PICTURE_ASPECT_256_135:
1907 out->flags |= DRM_MODE_FLAG_PIC_AR_256_135;
1908 break;
Shashank Sharma222ec162018-05-08 16:39:44 +05301909 case HDMI_PICTURE_ASPECT_RESERVED:
1910 default:
1911 out->flags |= DRM_MODE_FLAG_PIC_AR_NONE;
1912 break;
1913 }
1914
Daniel Stone934a8a82015-05-22 13:34:48 +01001915 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1916 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1917}
1918
1919/**
1920 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
Ville Syrjälä75a655e2017-11-14 20:32:57 +02001921 * @dev: drm device
Daniel Stone934a8a82015-05-22 13:34:48 +01001922 * @out: drm_display_mode to return to the user
1923 * @in: drm_mode_modeinfo to use
1924 *
1925 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1926 * the caller.
1927 *
1928 * Returns:
1929 * Zero on success, negative errno on failure.
1930 */
Ville Syrjälä75a655e2017-11-14 20:32:57 +02001931int drm_mode_convert_umode(struct drm_device *dev,
1932 struct drm_display_mode *out,
Daniel Stone934a8a82015-05-22 13:34:48 +01001933 const struct drm_mode_modeinfo *in)
1934{
Ville Syrjälädf550542018-03-13 17:07:57 +02001935 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1936 return -ERANGE;
Daniel Stone934a8a82015-05-22 13:34:48 +01001937
Daniel Stone934a8a82015-05-22 13:34:48 +01001938 out->clock = in->clock;
1939 out->hdisplay = in->hdisplay;
1940 out->hsync_start = in->hsync_start;
1941 out->hsync_end = in->hsync_end;
1942 out->htotal = in->htotal;
1943 out->hskew = in->hskew;
1944 out->vdisplay = in->vdisplay;
1945 out->vsync_start = in->vsync_start;
1946 out->vsync_end = in->vsync_end;
1947 out->vtotal = in->vtotal;
1948 out->vscan = in->vscan;
1949 out->vrefresh = in->vrefresh;
1950 out->flags = in->flags;
Ville Syrjäläa01c4772018-03-21 23:12:46 +02001951 /*
1952 * Old xf86-video-vmware (possibly others too) used to
1953 * leave 'type' unititialized. Just ignore any bits we
1954 * don't like. It's a just hint after all, and more
1955 * useful for the kernel->userspace direction anyway.
1956 */
1957 out->type = in->type & DRM_MODE_TYPE_ALL;
Daniel Stone934a8a82015-05-22 13:34:48 +01001958 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1959 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1960
Shashank Sharma222ec162018-05-08 16:39:44 +05301961 /* Clearing picture aspect ratio bits from out flags,
1962 * as the aspect-ratio information is not stored in
1963 * flags for kernel-mode, but in picture_aspect_ratio.
1964 */
1965 out->flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
1966
1967 switch (in->flags & DRM_MODE_FLAG_PIC_AR_MASK) {
1968 case DRM_MODE_FLAG_PIC_AR_4_3:
1969 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_4_3;
1970 break;
1971 case DRM_MODE_FLAG_PIC_AR_16_9:
1972 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_16_9;
1973 break;
Shashank Sharma900aa8a2018-05-08 16:39:45 +05301974 case DRM_MODE_FLAG_PIC_AR_64_27:
1975 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_64_27;
1976 break;
1977 case DRM_MODE_FLAG_PIC_AR_256_135:
1978 out->picture_aspect_ratio |= HDMI_PICTURE_ASPECT_256_135;
1979 break;
Shashank Sharma222ec162018-05-08 16:39:44 +05301980 default:
1981 out->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
1982 break;
1983 }
1984
Ville Syrjälä75a655e2017-11-14 20:32:57 +02001985 out->status = drm_mode_validate_driver(dev, out);
Daniel Stone934a8a82015-05-22 13:34:48 +01001986 if (out->status != MODE_OK)
Ville Syrjälädf550542018-03-13 17:07:57 +02001987 return -EINVAL;
Daniel Stone934a8a82015-05-22 13:34:48 +01001988
Tomi Valkeinenb201e742016-05-31 15:03:15 +03001989 drm_mode_set_crtcinfo(out, CRTC_INTERLACE_HALVE_V);
1990
Ville Syrjälädf550542018-03-13 17:07:57 +02001991 return 0;
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +01001992}
Shashank Sharma2570fe22017-07-13 21:03:14 +05301993
1994/**
1995 * drm_mode_is_420_only - if a given videomode can be only supported in YCBCR420
1996 * output format
1997 *
Sean Paul8d0873a2017-07-20 13:47:40 -04001998 * @display: display under action
Shashank Sharma2570fe22017-07-13 21:03:14 +05301999 * @mode: video mode to be tested.
2000 *
2001 * Returns:
2002 * true if the mode can be supported in YCBCR420 format
2003 * false if not.
2004 */
2005bool drm_mode_is_420_only(const struct drm_display_info *display,
2006 const struct drm_display_mode *mode)
2007{
2008 u8 vic = drm_match_cea_mode(mode);
2009
2010 return test_bit(vic, display->hdmi.y420_vdb_modes);
2011}
2012EXPORT_SYMBOL(drm_mode_is_420_only);
2013
2014/**
2015 * drm_mode_is_420_also - if a given videomode can be supported in YCBCR420
2016 * output format also (along with RGB/YCBCR444/422)
2017 *
2018 * @display: display under action.
2019 * @mode: video mode to be tested.
2020 *
2021 * Returns:
2022 * true if the mode can be support YCBCR420 format
2023 * false if not.
2024 */
2025bool drm_mode_is_420_also(const struct drm_display_info *display,
2026 const struct drm_display_mode *mode)
2027{
2028 u8 vic = drm_match_cea_mode(mode);
2029
2030 return test_bit(vic, display->hdmi.y420_cmdb_modes);
2031}
2032EXPORT_SYMBOL(drm_mode_is_420_also);
2033/**
2034 * drm_mode_is_420 - if a given videomode can be supported in YCBCR420
2035 * output format
2036 *
2037 * @display: display under action.
2038 * @mode: video mode to be tested.
2039 *
2040 * Returns:
2041 * true if the mode can be supported in YCBCR420 format
2042 * false if not.
2043 */
2044bool drm_mode_is_420(const struct drm_display_info *display,
2045 const struct drm_display_mode *mode)
2046{
2047 return drm_mode_is_420_only(display, mode) ||
2048 drm_mode_is_420_also(display, mode);
2049}
2050EXPORT_SYMBOL(drm_mode_is_420);