a15ce0cf7b0a9c35424b661aae38bb80521a86fe
[platform/upstream/libdrm.git] / linux-core / drm_crtc.h
1 /*
2  * Copyright © 2006 Keith Packard
3  * Copyright © 2007 Intel Corporation
4  *   Jesse Barnes <jesse.barnes@intel.com>
5  */
6 #ifndef __DRM_CRTC_H__
7 #define __DRM_CRTC_H__
8
9 #include <linux/i2c.h>
10 #include <linux/spinlock.h>
11 #include <linux/types.h>
12 #include <linux/idr.h>
13
14 #include <linux/fb.h>
15
16 struct drm_device;
17
18 /*
19  * Note on terminology:  here, for brevity and convenience, we refer to output
20  * control chips as 'CRTCs'.  They can control any type of output, VGA, LVDS,
21  * DVI, etc.  And 'screen' refers to the whole of the visible display, which
22  * may span multiple monitors (and therefore multiple CRTC and output
23  * structures).
24  */
25
26 enum drm_mode_status {
27     MODE_OK     = 0,    /* Mode OK */
28     MODE_HSYNC,         /* hsync out of range */
29     MODE_VSYNC,         /* vsync out of range */
30     MODE_H_ILLEGAL,     /* mode has illegal horizontal timings */
31     MODE_V_ILLEGAL,     /* mode has illegal horizontal timings */
32     MODE_BAD_WIDTH,     /* requires an unsupported linepitch */
33     MODE_NOMODE,        /* no mode with a maching name */
34     MODE_NO_INTERLACE,  /* interlaced mode not supported */
35     MODE_NO_DBLESCAN,   /* doublescan mode not supported */
36     MODE_NO_VSCAN,      /* multiscan mode not supported */
37     MODE_MEM,           /* insufficient video memory */
38     MODE_VIRTUAL_X,     /* mode width too large for specified virtual size */
39     MODE_VIRTUAL_Y,     /* mode height too large for specified virtual size */
40     MODE_MEM_VIRT,      /* insufficient video memory given virtual size */
41     MODE_NOCLOCK,       /* no fixed clock available */
42     MODE_CLOCK_HIGH,    /* clock required is too high */
43     MODE_CLOCK_LOW,     /* clock required is too low */
44     MODE_CLOCK_RANGE,   /* clock/mode isn't in a ClockRange */
45     MODE_BAD_HVALUE,    /* horizontal timing was out of range */
46     MODE_BAD_VVALUE,    /* vertical timing was out of range */
47     MODE_BAD_VSCAN,     /* VScan value out of range */
48     MODE_HSYNC_NARROW,  /* horizontal sync too narrow */
49     MODE_HSYNC_WIDE,    /* horizontal sync too wide */
50     MODE_HBLANK_NARROW, /* horizontal blanking too narrow */
51     MODE_HBLANK_WIDE,   /* horizontal blanking too wide */
52     MODE_VSYNC_NARROW,  /* vertical sync too narrow */
53     MODE_VSYNC_WIDE,    /* vertical sync too wide */
54     MODE_VBLANK_NARROW, /* vertical blanking too narrow */
55     MODE_VBLANK_WIDE,   /* vertical blanking too wide */
56     MODE_PANEL,         /* exceeds panel dimensions */
57     MODE_INTERLACE_WIDTH, /* width too large for interlaced mode */
58     MODE_ONE_WIDTH,     /* only one width is supported */
59     MODE_ONE_HEIGHT,    /* only one height is supported */
60     MODE_ONE_SIZE,      /* only one resolution is supported */
61     MODE_NO_REDUCED,    /* monitor doesn't accept reduced blanking */
62     MODE_UNVERIFIED = -3, /* mode needs to reverified */
63     MODE_BAD = -2,      /* unspecified reason */
64     MODE_ERROR  = -1    /* error condition */
65 };
66
67 #define DRM_MODE_TYPE_BUILTIN   (1<<0)
68 #define DRM_MODE_TYPE_CLOCK_C   ((1<<1) | DRM_MODE_TYPE_BUILTIN)
69 #define DRM_MODE_TYPE_CRTC_C    ((1<<2) | DRM_MODE_TYPE_BUILTIN)
70 #define DRM_MODE_TYPE_PREFERRED (1<<3)
71 #define DRM_MODE_TYPE_DEFAULT   (1<<4)
72 #define DRM_MODE_TYPE_USERDEF   (1<<5)
73 #define DRM_MODE_TYPE_DRIVER    (1<<6)
74
75 #define DRM_MODE_TYPE_CLOCK_CRTC_C (DRM_MODE_TYPE_CLOCK_C | \
76                                     DRM_MODE_TYPE_CRTC_C)
77
78 #define DRM_MODE(nm, t, c, hd, hss, hse, ht, hsk, vd, vss, vse, vt, vs, f) \
79         .name = nm, .status = 0, .type = (t), .clock = (c), \
80         .hdisplay = (hd), .hsync_start = (hss), .hsync_end = (hse), \
81         .htotal = (ht), .hskew = (hsk), .vdisplay = (vd), \
82         .vsync_start = (vss), .vsync_end = (vse), .vtotal = (vt), \
83         .vscan = (vs), .flags = (f), .vrefresh = 0
84
85 struct drm_display_mode {
86         /* Header */
87         struct list_head head;
88         char name[DRM_DISPLAY_MODE_LEN];
89         int mode_id;
90         enum drm_mode_status status;
91         int type;
92
93         /* Proposed mode values */
94         int clock;
95         int hdisplay;
96         int hsync_start;
97         int hsync_end;
98         int htotal;
99         int hskew;
100         int vdisplay;
101         int vsync_start;
102         int vsync_end;
103         int vtotal;
104         int vscan;
105         unsigned int flags;
106
107         /* Actual mode we give to hw */
108         int clock_index;
109         int synth_clock;
110         int crtc_hdisplay;
111         int crtc_hblank_start;
112         int crtc_hblank_end;
113         int crtc_hsync_start;
114         int crtc_hsync_end;
115         int crtc_htotal;
116         int crtc_hskew;
117         int crtc_vdisplay;
118         int crtc_vblank_start;
119         int crtc_vblank_end;
120         int crtc_vsync_start;
121         int crtc_vsync_end;
122         int crtc_vtotal;
123         int crtc_hadjusted;
124         int crtc_vadjusted;
125
126         /* Driver private mode info */
127         int private_size;
128         int *private;
129         int private_flags;
130
131         int vrefresh;
132         float hsync;
133 };
134
135 /* Video mode flags */
136 #define V_PHSYNC        (1<<0)
137 #define V_NHSYNC        (1<<1)
138 #define V_PVSYNC        (1<<2)
139 #define V_NVSYNC        (1<<3)
140 #define V_INTERLACE     (1<<4)
141 #define V_DBLSCAN       (1<<5)
142 #define V_CSYNC         (1<<6)
143 #define V_PCSYNC        (1<<7)
144 #define V_NCSYNC        (1<<8)
145 #define V_HSKEW         (1<<9) /* hskew provided */
146 #define V_BCAST         (1<<10)
147 #define V_PIXMUX        (1<<11)
148 #define V_DBLCLK        (1<<12)
149 #define V_CLKDIV2       (1<<13)
150
151 #define CRTC_INTERLACE_HALVE_V 0x1 /* halve V values for interlacing */
152 #define DPMSModeOn 0
153 #define DPMSModeStandby 1
154 #define DPMSModeSuspend 2
155 #define DPMSModeOff 3
156
157 enum drm_output_status {
158         output_status_connected,
159         output_status_disconnected,
160         output_status_unknown,
161 };
162
163 enum subpixel_order {
164         SubPixelUnknown = 0,
165         SubPixelHorizontalRGB,
166         SubPixelHorizontalBGR,
167         SubPixelVerticalRGB,
168         SubPixelVerticalBGR,
169         SubPixelNone,
170 };
171
172 /*
173  * Describes a given display (e.g. CRT or flat panel) and its limitations.
174  */
175 struct drm_display_info {
176         char name[DRM_DISPLAY_INFO_LEN];
177         /* Input info */
178         bool serration_vsync;
179         bool sync_on_green;
180         bool composite_sync;
181         bool separate_syncs;
182         bool blank_to_black;
183         unsigned char video_level;
184         bool digital;
185         /* Physical size */
186         unsigned int width_mm;
187         unsigned int height_mm;
188
189         /* Display parameters */
190         unsigned char gamma; /* FIXME: storage format */
191         bool gtf_supported;
192         bool standard_color;
193         enum {
194                 monochrome,
195                 rgb,
196                 other,
197                 unknown,
198         } display_type;
199         bool active_off_supported;
200         bool suspend_supported;
201         bool standby_supported;
202
203         /* Color info FIXME: storage format */
204         unsigned short redx, redy;
205         unsigned short greenx, greeny;
206         unsigned short bluex, bluey;
207         unsigned short whitex, whitey;
208
209         /* Clock limits FIXME: storage format */
210         unsigned int min_vfreq, max_vfreq;
211         unsigned int min_hfreq, max_hfreq;
212         unsigned int pixel_clock;
213
214         /* White point indices FIXME: storage format */
215         unsigned int wpx1, wpy1;
216         unsigned int wpgamma1;
217         unsigned int wpx2, wpy2;
218         unsigned int wpgamma2;
219
220         /* Preferred mode (if any) */
221         struct drm_display_mode *preferred_mode;
222         char *raw_edid; /* if any */
223 };
224
225 struct drm_framebuffer {
226         struct drm_device *dev;
227         struct list_head head;
228         int id; /* idr assigned */
229         unsigned int pitch;
230         unsigned long offset;
231         unsigned int width;
232         unsigned int height;
233         /* depth can be 15 or 16 */
234         unsigned int depth;
235         int bits_per_pixel;
236         int flags;
237         struct drm_buffer_object *bo;
238         void *fbdev;
239         u32 pseudo_palette[17];
240         void *virtual_base;
241         struct list_head filp_head;
242 };
243 struct drm_crtc;
244 struct drm_output;
245
246 /**
247  * drm_crtc_funcs - control CRTCs for a given device
248  * @dpms: control display power levels
249  * @save: save CRTC state
250  * @resore: restore CRTC state
251  * @lock: lock the CRTC
252  * @unlock: unlock the CRTC
253  * @shadow_allocate: allocate shadow pixmap
254  * @shadow_create: create shadow pixmap for rotation support
255  * @shadow_destroy: free shadow pixmap
256  * @mode_fixup: fixup proposed mode
257  * @mode_set: set the desired mode on the CRTC
258  * @gamma_set: specify color ramp for CRTC
259  * @cleanup: cleanup driver private state prior to close
260  *
261  * The drm_crtc_funcs structure is the central CRTC management structure
262  * in the DRM.  Each CRTC controls one or more outputs (note that the name
263  * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
264  * outputs, not just CRTs).
265  *
266  * Each driver is responsible for filling out this structure at startup time,
267  * in addition to providing other modesetting features, like i2c and DDC
268  * bus accessors.
269  */
270 struct drm_crtc_funcs {
271         /*
272          * Control power levels on the CRTC.  If the mode passed in is
273          * unsupported, the provider must use the next lowest power level.
274          */
275         void (*dpms)(struct drm_crtc *crtc, int mode);
276
277         /* JJJ:  Are these needed? */
278         /* Save CRTC state */
279         void (*save)(struct drm_crtc *crtc); /* suspend? */
280         /* Restore CRTC state */
281         void (*restore)(struct drm_crtc *crtc); /* resume? */
282         bool (*lock)(struct drm_crtc *crtc);
283         void (*unlock)(struct drm_crtc *crtc);
284
285         void (*prepare)(struct drm_crtc *crtc);
286         void (*commit)(struct drm_crtc *crtc);
287
288         /* Provider can fixup or change mode timings before modeset occurs */
289         bool (*mode_fixup)(struct drm_crtc *crtc,
290                            struct drm_display_mode *mode,
291                            struct drm_display_mode *adjusted_mode);
292         /* Actually set the mode */
293         void (*mode_set)(struct drm_crtc *crtc, struct drm_display_mode *mode,
294                          struct drm_display_mode *adjusted_mode, int x, int y);
295         /* Set gamma on the CRTC */
296         void (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
297                           int size);
298         /* Driver cleanup routine */
299         void (*cleanup)(struct drm_crtc *crtc);
300 };
301
302 /**
303  * drm_crtc - central CRTC control structure
304  * @enabled: is this CRTC enabled?
305  * @x: x position on screen
306  * @y: y position on screen
307  * @desired_mode: new desired mode
308  * @desired_x: desired x for desired_mode
309  * @desired_y: desired y for desired_mode
310  * @funcs: CRTC control functions
311  * @driver_private: arbitrary driver data
312  *
313  * Each CRTC may have one or more outputs associated with it.  This structure
314  * allows the CRTC to be controlled.
315  */
316 struct drm_crtc {
317         struct drm_device *dev;
318         struct list_head head;
319
320         int id; /* idr assigned */
321
322         /* framebuffer the CRTC is currently bound to */
323         struct drm_framebuffer *fb;
324
325         bool enabled;
326
327         /* JJJ: are these needed? */
328         bool cursor_in_range;
329         bool cursor_shown;
330
331         struct drm_display_mode mode;
332
333         int x, y;
334         struct drm_display_mode *desired_mode;
335         int desired_x, desired_y;
336         const struct drm_crtc_funcs *funcs;
337         void *driver_private;
338
339         /* RRCrtcPtr randr_crtc? */
340 };
341
342 extern struct drm_crtc *drm_crtc_create(struct drm_device *dev,
343                                         const struct drm_crtc_funcs *funcs);
344
345 /**
346  * drm_output_funcs - control outputs on a given device
347  * @init: setup this output
348  * @dpms: set power state (see drm_crtc_funcs above)
349  * @save: save output state
350  * @restore: restore output state
351  * @mode_valid: is this mode valid on the given output?
352  * @mode_fixup: try to fixup proposed mode for this output
353  * @mode_set: set this mode
354  * @detect: is this output active?
355  * @get_modes: get mode list for this output
356  * @set_property: property for this output may need update
357  * @cleanup: output is going away, cleanup
358  *
359  * Each CRTC may have one or more outputs attached to it.  The functions
360  * below allow the core DRM code to control outputs, enumerate available modes,
361  * etc.
362  */
363 struct drm_output_funcs {
364         void (*init)(struct drm_output *output);
365         void (*dpms)(struct drm_output *output, int mode);
366         void (*save)(struct drm_output *output);
367         void (*restore)(struct drm_output *output);
368         int (*mode_valid)(struct drm_output *output,
369                           struct drm_display_mode *mode);
370         bool (*mode_fixup)(struct drm_output *output,
371                            struct drm_display_mode *mode,
372                            struct drm_display_mode *adjusted_mode);
373         void (*prepare)(struct drm_output *output);
374         void (*commit)(struct drm_output *output);
375         void (*mode_set)(struct drm_output *output,
376                          struct drm_display_mode *mode,
377                          struct drm_display_mode *adjusted_mode);
378         enum drm_output_status (*detect)(struct drm_output *output);
379         int (*get_modes)(struct drm_output *output);
380         /* JJJ: type checking for properties via property value type */
381         bool (*set_property)(struct drm_output *output, int prop, void *val);
382         void (*cleanup)(struct drm_output *output);
383 };
384
385 #define DRM_OUTPUT_LEN 32
386 /**
387  * drm_output - central DRM output control structure
388  * @crtc: CRTC this output is currently connected to, NULL if none
389  * @possible_crtcs: bitmap of CRTCS this output could be attached to
390  * @possible_clones: bitmap of possible outputs this output could clone
391  * @interlace_allowed: can this output handle interlaced modes?
392  * @doublescan_allowed: can this output handle doublescan?
393  * @available_modes: modes available on this output (from get_modes() + user)
394  * @initial_x: initial x position for this output
395  * @initial_y: initial y position for this output
396  * @status: output connected?
397  * @subpixel_order: for this output
398  * @mm_width: displayable width of output in mm
399  * @mm_height: displayable height of output in mm
400  * @name: name of output (should be one of a few standard names)
401  * @funcs: output control functions
402  * @driver_private: private driver data
403  *
404  * Each output may be connected to one or more CRTCs, or may be clonable by
405  * another output if they can share a CRTC.  Each output also has a specific
406  * position in the broader display (referred to as a 'screen' though it could
407  * span multiple monitors).
408  */
409 struct drm_output {
410         struct drm_device *dev;
411         struct list_head head;
412         struct drm_crtc *crtc;
413         int id; /* idr assigned */
414         unsigned long possible_crtcs;
415         unsigned long possible_clones;
416         bool interlace_allowed;
417         bool doublescan_allowed;
418         spinlock_t modes_lock; /* protects modes and probed_modes lists */
419         struct list_head modes; /* list of modes on this output */
420         /*
421           OptionInfoPtr options;
422           XF86ConfMonitorPtr conf_monitor;
423          */
424         int initial_x, initial_y;
425         enum drm_output_status status;
426
427         /* these are modes added by probing with DDC or the BIOS */
428         struct list_head probed_modes;
429         
430         /* xf86MonPtr MonInfo; */
431         enum subpixel_order subpixel_order;
432         int mm_width, mm_height;
433         struct drm_display_info *monitor_info; /* if any */
434         char name[DRM_OUTPUT_LEN];
435         const struct drm_output_funcs *funcs;
436         void *driver_private;
437         /* RROutputPtr randr_output? */
438 };
439
440 /**
441  * struct drm_mode_config_funcs - configure CRTCs for a given screen layout
442  * @resize: adjust CRTCs as necessary for the proposed layout
443  *
444  * Currently only a resize hook is available.  DRM will call back into the
445  * driver with a new screen width and height.  If the driver can't support
446  * the proposed size, it can return false.  Otherwise it should adjust
447  * the CRTC<->output mappings as needed and update its view of the screen.
448  */
449 struct drm_mode_config_funcs {
450         bool (*resize)(struct drm_device *dev, int width, int height);
451 };
452
453 /**
454  * drm_mode_config - Mode configuration control structure
455  *
456  */
457 struct drm_mode_config {
458         spinlock_t config_lock; /* protects configuration and IDR */
459         struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, output, modes - just makes life easier */
460         /* this is limited to one for now */
461         int num_fb;
462         struct list_head fb_list;
463         int num_output;
464         struct list_head output_list;
465
466         /* int compat_output? */
467         int num_crtc;
468         struct list_head crtc_list;
469
470         int min_width, min_height;
471         int max_width, max_height;
472         /* DamagePtr rotationDamage? */
473         /* DGA stuff? */
474         struct drm_mode_config_funcs *funcs;
475         unsigned long fb_base;
476 };
477
478 struct drm_output *drm_output_create(struct drm_device *dev,
479                                      const struct drm_output_funcs *funcs,
480                                      const char *name);
481 extern void drm_output_destroy(struct drm_output *output);
482 extern bool drm_output_rename(struct drm_output *output, const char *name);
483
484 extern int drm_add_edid_modes(struct drm_output *output,
485                         struct i2c_adapter *adapter);
486 extern void drm_mode_probed_add(struct drm_output *output, struct drm_display_mode *mode);
487 extern void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode);
488 extern struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
489                                                    struct drm_display_mode *mode);
490 extern void drm_mode_debug_printmodeline(struct drm_device *dev,
491                                          struct drm_display_mode *mode);
492 extern void drm_mode_config_init(struct drm_device *dev);
493 extern void drm_mode_config_cleanup(struct drm_device *dev);
494 extern void drm_mode_set_name(struct drm_display_mode *mode);
495 extern void drm_disable_unused_functions(struct drm_device *dev);
496
497 extern struct drm_display_mode *drm_mode_create(struct drm_device *dev);
498 extern void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode);
499 extern void drm_mode_list_concat(struct list_head *head,
500                                  struct list_head *new);
501 extern void drm_mode_validate_size(struct drm_device *dev,
502                                    struct list_head *mode_list,
503                                    int maxX, int maxY, int maxPitch);
504 extern void drm_mode_prune_invalid(struct drm_device *dev,
505                                    struct list_head *mode_list, bool verbose);
506 extern void drm_mode_sort(struct list_head *mode_list);
507 extern int drm_mode_vrefresh(struct drm_display_mode *mode);
508 extern void drm_mode_set_crtcinfo(struct drm_display_mode *p,
509                                   int adjust_flags);
510 extern struct drm_display_mode *drm_crtc_mode_create(struct drm_device *dev);
511 extern bool drm_initial_config(struct drm_device *dev, bool cangrow);
512 extern void drm_framebuffer_set_object(struct drm_device *dev,
513                                        unsigned long handle);
514 extern bool drm_set_desired_modes(struct drm_device *dev);
515 extern int drmfb_probe(struct drm_device *dev, struct drm_framebuffer *fb);
516 extern int drmfb_remove(struct drm_device *dev, struct drm_framebuffer *fb);
517
518 /* IOCTLs */
519 extern int drm_mode_getresources(struct inode *inode, struct file *filp,
520                                  unsigned int cmd, unsigned long arg);
521
522 extern int drm_mode_getcrtc(struct inode *inode, struct file *filp,
523                             unsigned int cmd, unsigned long arg);
524 extern int drm_mode_getoutput(struct inode *inode, struct file *filp,
525                               unsigned int cmd, unsigned long arg);
526 extern int drm_mode_setcrtc(struct inode *inode, struct file *filp,
527                             unsigned int cmd, unsigned long arg);
528 extern int drm_mode_addfb(struct inode *inode, struct file *filp,
529                           unsigned int cmd, unsigned long arg);
530 extern int drm_mode_rmfb(struct inode *inode, struct file *filp,
531                          unsigned int cmd, unsigned long arg);
532 extern int drm_mode_getfb(struct inode *inode, struct file *filp,
533                           unsigned int cmd, unsigned long arg);
534 #endif /* __DRM_CRTC_H__ */
535