2 * Copyright © 2006 Keith Packard
3 * Copyright © 2007 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
10 #include <linux/spinlock.h>
11 #include <linux/types.h>
12 #include <linux/idr.h>
20 * Note on terminology: here, for brevity and convenience, we refer to output
21 * control chips as 'CRTCs'. They can control any type of output, VGA, LVDS,
22 * DVI, etc. And 'screen' refers to the whole of the visible display, which
23 * may span multiple monitors (and therefore multiple CRTC and output
27 enum drm_mode_status {
28 MODE_OK = 0, /* Mode OK */
29 MODE_HSYNC, /* hsync out of range */
30 MODE_VSYNC, /* vsync out of range */
31 MODE_H_ILLEGAL, /* mode has illegal horizontal timings */
32 MODE_V_ILLEGAL, /* mode has illegal horizontal timings */
33 MODE_BAD_WIDTH, /* requires an unsupported linepitch */
34 MODE_NOMODE, /* no mode with a maching name */
35 MODE_NO_INTERLACE, /* interlaced mode not supported */
36 MODE_NO_DBLESCAN, /* doublescan mode not supported */
37 MODE_NO_VSCAN, /* multiscan mode not supported */
38 MODE_MEM, /* insufficient video memory */
39 MODE_VIRTUAL_X, /* mode width too large for specified virtual size */
40 MODE_VIRTUAL_Y, /* mode height too large for specified virtual size */
41 MODE_MEM_VIRT, /* insufficient video memory given virtual size */
42 MODE_NOCLOCK, /* no fixed clock available */
43 MODE_CLOCK_HIGH, /* clock required is too high */
44 MODE_CLOCK_LOW, /* clock required is too low */
45 MODE_CLOCK_RANGE, /* clock/mode isn't in a ClockRange */
46 MODE_BAD_HVALUE, /* horizontal timing was out of range */
47 MODE_BAD_VVALUE, /* vertical timing was out of range */
48 MODE_BAD_VSCAN, /* VScan value out of range */
49 MODE_HSYNC_NARROW, /* horizontal sync too narrow */
50 MODE_HSYNC_WIDE, /* horizontal sync too wide */
51 MODE_HBLANK_NARROW, /* horizontal blanking too narrow */
52 MODE_HBLANK_WIDE, /* horizontal blanking too wide */
53 MODE_VSYNC_NARROW, /* vertical sync too narrow */
54 MODE_VSYNC_WIDE, /* vertical sync too wide */
55 MODE_VBLANK_NARROW, /* vertical blanking too narrow */
56 MODE_VBLANK_WIDE, /* vertical blanking too wide */
57 MODE_PANEL, /* exceeds panel dimensions */
58 MODE_INTERLACE_WIDTH, /* width too large for interlaced mode */
59 MODE_ONE_WIDTH, /* only one width is supported */
60 MODE_ONE_HEIGHT, /* only one height is supported */
61 MODE_ONE_SIZE, /* only one resolution is supported */
62 MODE_NO_REDUCED, /* monitor doesn't accept reduced blanking */
63 MODE_UNVERIFIED = -3, /* mode needs to reverified */
64 MODE_BAD = -2, /* unspecified reason */
65 MODE_ERROR = -1 /* error condition */
68 #define DRM_MODE_TYPE_CLOCK_CRTC_C (DRM_MODE_TYPE_CLOCK_C | \
71 #define DRM_MODE(nm, t, c, hd, hss, hse, ht, hsk, vd, vss, vse, vt, vs, f) \
72 .name = nm, .status = 0, .type = (t), .clock = (c), \
73 .hdisplay = (hd), .hsync_start = (hss), .hsync_end = (hse), \
74 .htotal = (ht), .hskew = (hsk), .vdisplay = (vd), \
75 .vsync_start = (vss), .vsync_end = (vse), .vtotal = (vt), \
76 .vscan = (vs), .flags = (f), .vrefresh = 0
78 struct drm_display_mode {
80 struct list_head head;
81 char name[DRM_DISPLAY_MODE_LEN];
84 enum drm_mode_status status;
87 /* Proposed mode values */
101 /* Actual mode we give to hw */
105 int crtc_hblank_start;
107 int crtc_hsync_start;
112 int crtc_vblank_start;
114 int crtc_vsync_start;
120 /* Driver private mode info */
129 /* Video mode flags */
130 #define V_PHSYNC (1<<0)
131 #define V_NHSYNC (1<<1)
132 #define V_PVSYNC (1<<2)
133 #define V_NVSYNC (1<<3)
134 #define V_INTERLACE (1<<4)
135 #define V_DBLSCAN (1<<5)
136 #define V_CSYNC (1<<6)
137 #define V_PCSYNC (1<<7)
138 #define V_NCSYNC (1<<8)
139 #define V_HSKEW (1<<9) /* hskew provided */
140 #define V_BCAST (1<<10)
141 #define V_PIXMUX (1<<11)
142 #define V_DBLCLK (1<<12)
143 #define V_CLKDIV2 (1<<13)
145 #define CRTC_INTERLACE_HALVE_V 0x1 /* halve V values for interlacing */
148 #define DPMSModeStandby 1
149 #define DPMSModeSuspend 2
150 #define DPMSModeOff 3
152 #define ConnectorUnknown 0
153 #define ConnectorVGA 1
154 #define ConnectorDVII 2
155 #define ConnectorDVID 3
156 #define ConnectorDVIA 4
157 #define ConnectorComposite 5
158 #define ConnectorSVIDEO 6
159 #define ConnectorLVDS 7
160 #define ConnectorComponent 8
161 #define Connector9PinDIN 9
162 #define ConnectorDisplayPort 10
163 #define ConnectorHDMIA 11
164 #define ConnectorHDMIB 12
166 enum drm_output_status {
167 output_status_connected = 1,
168 output_status_disconnected = 2,
169 output_status_unknown = 3,
172 enum subpixel_order {
174 SubPixelHorizontalRGB,
175 SubPixelHorizontalBGR,
182 * Describes a given display (e.g. CRT or flat panel) and its limitations.
184 struct drm_display_info {
185 char name[DRM_DISPLAY_INFO_LEN];
187 bool serration_vsync;
192 unsigned char video_level;
195 unsigned int width_mm;
196 unsigned int height_mm;
198 /* Display parameters */
199 unsigned char gamma; /* FIXME: storage format */
208 bool active_off_supported;
209 bool suspend_supported;
210 bool standby_supported;
212 /* Color info FIXME: storage format */
213 unsigned short redx, redy;
214 unsigned short greenx, greeny;
215 unsigned short bluex, bluey;
216 unsigned short whitex, whitey;
218 /* Clock limits FIXME: storage format */
219 unsigned int min_vfreq, max_vfreq;
220 unsigned int min_hfreq, max_hfreq;
221 unsigned int pixel_clock;
223 /* White point indices FIXME: storage format */
224 unsigned int wpx1, wpy1;
225 unsigned int wpgamma1;
226 unsigned int wpx2, wpy2;
227 unsigned int wpgamma2;
229 enum subpixel_order subpixel_order;
231 /* Preferred mode (if any) */
232 struct drm_display_mode *preferred_mode;
233 char *raw_edid; /* if any */
236 struct drm_framebuffer {
237 struct drm_device *dev;
238 struct list_head head;
239 int id; /* idr assigned */
243 /* depth can be 15 or 16 */
247 struct drm_buffer_object *bo;
249 u32 pseudo_palette[17];
250 struct drm_bo_kmap_obj kmap;
251 struct list_head filp_head;
254 struct drm_property_blob {
255 struct list_head head;
261 struct drm_property_enum {
263 struct list_head head;
264 char name[DRM_PROP_NAME_LEN];
267 struct drm_property {
268 struct list_head head;
269 int id; /* idr assigned */
271 char name[DRM_PROP_NAME_LEN];
275 struct list_head enum_blob_list;
283 * drm_crtc_funcs - control CRTCs for a given device
284 * @dpms: control display power levels
285 * @save: save CRTC state
286 * @resore: restore CRTC state
287 * @lock: lock the CRTC
288 * @unlock: unlock the CRTC
289 * @shadow_allocate: allocate shadow pixmap
290 * @shadow_create: create shadow pixmap for rotation support
291 * @shadow_destroy: free shadow pixmap
292 * @mode_fixup: fixup proposed mode
293 * @mode_set: set the desired mode on the CRTC
294 * @gamma_set: specify color ramp for CRTC
295 * @destroy: deinit and free object.
297 * The drm_crtc_funcs structure is the central CRTC management structure
298 * in the DRM. Each CRTC controls one or more outputs (note that the name
299 * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
300 * outputs, not just CRTs).
302 * Each driver is responsible for filling out this structure at startup time,
303 * in addition to providing other modesetting features, like i2c and DDC
306 struct drm_crtc_funcs {
308 * Control power levels on the CRTC. If the mode passed in is
309 * unsupported, the provider must use the next lowest power level.
311 void (*dpms)(struct drm_crtc *crtc, int mode);
313 /* Save CRTC state */
314 void (*save)(struct drm_crtc *crtc); /* suspend? */
315 /* Restore CRTC state */
316 void (*restore)(struct drm_crtc *crtc); /* resume? */
318 /* cursor controls */
319 int (*cursor_set)(struct drm_crtc *crtc, struct drm_buffer_object *bo,
320 uint32_t width, uint32_t height);
321 int (*cursor_move)(struct drm_crtc *crtc, int x, int y);
323 /* Set gamma on the CRTC */
324 void (*gamma_set)(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
326 /* Object destroy routine */
327 void (*destroy)(struct drm_crtc *crtc);
329 int (*set_config)(struct drm_mode_set *set);
333 * drm_crtc - central CRTC control structure
334 * @enabled: is this CRTC enabled?
335 * @x: x position on screen
336 * @y: y position on screen
337 * @desired_mode: new desired mode
338 * @desired_x: desired x for desired_mode
339 * @desired_y: desired y for desired_mode
340 * @funcs: CRTC control functions
342 * Each CRTC may have one or more outputs associated with it. This structure
343 * allows the CRTC to be controlled.
346 struct drm_device *dev;
347 struct list_head head;
349 int id; /* idr assigned */
351 /* framebuffer the output is currently bound to */
352 struct drm_framebuffer *fb;
356 struct drm_display_mode mode;
359 struct drm_display_mode *desired_mode;
360 int desired_x, desired_y;
361 const struct drm_crtc_funcs *funcs;
363 /* if you are using the helper */
364 void *helper_private;
368 * drm_output_funcs - control outputs on a given device
369 * @dpms: set power state (see drm_crtc_funcs above)
370 * @save: save output state
371 * @restore: restore output state
372 * @mode_valid: is this mode valid on the given output?
373 * @mode_fixup: try to fixup proposed mode for this output
374 * @mode_set: set this mode
375 * @detect: is this output active?
376 * @get_modes: get mode list for this output
377 * @set_property: property for this output may need update
378 * @destroy: make object go away
380 * Each CRTC may have one or more outputs attached to it. The functions
381 * below allow the core DRM code to control outputs, enumerate available modes,
384 struct drm_output_funcs {
385 void (*dpms)(struct drm_output *output, int mode);
386 void (*save)(struct drm_output *output);
387 void (*restore)(struct drm_output *output);
388 enum drm_output_status (*detect)(struct drm_output *output);
389 int (*get_modes)(struct drm_output *output);
390 bool (*set_property)(struct drm_output *output, struct drm_property *property,
392 void (*destroy)(struct drm_output *output);
393 int (*mode_valid)(struct drm_output *output,
394 struct drm_display_mode *mode);
398 struct drm_encoder_funcs {
399 void (*destroy)(struct drm_encoder *encoder);
402 #define DRM_OUTPUT_MAX_UMODES 16
403 #define DRM_OUTPUT_MAX_PROPERTY 16
404 #define DRM_OUTPUT_LEN 32
405 #define DRM_OUTPUT_MAX_ENCODER 2
408 * drm_encoder - central DRM encoder structure
411 struct drm_device *dev;
412 struct list_head head;
416 uint32_t possible_crtcs;
417 uint32_t possible_clones;
419 const struct drm_encoder_funcs *funcs;
420 void *helper_private;
424 * drm_output - central DRM output control structure
425 * @crtc: CRTC this output is currently connected to, NULL if none
426 * @possible_crtcs: bitmap of CRTCS this output could be attached to
427 * @possible_clones: bitmap of possible outputs this output could clone
428 * @interlace_allowed: can this output handle interlaced modes?
429 * @doublescan_allowed: can this output handle doublescan?
430 * @available_modes: modes available on this output (from get_modes() + user)
431 * @initial_x: initial x position for this output
432 * @initial_y: initial y position for this output
433 * @status: output connected?
434 * @funcs: output control functions
436 * Each output may be connected to one or more CRTCs, or may be clonable by
437 * another output if they can share a CRTC. Each output also has a specific
438 * position in the broader display (referred to as a 'screen' though it could
439 * span multiple monitors).
442 struct drm_device *dev;
444 struct device_attribute *attr;
445 struct list_head head;
446 struct drm_crtc *crtc;
447 int id; /* idr assigned */
451 unsigned long possible_crtcs;
452 unsigned long possible_clones;
453 bool interlace_allowed;
454 bool doublescan_allowed;
455 struct list_head modes; /* list of modes on this output */
457 int initial_x, initial_y;
458 enum drm_output_status status;
460 /* these are modes added by probing with DDC or the BIOS */
461 struct list_head probed_modes;
463 struct drm_display_info display_info;
464 const struct drm_output_funcs *funcs;
466 struct list_head user_modes;
467 struct drm_property_blob *edid_blob_ptr;
468 u32 property_ids[DRM_OUTPUT_MAX_PROPERTY];
469 uint64_t property_values[DRM_OUTPUT_MAX_PROPERTY];
471 void *helper_private;
473 u32 encoder_ids[DRM_OUTPUT_MAX_ENCODER];
477 * struct drm_mode_set
479 * Represents a single crtc the outputs that it drives with what mode
480 * and from which framebuffer it scans out from.
482 * This is used to set modes.
486 struct drm_framebuffer *fb;
487 struct drm_crtc *crtc;
488 struct drm_display_mode *mode;
493 struct drm_output **outputs;
498 * struct drm_mode_config_funcs - configure CRTCs for a given screen layout
499 * @resize: adjust CRTCs as necessary for the proposed layout
501 * Currently only a resize hook is available. DRM will call back into the
502 * driver with a new screen width and height. If the driver can't support
503 * the proposed size, it can return false. Otherwise it should adjust
504 * the CRTC<->output mappings as needed and update its view of the screen.
506 struct drm_mode_config_funcs {
507 bool (*resize_fb)(struct drm_device *dev, struct drm_framebuffer *fb);
511 * drm_mode_config - Mode configuration control structure
514 struct drm_mode_config {
515 struct mutex mutex; /* protects configuration and IDR */
516 struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, output, modes - just makes life easier */
517 /* this is limited to one for now */
519 struct list_head fb_list;
521 struct list_head output_list;
523 struct list_head encoder_list;
526 struct list_head crtc_list;
528 struct list_head property_list;
530 int min_width, min_height;
531 int max_width, max_height;
532 struct drm_mode_config_funcs *funcs;
533 unsigned long fb_base;
535 /* pointers to standard properties */
536 struct list_head property_blob_list;
537 struct drm_property *edid_property;
538 struct drm_property *dpms_property;
539 struct drm_property *connector_type_property;
540 struct drm_property *connector_num_property;
543 struct drm_property *tv_mode_property;
544 struct drm_property *tv_left_margin_property;
545 struct drm_property *tv_right_margin_property;
546 struct drm_property *tv_top_margin_property;
547 struct drm_property *tv_bottom_margin_property;
550 uint32_t hotplug_counter;
554 extern void drm_crtc_init(struct drm_device *dev,
555 struct drm_crtc *crtc,
556 const struct drm_crtc_funcs *funcs);
557 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
559 extern void drm_output_init(struct drm_device *dev,
560 struct drm_output *output,
561 const struct drm_output_funcs *funcs,
564 extern void drm_output_cleanup(struct drm_output *output);
566 extern void drm_encoder_init(struct drm_device *dev,
567 struct drm_encoder *encoder,
568 const struct drm_encoder_funcs *funcs,
571 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
573 extern char *drm_get_output_name(struct drm_output *output);
574 extern char *drm_get_dpms_name(int val);
575 extern void drm_fb_release(struct file *filp);
577 extern struct edid *drm_get_edid(struct drm_output *output,
578 struct i2c_adapter *adapter);
579 extern int drm_add_edid_modes(struct drm_output *output, struct edid *edid);
580 extern void drm_mode_probed_add(struct drm_output *output, struct drm_display_mode *mode);
581 extern void drm_mode_remove(struct drm_output *output, struct drm_display_mode *mode);
582 extern struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
583 struct drm_display_mode *mode);
584 extern void drm_mode_debug_printmodeline(struct drm_display_mode *mode);
585 extern void drm_mode_config_init(struct drm_device *dev);
586 extern void drm_mode_config_cleanup(struct drm_device *dev);
587 extern void drm_mode_set_name(struct drm_display_mode *mode);
588 extern bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2);
589 extern void drm_disable_unused_functions(struct drm_device *dev);
591 /* for us by fb module */
592 extern int drm_mode_attachmode_crtc(struct drm_device *dev,
593 struct drm_crtc *crtc,
594 struct drm_display_mode *mode);
595 extern int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode);
597 extern struct drm_display_mode *drm_mode_create(struct drm_device *dev);
598 extern void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode);
599 extern void drm_mode_list_concat(struct list_head *head,
600 struct list_head *new);
601 extern void drm_mode_validate_size(struct drm_device *dev,
602 struct list_head *mode_list,
603 int maxX, int maxY, int maxPitch);
604 extern void drm_mode_prune_invalid(struct drm_device *dev,
605 struct list_head *mode_list, bool verbose);
606 extern void drm_mode_sort(struct list_head *mode_list);
607 extern int drm_mode_vrefresh(struct drm_display_mode *mode);
608 extern void drm_mode_set_crtcinfo(struct drm_display_mode *p,
610 extern void drm_mode_output_list_update(struct drm_output *output);
611 extern int drm_mode_output_update_edid_property(struct drm_output *output,
613 extern int drm_output_property_set_value(struct drm_output *output,
614 struct drm_property *property,
616 extern int drm_output_property_get_value(struct drm_output *output,
617 struct drm_property *property,
619 extern struct drm_display_mode *drm_crtc_mode_create(struct drm_device *dev);
620 extern void drm_framebuffer_set_object(struct drm_device *dev,
621 unsigned long handle);
622 extern struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev);
623 extern void drm_framebuffer_destroy(struct drm_framebuffer *fb);
624 extern int drmfb_probe(struct drm_device *dev, struct drm_crtc *crtc);
625 extern int drmfb_remove(struct drm_device *dev, struct drm_framebuffer *fb);
626 extern void drm_crtc_probe_output_modes(struct drm_device *dev, int maxX, int maxY);
627 extern bool drm_crtc_in_use(struct drm_crtc *crtc);
629 extern int drm_output_attach_property(struct drm_output *output,
630 struct drm_property *property, uint64_t init_val);
631 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
632 const char *name, int num_values);
633 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
634 extern int drm_property_add_enum(struct drm_property *property, int index,
635 uint64_t value, const char *name);
636 extern bool drm_create_tv_properties(struct drm_device *dev, int num_formats,
639 extern int drm_mode_output_attach_encoder(struct drm_output *output,
640 struct drm_encoder *encoder);
641 extern void drm_mode_output_detach_encoder(struct drm_output *output,
642 struct drm_encoder *encoder);
645 extern int drm_mode_getresources(struct drm_device *dev,
646 void *data, struct drm_file *file_priv);
648 extern int drm_mode_getcrtc(struct drm_device *dev,
649 void *data, struct drm_file *file_priv);
650 extern int drm_mode_getoutput(struct drm_device *dev,
651 void *data, struct drm_file *file_priv);
652 extern int drm_mode_setcrtc(struct drm_device *dev,
653 void *data, struct drm_file *file_priv);
654 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
655 void *data, struct drm_file *file_priv);
656 extern int drm_mode_addfb(struct drm_device *dev,
657 void *data, struct drm_file *file_priv);
658 extern int drm_mode_rmfb(struct drm_device *dev,
659 void *data, struct drm_file *file_priv);
660 extern int drm_mode_getfb(struct drm_device *dev,
661 void *data, struct drm_file *file_priv);
662 extern int drm_mode_addmode_ioctl(struct drm_device *dev,
663 void *data, struct drm_file *file_priv);
664 extern int drm_mode_rmmode_ioctl(struct drm_device *dev,
665 void *data, struct drm_file *file_priv);
666 extern int drm_mode_attachmode_ioctl(struct drm_device *dev,
667 void *data, struct drm_file *file_priv);
668 extern int drm_mode_detachmode_ioctl(struct drm_device *dev,
669 void *data, struct drm_file *file_priv);
671 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
672 void *data, struct drm_file *file_priv);
673 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
674 void *data, struct drm_file *file_priv);
675 extern int drm_mode_output_property_set_ioctl(struct drm_device *dev,
676 void *data, struct drm_file *file_priv);
677 extern int drm_mode_hotplug_ioctl(struct drm_device *dev,
678 void *data, struct drm_file *file_priv);
679 extern int drm_mode_replacefb(struct drm_device *dev,
680 void *data, struct drm_file *file_priv);
681 int drm_mode_getencoder(struct drm_device *dev,
682 void *data, struct drm_file *file_priv);
683 #endif /* __DRM_CRTC_H__ */