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 static struct slow_work_ops output_status_change_ops;
46 /* simple single crtc case helper function */
47 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
49 struct drm_device *dev = fb_helper->dev;
50 struct drm_connector *connector;
53 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
54 struct drm_fb_helper_connector *fb_helper_connector;
56 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
57 if (!fb_helper_connector)
60 fb_helper_connector->connector = connector;
61 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
65 for (i = 0; i < fb_helper->connector_count; i++) {
66 kfree(fb_helper->connector_info[i]);
67 fb_helper->connector_info[i] = NULL;
69 fb_helper->connector_count = 0;
72 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
75 * drm_fb_helper_connector_parse_command_line - parse command line for connector
76 * @connector - connector to parse line for
77 * @mode_option - per connector mode option
79 * This parses the connector specific then generic command lines for
80 * modes and options to configure the connector.
82 * This uses the same parameters as the fb modedb.c, except for extra
83 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
85 * enable/enable Digital/disable bit at the end
87 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
88 const char *mode_option)
92 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
93 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
94 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
96 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
97 struct drm_fb_helper_cmdline_mode *cmdline_mode;
98 struct drm_connector *connector = fb_helper_conn->connector;
103 cmdline_mode = &fb_helper_conn->cmdline_mode;
105 mode_option = fb_mode_option;
108 cmdline_mode->specified = false;
113 namelen = strlen(name);
114 for (i = namelen-1; i >= 0; i--) {
118 if (!refresh_specified && !bpp_specified &&
120 refresh = simple_strtol(&name[i+1], NULL, 10);
121 refresh_specified = 1;
129 if (!bpp_specified && !yres_specified) {
130 bpp = simple_strtol(&name[i+1], NULL, 10);
138 if (!yres_specified) {
139 yres = simple_strtol(&name[i+1], NULL, 10);
162 force = DRM_FORCE_ON;
165 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
166 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
167 force = DRM_FORCE_ON;
169 force = DRM_FORCE_ON_DIGITAL;
172 force = DRM_FORCE_OFF;
178 if (i < 0 && yres_specified) {
179 xres = simple_strtol(name, NULL, 10);
184 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
185 drm_get_connector_name(connector), xres, yres,
186 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
187 "", (margins) ? " with margins" : "", (interlace) ?
193 case DRM_FORCE_OFF: s = "OFF"; break;
194 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
196 case DRM_FORCE_ON: s = "ON"; break;
199 DRM_INFO("forcing %s connector %s\n",
200 drm_get_connector_name(connector), s);
201 connector->force = force;
205 cmdline_mode->specified = true;
206 cmdline_mode->xres = xres;
207 cmdline_mode->yres = yres;
210 if (refresh_specified) {
211 cmdline_mode->refresh_specified = true;
212 cmdline_mode->refresh = refresh;
216 cmdline_mode->bpp_specified = true;
217 cmdline_mode->bpp = bpp;
219 cmdline_mode->rb = rb ? true : false;
220 cmdline_mode->cvt = cvt ? true : false;
221 cmdline_mode->interlace = interlace ? true : false;
226 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
228 struct drm_fb_helper_connector *fb_helper_conn;
231 for (i = 0; i < fb_helper->connector_count; i++) {
234 fb_helper_conn = fb_helper->connector_info[i];
236 /* do something on return - turn off connector maybe */
237 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
240 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
245 bool drm_fb_helper_force_kernel_mode(void)
248 bool ret, error = false;
249 struct drm_fb_helper *helper;
251 if (list_empty(&kernel_fb_helper_list))
254 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
255 for (i = 0; i < helper->crtc_count; i++) {
256 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
257 ret = drm_crtc_helper_set_config(mode_set);
265 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
268 DRM_ERROR("panic occurred, switching back to text console\n");
269 return drm_fb_helper_force_kernel_mode();
272 EXPORT_SYMBOL(drm_fb_helper_panic);
274 static struct notifier_block paniced = {
275 .notifier_call = drm_fb_helper_panic,
279 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
281 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
283 void drm_fb_helper_restore(void)
286 ret = drm_fb_helper_force_kernel_mode();
288 DRM_ERROR("Failed to restore crtc configuration\n");
290 EXPORT_SYMBOL(drm_fb_helper_restore);
292 #ifdef CONFIG_MAGIC_SYSRQ
293 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
295 drm_fb_helper_restore();
297 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
299 static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
301 schedule_work(&drm_fb_helper_restore_work);
304 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
305 .handler = drm_fb_helper_sysrq,
306 .help_msg = "force-fb(V)",
307 .action_msg = "Restore framebuffer console",
310 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
313 static void drm_fb_helper_on(struct fb_info *info)
315 struct drm_fb_helper *fb_helper = info->par;
316 struct drm_device *dev = fb_helper->dev;
317 struct drm_crtc *crtc;
318 struct drm_crtc_helper_funcs *crtc_funcs;
319 struct drm_encoder *encoder;
323 * For each CRTC in this fb, turn the crtc on then,
324 * find all associated encoders and turn them on.
326 mutex_lock(&dev->mode_config.mutex);
327 for (i = 0; i < fb_helper->crtc_count; i++) {
328 crtc = fb_helper->crtc_info[i].mode_set.crtc;
329 crtc_funcs = crtc->helper_private;
334 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
337 /* Found a CRTC on this fb, now find encoders */
338 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
339 if (encoder->crtc == crtc) {
340 struct drm_encoder_helper_funcs *encoder_funcs;
342 encoder_funcs = encoder->helper_private;
343 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
347 mutex_unlock(&dev->mode_config.mutex);
350 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
352 struct drm_fb_helper *fb_helper = info->par;
353 struct drm_device *dev = fb_helper->dev;
354 struct drm_crtc *crtc;
355 struct drm_crtc_helper_funcs *crtc_funcs;
356 struct drm_encoder *encoder;
360 * For each CRTC in this fb, find all associated encoders
361 * and turn them off, then turn off the CRTC.
363 mutex_lock(&dev->mode_config.mutex);
364 for (i = 0; i < fb_helper->crtc_count; i++) {
365 crtc = fb_helper->crtc_info[i].mode_set.crtc;
366 crtc_funcs = crtc->helper_private;
371 /* Found a CRTC on this fb, now find encoders */
372 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
373 if (encoder->crtc == crtc) {
374 struct drm_encoder_helper_funcs *encoder_funcs;
376 encoder_funcs = encoder->helper_private;
377 encoder_funcs->dpms(encoder, dpms_mode);
380 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
382 mutex_unlock(&dev->mode_config.mutex);
385 int drm_fb_helper_blank(int blank, struct fb_info *info)
388 /* Display: On; HSync: On, VSync: On */
389 case FB_BLANK_UNBLANK:
390 drm_fb_helper_on(info);
392 /* Display: Off; HSync: On, VSync: On */
393 case FB_BLANK_NORMAL:
394 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
396 /* Display: Off; HSync: Off, VSync: On */
397 case FB_BLANK_HSYNC_SUSPEND:
398 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
400 /* Display: Off; HSync: On, VSync: Off */
401 case FB_BLANK_VSYNC_SUSPEND:
402 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
404 /* Display: Off; HSync: Off, VSync: Off */
405 case FB_BLANK_POWERDOWN:
406 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
411 EXPORT_SYMBOL(drm_fb_helper_blank);
413 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
417 for (i = 0; i < helper->connector_count; i++)
418 kfree(helper->connector_info[i]);
419 kfree(helper->connector_info);
420 for (i = 0; i < helper->crtc_count; i++)
421 kfree(helper->crtc_info[i].mode_set.connectors);
422 kfree(helper->crtc_info);
425 int drm_fb_helper_init(struct drm_device *dev,
426 struct drm_fb_helper *fb_helper,
427 int crtc_count, int max_conn_count,
430 struct drm_crtc *crtc;
434 fb_helper->dev = dev;
435 fb_helper->poll_enabled = polled;
437 slow_work_register_user(THIS_MODULE);
438 delayed_slow_work_init(&fb_helper->output_status_change_slow_work,
439 &output_status_change_ops);
441 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
443 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
444 if (!fb_helper->crtc_info)
447 fb_helper->crtc_count = crtc_count;
448 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
449 if (!fb_helper->connector_info) {
450 kfree(fb_helper->crtc_info);
453 fb_helper->connector_count = 0;
455 for (i = 0; i < crtc_count; i++) {
456 fb_helper->crtc_info[i].mode_set.connectors =
457 kcalloc(max_conn_count,
458 sizeof(struct drm_connector *),
461 if (!fb_helper->crtc_info[i].mode_set.connectors) {
465 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
469 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
470 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
471 fb_helper->crtc_info[i].mode_set.crtc = crtc;
474 fb_helper->conn_limit = max_conn_count;
477 drm_fb_helper_crtc_free(fb_helper);
480 EXPORT_SYMBOL(drm_fb_helper_init);
482 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
484 if (!list_empty(&fb_helper->kernel_fb_list)) {
485 list_del(&fb_helper->kernel_fb_list);
486 if (list_empty(&kernel_fb_helper_list)) {
487 printk(KERN_INFO "unregistered panic notifier\n");
488 atomic_notifier_chain_unregister(&panic_notifier_list,
490 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
494 drm_fb_helper_crtc_free(fb_helper);
496 delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
497 slow_work_unregister_user(THIS_MODULE);
499 EXPORT_SYMBOL(drm_fb_helper_fini);
501 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
502 u16 blue, u16 regno, struct fb_info *info)
504 struct drm_fb_helper *fb_helper = info->par;
505 struct drm_framebuffer *fb = fb_helper->fb;
508 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
511 /* place color in psuedopalette */
514 palette = (u32 *)info->pseudo_palette;
515 red >>= (16 - info->var.red.length);
516 green >>= (16 - info->var.green.length);
517 blue >>= (16 - info->var.blue.length);
518 value = (red << info->var.red.offset) |
519 (green << info->var.green.offset) |
520 (blue << info->var.blue.offset);
521 palette[regno] = value;
527 if (fb->bits_per_pixel == 16) {
530 if (fb->depth == 16 && regno > 63)
532 if (fb->depth == 15 && regno > 31)
535 if (fb->depth == 16) {
539 for (i = 0; i < 8; i++)
540 fb_helper->funcs->gamma_set(crtc, red,
541 green, blue, pindex + i);
544 fb_helper->funcs->gamma_get(crtc, &r,
548 for (i = 0; i < 4; i++)
549 fb_helper->funcs->gamma_set(crtc, r,
556 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
560 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
562 struct drm_fb_helper *fb_helper = info->par;
563 struct drm_crtc_helper_funcs *crtc_funcs;
564 u16 *red, *green, *blue, *transp;
565 struct drm_crtc *crtc;
569 for (i = 0; i < fb_helper->crtc_count; i++) {
570 crtc = fb_helper->crtc_info[i].mode_set.crtc;
571 crtc_funcs = crtc->helper_private;
576 transp = cmap->transp;
579 for (i = 0; i < cmap->len; i++) {
580 u16 hred, hgreen, hblue, htransp = 0xffff;
589 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
593 crtc_funcs->load_lut(crtc);
597 EXPORT_SYMBOL(drm_fb_helper_setcmap);
599 int drm_fb_helper_setcolreg(unsigned regno,
604 struct fb_info *info)
606 struct drm_fb_helper *fb_helper = info->par;
607 struct drm_crtc *crtc;
608 struct drm_crtc_helper_funcs *crtc_funcs;
615 for (i = 0; i < fb_helper->crtc_count; i++) {
616 crtc = fb_helper->crtc_info[i].mode_set.crtc;
617 crtc_funcs = crtc->helper_private;
619 ret = setcolreg(crtc, red, green, blue, regno, info);
623 crtc_funcs->load_lut(crtc);
627 EXPORT_SYMBOL(drm_fb_helper_setcolreg);
629 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
630 struct fb_info *info)
632 struct drm_fb_helper *fb_helper = info->par;
633 struct drm_framebuffer *fb = fb_helper->fb;
636 if (var->pixclock != 0)
639 /* Need to resize the fb object !!! */
640 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
641 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
642 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
643 fb->width, fb->height, fb->bits_per_pixel);
647 switch (var->bits_per_pixel) {
649 depth = (var->green.length == 6) ? 16 : 15;
652 depth = (var->transp.length > 0) ? 32 : 24;
655 depth = var->bits_per_pixel;
662 var->green.offset = 0;
663 var->blue.offset = 0;
665 var->green.length = 8;
666 var->blue.length = 8;
667 var->transp.length = 0;
668 var->transp.offset = 0;
671 var->red.offset = 10;
672 var->green.offset = 5;
673 var->blue.offset = 0;
675 var->green.length = 5;
676 var->blue.length = 5;
677 var->transp.length = 1;
678 var->transp.offset = 15;
681 var->red.offset = 11;
682 var->green.offset = 5;
683 var->blue.offset = 0;
685 var->green.length = 6;
686 var->blue.length = 5;
687 var->transp.length = 0;
688 var->transp.offset = 0;
691 var->red.offset = 16;
692 var->green.offset = 8;
693 var->blue.offset = 0;
695 var->green.length = 8;
696 var->blue.length = 8;
697 var->transp.length = 0;
698 var->transp.offset = 0;
701 var->red.offset = 16;
702 var->green.offset = 8;
703 var->blue.offset = 0;
705 var->green.length = 8;
706 var->blue.length = 8;
707 var->transp.length = 8;
708 var->transp.offset = 24;
715 EXPORT_SYMBOL(drm_fb_helper_check_var);
717 /* this will let fbcon do the mode init */
718 int drm_fb_helper_set_par(struct fb_info *info)
720 struct drm_fb_helper *fb_helper = info->par;
721 struct drm_device *dev = fb_helper->dev;
722 struct fb_var_screeninfo *var = &info->var;
723 struct drm_crtc *crtc;
727 if (var->pixclock != 0) {
728 DRM_ERROR("PIXEL CLOCK SET\n");
732 mutex_lock(&dev->mode_config.mutex);
733 for (i = 0; i < fb_helper->crtc_count; i++) {
734 crtc = fb_helper->crtc_info[i].mode_set.crtc;
735 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
737 mutex_unlock(&dev->mode_config.mutex);
741 mutex_unlock(&dev->mode_config.mutex);
743 if (fb_helper->delayed_hotplug) {
744 fb_helper->delayed_hotplug = false;
745 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
749 EXPORT_SYMBOL(drm_fb_helper_set_par);
751 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
752 struct fb_info *info)
754 struct drm_fb_helper *fb_helper = info->par;
755 struct drm_device *dev = fb_helper->dev;
756 struct drm_mode_set *modeset;
757 struct drm_crtc *crtc;
761 mutex_lock(&dev->mode_config.mutex);
762 for (i = 0; i < fb_helper->crtc_count; i++) {
763 crtc = fb_helper->crtc_info[i].mode_set.crtc;
765 modeset = &fb_helper->crtc_info[i].mode_set;
767 modeset->x = var->xoffset;
768 modeset->y = var->yoffset;
770 if (modeset->num_connectors) {
771 ret = crtc->funcs->set_config(modeset);
773 info->var.xoffset = var->xoffset;
774 info->var.yoffset = var->yoffset;
778 mutex_unlock(&dev->mode_config.mutex);
781 EXPORT_SYMBOL(drm_fb_helper_pan_display);
783 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
789 struct fb_info *info;
790 struct drm_fb_helper_surface_size sizes;
793 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
794 sizes.surface_depth = 24;
795 sizes.surface_bpp = 32;
796 sizes.fb_width = (unsigned)-1;
797 sizes.fb_height = (unsigned)-1;
799 /* if driver picks 8 or 16 by default use that
800 for both depth/bpp */
801 if (preferred_bpp != sizes.surface_bpp) {
802 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
804 /* first up get a count of crtcs now in use and new min/maxes width/heights */
805 for (i = 0; i < fb_helper->connector_count; i++) {
806 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
807 struct drm_fb_helper_cmdline_mode *cmdline_mode;
809 cmdline_mode = &fb_helper_conn->cmdline_mode;
811 if (cmdline_mode->bpp_specified) {
812 switch (cmdline_mode->bpp) {
814 sizes.surface_depth = sizes.surface_bpp = 8;
817 sizes.surface_depth = 15;
818 sizes.surface_bpp = 16;
821 sizes.surface_depth = sizes.surface_bpp = 16;
824 sizes.surface_depth = sizes.surface_bpp = 24;
827 sizes.surface_depth = 24;
828 sizes.surface_bpp = 32;
836 for (i = 0; i < fb_helper->crtc_count; i++) {
837 struct drm_display_mode *desired_mode;
838 desired_mode = fb_helper->crtc_info[i].desired_mode;
842 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
843 if (desired_mode->hdisplay < sizes.fb_width)
844 sizes.fb_width = desired_mode->hdisplay;
845 if (desired_mode->vdisplay < sizes.fb_height)
846 sizes.fb_height = desired_mode->vdisplay;
847 if (desired_mode->hdisplay > sizes.surface_width)
848 sizes.surface_width = desired_mode->hdisplay;
849 if (desired_mode->vdisplay > sizes.surface_height)
850 sizes.surface_height = desired_mode->vdisplay;
855 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
856 /* hmm everyone went away - assume VGA cable just fell out
857 and will come back later. */
858 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
859 sizes.fb_width = sizes.surface_width = 1024;
860 sizes.fb_height = sizes.surface_height = 768;
863 /* push down into drivers */
864 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
868 info = fb_helper->fbdev;
870 /* set the fb pointer */
871 for (i = 0; i < fb_helper->crtc_count; i++) {
872 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
876 info->var.pixclock = 0;
877 if (register_framebuffer(info) < 0) {
881 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
885 drm_fb_helper_set_par(info);
888 /* Switch back to kernel console on panic */
889 /* multi card linked list maybe */
890 if (list_empty(&kernel_fb_helper_list)) {
891 printk(KERN_INFO "registered panic notifier\n");
892 atomic_notifier_chain_register(&panic_notifier_list,
894 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
897 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
901 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
903 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
906 info->fix.type = FB_TYPE_PACKED_PIXELS;
907 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
909 info->fix.type_aux = 0;
910 info->fix.xpanstep = 1; /* doing it in hw */
911 info->fix.ypanstep = 1; /* doing it in hw */
912 info->fix.ywrapstep = 0;
913 info->fix.accel = FB_ACCEL_NONE;
914 info->fix.type_aux = 0;
916 info->fix.line_length = pitch;
919 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
921 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
922 uint32_t fb_width, uint32_t fb_height)
924 struct drm_framebuffer *fb = fb_helper->fb;
925 info->pseudo_palette = fb_helper->pseudo_palette;
926 info->var.xres_virtual = fb->width;
927 info->var.yres_virtual = fb->height;
928 info->var.bits_per_pixel = fb->bits_per_pixel;
929 info->var.xoffset = 0;
930 info->var.yoffset = 0;
931 info->var.activate = FB_ACTIVATE_NOW;
932 info->var.height = -1;
933 info->var.width = -1;
937 info->var.red.offset = 0;
938 info->var.green.offset = 0;
939 info->var.blue.offset = 0;
940 info->var.red.length = 8; /* 8bit DAC */
941 info->var.green.length = 8;
942 info->var.blue.length = 8;
943 info->var.transp.offset = 0;
944 info->var.transp.length = 0;
947 info->var.red.offset = 10;
948 info->var.green.offset = 5;
949 info->var.blue.offset = 0;
950 info->var.red.length = 5;
951 info->var.green.length = 5;
952 info->var.blue.length = 5;
953 info->var.transp.offset = 15;
954 info->var.transp.length = 1;
957 info->var.red.offset = 11;
958 info->var.green.offset = 5;
959 info->var.blue.offset = 0;
960 info->var.red.length = 5;
961 info->var.green.length = 6;
962 info->var.blue.length = 5;
963 info->var.transp.offset = 0;
966 info->var.red.offset = 16;
967 info->var.green.offset = 8;
968 info->var.blue.offset = 0;
969 info->var.red.length = 8;
970 info->var.green.length = 8;
971 info->var.blue.length = 8;
972 info->var.transp.offset = 0;
973 info->var.transp.length = 0;
976 info->var.red.offset = 16;
977 info->var.green.offset = 8;
978 info->var.blue.offset = 0;
979 info->var.red.length = 8;
980 info->var.green.length = 8;
981 info->var.blue.length = 8;
982 info->var.transp.offset = 24;
983 info->var.transp.length = 8;
989 info->var.xres = fb_width;
990 info->var.yres = fb_height;
992 EXPORT_SYMBOL(drm_fb_helper_fill_var);
994 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
998 struct drm_connector *connector;
1002 for (i = 0; i < fb_helper->connector_count; i++) {
1003 connector = fb_helper->connector_info[i]->connector;
1004 count += connector->funcs->fill_modes(connector, maxX, maxY);
1010 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1012 struct drm_display_mode *mode;
1014 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1015 if (drm_mode_width(mode) > width ||
1016 drm_mode_height(mode) > height)
1018 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1024 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1026 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1027 cmdline_mode = &fb_connector->cmdline_mode;
1028 return cmdline_mode->specified;
1031 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1032 int width, int height)
1034 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1035 struct drm_display_mode *mode = NULL;
1037 cmdline_mode = &fb_helper_conn->cmdline_mode;
1038 if (cmdline_mode->specified == false)
1041 /* attempt to find a matching mode in the list of modes
1042 * we have gotten so far, if not add a CVT mode that conforms
1044 if (cmdline_mode->rb || cmdline_mode->margins)
1047 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1048 /* check width/height */
1049 if (mode->hdisplay != cmdline_mode->xres ||
1050 mode->vdisplay != cmdline_mode->yres)
1053 if (cmdline_mode->refresh_specified) {
1054 if (mode->vrefresh != cmdline_mode->refresh)
1058 if (cmdline_mode->interlace) {
1059 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1066 mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1068 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1069 cmdline_mode->rb, cmdline_mode->interlace,
1070 cmdline_mode->margins);
1071 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1072 list_add(&mode->head, &fb_helper_conn->connector->modes);
1076 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1081 enable = connector->status == connector_status_connected;
1083 enable = connector->status != connector_status_disconnected;
1088 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1091 bool any_enabled = false;
1092 struct drm_connector *connector;
1095 for (i = 0; i < fb_helper->connector_count; i++) {
1096 connector = fb_helper->connector_info[i]->connector;
1097 enabled[i] = drm_connector_enabled(connector, true);
1098 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1099 enabled[i] ? "yes" : "no");
1100 any_enabled |= enabled[i];
1106 for (i = 0; i < fb_helper->connector_count; i++) {
1107 connector = fb_helper->connector_info[i]->connector;
1108 enabled[i] = drm_connector_enabled(connector, false);
1112 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1113 struct drm_display_mode **modes,
1114 bool *enabled, int width, int height)
1116 struct drm_fb_helper_connector *fb_helper_conn;
1119 for (i = 0; i < fb_helper->connector_count; i++) {
1120 fb_helper_conn = fb_helper->connector_info[i];
1122 if (enabled[i] == false)
1125 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1126 fb_helper_conn->connector->base.id);
1128 /* got for command line mode first */
1129 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1131 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1132 fb_helper_conn->connector->base.id);
1133 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1135 /* No preferred modes, pick one off the list */
1136 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1137 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1140 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1146 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1147 struct drm_fb_helper_crtc **best_crtcs,
1148 struct drm_display_mode **modes,
1149 int n, int width, int height)
1152 struct drm_device *dev = fb_helper->dev;
1153 struct drm_connector *connector;
1154 struct drm_connector_helper_funcs *connector_funcs;
1155 struct drm_encoder *encoder;
1156 struct drm_fb_helper_crtc *best_crtc;
1157 int my_score, best_score, score;
1158 struct drm_fb_helper_crtc **crtcs, *crtc;
1159 struct drm_fb_helper_connector *fb_helper_conn;
1161 if (n == fb_helper->connector_count)
1164 fb_helper_conn = fb_helper->connector_info[n];
1165 connector = fb_helper_conn->connector;
1167 best_crtcs[n] = NULL;
1169 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1170 if (modes[n] == NULL)
1173 crtcs = kzalloc(dev->mode_config.num_connector *
1174 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1179 if (connector->status == connector_status_connected)
1181 if (drm_has_cmdline_mode(fb_helper_conn))
1183 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1186 connector_funcs = connector->helper_private;
1187 encoder = connector_funcs->best_encoder(connector);
1191 /* select a crtc for this connector and then attempt to configure
1192 remaining connectors */
1193 for (c = 0; c < fb_helper->crtc_count; c++) {
1194 crtc = &fb_helper->crtc_info[c];
1196 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1200 for (o = 0; o < n; o++)
1201 if (best_crtcs[o] == crtc)
1205 /* ignore cloning for now */
1210 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1211 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1213 if (score > best_score) {
1216 memcpy(best_crtcs, crtcs,
1217 dev->mode_config.num_connector *
1218 sizeof(struct drm_fb_helper_crtc *));
1226 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1228 struct drm_device *dev = fb_helper->dev;
1229 struct drm_fb_helper_crtc **crtcs;
1230 struct drm_display_mode **modes;
1231 struct drm_encoder *encoder;
1232 struct drm_mode_set *modeset;
1237 DRM_DEBUG_KMS("\n");
1239 width = dev->mode_config.max_width;
1240 height = dev->mode_config.max_height;
1242 /* clean out all the encoder/crtc combos */
1243 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1244 encoder->crtc = NULL;
1247 crtcs = kcalloc(dev->mode_config.num_connector,
1248 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1249 modes = kcalloc(dev->mode_config.num_connector,
1250 sizeof(struct drm_display_mode *), GFP_KERNEL);
1251 enabled = kcalloc(dev->mode_config.num_connector,
1252 sizeof(bool), GFP_KERNEL);
1254 drm_enable_connectors(fb_helper, enabled);
1256 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1258 DRM_ERROR("Unable to find initial modes\n");
1260 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1262 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1264 /* need to set the modesets up here for use later */
1265 /* fill out the connector<->crtc mappings into the modesets */
1266 for (i = 0; i < fb_helper->crtc_count; i++) {
1267 modeset = &fb_helper->crtc_info[i].mode_set;
1268 modeset->num_connectors = 0;
1271 for (i = 0; i < fb_helper->connector_count; i++) {
1272 struct drm_display_mode *mode = modes[i];
1273 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1274 modeset = &fb_crtc->mode_set;
1276 if (mode && fb_crtc) {
1277 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1278 mode->name, fb_crtc->mode_set.crtc->base.id);
1279 fb_crtc->desired_mode = mode;
1281 drm_mode_destroy(dev, modeset->mode);
1282 modeset->mode = drm_mode_duplicate(dev,
1283 fb_crtc->desired_mode);
1284 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1294 * drm_helper_initial_config - setup a sane initial connector configuration
1298 * Called at init time, must take mode config lock.
1300 * Scan the CRTCs and connectors and try to put together an initial setup.
1301 * At the moment, this is a cloned configuration across all heads with
1302 * a new framebuffer object as the backing store.
1305 * Zero if everything went ok, nonzero otherwise.
1307 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1309 struct drm_device *dev = fb_helper->dev;
1312 /* disable all the possible outputs/crtcs before entering KMS mode */
1313 drm_helper_disable_unused_functions(fb_helper->dev);
1315 drm_fb_helper_parse_command_line(fb_helper);
1317 count = drm_fb_helper_probe_connector_modes(fb_helper,
1318 dev->mode_config.max_width,
1319 dev->mode_config.max_height);
1321 * we shouldn't end up with no modes here.
1324 if (fb_helper->poll_enabled) {
1325 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work,
1327 printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1329 printk(KERN_INFO "No connectors reported connected with modes\n");
1331 drm_setup_crtcs(fb_helper);
1333 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1335 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1337 /* we got a hotplug irq - need to update fbcon */
1338 void drm_helper_fb_hpd_irq_event(struct drm_fb_helper *fb_helper)
1340 /* if we don't have the fbdev registered yet do nothing */
1341 if (!fb_helper->fbdev)
1344 /* schedule a slow work asap */
1345 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
1347 EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event);
1349 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, bool polled)
1353 u32 max_width, max_height, bpp_sel;
1357 DRM_DEBUG_KMS("\n");
1359 max_width = fb_helper->fb->width;
1360 max_height = fb_helper->fb->height;
1361 bpp_sel = fb_helper->fb->bits_per_pixel;
1363 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1365 if (fb_helper->poll_enabled && !polled) {
1367 delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
1369 ret = delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 5*HZ);
1372 drm_setup_crtcs(fb_helper);
1374 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1376 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
1379 * delayed work queue execution function
1380 * - check if fbdev is actually in use on the gpu
1381 * - if not set delayed flag and repoll if necessary
1382 * - check for connector status change
1383 * - repoll if 0 modes found
1384 *- call driver output status changed notifier
1386 static void output_status_change_execute(struct slow_work *work)
1388 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
1389 struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_status_change_slow_work);
1390 struct drm_connector *connector;
1391 enum drm_connector_status old_status, status;
1392 bool repoll, changed = false;
1395 bool bound = false, crtcs_bound = false;
1396 struct drm_crtc *crtc;
1398 repoll = fb_helper->poll_enabled;
1400 /* first of all check the fbcon framebuffer is actually bound to any crtc */
1401 /* take into account that no crtc at all maybe bound */
1402 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1405 if (crtc->fb == fb_helper->fb)
1409 if (bound == false && crtcs_bound) {
1410 fb_helper->delayed_hotplug = true;
1414 for (i = 0; i < fb_helper->connector_count; i++) {
1415 connector = fb_helper->connector_info[i]->connector;
1416 old_status = connector->status;
1417 status = connector->funcs->detect(connector);
1418 if (old_status != status) {
1421 if (status == connector_status_connected && repoll) {
1422 DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1428 if (fb_helper->funcs->fb_output_status_changed)
1429 fb_helper->funcs->fb_output_status_changed(fb_helper);
1434 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1436 DRM_ERROR("delayed enqueue failed %d\n", ret);
1440 static struct slow_work_ops output_status_change_ops = {
1441 .execute = output_status_change_execute,