drm/i915: Update to Linux 4.1
[dragonfly.git] / sys / dev / 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 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <video/videomode.h>
39 #include <drm/drm_modes.h>
40
41 #include "drm_crtc_internal.h"
42
43 /**
44  * drm_mode_debug_printmodeline - print a mode to dmesg
45  * @mode: mode to print
46  *
47  * Describe @mode using DRM_DEBUG.
48  */
49 void drm_mode_debug_printmodeline(const struct drm_display_mode *mode)
50 {
51         DRM_DEBUG_KMS("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d "
52                         "0x%x 0x%x\n",
53                 mode->base.id, mode->name, mode->vrefresh, mode->clock,
54                 mode->hdisplay, mode->hsync_start,
55                 mode->hsync_end, mode->htotal,
56                 mode->vdisplay, mode->vsync_start,
57                 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
58 }
59 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
60
61 /**
62  * drm_mode_create - create a new display mode
63  * @dev: DRM device
64  *
65  * Create a new, cleared drm_display_mode with kzalloc, allocate an ID for it
66  * and return it.
67  *
68  * Returns:
69  * Pointer to new mode on success, NULL on error.
70  */
71 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
72 {
73         struct drm_display_mode *nmode;
74
75         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
76         if (!nmode)
77                 return NULL;
78
79         if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
80                 kfree(nmode);
81                 return NULL;
82         }
83
84         return nmode;
85 }
86 EXPORT_SYMBOL(drm_mode_create);
87
88 /**
89  * drm_mode_destroy - remove a mode
90  * @dev: DRM device
91  * @mode: mode to remove
92  *
93  * Release @mode's unique ID, then free it @mode structure itself using kfree.
94  */
95 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
96 {
97         if (!mode)
98                 return;
99
100         drm_mode_object_put(dev, &mode->base);
101
102         kfree(mode);
103 }
104 EXPORT_SYMBOL(drm_mode_destroy);
105
106 /**
107  * drm_mode_probed_add - add a mode to a connector's probed_mode list
108  * @connector: connector the new mode
109  * @mode: mode data
110  *
111  * Add @mode to @connector's probed_mode list for later use. This list should
112  * then in a second step get filtered and all the modes actually supported by
113  * the hardware moved to the @connector's modes list.
114  */
115 void drm_mode_probed_add(struct drm_connector *connector,
116                          struct drm_display_mode *mode)
117 {
118         WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
119
120         list_add_tail(&mode->head, &connector->probed_modes);
121 }
122 EXPORT_SYMBOL(drm_mode_probed_add);
123
124 /**
125  * drm_cvt_mode -create a modeline based on the CVT algorithm
126  * @dev: drm device
127  * @hdisplay: hdisplay size
128  * @vdisplay: vdisplay size
129  * @vrefresh: vrefresh rate
130  * @reduced: whether to use reduced blanking
131  * @interlaced: whether to compute an interlaced mode
132  * @margins: whether to add margins (borders)
133  *
134  * This function is called to generate the modeline based on CVT algorithm
135  * according to the hdisplay, vdisplay, vrefresh.
136  * It is based from the VESA(TM) Coordinated Video Timing Generator by
137  * Graham Loveridge April 9, 2003 available at
138  * http://www.elo.utfsm.cl/~elo212/docs/CVTd6r1.xls 
139  *
140  * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
141  * What I have done is to translate it by using integer calculation.
142  *
143  * Returns:
144  * The modeline based on the CVT algorithm stored in a drm_display_mode object.
145  * The display mode object is allocated with drm_mode_create(). Returns NULL
146  * when no mode could be allocated.
147  */
148 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
149                                       int vdisplay, int vrefresh,
150                                       bool reduced, bool interlaced, bool margins)
151 {
152 #define HV_FACTOR                       1000
153         /* 1) top/bottom margin size (% of height) - default: 1.8, */
154 #define CVT_MARGIN_PERCENTAGE           18
155         /* 2) character cell horizontal granularity (pixels) - default 8 */
156 #define CVT_H_GRANULARITY               8
157         /* 3) Minimum vertical porch (lines) - default 3 */
158 #define CVT_MIN_V_PORCH                 3
159         /* 4) Minimum number of vertical back porch lines - default 6 */
160 #define CVT_MIN_V_BPORCH                6
161         /* Pixel Clock step (kHz) */
162 #define CVT_CLOCK_STEP                  250
163         struct drm_display_mode *drm_mode;
164         unsigned int vfieldrate, hperiod;
165         int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
166         int interlace;
167
168         /* allocate the drm_display_mode structure. If failure, we will
169          * return directly
170          */
171         drm_mode = drm_mode_create(dev);
172         if (!drm_mode)
173                 return NULL;
174
175         /* the CVT default refresh rate is 60Hz */
176         if (!vrefresh)
177                 vrefresh = 60;
178
179         /* the required field fresh rate */
180         if (interlaced)
181                 vfieldrate = vrefresh * 2;
182         else
183                 vfieldrate = vrefresh;
184
185         /* horizontal pixels */
186         hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
187
188         /* determine the left&right borders */
189         hmargin = 0;
190         if (margins) {
191                 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
192                 hmargin -= hmargin % CVT_H_GRANULARITY;
193         }
194         /* find the total active pixels */
195         drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
196
197         /* find the number of lines per field */
198         if (interlaced)
199                 vdisplay_rnd = vdisplay / 2;
200         else
201                 vdisplay_rnd = vdisplay;
202
203         /* find the top & bottom borders */
204         vmargin = 0;
205         if (margins)
206                 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
207
208         drm_mode->vdisplay = vdisplay + 2 * vmargin;
209
210         /* Interlaced */
211         if (interlaced)
212                 interlace = 1;
213         else
214                 interlace = 0;
215
216         /* Determine VSync Width from aspect ratio */
217         if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
218                 vsync = 4;
219         else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
220                 vsync = 5;
221         else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
222                 vsync = 6;
223         else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
224                 vsync = 7;
225         else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
226                 vsync = 7;
227         else /* custom */
228                 vsync = 10;
229
230         if (!reduced) {
231                 /* simplify the GTF calculation */
232                 /* 4) Minimum time of vertical sync + back porch interval (µs)
233                  * default 550.0
234                  */
235                 int tmp1, tmp2;
236 #define CVT_MIN_VSYNC_BP        550
237                 /* 3) Nominal HSync width (% of line period) - default 8 */
238 #define CVT_HSYNC_PERCENTAGE    8
239                 unsigned int hblank_percentage;
240                 int vsyncandback_porch, vback_porch, hblank;
241
242                 /* estimated the horizontal period */
243                 tmp1 = HV_FACTOR * 1000000  -
244                                 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
245                 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
246                                 interlace;
247                 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
248
249                 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
250                 /* 9. Find number of lines in sync + backporch */
251                 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
252                         vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
253                 else
254                         vsyncandback_porch = tmp1;
255                 /* 10. Find number of lines in back porch */
256                 vback_porch = vsyncandback_porch - vsync;
257                 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
258                                 vsyncandback_porch + CVT_MIN_V_PORCH;
259                 /* 5) Definition of Horizontal blanking time limitation */
260                 /* Gradient (%/kHz) - default 600 */
261 #define CVT_M_FACTOR    600
262                 /* Offset (%) - default 40 */
263 #define CVT_C_FACTOR    40
264                 /* Blanking time scaling factor - default 128 */
265 #define CVT_K_FACTOR    128
266                 /* Scaling factor weighting - default 20 */
267 #define CVT_J_FACTOR    20
268 #define CVT_M_PRIME     (CVT_M_FACTOR * CVT_K_FACTOR / 256)
269 #define CVT_C_PRIME     ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
270                          CVT_J_FACTOR)
271                 /* 12. Find ideal blanking duty cycle from formula */
272                 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
273                                         hperiod / 1000;
274                 /* 13. Blanking time */
275                 if (hblank_percentage < 20 * HV_FACTOR)
276                         hblank_percentage = 20 * HV_FACTOR;
277                 hblank = drm_mode->hdisplay * hblank_percentage /
278                          (100 * HV_FACTOR - hblank_percentage);
279                 hblank -= hblank % (2 * CVT_H_GRANULARITY);
280                 /* 14. find the total pixels per line */
281                 drm_mode->htotal = drm_mode->hdisplay + hblank;
282                 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
283                 drm_mode->hsync_start = drm_mode->hsync_end -
284                         (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
285                 drm_mode->hsync_start += CVT_H_GRANULARITY -
286                         drm_mode->hsync_start % CVT_H_GRANULARITY;
287                 /* fill the Vsync values */
288                 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
289                 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
290         } else {
291                 /* Reduced blanking */
292                 /* Minimum vertical blanking interval time (µs)- default 460 */
293 #define CVT_RB_MIN_VBLANK       460
294                 /* Fixed number of clocks for horizontal sync */
295 #define CVT_RB_H_SYNC           32
296                 /* Fixed number of clocks for horizontal blanking */
297 #define CVT_RB_H_BLANK          160
298                 /* Fixed number of lines for vertical front porch - default 3*/
299 #define CVT_RB_VFPORCH          3
300                 int vbilines;
301                 int tmp1, tmp2;
302                 /* 8. Estimate Horizontal period. */
303                 tmp1 = HV_FACTOR * 1000000 -
304                         CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
305                 tmp2 = vdisplay_rnd + 2 * vmargin;
306                 hperiod = tmp1 / (tmp2 * vfieldrate);
307                 /* 9. Find number of lines in vertical blanking */
308                 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
309                 /* 10. Check if vertical blanking is sufficient */
310                 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
311                         vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
312                 /* 11. Find total number of lines in vertical field */
313                 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
314                 /* 12. Find total number of pixels in a line */
315                 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
316                 /* Fill in HSync values */
317                 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
318                 drm_mode->hsync_start = drm_mode->hsync_end - CVT_RB_H_SYNC;
319                 /* Fill in VSync values */
320                 drm_mode->vsync_start = drm_mode->vdisplay + CVT_RB_VFPORCH;
321                 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
322         }
323         /* 15/13. Find pixel clock frequency (kHz for xf86) */
324         drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
325         drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
326         /* 18/16. Find actual vertical frame frequency */
327         /* ignore - just set the mode flag for interlaced */
328         if (interlaced) {
329                 drm_mode->vtotal *= 2;
330                 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
331         }
332         /* Fill the mode line name */
333         drm_mode_set_name(drm_mode);
334         if (reduced)
335                 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
336                                         DRM_MODE_FLAG_NVSYNC);
337         else
338                 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
339                                         DRM_MODE_FLAG_NHSYNC);
340
341         return drm_mode;
342 }
343 EXPORT_SYMBOL(drm_cvt_mode);
344
345 /**
346  * drm_gtf_mode_complex - create the modeline based on the full GTF algorithm
347  * @dev: drm device
348  * @hdisplay: hdisplay size
349  * @vdisplay: vdisplay size
350  * @vrefresh: vrefresh rate.
351  * @interlaced: whether to compute an interlaced mode
352  * @margins: desired margin (borders) size
353  * @GTF_M: extended GTF formula parameters
354  * @GTF_2C: extended GTF formula parameters
355  * @GTF_K: extended GTF formula parameters
356  * @GTF_2J: extended GTF formula parameters
357  *
358  * GTF feature blocks specify C and J in multiples of 0.5, so we pass them
359  * in here multiplied by two.  For a C of 40, pass in 80.
360  *
361  * Returns:
362  * The modeline based on the full GTF algorithm stored in a drm_display_mode object.
363  * The display mode object is allocated with drm_mode_create(). Returns NULL
364  * when no mode could be allocated.
365  */
366 struct drm_display_mode *
367 drm_gtf_mode_complex(struct drm_device *dev, int hdisplay, int vdisplay,
368                      int vrefresh, bool interlaced, int margins,
369                      int GTF_M, int GTF_2C, int GTF_K, int GTF_2J)
370 {       /* 1) top/bottom margin size (% of height) - default: 1.8, */
371 #define GTF_MARGIN_PERCENTAGE           18
372         /* 2) character cell horizontal granularity (pixels) - default 8 */
373 #define GTF_CELL_GRAN                   8
374         /* 3) Minimum vertical porch (lines) - default 3 */
375 #define GTF_MIN_V_PORCH                 1
376         /* width of vsync in lines */
377 #define V_SYNC_RQD                      3
378         /* width of hsync as % of total line */
379 #define H_SYNC_PERCENT                  8
380         /* min time of vsync + back porch (microsec) */
381 #define MIN_VSYNC_PLUS_BP               550
382         /* C' and M' are part of the Blanking Duty Cycle computation */
383 #define GTF_C_PRIME     ((((GTF_2C - GTF_2J) * GTF_K / 256) + GTF_2J) / 2)
384 #define GTF_M_PRIME     (GTF_K * GTF_M / 256)
385         struct drm_display_mode *drm_mode;
386         unsigned int hdisplay_rnd, vdisplay_rnd, vfieldrate_rqd;
387         int top_margin, bottom_margin;
388         int interlace;
389         unsigned int hfreq_est;
390         int vsync_plus_bp, vback_porch;
391         unsigned int vtotal_lines, vfieldrate_est, hperiod;
392         unsigned int vfield_rate, vframe_rate;
393         int left_margin, right_margin;
394         unsigned int total_active_pixels, ideal_duty_cycle;
395         unsigned int hblank, total_pixels, pixel_freq;
396         int hsync, hfront_porch, vodd_front_porch_lines;
397         unsigned int tmp1, tmp2;
398
399         drm_mode = drm_mode_create(dev);
400         if (!drm_mode)
401                 return NULL;
402
403         /* 1. In order to give correct results, the number of horizontal
404          * pixels requested is first processed to ensure that it is divisible
405          * by the character size, by rounding it to the nearest character
406          * cell boundary:
407          */
408         hdisplay_rnd = (hdisplay + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
409         hdisplay_rnd = hdisplay_rnd * GTF_CELL_GRAN;
410
411         /* 2. If interlace is requested, the number of vertical lines assumed
412          * by the calculation must be halved, as the computation calculates
413          * the number of vertical lines per field.
414          */
415         if (interlaced)
416                 vdisplay_rnd = vdisplay / 2;
417         else
418                 vdisplay_rnd = vdisplay;
419
420         /* 3. Find the frame rate required: */
421         if (interlaced)
422                 vfieldrate_rqd = vrefresh * 2;
423         else
424                 vfieldrate_rqd = vrefresh;
425
426         /* 4. Find number of lines in Top margin: */
427         top_margin = 0;
428         if (margins)
429                 top_margin = (vdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
430                                 1000;
431         /* 5. Find number of lines in bottom margin: */
432         bottom_margin = top_margin;
433
434         /* 6. If interlace is required, then set variable interlace: */
435         if (interlaced)
436                 interlace = 1;
437         else
438                 interlace = 0;
439
440         /* 7. Estimate the Horizontal frequency */
441         {
442                 tmp1 = (1000000  - MIN_VSYNC_PLUS_BP * vfieldrate_rqd) / 500;
443                 tmp2 = (vdisplay_rnd + 2 * top_margin + GTF_MIN_V_PORCH) *
444                                 2 + interlace;
445                 hfreq_est = (tmp2 * 1000 * vfieldrate_rqd) / tmp1;
446         }
447
448         /* 8. Find the number of lines in V sync + back porch */
449         /* [V SYNC+BP] = RINT(([MIN VSYNC+BP] * hfreq_est / 1000000)) */
450         vsync_plus_bp = MIN_VSYNC_PLUS_BP * hfreq_est / 1000;
451         vsync_plus_bp = (vsync_plus_bp + 500) / 1000;
452         /*  9. Find the number of lines in V back porch alone: */
453         vback_porch = vsync_plus_bp - V_SYNC_RQD;
454         /*  10. Find the total number of lines in Vertical field period: */
455         vtotal_lines = vdisplay_rnd + top_margin + bottom_margin +
456                         vsync_plus_bp + GTF_MIN_V_PORCH;
457         /*  11. Estimate the Vertical field frequency: */
458         vfieldrate_est = hfreq_est / vtotal_lines;
459         /*  12. Find the actual horizontal period: */
460         hperiod = 1000000 / (vfieldrate_rqd * vtotal_lines);
461
462         /*  13. Find the actual Vertical field frequency: */
463         vfield_rate = hfreq_est / vtotal_lines;
464         /*  14. Find the Vertical frame frequency: */
465         if (interlaced)
466                 vframe_rate = vfield_rate / 2;
467         else
468                 vframe_rate = vfield_rate;
469         /*  15. Find number of pixels in left margin: */
470         if (margins)
471                 left_margin = (hdisplay_rnd * GTF_MARGIN_PERCENTAGE + 500) /
472                                 1000;
473         else
474                 left_margin = 0;
475
476         /* 16.Find number of pixels in right margin: */
477         right_margin = left_margin;
478         /* 17.Find total number of active pixels in image and left and right */
479         total_active_pixels = hdisplay_rnd + left_margin + right_margin;
480         /* 18.Find the ideal blanking duty cycle from blanking duty cycle */
481         ideal_duty_cycle = GTF_C_PRIME * 1000 -
482                                 (GTF_M_PRIME * 1000000 / hfreq_est);
483         /* 19.Find the number of pixels in the blanking time to the nearest
484          * double character cell: */
485         hblank = total_active_pixels * ideal_duty_cycle /
486                         (100000 - ideal_duty_cycle);
487         hblank = (hblank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN);
488         hblank = hblank * 2 * GTF_CELL_GRAN;
489         /* 20.Find total number of pixels: */
490         total_pixels = total_active_pixels + hblank;
491         /* 21.Find pixel clock frequency: */
492         pixel_freq = total_pixels * hfreq_est / 1000;
493         /* Stage 1 computations are now complete; I should really pass
494          * the results to another function and do the Stage 2 computations,
495          * but I only need a few more values so I'll just append the
496          * computations here for now */
497         /* 17. Find the number of pixels in the horizontal sync period: */
498         hsync = H_SYNC_PERCENT * total_pixels / 100;
499         hsync = (hsync + GTF_CELL_GRAN / 2) / GTF_CELL_GRAN;
500         hsync = hsync * GTF_CELL_GRAN;
501         /* 18. Find the number of pixels in horizontal front porch period */
502         hfront_porch = hblank / 2 - hsync;
503         /*  36. Find the number of lines in the odd front porch period: */
504         vodd_front_porch_lines = GTF_MIN_V_PORCH ;
505
506         /* finally, pack the results in the mode struct */
507         drm_mode->hdisplay = hdisplay_rnd;
508         drm_mode->hsync_start = hdisplay_rnd + hfront_porch;
509         drm_mode->hsync_end = drm_mode->hsync_start + hsync;
510         drm_mode->htotal = total_pixels;
511         drm_mode->vdisplay = vdisplay_rnd;
512         drm_mode->vsync_start = vdisplay_rnd + vodd_front_porch_lines;
513         drm_mode->vsync_end = drm_mode->vsync_start + V_SYNC_RQD;
514         drm_mode->vtotal = vtotal_lines;
515
516         drm_mode->clock = pixel_freq;
517
518         if (interlaced) {
519                 drm_mode->vtotal *= 2;
520                 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
521         }
522
523         drm_mode_set_name(drm_mode);
524         if (GTF_M == 600 && GTF_2C == 80 && GTF_K == 128 && GTF_2J == 40)
525                 drm_mode->flags = DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC;
526         else
527                 drm_mode->flags = DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC;
528
529         return drm_mode;
530 }
531 EXPORT_SYMBOL(drm_gtf_mode_complex);
532
533 /**
534  * drm_gtf_mode - create the modeline based on the GTF algorithm
535  * @dev: drm device
536  * @hdisplay: hdisplay size
537  * @vdisplay: vdisplay size
538  * @vrefresh: vrefresh rate.
539  * @interlaced: whether to compute an interlaced mode
540  * @margins: desired margin (borders) size
541  *
542  * return the modeline based on GTF algorithm
543  *
544  * This function is to create the modeline based on the GTF algorithm.
545  * Generalized Timing Formula is derived from:
546  *      GTF Spreadsheet by Andy Morrish (1/5/97)
547  *      available at http://www.vesa.org
548  *
549  * And it is copied from the file of xserver/hw/xfree86/modes/xf86gtf.c.
550  * What I have done is to translate it by using integer calculation.
551  * I also refer to the function of fb_get_mode in the file of
552  * drivers/video/fbmon.c
553  *
554  * Standard GTF parameters:
555  * M = 600
556  * C = 40
557  * K = 128
558  * J = 20
559  *
560  * Returns:
561  * The modeline based on the GTF algorithm stored in a drm_display_mode object.
562  * The display mode object is allocated with drm_mode_create(). Returns NULL
563  * when no mode could be allocated.
564  */
565 struct drm_display_mode *
566 drm_gtf_mode(struct drm_device *dev, int hdisplay, int vdisplay, int vrefresh,
567              bool interlaced, int margins)
568 {
569         return drm_gtf_mode_complex(dev, hdisplay, vdisplay, vrefresh,
570                                     interlaced, margins,
571                                     600, 40 * 2, 128, 20 * 2);
572 }
573 EXPORT_SYMBOL(drm_gtf_mode);
574
575 #ifdef CONFIG_VIDEOMODE_HELPERS
576 /**
577  * drm_display_mode_from_videomode - fill in @dmode using @vm,
578  * @vm: videomode structure to use as source
579  * @dmode: drm_display_mode structure to use as destination
580  *
581  * Fills out @dmode using the display mode specified in @vm.
582  */
583 void drm_display_mode_from_videomode(const struct videomode *vm,
584                                      struct drm_display_mode *dmode)
585 {
586         dmode->hdisplay = vm->hactive;
587         dmode->hsync_start = dmode->hdisplay + vm->hfront_porch;
588         dmode->hsync_end = dmode->hsync_start + vm->hsync_len;
589         dmode->htotal = dmode->hsync_end + vm->hback_porch;
590
591         dmode->vdisplay = vm->vactive;
592         dmode->vsync_start = dmode->vdisplay + vm->vfront_porch;
593         dmode->vsync_end = dmode->vsync_start + vm->vsync_len;
594         dmode->vtotal = dmode->vsync_end + vm->vback_porch;
595
596         dmode->clock = vm->pixelclock / 1000;
597
598         dmode->flags = 0;
599         if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
600                 dmode->flags |= DRM_MODE_FLAG_PHSYNC;
601         else if (vm->flags & DISPLAY_FLAGS_HSYNC_LOW)
602                 dmode->flags |= DRM_MODE_FLAG_NHSYNC;
603         if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
604                 dmode->flags |= DRM_MODE_FLAG_PVSYNC;
605         else if (vm->flags & DISPLAY_FLAGS_VSYNC_LOW)
606                 dmode->flags |= DRM_MODE_FLAG_NVSYNC;
607         if (vm->flags & DISPLAY_FLAGS_INTERLACED)
608                 dmode->flags |= DRM_MODE_FLAG_INTERLACE;
609         if (vm->flags & DISPLAY_FLAGS_DOUBLESCAN)
610                 dmode->flags |= DRM_MODE_FLAG_DBLSCAN;
611         if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
612                 dmode->flags |= DRM_MODE_FLAG_DBLCLK;
613         drm_mode_set_name(dmode);
614 }
615
616 /**
617  * drm_display_mode_to_videomode - fill in @vm using @dmode,
618  * @dmode: drm_display_mode structure to use as source
619  * @vm: videomode structure to use as destination
620  *
621  * Fills out @vm using the display mode specified in @dmode.
622  */
623 void drm_display_mode_to_videomode(const struct drm_display_mode *dmode,
624                                    struct videomode *vm)
625 {
626         vm->hactive = dmode->hdisplay;
627         vm->hfront_porch = dmode->hsync_start - dmode->hdisplay;
628         vm->hsync_len = dmode->hsync_end - dmode->hsync_start;
629         vm->hback_porch = dmode->htotal - dmode->hsync_end;
630
631         vm->vactive = dmode->vdisplay;
632         vm->vfront_porch = dmode->vsync_start - dmode->vdisplay;
633         vm->vsync_len = dmode->vsync_end - dmode->vsync_start;
634         vm->vback_porch = dmode->vtotal - dmode->vsync_end;
635
636         vm->pixelclock = dmode->clock * 1000;
637
638         vm->flags = 0;
639         if (dmode->flags & DRM_MODE_FLAG_PHSYNC)
640                 vm->flags |= DISPLAY_FLAGS_HSYNC_HIGH;
641         else if (dmode->flags & DRM_MODE_FLAG_NHSYNC)
642                 vm->flags |= DISPLAY_FLAGS_HSYNC_LOW;
643         if (dmode->flags & DRM_MODE_FLAG_PVSYNC)
644                 vm->flags |= DISPLAY_FLAGS_VSYNC_HIGH;
645         else if (dmode->flags & DRM_MODE_FLAG_NVSYNC)
646                 vm->flags |= DISPLAY_FLAGS_VSYNC_LOW;
647         if (dmode->flags & DRM_MODE_FLAG_INTERLACE)
648                 vm->flags |= DISPLAY_FLAGS_INTERLACED;
649         if (dmode->flags & DRM_MODE_FLAG_DBLSCAN)
650                 vm->flags |= DISPLAY_FLAGS_DOUBLESCAN;
651         if (dmode->flags & DRM_MODE_FLAG_DBLCLK)
652                 vm->flags |= DISPLAY_FLAGS_DOUBLECLK;
653 }
654
655 #ifdef CONFIG_OF
656 /**
657  * of_get_drm_display_mode - get a drm_display_mode from devicetree
658  * @np: device_node with the timing specification
659  * @dmode: will be set to the return value
660  * @index: index into the list of display timings in devicetree
661  *
662  * This function is expensive and should only be used, if only one mode is to be
663  * read from DT. To get multiple modes start with of_get_display_timings and
664  * work with that instead.
665  *
666  * Returns:
667  * 0 on success, a negative errno code when no of videomode node was found.
668  */
669 int of_get_drm_display_mode(struct device_node *np,
670                             struct drm_display_mode *dmode, int index)
671 {
672         struct videomode vm;
673         int ret;
674
675         ret = of_get_videomode(np, &vm, index);
676         if (ret)
677                 return ret;
678
679         drm_display_mode_from_videomode(&vm, dmode);
680
681         pr_debug("%s: got %dx%d display mode from %s\n",
682                 of_node_full_name(np), vm.hactive, vm.vactive, np->name);
683         drm_mode_debug_printmodeline(dmode);
684
685         return 0;
686 }
687 #endif /* CONFIG_OF */
688 #endif /* CONFIG_VIDEOMODE_HELPERS */
689
690 /**
691  * drm_mode_set_name - set the name on a mode
692  * @mode: name will be set in this mode
693  *
694  * Set the name of @mode to a standard format which is <hdisplay>x<vdisplay>
695  * with an optional 'i' suffix for interlaced modes.
696  */
697 void drm_mode_set_name(struct drm_display_mode *mode)
698 {
699         bool interlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
700
701         ksnprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d%s",
702                  mode->hdisplay, mode->vdisplay,
703                  interlaced ? "i" : "");
704 }
705 EXPORT_SYMBOL(drm_mode_set_name);
706
707 /** drm_mode_hsync - get the hsync of a mode
708  * @mode: mode
709  *
710  * Returns:
711  * @modes's hsync rate in kHz, rounded to the nearest integer. Calculates the
712  * value first if it is not yet set.
713  */
714 int drm_mode_hsync(const struct drm_display_mode *mode)
715 {
716         unsigned int calc_val;
717
718         if (mode->hsync)
719                 return mode->hsync;
720
721         if (mode->htotal < 0)
722                 return 0;
723
724         calc_val = (mode->clock * 1000) / mode->htotal; /* hsync in Hz */
725         calc_val += 500;                                /* round to 1000Hz */
726         calc_val /= 1000;                               /* truncate to kHz */
727
728         return calc_val;
729 }
730 EXPORT_SYMBOL(drm_mode_hsync);
731
732 /**
733  * drm_mode_vrefresh - get the vrefresh of a mode
734  * @mode: mode
735  *
736  * Returns:
737  * @modes's vrefresh rate in Hz, rounded to the nearest integer. Calculates the
738  * value first if it is not yet set.
739  */
740 int drm_mode_vrefresh(const struct drm_display_mode *mode)
741 {
742         int refresh = 0;
743         unsigned int calc_val;
744
745         if (mode->vrefresh > 0)
746                 refresh = mode->vrefresh;
747         else if (mode->htotal > 0 && mode->vtotal > 0) {
748                 int vtotal;
749                 vtotal = mode->vtotal;
750                 /* work out vrefresh the value will be x1000 */
751                 calc_val = (mode->clock * 1000);
752                 calc_val /= mode->htotal;
753                 refresh = (calc_val + vtotal / 2) / vtotal;
754
755                 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
756                         refresh *= 2;
757                 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
758                         refresh /= 2;
759                 if (mode->vscan > 1)
760                         refresh /= mode->vscan;
761         }
762         return refresh;
763 }
764 EXPORT_SYMBOL(drm_mode_vrefresh);
765
766 /**
767  * drm_mode_set_crtcinfo - set CRTC modesetting timing parameters
768  * @p: mode
769  * @adjust_flags: a combination of adjustment flags
770  *
771  * Setup the CRTC modesetting timing parameters for @p, adjusting if necessary.
772  *
773  * - The CRTC_INTERLACE_HALVE_V flag can be used to halve vertical timings of
774  *   interlaced modes.
775  * - The CRTC_STEREO_DOUBLE flag can be used to compute the timings for
776  *   buffers containing two eyes (only adjust the timings when needed, eg. for
777  *   "frame packing" or "side by side full").
778  * - The CRTC_NO_DBLSCAN and CRTC_NO_VSCAN flags request that adjustment *not*
779  *   be performed for doublescan and vscan > 1 modes respectively.
780  */
781 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
782 {
783         if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
784                 return;
785
786         p->crtc_clock = p->clock;
787         p->crtc_hdisplay = p->hdisplay;
788         p->crtc_hsync_start = p->hsync_start;
789         p->crtc_hsync_end = p->hsync_end;
790         p->crtc_htotal = p->htotal;
791         p->crtc_hskew = p->hskew;
792         p->crtc_vdisplay = p->vdisplay;
793         p->crtc_vsync_start = p->vsync_start;
794         p->crtc_vsync_end = p->vsync_end;
795         p->crtc_vtotal = p->vtotal;
796
797         if (p->flags & DRM_MODE_FLAG_INTERLACE) {
798                 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
799                         p->crtc_vdisplay /= 2;
800                         p->crtc_vsync_start /= 2;
801                         p->crtc_vsync_end /= 2;
802                         p->crtc_vtotal /= 2;
803                 }
804         }
805
806         if (!(adjust_flags & CRTC_NO_DBLSCAN)) {
807                 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
808                         p->crtc_vdisplay *= 2;
809                         p->crtc_vsync_start *= 2;
810                         p->crtc_vsync_end *= 2;
811                         p->crtc_vtotal *= 2;
812                 }
813         }
814
815         if (!(adjust_flags & CRTC_NO_VSCAN)) {
816                 if (p->vscan > 1) {
817                         p->crtc_vdisplay *= p->vscan;
818                         p->crtc_vsync_start *= p->vscan;
819                         p->crtc_vsync_end *= p->vscan;
820                         p->crtc_vtotal *= p->vscan;
821                 }
822         }
823
824         if (adjust_flags & CRTC_STEREO_DOUBLE) {
825                 unsigned int layout = p->flags & DRM_MODE_FLAG_3D_MASK;
826
827                 switch (layout) {
828                 case DRM_MODE_FLAG_3D_FRAME_PACKING:
829                         p->crtc_clock *= 2;
830                         p->crtc_vdisplay += p->crtc_vtotal;
831                         p->crtc_vsync_start += p->crtc_vtotal;
832                         p->crtc_vsync_end += p->crtc_vtotal;
833                         p->crtc_vtotal += p->crtc_vtotal;
834                         break;
835                 }
836         }
837
838         p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
839         p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
840         p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
841         p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
842 }
843 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
844
845 /**
846  * drm_mode_copy - copy the mode
847  * @dst: mode to overwrite
848  * @src: mode to copy
849  *
850  * Copy an existing mode into another mode, preserving the object id and
851  * list head of the destination mode.
852  */
853 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
854 {
855         int id = dst->base.id;
856         struct list_head head = dst->head;
857
858         *dst = *src;
859         dst->base.id = id;
860         dst->head = head;
861 }
862 EXPORT_SYMBOL(drm_mode_copy);
863
864 /**
865  * drm_mode_duplicate - allocate and duplicate an existing mode
866  * @dev: drm_device to allocate the duplicated mode for
867  * @mode: mode to duplicate
868  *
869  * Just allocate a new mode, copy the existing mode into it, and return
870  * a pointer to it.  Used to create new instances of established modes.
871  *
872  * Returns:
873  * Pointer to duplicated mode on success, NULL on error.
874  */
875 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
876                                             const struct drm_display_mode *mode)
877 {
878         struct drm_display_mode *nmode;
879
880         nmode = drm_mode_create(dev);
881         if (!nmode)
882                 return NULL;
883
884         drm_mode_copy(nmode, mode);
885
886         return nmode;
887 }
888 EXPORT_SYMBOL(drm_mode_duplicate);
889
890 /**
891  * drm_mode_equal - test modes for equality
892  * @mode1: first mode
893  * @mode2: second mode
894  *
895  * Check to see if @mode1 and @mode2 are equivalent.
896  *
897  * Returns:
898  * True if the modes are equal, false otherwise.
899  */
900 bool drm_mode_equal(const struct drm_display_mode *mode1, const struct drm_display_mode *mode2)
901 {
902         if (!mode1 && !mode2)
903                 return true;
904
905         if (!mode1 || !mode2)
906                 return false;
907
908         /* do clock check convert to PICOS so fb modes get matched
909          * the same */
910         if (mode1->clock && mode2->clock) {
911                 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
912                         return false;
913         } else if (mode1->clock != mode2->clock)
914                 return false;
915
916         if ((mode1->flags & DRM_MODE_FLAG_3D_MASK) !=
917             (mode2->flags & DRM_MODE_FLAG_3D_MASK))
918                 return false;
919
920         return drm_mode_equal_no_clocks_no_stereo(mode1, mode2);
921 }
922 EXPORT_SYMBOL(drm_mode_equal);
923
924 /**
925  * drm_mode_equal_no_clocks_no_stereo - test modes for equality
926  * @mode1: first mode
927  * @mode2: second mode
928  *
929  * Check to see if @mode1 and @mode2 are equivalent, but
930  * don't check the pixel clocks nor the stereo layout.
931  *
932  * Returns:
933  * True if the modes are equal, false otherwise.
934  */
935 bool drm_mode_equal_no_clocks_no_stereo(const struct drm_display_mode *mode1,
936                                         const struct drm_display_mode *mode2)
937 {
938         if (mode1->hdisplay == mode2->hdisplay &&
939             mode1->hsync_start == mode2->hsync_start &&
940             mode1->hsync_end == mode2->hsync_end &&
941             mode1->htotal == mode2->htotal &&
942             mode1->hskew == mode2->hskew &&
943             mode1->vdisplay == mode2->vdisplay &&
944             mode1->vsync_start == mode2->vsync_start &&
945             mode1->vsync_end == mode2->vsync_end &&
946             mode1->vtotal == mode2->vtotal &&
947             mode1->vscan == mode2->vscan &&
948             (mode1->flags & ~DRM_MODE_FLAG_3D_MASK) ==
949              (mode2->flags & ~DRM_MODE_FLAG_3D_MASK))
950                 return true;
951
952         return false;
953 }
954 EXPORT_SYMBOL(drm_mode_equal_no_clocks_no_stereo);
955
956 /**
957  * drm_mode_validate_basic - make sure the mode is somewhat sane
958  * @mode: mode to check
959  *
960  * Check that the mode timings are at least somewhat reasonable.
961  * Any hardware specific limits are left up for each driver to check.
962  *
963  * Returns:
964  * The mode status
965  */
966 enum drm_mode_status
967 drm_mode_validate_basic(const struct drm_display_mode *mode)
968 {
969         if (mode->clock == 0)
970                 return MODE_CLOCK_LOW;
971
972         if (mode->hdisplay == 0 ||
973             mode->hsync_start < mode->hdisplay ||
974             mode->hsync_end < mode->hsync_start ||
975             mode->htotal < mode->hsync_end)
976                 return MODE_H_ILLEGAL;
977
978         if (mode->vdisplay == 0 ||
979             mode->vsync_start < mode->vdisplay ||
980             mode->vsync_end < mode->vsync_start ||
981             mode->vtotal < mode->vsync_end)
982                 return MODE_V_ILLEGAL;
983
984         return MODE_OK;
985 }
986 EXPORT_SYMBOL(drm_mode_validate_basic);
987
988 /**
989  * drm_mode_validate_size - make sure modes adhere to size constraints
990  * @mode: mode to check
991  * @maxX: maximum width
992  * @maxY: maximum height
993  *
994  * This function is a helper which can be used to validate modes against size
995  * limitations of the DRM device/connector. If a mode is too big its status
996  * member is updated with the appropriate validation failure code. The list
997  * itself is not changed.
998  *
999  * Returns:
1000  * The mode status
1001  */
1002 enum drm_mode_status
1003 drm_mode_validate_size(const struct drm_display_mode *mode,
1004                        int maxX, int maxY)
1005 {
1006         if (maxX > 0 && mode->hdisplay > maxX)
1007                 return MODE_VIRTUAL_X;
1008
1009         if (maxY > 0 && mode->vdisplay > maxY)
1010                 return MODE_VIRTUAL_Y;
1011
1012         return MODE_OK;
1013 }
1014 EXPORT_SYMBOL(drm_mode_validate_size);
1015
1016 /**
1017  * drm_mode_prune_invalid - remove invalid modes from mode list
1018  * @dev: DRM device
1019  * @mode_list: list of modes to check
1020  * @verbose: be verbose about it
1021  *
1022  * This helper function can be used to prune a display mode list after
1023  * validation has been completed. All modes who's status is not MODE_OK will be
1024  * removed from the list, and if @verbose the status code and mode name is also
1025  * printed to dmesg.
1026  */
1027 void drm_mode_prune_invalid(struct drm_device *dev,
1028                             struct list_head *mode_list, bool verbose)
1029 {
1030         struct drm_display_mode *mode, *t;
1031
1032         list_for_each_entry_safe(mode, t, mode_list, head) {
1033                 if (mode->status != MODE_OK) {
1034                         list_del(&mode->head);
1035                         if (verbose) {
1036                                 drm_mode_debug_printmodeline(mode);
1037                                 DRM_DEBUG_KMS("Not using %s mode %d\n",
1038                                         mode->name, mode->status);
1039                         }
1040                         drm_mode_destroy(dev, mode);
1041                 }
1042         }
1043 }
1044 EXPORT_SYMBOL(drm_mode_prune_invalid);
1045
1046 /**
1047  * drm_mode_compare - compare modes for favorability
1048  * @priv: unused
1049  * @lh_a: list_head for first mode
1050  * @lh_b: list_head for second mode
1051  *
1052  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
1053  * which is better.
1054  *
1055  * Returns:
1056  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
1057  * positive if @lh_b is better than @lh_a.
1058  */
1059 static int drm_mode_compare(void *priv, struct list_head *lh_a, struct list_head *lh_b)
1060 {
1061         struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
1062         struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
1063         int diff;
1064
1065         diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
1066                 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
1067         if (diff)
1068                 return diff;
1069         diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
1070         if (diff)
1071                 return diff;
1072
1073         diff = b->vrefresh - a->vrefresh;
1074         if (diff)
1075                 return diff;
1076
1077         diff = b->clock - a->clock;
1078         return diff;
1079 }
1080
1081 /**
1082  * drm_mode_sort - sort mode list
1083  * @mode_list: list of drm_display_mode structures to sort
1084  *
1085  * Sort @mode_list by favorability, moving good modes to the head of the list.
1086  */
1087 void drm_mode_sort(struct list_head *mode_list)
1088 {
1089         list_sort(NULL, mode_list, drm_mode_compare);
1090 }
1091 EXPORT_SYMBOL(drm_mode_sort);
1092
1093 /**
1094  * drm_mode_connector_list_update - update the mode list for the connector
1095  * @connector: the connector to update
1096  * @merge_type_bits: whether to merge or overwrite type bits
1097  *
1098  * This moves the modes from the @connector probed_modes list
1099  * to the actual mode list. It compares the probed mode against the current
1100  * list and only adds different/new modes.
1101  *
1102  * This is just a helper functions doesn't validate any modes itself and also
1103  * doesn't prune any invalid modes. Callers need to do that themselves.
1104  */
1105 void drm_mode_connector_list_update(struct drm_connector *connector,
1106                                     bool merge_type_bits)
1107 {
1108         struct drm_display_mode *mode;
1109         struct drm_display_mode *pmode, *pt;
1110         int found_it;
1111
1112         WARN_ON(!mutex_is_locked(&connector->dev->mode_config.mutex));
1113
1114         list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
1115                                  head) {
1116                 found_it = 0;
1117                 /* go through current modes checking for the new probed mode */
1118                 list_for_each_entry(mode, &connector->modes, head) {
1119                         if (drm_mode_equal(pmode, mode)) {
1120                                 found_it = 1;
1121                                 /* if equal delete the probed mode */
1122                                 mode->status = pmode->status;
1123                                 /* Merge type bits together */
1124                                 if (merge_type_bits)
1125                                         mode->type |= pmode->type;
1126                                 else
1127                                         mode->type = pmode->type;
1128                                 list_del(&pmode->head);
1129                                 drm_mode_destroy(connector->dev, pmode);
1130                                 break;
1131                         }
1132                 }
1133
1134                 if (!found_it) {
1135                         list_move_tail(&pmode->head, &connector->modes);
1136                 }
1137         }
1138 }
1139 EXPORT_SYMBOL(drm_mode_connector_list_update);
1140
1141 /**
1142  * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
1143  * @mode_option: optional per connector mode option
1144  * @connector: connector to parse modeline for
1145  * @mode: preallocated drm_cmdline_mode structure to fill out
1146  *
1147  * This parses @mode_option command line modeline for modes and options to
1148  * configure the connector. If @mode_option is NULL the default command line
1149  * modeline in fb_mode_option will be parsed instead.
1150  *
1151  * This uses the same parameters as the fb modedb.c, except for an extra
1152  * force-enable, force-enable-digital and force-disable bit at the end:
1153  *
1154  *      <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
1155  *
1156  * The intermediate drm_cmdline_mode structure is required to store additional
1157  * options from the command line modline like the force-enable/disable flag.
1158  *
1159  * Returns:
1160  * True if a valid modeline has been parsed, false otherwise.
1161  */
1162 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1163                                                struct drm_connector *connector,
1164                                                struct drm_cmdline_mode *mode)
1165 {
1166         const char *name;
1167         unsigned int namelen;
1168         bool res_specified = false, bpp_specified = false, refresh_specified = false;
1169         unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
1170         bool yres_specified = false, cvt = false, rb = false;
1171         bool interlace = false, margins = false, was_digit = false;
1172         int i;
1173         enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
1174
1175 #ifdef CONFIG_FB
1176         if (!mode_option)
1177                 mode_option = fb_mode_option;
1178 #endif
1179
1180         if (!mode_option) {
1181                 mode->specified = false;
1182                 return false;
1183         }
1184
1185         name = mode_option;
1186         namelen = strlen(name);
1187         for (i = namelen-1; i >= 0; i--) {
1188                 switch (name[i]) {
1189                 case '@':
1190                         if (!refresh_specified && !bpp_specified &&
1191                             !yres_specified && !cvt && !rb && was_digit) {
1192                                 refresh = simple_strtol(&name[i+1], NULL, 10);
1193                                 refresh_specified = true;
1194                                 was_digit = false;
1195                         } else
1196                                 goto done;
1197                         break;
1198                 case '-':
1199                         if (!bpp_specified && !yres_specified && !cvt &&
1200                             !rb && was_digit) {
1201                                 bpp = simple_strtol(&name[i+1], NULL, 10);
1202                                 bpp_specified = true;
1203                                 was_digit = false;
1204                         } else
1205                                 goto done;
1206                         break;
1207                 case 'x':
1208                         if (!yres_specified && was_digit) {
1209                                 yres = simple_strtol(&name[i+1], NULL, 10);
1210                                 yres_specified = true;
1211                                 was_digit = false;
1212                         } else
1213                                 goto done;
1214                         break;
1215                 case '0' ... '9':
1216                         was_digit = true;
1217                         break;
1218                 case 'M':
1219                         if (yres_specified || cvt || was_digit)
1220                                 goto done;
1221                         cvt = true;
1222                         break;
1223                 case 'R':
1224                         if (yres_specified || cvt || rb || was_digit)
1225                                 goto done;
1226                         rb = true;
1227                         break;
1228                 case 'm':
1229                         if (cvt || yres_specified || was_digit)
1230                                 goto done;
1231                         margins = true;
1232                         break;
1233                 case 'i':
1234                         if (cvt || yres_specified || was_digit)
1235                                 goto done;
1236                         interlace = true;
1237                         break;
1238                 case 'e':
1239                         if (yres_specified || bpp_specified || refresh_specified ||
1240                             was_digit || (force != DRM_FORCE_UNSPECIFIED))
1241                                 goto done;
1242
1243                         force = DRM_FORCE_ON;
1244                         break;
1245                 case 'D':
1246                         if (yres_specified || bpp_specified || refresh_specified ||
1247                             was_digit || (force != DRM_FORCE_UNSPECIFIED))
1248                                 goto done;
1249
1250                         if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1251                             (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1252                                 force = DRM_FORCE_ON;
1253                         else
1254                                 force = DRM_FORCE_ON_DIGITAL;
1255                         break;
1256                 case 'd':
1257                         if (yres_specified || bpp_specified || refresh_specified ||
1258                             was_digit || (force != DRM_FORCE_UNSPECIFIED))
1259                                 goto done;
1260
1261                         force = DRM_FORCE_OFF;
1262                         break;
1263                 default:
1264                         goto done;
1265                 }
1266         }
1267
1268         if (i < 0 && yres_specified) {
1269                 char *ch;
1270                 xres = simple_strtol(name, &ch, 10);
1271                 if ((ch != NULL) && (*ch == 'x'))
1272                         res_specified = true;
1273                 else
1274                         i = ch - name;
1275         } else if (!yres_specified && was_digit) {
1276                 /* catch mode that begins with digits but has no 'x' */
1277                 i = 0;
1278         }
1279 done:
1280         if (i >= 0) {
1281                 printk(KERN_WARNING
1282                         "parse error at position %i in video mode '%s'\n",
1283                         i, name);
1284                 mode->specified = false;
1285                 return false;
1286         }
1287
1288         if (res_specified) {
1289                 mode->specified = true;
1290                 mode->xres = xres;
1291                 mode->yres = yres;
1292         }
1293
1294         if (refresh_specified) {
1295                 mode->refresh_specified = true;
1296                 mode->refresh = refresh;
1297         }
1298
1299         if (bpp_specified) {
1300                 mode->bpp_specified = true;
1301                 mode->bpp = bpp;
1302         }
1303         mode->rb = rb;
1304         mode->cvt = cvt;
1305         mode->interlace = interlace;
1306         mode->margins = margins;
1307         mode->force = force;
1308
1309         return true;
1310 }
1311 EXPORT_SYMBOL(drm_mode_parse_command_line_for_connector);
1312
1313 /**
1314  * drm_mode_create_from_cmdline_mode - convert a command line modeline into a DRM display mode
1315  * @dev: DRM device to create the new mode for
1316  * @cmd: input command line modeline
1317  *
1318  * Returns:
1319  * Pointer to converted mode on success, NULL on error.
1320  */
1321 struct drm_display_mode *
1322 drm_mode_create_from_cmdline_mode(struct drm_device *dev,
1323                                   struct drm_cmdline_mode *cmd)
1324 {
1325         struct drm_display_mode *mode;
1326
1327         if (cmd->cvt)
1328                 mode = drm_cvt_mode(dev,
1329                                     cmd->xres, cmd->yres,
1330                                     cmd->refresh_specified ? cmd->refresh : 60,
1331                                     cmd->rb, cmd->interlace,
1332                                     cmd->margins);
1333         else
1334                 mode = drm_gtf_mode(dev,
1335                                     cmd->xres, cmd->yres,
1336                                     cmd->refresh_specified ? cmd->refresh : 60,
1337                                     cmd->interlace,
1338                                     cmd->margins);
1339         if (!mode)
1340                 return NULL;
1341
1342         mode->type |= DRM_MODE_TYPE_USERDEF;
1343         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1344         return mode;
1345 }
1346 EXPORT_SYMBOL(drm_mode_create_from_cmdline_mode);