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