2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 * DRM framebuffer helper functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
35 #include "drm_fb_helper.h"
36 #include "drm_crtc_helper.h"
38 MODULE_AUTHOR("David Airlie, Jesse Barnes");
39 MODULE_DESCRIPTION("DRM KMS helper");
40 MODULE_LICENSE("GPL and additional rights");
42 static LIST_HEAD(kernel_fb_helper_list);
44 int drm_fb_helper_add_connector(struct drm_connector *connector)
46 connector->fb_helper_private = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
47 if (!connector->fb_helper_private)
52 EXPORT_SYMBOL(drm_fb_helper_add_connector);
55 * drm_fb_helper_connector_parse_command_line - parse command line for connector
56 * @connector - connector to parse line for
57 * @mode_option - per connector mode option
59 * This parses the connector specific then generic command lines for
60 * modes and options to configure the connector.
62 * This uses the same parameters as the fb modedb.c, except for extra
63 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
65 * enable/enable Digital/disable bit at the end
67 static bool drm_fb_helper_connector_parse_command_line(struct drm_connector *connector,
68 const char *mode_option)
72 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
73 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
74 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
76 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
77 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
78 struct drm_fb_helper_cmdline_mode *cmdline_mode;
83 cmdline_mode = &fb_help_conn->cmdline_mode;
85 mode_option = fb_mode_option;
88 cmdline_mode->specified = false;
93 namelen = strlen(name);
94 for (i = namelen-1; i >= 0; i--) {
98 if (!refresh_specified && !bpp_specified &&
100 refresh = simple_strtol(&name[i+1], NULL, 10);
101 refresh_specified = 1;
109 if (!bpp_specified && !yres_specified) {
110 bpp = simple_strtol(&name[i+1], NULL, 10);
118 if (!yres_specified) {
119 yres = simple_strtol(&name[i+1], NULL, 10);
142 force = DRM_FORCE_ON;
145 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
146 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
147 force = DRM_FORCE_ON;
149 force = DRM_FORCE_ON_DIGITAL;
152 force = DRM_FORCE_OFF;
158 if (i < 0 && yres_specified) {
159 xres = simple_strtol(name, NULL, 10);
164 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
165 drm_get_connector_name(connector), xres, yres,
166 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
167 "", (margins) ? " with margins" : "", (interlace) ?
173 case DRM_FORCE_OFF: s = "OFF"; break;
174 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
176 case DRM_FORCE_ON: s = "ON"; break;
179 DRM_INFO("forcing %s connector %s\n",
180 drm_get_connector_name(connector), s);
181 connector->force = force;
185 cmdline_mode->specified = true;
186 cmdline_mode->xres = xres;
187 cmdline_mode->yres = yres;
190 if (refresh_specified) {
191 cmdline_mode->refresh_specified = true;
192 cmdline_mode->refresh = refresh;
196 cmdline_mode->bpp_specified = true;
197 cmdline_mode->bpp = bpp;
199 cmdline_mode->rb = rb ? true : false;
200 cmdline_mode->cvt = cvt ? true : false;
201 cmdline_mode->interlace = interlace ? true : false;
206 int drm_fb_helper_parse_command_line(struct drm_device *dev)
208 struct drm_connector *connector;
210 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
213 /* do something on return - turn off connector maybe */
214 if (fb_get_options(drm_get_connector_name(connector), &option))
217 drm_fb_helper_connector_parse_command_line(connector, option);
222 bool drm_fb_helper_force_kernel_mode(void)
225 bool ret, error = false;
226 struct drm_fb_helper *helper;
228 if (list_empty(&kernel_fb_helper_list))
231 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
232 for (i = 0; i < helper->crtc_count; i++) {
233 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
234 ret = drm_crtc_helper_set_config(mode_set);
242 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
245 DRM_ERROR("panic occurred, switching back to text console\n");
246 return drm_fb_helper_force_kernel_mode();
249 EXPORT_SYMBOL(drm_fb_helper_panic);
251 static struct notifier_block paniced = {
252 .notifier_call = drm_fb_helper_panic,
256 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
258 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
260 void drm_fb_helper_restore(void)
263 ret = drm_fb_helper_force_kernel_mode();
265 DRM_ERROR("Failed to restore crtc configuration\n");
267 EXPORT_SYMBOL(drm_fb_helper_restore);
269 #ifdef CONFIG_MAGIC_SYSRQ
270 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
272 drm_fb_helper_restore();
274 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
276 static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
278 schedule_work(&drm_fb_helper_restore_work);
281 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
282 .handler = drm_fb_helper_sysrq,
283 .help_msg = "force-fb(V)",
284 .action_msg = "Restore framebuffer console",
287 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
290 static void drm_fb_helper_on(struct fb_info *info)
292 struct drm_fb_helper *fb_helper = info->par;
293 struct drm_device *dev = fb_helper->dev;
294 struct drm_crtc *crtc;
295 struct drm_crtc_helper_funcs *crtc_funcs;
296 struct drm_encoder *encoder;
300 * For each CRTC in this fb, turn the crtc on then,
301 * find all associated encoders and turn them on.
303 mutex_lock(&dev->mode_config.mutex);
304 for (i = 0; i < fb_helper->crtc_count; i++) {
305 crtc = fb_helper->crtc_info[i].mode_set.crtc;
306 crtc_funcs = crtc->helper_private;
311 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
314 /* Found a CRTC on this fb, now find encoders */
315 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
316 if (encoder->crtc == crtc) {
317 struct drm_encoder_helper_funcs *encoder_funcs;
319 encoder_funcs = encoder->helper_private;
320 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
324 mutex_unlock(&dev->mode_config.mutex);
327 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
329 struct drm_fb_helper *fb_helper = info->par;
330 struct drm_device *dev = fb_helper->dev;
331 struct drm_crtc *crtc;
332 struct drm_crtc_helper_funcs *crtc_funcs;
333 struct drm_encoder *encoder;
337 * For each CRTC in this fb, find all associated encoders
338 * and turn them off, then turn off the CRTC.
340 mutex_lock(&dev->mode_config.mutex);
341 for (i = 0; i < fb_helper->crtc_count; i++) {
342 crtc = fb_helper->crtc_info[i].mode_set.crtc;
343 crtc_funcs = crtc->helper_private;
348 /* Found a CRTC on this fb, now find encoders */
349 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
350 if (encoder->crtc == crtc) {
351 struct drm_encoder_helper_funcs *encoder_funcs;
353 encoder_funcs = encoder->helper_private;
354 encoder_funcs->dpms(encoder, dpms_mode);
357 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
359 mutex_unlock(&dev->mode_config.mutex);
362 int drm_fb_helper_blank(int blank, struct fb_info *info)
365 /* Display: On; HSync: On, VSync: On */
366 case FB_BLANK_UNBLANK:
367 drm_fb_helper_on(info);
369 /* Display: Off; HSync: On, VSync: On */
370 case FB_BLANK_NORMAL:
371 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
373 /* Display: Off; HSync: Off, VSync: On */
374 case FB_BLANK_HSYNC_SUSPEND:
375 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
377 /* Display: Off; HSync: On, VSync: Off */
378 case FB_BLANK_VSYNC_SUSPEND:
379 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
381 /* Display: Off; HSync: Off, VSync: Off */
382 case FB_BLANK_POWERDOWN:
383 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
388 EXPORT_SYMBOL(drm_fb_helper_blank);
390 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
394 for (i = 0; i < helper->crtc_count; i++)
395 kfree(helper->crtc_info[i].mode_set.connectors);
396 kfree(helper->crtc_info);
399 int drm_fb_helper_init_crtc_count(struct drm_device *dev,
400 struct drm_fb_helper *helper,
401 int crtc_count, int max_conn_count)
403 struct drm_crtc *crtc;
407 INIT_LIST_HEAD(&helper->kernel_fb_list);
409 helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
410 if (!helper->crtc_info)
412 helper->crtc_count = crtc_count;
414 for (i = 0; i < crtc_count; i++) {
415 helper->crtc_info[i].mode_set.connectors =
416 kcalloc(max_conn_count,
417 sizeof(struct drm_connector *),
420 if (!helper->crtc_info[i].mode_set.connectors) {
424 helper->crtc_info[i].mode_set.num_connectors = 0;
428 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
429 helper->crtc_info[i].crtc_id = crtc->base.id;
430 helper->crtc_info[i].mode_set.crtc = crtc;
433 helper->conn_limit = max_conn_count;
436 drm_fb_helper_crtc_free(helper);
439 EXPORT_SYMBOL(drm_fb_helper_init_crtc_count);
441 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
442 u16 blue, u16 regno, struct fb_info *info)
444 struct drm_fb_helper *fb_helper = info->par;
445 struct drm_framebuffer *fb = fb_helper->fb;
448 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
451 /* place color in psuedopalette */
454 palette = (u32 *)info->pseudo_palette;
455 red >>= (16 - info->var.red.length);
456 green >>= (16 - info->var.green.length);
457 blue >>= (16 - info->var.blue.length);
458 value = (red << info->var.red.offset) |
459 (green << info->var.green.offset) |
460 (blue << info->var.blue.offset);
461 palette[regno] = value;
467 if (fb->bits_per_pixel == 16) {
470 if (fb->depth == 16 && regno > 63)
472 if (fb->depth == 15 && regno > 31)
475 if (fb->depth == 16) {
479 for (i = 0; i < 8; i++)
480 fb_helper->funcs->gamma_set(crtc, red,
481 green, blue, pindex + i);
484 fb_helper->funcs->gamma_get(crtc, &r,
488 for (i = 0; i < 4; i++)
489 fb_helper->funcs->gamma_set(crtc, r,
496 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
500 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
502 struct drm_fb_helper *fb_helper = info->par;
503 struct drm_crtc_helper_funcs *crtc_funcs;
504 u16 *red, *green, *blue, *transp;
505 struct drm_crtc *crtc;
509 for (i = 0; i < fb_helper->crtc_count; i++) {
510 crtc = fb_helper->crtc_info[i].mode_set.crtc;
511 crtc_funcs = crtc->helper_private;
516 transp = cmap->transp;
519 for (i = 0; i < cmap->len; i++) {
520 u16 hred, hgreen, hblue, htransp = 0xffff;
529 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
533 crtc_funcs->load_lut(crtc);
537 EXPORT_SYMBOL(drm_fb_helper_setcmap);
539 int drm_fb_helper_setcolreg(unsigned regno,
544 struct fb_info *info)
546 struct drm_fb_helper *fb_helper = info->par;
547 struct drm_crtc *crtc;
548 struct drm_crtc_helper_funcs *crtc_funcs;
555 for (i = 0; i < fb_helper->crtc_count; i++) {
556 crtc = fb_helper->crtc_info[i].mode_set.crtc;
557 crtc_funcs = crtc->helper_private;
559 ret = setcolreg(crtc, red, green, blue, regno, info);
563 crtc_funcs->load_lut(crtc);
567 EXPORT_SYMBOL(drm_fb_helper_setcolreg);
569 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
570 struct fb_info *info)
572 struct drm_fb_helper *fb_helper = info->par;
573 struct drm_framebuffer *fb = fb_helper->fb;
576 if (var->pixclock != 0)
579 /* Need to resize the fb object !!! */
580 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
581 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
582 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
583 fb->width, fb->height, fb->bits_per_pixel);
587 switch (var->bits_per_pixel) {
589 depth = (var->green.length == 6) ? 16 : 15;
592 depth = (var->transp.length > 0) ? 32 : 24;
595 depth = var->bits_per_pixel;
602 var->green.offset = 0;
603 var->blue.offset = 0;
605 var->green.length = 8;
606 var->blue.length = 8;
607 var->transp.length = 0;
608 var->transp.offset = 0;
611 var->red.offset = 10;
612 var->green.offset = 5;
613 var->blue.offset = 0;
615 var->green.length = 5;
616 var->blue.length = 5;
617 var->transp.length = 1;
618 var->transp.offset = 15;
621 var->red.offset = 11;
622 var->green.offset = 5;
623 var->blue.offset = 0;
625 var->green.length = 6;
626 var->blue.length = 5;
627 var->transp.length = 0;
628 var->transp.offset = 0;
631 var->red.offset = 16;
632 var->green.offset = 8;
633 var->blue.offset = 0;
635 var->green.length = 8;
636 var->blue.length = 8;
637 var->transp.length = 0;
638 var->transp.offset = 0;
641 var->red.offset = 16;
642 var->green.offset = 8;
643 var->blue.offset = 0;
645 var->green.length = 8;
646 var->blue.length = 8;
647 var->transp.length = 8;
648 var->transp.offset = 24;
655 EXPORT_SYMBOL(drm_fb_helper_check_var);
657 /* this will let fbcon do the mode init */
658 int drm_fb_helper_set_par(struct fb_info *info)
660 struct drm_fb_helper *fb_helper = info->par;
661 struct drm_device *dev = fb_helper->dev;
662 struct fb_var_screeninfo *var = &info->var;
663 struct drm_crtc *crtc;
667 if (var->pixclock != 0) {
668 DRM_ERROR("PIXEL CLOCK SET\n");
672 mutex_lock(&dev->mode_config.mutex);
673 for (i = 0; i < fb_helper->crtc_count; i++) {
674 crtc = fb_helper->crtc_info[i].mode_set.crtc;
676 if (crtc->fb != fb_helper->crtc_info[i].mode_set.fb) {
677 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
680 mutex_unlock(&dev->mode_config.mutex);
685 mutex_unlock(&dev->mode_config.mutex);
688 EXPORT_SYMBOL(drm_fb_helper_set_par);
690 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
691 struct fb_info *info)
693 struct drm_fb_helper *fb_helper = info->par;
694 struct drm_device *dev = fb_helper->dev;
695 struct drm_mode_set *modeset;
696 struct drm_crtc *crtc;
700 mutex_lock(&dev->mode_config.mutex);
701 for (i = 0; i < fb_helper->crtc_count; i++) {
702 crtc = fb_helper->crtc_info[i].mode_set.crtc;
704 modeset = &fb_helper->crtc_info[i].mode_set;
706 modeset->x = var->xoffset;
707 modeset->y = var->yoffset;
709 if (modeset->num_connectors) {
710 ret = crtc->funcs->set_config(modeset);
712 info->var.xoffset = var->xoffset;
713 info->var.yoffset = var->yoffset;
717 mutex_unlock(&dev->mode_config.mutex);
720 EXPORT_SYMBOL(drm_fb_helper_pan_display);
722 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
725 struct drm_device *dev = fb_helper->dev;
726 struct drm_connector *connector;
730 struct fb_info *info;
731 struct drm_fb_helper_surface_size sizes;
734 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
735 sizes.surface_depth = 24;
736 sizes.surface_bpp = 32;
737 sizes.fb_width = (unsigned)-1;
738 sizes.fb_height = (unsigned)-1;
740 /* if driver picks 8 or 16 by default use that
741 for both depth/bpp */
742 if (preferred_bpp != sizes.surface_bpp) {
743 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
745 /* first up get a count of crtcs now in use and new min/maxes width/heights */
746 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
747 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
748 struct drm_fb_helper_cmdline_mode *cmdline_mode;
753 cmdline_mode = &fb_help_conn->cmdline_mode;
755 if (cmdline_mode->bpp_specified) {
756 switch (cmdline_mode->bpp) {
758 sizes.surface_depth = sizes.surface_bpp = 8;
761 sizes.surface_depth = 15;
762 sizes.surface_bpp = 16;
765 sizes.surface_depth = sizes.surface_bpp = 16;
768 sizes.surface_depth = sizes.surface_bpp = 24;
771 sizes.surface_depth = 24;
772 sizes.surface_bpp = 32;
780 for (i = 0; i < fb_helper->crtc_count; i++) {
781 struct drm_display_mode *desired_mode;
782 desired_mode = fb_helper->crtc_info[i].desired_mode;
786 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
787 if (desired_mode->hdisplay < sizes.fb_width)
788 sizes.fb_width = desired_mode->hdisplay;
789 if (desired_mode->vdisplay < sizes.fb_height)
790 sizes.fb_height = desired_mode->vdisplay;
791 if (desired_mode->hdisplay > sizes.surface_width)
792 sizes.surface_width = desired_mode->hdisplay;
793 if (desired_mode->vdisplay > sizes.surface_height)
794 sizes.surface_height = desired_mode->vdisplay;
799 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
800 /* hmm everyone went away - assume VGA cable just fell out
801 and will come back later. */
805 /* push down into drivers */
806 new_fb = (*fb_helper->fb_probe)(fb_helper, &sizes);
810 info = fb_helper->fbdev;
812 /* set the fb pointer */
813 for (i = 0; i < fb_helper->crtc_count; i++) {
814 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
818 info->var.pixclock = 0;
819 ret = fb_alloc_cmap(&info->cmap, gamma_size, 0);
822 if (register_framebuffer(info) < 0) {
823 fb_dealloc_cmap(&info->cmap);
827 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
831 drm_fb_helper_set_par(info);
834 /* Switch back to kernel console on panic */
835 /* multi card linked list maybe */
836 if (list_empty(&kernel_fb_helper_list)) {
837 printk(KERN_INFO "registered panic notifier\n");
838 atomic_notifier_chain_register(&panic_notifier_list,
840 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
843 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
847 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
849 void drm_fb_helper_free(struct drm_fb_helper *helper)
851 if (!list_empty(&helper->kernel_fb_list)) {
852 list_del(&helper->kernel_fb_list);
853 if (list_empty(&kernel_fb_helper_list)) {
854 printk(KERN_INFO "unregistered panic notifier\n");
855 atomic_notifier_chain_unregister(&panic_notifier_list,
857 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
860 drm_fb_helper_crtc_free(helper);
861 if (helper->fbdev->cmap.len)
862 fb_dealloc_cmap(&helper->fbdev->cmap);
864 EXPORT_SYMBOL(drm_fb_helper_free);
866 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
869 info->fix.type = FB_TYPE_PACKED_PIXELS;
870 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
872 info->fix.type_aux = 0;
873 info->fix.xpanstep = 1; /* doing it in hw */
874 info->fix.ypanstep = 1; /* doing it in hw */
875 info->fix.ywrapstep = 0;
876 info->fix.accel = FB_ACCEL_NONE;
877 info->fix.type_aux = 0;
879 info->fix.line_length = pitch;
882 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
884 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
885 uint32_t fb_width, uint32_t fb_height)
887 struct drm_framebuffer *fb = fb_helper->fb;
888 info->pseudo_palette = fb_helper->pseudo_palette;
889 info->var.xres_virtual = fb->width;
890 info->var.yres_virtual = fb->height;
891 info->var.bits_per_pixel = fb->bits_per_pixel;
892 info->var.xoffset = 0;
893 info->var.yoffset = 0;
894 info->var.activate = FB_ACTIVATE_NOW;
895 info->var.height = -1;
896 info->var.width = -1;
900 info->var.red.offset = 0;
901 info->var.green.offset = 0;
902 info->var.blue.offset = 0;
903 info->var.red.length = 8; /* 8bit DAC */
904 info->var.green.length = 8;
905 info->var.blue.length = 8;
906 info->var.transp.offset = 0;
907 info->var.transp.length = 0;
910 info->var.red.offset = 10;
911 info->var.green.offset = 5;
912 info->var.blue.offset = 0;
913 info->var.red.length = 5;
914 info->var.green.length = 5;
915 info->var.blue.length = 5;
916 info->var.transp.offset = 15;
917 info->var.transp.length = 1;
920 info->var.red.offset = 11;
921 info->var.green.offset = 5;
922 info->var.blue.offset = 0;
923 info->var.red.length = 5;
924 info->var.green.length = 6;
925 info->var.blue.length = 5;
926 info->var.transp.offset = 0;
929 info->var.red.offset = 16;
930 info->var.green.offset = 8;
931 info->var.blue.offset = 0;
932 info->var.red.length = 8;
933 info->var.green.length = 8;
934 info->var.blue.length = 8;
935 info->var.transp.offset = 0;
936 info->var.transp.length = 0;
939 info->var.red.offset = 16;
940 info->var.green.offset = 8;
941 info->var.blue.offset = 0;
942 info->var.red.length = 8;
943 info->var.green.length = 8;
944 info->var.blue.length = 8;
945 info->var.transp.offset = 24;
946 info->var.transp.length = 8;
952 info->var.xres = fb_width;
953 info->var.yres = fb_height;
955 EXPORT_SYMBOL(drm_fb_helper_fill_var);
957 static int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
960 struct drm_connector *connector;
963 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
964 count += connector->funcs->fill_modes(connector, maxX, maxY);
970 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
972 struct drm_display_mode *mode;
974 list_for_each_entry(mode, &connector->modes, head) {
975 if (drm_mode_width(mode) > width ||
976 drm_mode_height(mode) > height)
978 if (mode->type & DRM_MODE_TYPE_PREFERRED)
984 static bool drm_has_cmdline_mode(struct drm_connector *connector)
986 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
987 struct drm_fb_helper_cmdline_mode *cmdline_mode;
992 cmdline_mode = &fb_help_conn->cmdline_mode;
993 return cmdline_mode->specified;
996 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
998 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
999 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1000 struct drm_display_mode *mode = NULL;
1005 cmdline_mode = &fb_help_conn->cmdline_mode;
1006 if (cmdline_mode->specified == false)
1009 /* attempt to find a matching mode in the list of modes
1010 * we have gotten so far, if not add a CVT mode that conforms
1012 if (cmdline_mode->rb || cmdline_mode->margins)
1015 list_for_each_entry(mode, &connector->modes, head) {
1016 /* check width/height */
1017 if (mode->hdisplay != cmdline_mode->xres ||
1018 mode->vdisplay != cmdline_mode->yres)
1021 if (cmdline_mode->refresh_specified) {
1022 if (mode->vrefresh != cmdline_mode->refresh)
1026 if (cmdline_mode->interlace) {
1027 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1034 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
1036 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1037 cmdline_mode->rb, cmdline_mode->interlace,
1038 cmdline_mode->margins);
1039 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1040 list_add(&mode->head, &connector->modes);
1044 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1049 enable = connector->status == connector_status_connected;
1051 enable = connector->status != connector_status_disconnected;
1056 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
1058 bool any_enabled = false;
1059 struct drm_connector *connector;
1062 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1063 enabled[i] = drm_connector_enabled(connector, true);
1064 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1065 enabled[i] ? "yes" : "no");
1066 any_enabled |= enabled[i];
1074 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1075 enabled[i] = drm_connector_enabled(connector, false);
1080 static bool drm_target_preferred(struct drm_device *dev,
1081 struct drm_display_mode **modes,
1082 bool *enabled, int width, int height)
1084 struct drm_connector *connector;
1087 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1089 if (enabled[i] == false) {
1094 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1095 connector->base.id);
1097 /* got for command line mode first */
1098 modes[i] = drm_pick_cmdline_mode(connector, width, height);
1100 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1101 connector->base.id);
1102 modes[i] = drm_has_preferred_mode(connector, width, height);
1104 /* No preferred modes, pick one off the list */
1105 if (!modes[i] && !list_empty(&connector->modes)) {
1106 list_for_each_entry(modes[i], &connector->modes, head)
1109 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1116 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1117 struct drm_fb_helper_crtc **best_crtcs,
1118 struct drm_display_mode **modes,
1119 int n, int width, int height)
1122 struct drm_device *dev = fb_helper->dev;
1123 struct drm_connector *connector;
1124 struct drm_connector_helper_funcs *connector_funcs;
1125 struct drm_encoder *encoder;
1126 struct drm_fb_helper_crtc *best_crtc;
1127 int my_score, best_score, score;
1128 struct drm_fb_helper_crtc **crtcs, *crtc;
1130 if (n == fb_helper->dev->mode_config.num_connector)
1133 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1139 best_crtcs[n] = NULL;
1141 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1142 if (modes[n] == NULL)
1145 crtcs = kzalloc(dev->mode_config.num_connector *
1146 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1151 if (connector->status == connector_status_connected)
1153 if (drm_has_cmdline_mode(connector))
1155 if (drm_has_preferred_mode(connector, width, height))
1158 connector_funcs = connector->helper_private;
1159 encoder = connector_funcs->best_encoder(connector);
1163 /* select a crtc for this connector and then attempt to configure
1164 remaining connectors */
1165 for (c = 0; c < fb_helper->crtc_count; c++) {
1166 crtc = &fb_helper->crtc_info[c];
1168 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1172 for (o = 0; o < n; o++)
1173 if (best_crtcs[o] == crtc)
1177 /* ignore cloning for now */
1182 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1183 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1185 if (score > best_score) {
1188 memcpy(best_crtcs, crtcs,
1189 dev->mode_config.num_connector *
1190 sizeof(struct drm_fb_helper_crtc *));
1198 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1200 struct drm_device *dev = fb_helper->dev;
1201 struct drm_fb_helper_crtc **crtcs;
1202 struct drm_display_mode **modes;
1203 struct drm_encoder *encoder;
1204 struct drm_connector *connector;
1205 struct drm_mode_set *modeset;
1210 DRM_DEBUG_KMS("\n");
1212 width = dev->mode_config.max_width;
1213 height = dev->mode_config.max_height;
1215 /* clean out all the encoder/crtc combos */
1216 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1217 encoder->crtc = NULL;
1220 crtcs = kcalloc(dev->mode_config.num_connector,
1221 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1222 modes = kcalloc(dev->mode_config.num_connector,
1223 sizeof(struct drm_display_mode *), GFP_KERNEL);
1224 enabled = kcalloc(dev->mode_config.num_connector,
1225 sizeof(bool), GFP_KERNEL);
1227 drm_enable_connectors(dev, enabled);
1229 ret = drm_target_preferred(dev, modes, enabled, width, height);
1231 DRM_ERROR("Unable to find initial modes\n");
1233 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1235 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1237 /* need to set the modesets up here for use later */
1238 /* fill out the connector<->crtc mappings into the modesets */
1239 for (i = 0; i < fb_helper->crtc_count; i++) {
1240 modeset = &fb_helper->crtc_info[i].mode_set;
1241 modeset->num_connectors = 0;
1245 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1246 struct drm_display_mode *mode = modes[i];
1247 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1248 modeset = &fb_crtc->mode_set;
1250 if (mode && fb_crtc) {
1251 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1252 mode->name, fb_crtc->mode_set.crtc->base.id);
1253 fb_crtc->desired_mode = mode;
1255 drm_mode_destroy(dev, modeset->mode);
1256 modeset->mode = drm_mode_duplicate(dev,
1257 fb_crtc->desired_mode);
1258 modeset->connectors[modeset->num_connectors++] = connector;
1269 * drm_helper_initial_config - setup a sane initial connector configuration
1273 * Called at init time, must take mode config lock.
1275 * Scan the CRTCs and connectors and try to put together an initial setup.
1276 * At the moment, this is a cloned configuration across all heads with
1277 * a new framebuffer object as the backing store.
1280 * Zero if everything went ok, nonzero otherwise.
1282 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
1284 struct drm_device *dev = fb_helper->dev;
1287 /* disable all the possible outputs/crtcs before entering KMS mode */
1288 drm_helper_disable_unused_functions(fb_helper->dev);
1290 drm_fb_helper_parse_command_line(fb_helper->dev);
1292 count = drm_helper_probe_connector_modes(dev,
1293 dev->mode_config.max_width,
1294 dev->mode_config.max_height);
1297 * we shouldn't end up with no modes here.
1300 printk(KERN_INFO "No connectors reported connected with modes\n");
1302 drm_setup_crtcs(fb_helper);
1306 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1308 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper,
1309 u32 max_width, u32 max_height)
1311 DRM_DEBUG_KMS("\n");
1313 drm_helper_probe_connector_modes(fb_helper->dev, max_width,
1316 drm_setup_crtcs(fb_helper);
1320 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);