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 connector
21 * control chips as 'CRTCs'. They can control any type of connector, 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 connector
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 DRM_MODE_CONNECTOR_Unknown 0
153 #define DRM_MODE_CONNECTOR_VGA 1
154 #define DRM_MODE_CONNECTOR_DVII 2
155 #define DRM_MODE_CONNECTOR_DVID 3
156 #define DRM_MODE_CONNECTOR_DVIA 4
157 #define DRM_MODE_CONNECTOR_Composite 5
158 #define DRM_MODE_CONNECTOR_SVIDEO 6
159 #define DRM_MODE_CONNECTOR_LVDS 7
160 #define DRM_MODE_CONNECTOR_Component 8
161 #define DRM_MODE_CONNECTOR_9PinDIN 9
162 #define DRM_MODE_CONNECTOR_DisplayPort 10
163 #define DRM_MODE_CONNECTOR_HDMIA 11
164 #define DRM_MODE_CONNECTOR_HDMIB 12
166 enum drm_connector_status {
167 connector_status_connected = 1,
168 connector_status_disconnected = 2,
169 connector_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;
279 struct drm_connector;
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 connectors (note that the name
299 * CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
300 * connectors, 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 connectors 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 connector 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_connector_funcs - control connectors on a given device
369 * @dpms: set power state (see drm_crtc_funcs above)
370 * @save: save connector state
371 * @restore: restore connector state
372 * @mode_valid: is this mode valid on the given connector?
373 * @mode_fixup: try to fixup proposed mode for this connector
374 * @mode_set: set this mode
375 * @detect: is this connector active?
376 * @get_modes: get mode list for this connector
377 * @set_property: property for this connector may need update
378 * @destroy: make object go away
380 * Each CRTC may have one or more connectors attached to it. The functions
381 * below allow the core DRM code to control connectors, enumerate available modes,
384 struct drm_connector_funcs {
385 void (*dpms)(struct drm_connector *connector, int mode);
386 void (*save)(struct drm_connector *connector);
387 void (*restore)(struct drm_connector *connector);
388 enum drm_connector_status (*detect)(struct drm_connector *connector);
389 int (*get_modes)(struct drm_connector *connector);
390 bool (*set_property)(struct drm_connector *connector, struct drm_property *property,
392 void (*destroy)(struct drm_connector *connector);
393 int (*mode_valid)(struct drm_connector *connector,
394 struct drm_display_mode *mode);
398 struct drm_encoder_funcs {
399 void (*destroy)(struct drm_encoder *encoder);
402 #define DRM_CONNECTOR_MAX_UMODES 16
403 #define DRM_CONNECTOR_MAX_PROPERTY 16
404 #define DRM_CONNECTOR_LEN 32
405 #define DRM_CONNECTOR_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_connector - central DRM connector control structure
425 * @crtc: CRTC this connector is currently connected to, NULL if none
426 * @interlace_allowed: can this connector handle interlaced modes?
427 * @doublescan_allowed: can this connector handle doublescan?
428 * @available_modes: modes available on this connector (from get_modes() + user)
429 * @initial_x: initial x position for this connector
430 * @initial_y: initial y position for this connector
431 * @status: connector connected?
432 * @funcs: connector control functions
434 * Each connector may be connected to one or more CRTCs, or may be clonable by
435 * another connector if they can share a CRTC. Each connector also has a specific
436 * position in the broader display (referred to as a 'screen' though it could
437 * span multiple monitors).
439 struct drm_connector {
440 struct drm_device *dev;
442 struct device_attribute *attr;
443 struct list_head head;
444 struct drm_crtc *crtc;
445 int id; /* idr assigned */
448 int connector_type_id;
449 bool interlace_allowed;
450 bool doublescan_allowed;
451 struct list_head modes; /* list of modes on this connector */
453 int initial_x, initial_y;
454 enum drm_connector_status status;
456 /* these are modes added by probing with DDC or the BIOS */
457 struct list_head probed_modes;
459 struct drm_display_info display_info;
460 const struct drm_connector_funcs *funcs;
462 struct list_head user_modes;
463 struct drm_property_blob *edid_blob_ptr;
464 u32 property_ids[DRM_CONNECTOR_MAX_PROPERTY];
465 uint64_t property_values[DRM_CONNECTOR_MAX_PROPERTY];
467 void *helper_private;
469 uint32_t encoder_ids[DRM_CONNECTOR_MAX_ENCODER];
470 uint32_t force_encoder_id;
471 uint32_t current_encoder_id;
475 * struct drm_mode_set
477 * Represents a single crtc the connectors that it drives with what mode
478 * and from which framebuffer it scans out from.
480 * This is used to set modes.
484 struct drm_framebuffer *fb;
485 struct drm_crtc *crtc;
486 struct drm_display_mode *mode;
491 struct drm_connector **connectors;
492 size_t num_connectors;
496 * struct drm_mode_config_funcs - configure CRTCs for a given screen layout
497 * @resize: adjust CRTCs as necessary for the proposed layout
499 * Currently only a resize hook is available. DRM will call back into the
500 * driver with a new screen width and height. If the driver can't support
501 * the proposed size, it can return false. Otherwise it should adjust
502 * the CRTC<->connector mappings as needed and update its view of the screen.
504 struct drm_mode_config_funcs {
505 bool (*resize_fb)(struct drm_device *dev, struct drm_framebuffer *fb);
509 * drm_mode_config - Mode configuration control structure
512 struct drm_mode_config {
513 struct mutex mutex; /* protects configuration and IDR */
514 struct idr crtc_idr; /* use this idr for all IDs, fb, crtc, connector, modes - just makes life easier */
515 /* this is limited to one for now */
517 struct list_head fb_list;
519 struct list_head connector_list;
521 struct list_head encoder_list;
524 struct list_head crtc_list;
526 struct list_head property_list;
528 int min_width, min_height;
529 int max_width, max_height;
530 struct drm_mode_config_funcs *funcs;
531 unsigned long fb_base;
533 /* pointers to standard properties */
534 struct list_head property_blob_list;
535 struct drm_property *edid_property;
536 struct drm_property *dpms_property;
539 struct drm_property *tv_mode_property;
540 struct drm_property *tv_left_margin_property;
541 struct drm_property *tv_right_margin_property;
542 struct drm_property *tv_top_margin_property;
543 struct drm_property *tv_bottom_margin_property;
546 uint32_t hotplug_counter;
550 extern void drm_crtc_init(struct drm_device *dev,
551 struct drm_crtc *crtc,
552 const struct drm_crtc_funcs *funcs);
553 extern void drm_crtc_cleanup(struct drm_crtc *crtc);
555 extern void drm_connector_init(struct drm_device *dev,
556 struct drm_connector *connector,
557 const struct drm_connector_funcs *funcs,
560 extern void drm_connector_cleanup(struct drm_connector *connector);
562 extern void drm_encoder_init(struct drm_device *dev,
563 struct drm_encoder *encoder,
564 const struct drm_encoder_funcs *funcs,
567 extern void drm_encoder_cleanup(struct drm_encoder *encoder);
569 extern char *drm_get_connector_name(struct drm_connector *connector);
570 extern char *drm_get_dpms_name(int val);
571 extern void drm_fb_release(struct file *filp);
573 extern struct edid *drm_get_edid(struct drm_connector *connector,
574 struct i2c_adapter *adapter);
575 extern int drm_add_edid_modes(struct drm_connector *connector, struct edid *edid);
576 extern void drm_mode_probed_add(struct drm_connector *connector, struct drm_display_mode *mode);
577 extern void drm_mode_remove(struct drm_connector *connector, struct drm_display_mode *mode);
578 extern struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
579 struct drm_display_mode *mode);
580 extern void drm_mode_debug_printmodeline(struct drm_display_mode *mode);
581 extern void drm_mode_config_init(struct drm_device *dev);
582 extern void drm_mode_config_cleanup(struct drm_device *dev);
583 extern void drm_mode_set_name(struct drm_display_mode *mode);
584 extern bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2);
585 extern void drm_disable_unused_functions(struct drm_device *dev);
587 /* for us by fb module */
588 extern int drm_mode_attachmode_crtc(struct drm_device *dev,
589 struct drm_crtc *crtc,
590 struct drm_display_mode *mode);
591 extern int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode);
593 extern struct drm_display_mode *drm_mode_create(struct drm_device *dev);
594 extern void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode);
595 extern void drm_mode_list_concat(struct list_head *head,
596 struct list_head *new);
597 extern void drm_mode_validate_size(struct drm_device *dev,
598 struct list_head *mode_list,
599 int maxX, int maxY, int maxPitch);
600 extern void drm_mode_prune_invalid(struct drm_device *dev,
601 struct list_head *mode_list, bool verbose);
602 extern void drm_mode_sort(struct list_head *mode_list);
603 extern int drm_mode_vrefresh(struct drm_display_mode *mode);
604 extern void drm_mode_set_crtcinfo(struct drm_display_mode *p,
606 extern void drm_mode_connector_list_update(struct drm_connector *connector);
607 extern int drm_mode_connector_update_edid_property(struct drm_connector *connector,
609 extern int drm_connector_property_set_value(struct drm_connector *connector,
610 struct drm_property *property,
612 extern int drm_connector_property_get_value(struct drm_connector *connector,
613 struct drm_property *property,
615 extern struct drm_display_mode *drm_crtc_mode_create(struct drm_device *dev);
616 extern void drm_framebuffer_set_object(struct drm_device *dev,
617 unsigned long handle);
618 extern struct drm_framebuffer *drm_framebuffer_create(struct drm_device *dev);
619 extern void drm_framebuffer_destroy(struct drm_framebuffer *fb);
620 extern int drmfb_probe(struct drm_device *dev, struct drm_crtc *crtc);
621 extern int drmfb_remove(struct drm_device *dev, struct drm_framebuffer *fb);
622 extern void drm_crtc_probe_connector_modes(struct drm_device *dev, int maxX, int maxY);
623 extern bool drm_crtc_in_use(struct drm_crtc *crtc);
625 extern int drm_connector_attach_property(struct drm_connector *connector,
626 struct drm_property *property, uint64_t init_val);
627 extern struct drm_property *drm_property_create(struct drm_device *dev, int flags,
628 const char *name, int num_values);
629 extern void drm_property_destroy(struct drm_device *dev, struct drm_property *property);
630 extern int drm_property_add_enum(struct drm_property *property, int index,
631 uint64_t value, const char *name);
632 extern bool drm_create_tv_properties(struct drm_device *dev, int num_formats,
635 extern int drm_mode_connector_attach_encoder(struct drm_connector *connector,
636 struct drm_encoder *encoder);
637 extern void drm_mode_connector_detach_encoder(struct drm_connector *connector,
638 struct drm_encoder *encoder);
641 extern int drm_mode_getresources(struct drm_device *dev,
642 void *data, struct drm_file *file_priv);
644 extern int drm_mode_getcrtc(struct drm_device *dev,
645 void *data, struct drm_file *file_priv);
646 extern int drm_mode_getconnector(struct drm_device *dev,
647 void *data, struct drm_file *file_priv);
648 extern int drm_mode_setcrtc(struct drm_device *dev,
649 void *data, struct drm_file *file_priv);
650 extern int drm_mode_cursor_ioctl(struct drm_device *dev,
651 void *data, struct drm_file *file_priv);
652 extern int drm_mode_addfb(struct drm_device *dev,
653 void *data, struct drm_file *file_priv);
654 extern int drm_mode_rmfb(struct drm_device *dev,
655 void *data, struct drm_file *file_priv);
656 extern int drm_mode_getfb(struct drm_device *dev,
657 void *data, struct drm_file *file_priv);
658 extern int drm_mode_addmode_ioctl(struct drm_device *dev,
659 void *data, struct drm_file *file_priv);
660 extern int drm_mode_rmmode_ioctl(struct drm_device *dev,
661 void *data, struct drm_file *file_priv);
662 extern int drm_mode_attachmode_ioctl(struct drm_device *dev,
663 void *data, struct drm_file *file_priv);
664 extern int drm_mode_detachmode_ioctl(struct drm_device *dev,
665 void *data, struct drm_file *file_priv);
667 extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
668 void *data, struct drm_file *file_priv);
669 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
670 void *data, struct drm_file *file_priv);
671 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
672 void *data, struct drm_file *file_priv);
673 extern int drm_mode_hotplug_ioctl(struct drm_device *dev,
674 void *data, struct drm_file *file_priv);
675 extern int drm_mode_replacefb(struct drm_device *dev,
676 void *data, struct drm_file *file_priv);
677 int drm_mode_getencoder(struct drm_device *dev,
678 void *data, struct drm_file *file_priv);
679 #endif /* __DRM_CRTC_H__ */