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