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