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