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_encoder *encoder;
299 * For each CRTC in this fb, turn the crtc on then,
300 * find all associated encoders and turn them on.
302 for (i = 0; i < fb_helper->crtc_count; i++) {
303 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
304 struct drm_crtc_helper_funcs *crtc_funcs =
305 crtc->helper_private;
307 /* Only mess with CRTCs in this fb */
308 if (crtc->base.id != fb_helper->crtc_info[i].crtc_id ||
312 mutex_lock(&dev->mode_config.mutex);
313 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
314 mutex_unlock(&dev->mode_config.mutex);
316 /* Found a CRTC on this fb, now find encoders */
317 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
318 if (encoder->crtc == crtc) {
319 struct drm_encoder_helper_funcs *encoder_funcs;
321 encoder_funcs = encoder->helper_private;
322 mutex_lock(&dev->mode_config.mutex);
323 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
324 mutex_unlock(&dev->mode_config.mutex);
331 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
333 struct drm_fb_helper *fb_helper = info->par;
334 struct drm_device *dev = fb_helper->dev;
335 struct drm_crtc *crtc;
336 struct drm_encoder *encoder;
340 * For each CRTC in this fb, find all associated encoders
341 * and turn them off, then turn off the CRTC.
343 for (i = 0; i < fb_helper->crtc_count; i++) {
344 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
345 struct drm_crtc_helper_funcs *crtc_funcs =
346 crtc->helper_private;
348 /* Only mess with CRTCs in this fb */
349 if (crtc->base.id != fb_helper->crtc_info[i].crtc_id ||
353 /* Found a CRTC on this fb, now find encoders */
354 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
355 if (encoder->crtc == crtc) {
356 struct drm_encoder_helper_funcs *encoder_funcs;
358 encoder_funcs = encoder->helper_private;
359 mutex_lock(&dev->mode_config.mutex);
360 encoder_funcs->dpms(encoder, dpms_mode);
361 mutex_unlock(&dev->mode_config.mutex);
364 mutex_lock(&dev->mode_config.mutex);
365 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
366 mutex_unlock(&dev->mode_config.mutex);
371 int drm_fb_helper_blank(int blank, struct fb_info *info)
374 /* Display: On; HSync: On, VSync: On */
375 case FB_BLANK_UNBLANK:
376 drm_fb_helper_on(info);
378 /* Display: Off; HSync: On, VSync: On */
379 case FB_BLANK_NORMAL:
380 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
382 /* Display: Off; HSync: Off, VSync: On */
383 case FB_BLANK_HSYNC_SUSPEND:
384 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
386 /* Display: Off; HSync: On, VSync: Off */
387 case FB_BLANK_VSYNC_SUSPEND:
388 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
390 /* Display: Off; HSync: Off, VSync: Off */
391 case FB_BLANK_POWERDOWN:
392 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
397 EXPORT_SYMBOL(drm_fb_helper_blank);
399 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
403 for (i = 0; i < helper->crtc_count; i++)
404 kfree(helper->crtc_info[i].mode_set.connectors);
405 kfree(helper->crtc_info);
408 int drm_fb_helper_init_crtc_count(struct drm_fb_helper *helper, int crtc_count, int max_conn_count)
410 struct drm_device *dev = helper->dev;
411 struct drm_crtc *crtc;
415 helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
416 if (!helper->crtc_info)
419 helper->crtc_count = crtc_count;
421 for (i = 0; i < crtc_count; i++) {
422 helper->crtc_info[i].mode_set.connectors =
423 kcalloc(max_conn_count,
424 sizeof(struct drm_connector *),
427 if (!helper->crtc_info[i].mode_set.connectors) {
431 helper->crtc_info[i].mode_set.num_connectors = 0;
435 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
436 helper->crtc_info[i].crtc_id = crtc->base.id;
437 helper->crtc_info[i].mode_set.crtc = crtc;
440 helper->conn_limit = max_conn_count;
443 drm_fb_helper_crtc_free(helper);
446 EXPORT_SYMBOL(drm_fb_helper_init_crtc_count);
448 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
449 u16 blue, u16 regno, struct fb_info *info)
451 struct drm_fb_helper *fb_helper = info->par;
452 struct drm_framebuffer *fb = fb_helper->fb;
455 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
458 /* place color in psuedopalette */
461 palette = (u32 *)info->pseudo_palette;
462 red >>= (16 - info->var.red.length);
463 green >>= (16 - info->var.green.length);
464 blue >>= (16 - info->var.blue.length);
465 value = (red << info->var.red.offset) |
466 (green << info->var.green.offset) |
467 (blue << info->var.blue.offset);
468 palette[regno] = value;
474 if (fb->bits_per_pixel == 16) {
477 if (fb->depth == 16 && regno > 63)
479 if (fb->depth == 15 && regno > 31)
482 if (fb->depth == 16) {
486 for (i = 0; i < 8; i++)
487 fb_helper->funcs->gamma_set(crtc, red,
488 green, blue, pindex + i);
491 fb_helper->funcs->gamma_get(crtc, &r,
495 for (i = 0; i < 4; i++)
496 fb_helper->funcs->gamma_set(crtc, r,
503 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
507 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
509 struct drm_fb_helper *fb_helper = info->par;
510 struct drm_device *dev = fb_helper->dev;
511 u16 *red, *green, *blue, *transp;
512 struct drm_crtc *crtc;
516 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
517 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
518 for (i = 0; i < fb_helper->crtc_count; i++) {
519 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
522 if (i == fb_helper->crtc_count)
528 transp = cmap->transp;
531 for (i = 0; i < cmap->len; i++) {
532 u16 hred, hgreen, hblue, htransp = 0xffff;
541 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
545 crtc_funcs->load_lut(crtc);
549 EXPORT_SYMBOL(drm_fb_helper_setcmap);
551 int drm_fb_helper_setcolreg(unsigned regno,
556 struct fb_info *info)
558 struct drm_fb_helper *fb_helper = info->par;
559 struct drm_device *dev = fb_helper->dev;
560 struct drm_crtc *crtc;
567 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
568 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
569 for (i = 0; i < fb_helper->crtc_count; i++) {
570 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
573 if (i == fb_helper->crtc_count)
576 ret = setcolreg(crtc, red, green, blue, regno, info);
580 crtc_funcs->load_lut(crtc);
584 EXPORT_SYMBOL(drm_fb_helper_setcolreg);
586 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
587 struct fb_info *info)
589 struct drm_fb_helper *fb_helper = info->par;
590 struct drm_framebuffer *fb = fb_helper->fb;
593 if (var->pixclock != 0)
596 /* Need to resize the fb object !!! */
597 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
598 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
599 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
600 fb->width, fb->height, fb->bits_per_pixel);
604 switch (var->bits_per_pixel) {
606 depth = (var->green.length == 6) ? 16 : 15;
609 depth = (var->transp.length > 0) ? 32 : 24;
612 depth = var->bits_per_pixel;
619 var->green.offset = 0;
620 var->blue.offset = 0;
622 var->green.length = 8;
623 var->blue.length = 8;
624 var->transp.length = 0;
625 var->transp.offset = 0;
628 var->red.offset = 10;
629 var->green.offset = 5;
630 var->blue.offset = 0;
632 var->green.length = 5;
633 var->blue.length = 5;
634 var->transp.length = 1;
635 var->transp.offset = 15;
638 var->red.offset = 11;
639 var->green.offset = 5;
640 var->blue.offset = 0;
642 var->green.length = 6;
643 var->blue.length = 5;
644 var->transp.length = 0;
645 var->transp.offset = 0;
648 var->red.offset = 16;
649 var->green.offset = 8;
650 var->blue.offset = 0;
652 var->green.length = 8;
653 var->blue.length = 8;
654 var->transp.length = 0;
655 var->transp.offset = 0;
658 var->red.offset = 16;
659 var->green.offset = 8;
660 var->blue.offset = 0;
662 var->green.length = 8;
663 var->blue.length = 8;
664 var->transp.length = 8;
665 var->transp.offset = 24;
672 EXPORT_SYMBOL(drm_fb_helper_check_var);
674 /* this will let fbcon do the mode init */
675 int drm_fb_helper_set_par(struct fb_info *info)
677 struct drm_fb_helper *fb_helper = info->par;
678 struct drm_device *dev = fb_helper->dev;
679 struct fb_var_screeninfo *var = &info->var;
680 struct drm_crtc *crtc;
684 if (var->pixclock != 0) {
685 DRM_ERROR("PIXEL CLOCK SET\n");
689 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
691 for (i = 0; i < fb_helper->crtc_count; i++) {
692 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
695 if (i == fb_helper->crtc_count)
698 if (crtc->fb == fb_helper->crtc_info[i].mode_set.fb) {
699 mutex_lock(&dev->mode_config.mutex);
700 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
701 mutex_unlock(&dev->mode_config.mutex);
708 EXPORT_SYMBOL(drm_fb_helper_set_par);
710 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
711 struct fb_info *info)
713 struct drm_fb_helper *fb_helper = info->par;
714 struct drm_device *dev = fb_helper->dev;
715 struct drm_mode_set *modeset;
716 struct drm_crtc *crtc;
720 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
721 for (i = 0; i < fb_helper->crtc_count; i++) {
722 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
726 if (i == fb_helper->crtc_count)
729 modeset = &fb_helper->crtc_info[i].mode_set;
731 modeset->x = var->xoffset;
732 modeset->y = var->yoffset;
734 if (modeset->num_connectors) {
735 mutex_lock(&dev->mode_config.mutex);
736 ret = crtc->funcs->set_config(modeset);
737 mutex_unlock(&dev->mode_config.mutex);
739 info->var.xoffset = var->xoffset;
740 info->var.yoffset = var->yoffset;
746 EXPORT_SYMBOL(drm_fb_helper_pan_display);
748 int drm_fb_helper_single_fb_probe(struct drm_device *dev,
750 int (*fb_find_or_create)(struct drm_device *dev,
751 struct drm_fb_helper_surface_size *sizes,
752 struct drm_fb_helper **fb_ptr))
754 struct drm_crtc *crtc;
755 struct drm_connector *connector;
758 int ret, i, conn_count = 0;
759 struct fb_info *info;
760 struct drm_mode_set *modeset = NULL;
761 struct drm_fb_helper *fb_helper;
762 struct drm_fb_helper_surface_size sizes;
764 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
765 sizes.surface_depth = 24;
766 sizes.surface_bpp = 32;
767 sizes.fb_width = (unsigned)-1;
768 sizes.fb_height = (unsigned)-1;
770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
772 if (preferred_bpp != sizes.surface_bpp) {
773 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
776 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
777 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
779 struct drm_fb_helper_cmdline_mode *cmdline_mode;
784 cmdline_mode = &fb_help_conn->cmdline_mode;
786 if (cmdline_mode->bpp_specified) {
787 switch (cmdline_mode->bpp) {
789 sizes.surface_depth = sizes.surface_bpp = 8;
792 sizes.surface_depth = 15;
793 sizes.surface_bpp = 16;
796 sizes.surface_depth = sizes.surface_bpp = 16;
799 sizes.surface_depth = sizes.surface_bpp = 24;
802 sizes.surface_depth = 24;
803 sizes.surface_bpp = 32;
810 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
811 if (drm_helper_crtc_in_use(crtc)) {
812 if (crtc->desired_mode) {
813 if (crtc->desired_mode->hdisplay < sizes.fb_width)
814 sizes.fb_width = crtc->desired_mode->hdisplay;
816 if (crtc->desired_mode->vdisplay < sizes.fb_height)
817 sizes.fb_height = crtc->desired_mode->vdisplay;
819 if (crtc->desired_mode->hdisplay > sizes.surface_width)
820 sizes.surface_width = crtc->desired_mode->hdisplay;
822 if (crtc->desired_mode->vdisplay > sizes.surface_height)
823 sizes.surface_height = crtc->desired_mode->vdisplay;
829 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
830 /* hmm everyone went away - assume VGA cable just fell out
831 and will come back later. */
835 /* push down into drivers */
836 new_fb = (*fb_find_or_create)(dev, &sizes,
841 info = fb_helper->fbdev;
844 /* okay we need to setup new connector sets in the crtcs */
845 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
846 modeset = &fb_helper->crtc_info[crtc_count].mode_set;
847 modeset->fb = fb_helper->fb;
849 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
850 if (connector->encoder)
851 if (connector->encoder->crtc == modeset->crtc) {
852 modeset->connectors[conn_count] = connector;
854 if (conn_count > fb_helper->conn_limit)
859 for (i = conn_count; i < fb_helper->conn_limit; i++)
860 modeset->connectors[i] = NULL;
862 modeset->crtc = crtc;
865 modeset->num_connectors = conn_count;
866 if (modeset->crtc->desired_mode) {
868 drm_mode_destroy(dev, modeset->mode);
869 modeset->mode = drm_mode_duplicate(dev,
870 modeset->crtc->desired_mode);
873 fb_helper->crtc_count = crtc_count;
876 info->var.pixclock = 0;
877 ret = fb_alloc_cmap(&info->cmap, modeset->crtc->gamma_size, 0);
880 if (register_framebuffer(info) < 0) {
881 fb_dealloc_cmap(&info->cmap);
885 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
889 drm_fb_helper_set_par(info);
892 /* Switch back to kernel console on panic */
893 /* multi card linked list maybe */
894 if (list_empty(&kernel_fb_helper_list)) {
895 printk(KERN_INFO "registered panic notifier\n");
896 atomic_notifier_chain_register(&panic_notifier_list,
898 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
901 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
905 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
907 void drm_fb_helper_free(struct drm_fb_helper *helper)
909 list_del(&helper->kernel_fb_list);
910 if (list_empty(&kernel_fb_helper_list)) {
911 printk(KERN_INFO "unregistered panic notifier\n");
912 atomic_notifier_chain_unregister(&panic_notifier_list,
914 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
916 drm_fb_helper_crtc_free(helper);
917 fb_dealloc_cmap(&helper->fbdev->cmap);
919 EXPORT_SYMBOL(drm_fb_helper_free);
921 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
924 info->fix.type = FB_TYPE_PACKED_PIXELS;
925 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
927 info->fix.type_aux = 0;
928 info->fix.xpanstep = 1; /* doing it in hw */
929 info->fix.ypanstep = 1; /* doing it in hw */
930 info->fix.ywrapstep = 0;
931 info->fix.accel = FB_ACCEL_NONE;
932 info->fix.type_aux = 0;
934 info->fix.line_length = pitch;
937 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
939 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
940 uint32_t fb_width, uint32_t fb_height)
942 struct drm_framebuffer *fb = fb_helper->fb;
943 info->pseudo_palette = fb_helper->pseudo_palette;
944 info->var.xres_virtual = fb->width;
945 info->var.yres_virtual = fb->height;
946 info->var.bits_per_pixel = fb->bits_per_pixel;
947 info->var.xoffset = 0;
948 info->var.yoffset = 0;
949 info->var.activate = FB_ACTIVATE_NOW;
950 info->var.height = -1;
951 info->var.width = -1;
955 info->var.red.offset = 0;
956 info->var.green.offset = 0;
957 info->var.blue.offset = 0;
958 info->var.red.length = 8; /* 8bit DAC */
959 info->var.green.length = 8;
960 info->var.blue.length = 8;
961 info->var.transp.offset = 0;
962 info->var.transp.length = 0;
965 info->var.red.offset = 10;
966 info->var.green.offset = 5;
967 info->var.blue.offset = 0;
968 info->var.red.length = 5;
969 info->var.green.length = 5;
970 info->var.blue.length = 5;
971 info->var.transp.offset = 15;
972 info->var.transp.length = 1;
975 info->var.red.offset = 11;
976 info->var.green.offset = 5;
977 info->var.blue.offset = 0;
978 info->var.red.length = 5;
979 info->var.green.length = 6;
980 info->var.blue.length = 5;
981 info->var.transp.offset = 0;
984 info->var.red.offset = 16;
985 info->var.green.offset = 8;
986 info->var.blue.offset = 0;
987 info->var.red.length = 8;
988 info->var.green.length = 8;
989 info->var.blue.length = 8;
990 info->var.transp.offset = 0;
991 info->var.transp.length = 0;
994 info->var.red.offset = 16;
995 info->var.green.offset = 8;
996 info->var.blue.offset = 0;
997 info->var.red.length = 8;
998 info->var.green.length = 8;
999 info->var.blue.length = 8;
1000 info->var.transp.offset = 24;
1001 info->var.transp.length = 8;
1007 info->var.xres = fb_width;
1008 info->var.yres = fb_height;
1010 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1012 static int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
1015 struct drm_connector *connector;
1018 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1019 count += connector->funcs->fill_modes(connector, maxX, maxY);
1025 static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
1027 struct drm_display_mode *mode;
1029 list_for_each_entry(mode, &connector->modes, head) {
1030 if (drm_mode_width(mode) > width ||
1031 drm_mode_height(mode) > height)
1033 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1039 static bool drm_has_cmdline_mode(struct drm_connector *connector)
1041 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
1042 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1047 cmdline_mode = &fb_help_conn->cmdline_mode;
1048 return cmdline_mode->specified;
1051 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
1053 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
1054 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1055 struct drm_display_mode *mode = NULL;
1060 cmdline_mode = &fb_help_conn->cmdline_mode;
1061 if (cmdline_mode->specified == false)
1064 /* attempt to find a matching mode in the list of modes
1065 * we have gotten so far, if not add a CVT mode that conforms
1067 if (cmdline_mode->rb || cmdline_mode->margins)
1070 list_for_each_entry(mode, &connector->modes, head) {
1071 /* check width/height */
1072 if (mode->hdisplay != cmdline_mode->xres ||
1073 mode->vdisplay != cmdline_mode->yres)
1076 if (cmdline_mode->refresh_specified) {
1077 if (mode->vrefresh != cmdline_mode->refresh)
1081 if (cmdline_mode->interlace) {
1082 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1089 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
1091 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1092 cmdline_mode->rb, cmdline_mode->interlace,
1093 cmdline_mode->margins);
1094 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1095 list_add(&mode->head, &connector->modes);
1099 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1104 enable = connector->status == connector_status_connected;
1106 enable = connector->status != connector_status_disconnected;
1111 static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
1113 bool any_enabled = false;
1114 struct drm_connector *connector;
1117 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1118 enabled[i] = drm_connector_enabled(connector, true);
1119 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1120 enabled[i] ? "yes" : "no");
1121 any_enabled |= enabled[i];
1129 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1130 enabled[i] = drm_connector_enabled(connector, false);
1135 static bool drm_target_preferred(struct drm_device *dev,
1136 struct drm_display_mode **modes,
1137 bool *enabled, int width, int height)
1139 struct drm_connector *connector;
1142 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1144 if (enabled[i] == false) {
1149 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1150 connector->base.id);
1152 /* got for command line mode first */
1153 modes[i] = drm_pick_cmdline_mode(connector, width, height);
1155 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1156 connector->base.id);
1157 modes[i] = drm_has_preferred_mode(connector, width, height);
1159 /* No preferred modes, pick one off the list */
1160 if (!modes[i] && !list_empty(&connector->modes)) {
1161 list_for_each_entry(modes[i], &connector->modes, head)
1164 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1171 static int drm_pick_crtcs(struct drm_device *dev,
1172 struct drm_crtc **best_crtcs,
1173 struct drm_display_mode **modes,
1174 int n, int width, int height)
1177 struct drm_connector *connector;
1178 struct drm_connector_helper_funcs *connector_funcs;
1179 struct drm_encoder *encoder;
1180 struct drm_crtc *best_crtc;
1181 int my_score, best_score, score;
1182 struct drm_crtc **crtcs, *crtc;
1184 if (n == dev->mode_config.num_connector)
1187 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1193 best_crtcs[n] = NULL;
1195 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
1196 if (modes[n] == NULL)
1199 crtcs = kmalloc(dev->mode_config.num_connector *
1200 sizeof(struct drm_crtc *), GFP_KERNEL);
1205 if (connector->status == connector_status_connected)
1207 if (drm_has_cmdline_mode(connector))
1209 if (drm_has_preferred_mode(connector, width, height))
1212 connector_funcs = connector->helper_private;
1213 encoder = connector_funcs->best_encoder(connector);
1217 connector->encoder = encoder;
1219 /* select a crtc for this connector and then attempt to configure
1220 remaining connectors */
1222 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1224 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1229 for (o = 0; o < n; o++)
1230 if (best_crtcs[o] == crtc)
1234 /* ignore cloning for now */
1240 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
1241 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
1243 if (score > best_score) {
1246 memcpy(best_crtcs, crtcs,
1247 dev->mode_config.num_connector *
1248 sizeof(struct drm_crtc *));
1257 static void drm_setup_crtcs(struct drm_device *dev)
1259 struct drm_crtc **crtcs;
1260 struct drm_display_mode **modes;
1261 struct drm_encoder *encoder;
1262 struct drm_connector *connector;
1267 DRM_DEBUG_KMS("\n");
1269 width = dev->mode_config.max_width;
1270 height = dev->mode_config.max_height;
1272 /* clean out all the encoder/crtc combos */
1273 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1274 encoder->crtc = NULL;
1277 crtcs = kcalloc(dev->mode_config.num_connector,
1278 sizeof(struct drm_crtc *), GFP_KERNEL);
1279 modes = kcalloc(dev->mode_config.num_connector,
1280 sizeof(struct drm_display_mode *), GFP_KERNEL);
1281 enabled = kcalloc(dev->mode_config.num_connector,
1282 sizeof(bool), GFP_KERNEL);
1284 drm_enable_connectors(dev, enabled);
1286 ret = drm_target_preferred(dev, modes, enabled, width, height);
1288 DRM_ERROR("Unable to find initial modes\n");
1290 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1292 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
1295 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1296 struct drm_display_mode *mode = modes[i];
1297 struct drm_crtc *crtc = crtcs[i];
1299 if (connector->encoder == NULL) {
1305 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1306 mode->name, crtc->base.id);
1307 crtc->desired_mode = mode;
1308 connector->encoder->crtc = crtc;
1310 connector->encoder->crtc = NULL;
1311 connector->encoder = NULL;
1322 * drm_helper_initial_config - setup a sane initial connector configuration
1326 * Called at init time, must take mode config lock.
1328 * Scan the CRTCs and connectors and try to put together an initial setup.
1329 * At the moment, this is a cloned configuration across all heads with
1330 * a new framebuffer object as the backing store.
1333 * Zero if everything went ok, nonzero otherwise.
1335 bool drm_helper_initial_config(struct drm_device *dev)
1339 /* disable all the possible outputs/crtcs before entering KMS mode */
1340 drm_helper_disable_unused_functions(dev);
1342 drm_fb_helper_parse_command_line(dev);
1344 count = drm_helper_probe_connector_modes(dev,
1345 dev->mode_config.max_width,
1346 dev->mode_config.max_height);
1349 * we shouldn't end up with no modes here.
1352 printk(KERN_INFO "No connectors reported connected with modes\n");
1354 drm_setup_crtcs(dev);
1358 EXPORT_SYMBOL(drm_helper_initial_config);
1360 bool drm_helper_fb_hotplug_event(struct drm_device *dev)
1362 DRM_DEBUG_KMS("\n");
1364 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
1365 dev->mode_config.max_height);
1367 drm_setup_crtcs(dev);
1371 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);