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_check_var(struct fb_var_screeninfo *var,
600 struct fb_info *info)
602 struct drm_fb_helper *fb_helper = info->par;
603 struct drm_framebuffer *fb = fb_helper->fb;
606 if (var->pixclock != 0)
609 /* Need to resize the fb object !!! */
610 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
611 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
612 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
613 fb->width, fb->height, fb->bits_per_pixel);
617 switch (var->bits_per_pixel) {
619 depth = (var->green.length == 6) ? 16 : 15;
622 depth = (var->transp.length > 0) ? 32 : 24;
625 depth = var->bits_per_pixel;
632 var->green.offset = 0;
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 = 10;
642 var->green.offset = 5;
643 var->blue.offset = 0;
645 var->green.length = 5;
646 var->blue.length = 5;
647 var->transp.length = 1;
648 var->transp.offset = 15;
651 var->red.offset = 11;
652 var->green.offset = 5;
653 var->blue.offset = 0;
655 var->green.length = 6;
656 var->blue.length = 5;
657 var->transp.length = 0;
658 var->transp.offset = 0;
661 var->red.offset = 16;
662 var->green.offset = 8;
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 = 16;
672 var->green.offset = 8;
673 var->blue.offset = 0;
675 var->green.length = 8;
676 var->blue.length = 8;
677 var->transp.length = 8;
678 var->transp.offset = 24;
685 EXPORT_SYMBOL(drm_fb_helper_check_var);
687 /* this will let fbcon do the mode init */
688 int drm_fb_helper_set_par(struct fb_info *info)
690 struct drm_fb_helper *fb_helper = info->par;
691 struct drm_device *dev = fb_helper->dev;
692 struct fb_var_screeninfo *var = &info->var;
693 struct drm_crtc *crtc;
697 if (var->pixclock != 0) {
698 DRM_ERROR("PIXEL CLOCK SET\n");
702 mutex_lock(&dev->mode_config.mutex);
703 for (i = 0; i < fb_helper->crtc_count; i++) {
704 crtc = fb_helper->crtc_info[i].mode_set.crtc;
705 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
707 mutex_unlock(&dev->mode_config.mutex);
711 mutex_unlock(&dev->mode_config.mutex);
713 if (fb_helper->delayed_hotplug) {
714 fb_helper->delayed_hotplug = false;
715 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
719 EXPORT_SYMBOL(drm_fb_helper_set_par);
721 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
722 struct fb_info *info)
724 struct drm_fb_helper *fb_helper = info->par;
725 struct drm_device *dev = fb_helper->dev;
726 struct drm_mode_set *modeset;
727 struct drm_crtc *crtc;
731 mutex_lock(&dev->mode_config.mutex);
732 for (i = 0; i < fb_helper->crtc_count; i++) {
733 crtc = fb_helper->crtc_info[i].mode_set.crtc;
735 modeset = &fb_helper->crtc_info[i].mode_set;
737 modeset->x = var->xoffset;
738 modeset->y = var->yoffset;
740 if (modeset->num_connectors) {
741 ret = crtc->funcs->set_config(modeset);
743 info->var.xoffset = var->xoffset;
744 info->var.yoffset = var->yoffset;
748 mutex_unlock(&dev->mode_config.mutex);
751 EXPORT_SYMBOL(drm_fb_helper_pan_display);
753 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
759 struct fb_info *info;
760 struct drm_fb_helper_surface_size sizes;
763 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
764 sizes.surface_depth = 24;
765 sizes.surface_bpp = 32;
766 sizes.fb_width = (unsigned)-1;
767 sizes.fb_height = (unsigned)-1;
769 /* if driver picks 8 or 16 by default use that
770 for both depth/bpp */
771 if (preferred_bpp != sizes.surface_bpp) {
772 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
774 /* first up get a count of crtcs now in use and new min/maxes width/heights */
775 for (i = 0; i < fb_helper->connector_count; i++) {
776 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
777 struct drm_fb_helper_cmdline_mode *cmdline_mode;
779 cmdline_mode = &fb_helper_conn->cmdline_mode;
781 if (cmdline_mode->bpp_specified) {
782 switch (cmdline_mode->bpp) {
784 sizes.surface_depth = sizes.surface_bpp = 8;
787 sizes.surface_depth = 15;
788 sizes.surface_bpp = 16;
791 sizes.surface_depth = sizes.surface_bpp = 16;
794 sizes.surface_depth = sizes.surface_bpp = 24;
797 sizes.surface_depth = 24;
798 sizes.surface_bpp = 32;
806 for (i = 0; i < fb_helper->crtc_count; i++) {
807 struct drm_display_mode *desired_mode;
808 desired_mode = fb_helper->crtc_info[i].desired_mode;
812 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
813 if (desired_mode->hdisplay < sizes.fb_width)
814 sizes.fb_width = desired_mode->hdisplay;
815 if (desired_mode->vdisplay < sizes.fb_height)
816 sizes.fb_height = desired_mode->vdisplay;
817 if (desired_mode->hdisplay > sizes.surface_width)
818 sizes.surface_width = desired_mode->hdisplay;
819 if (desired_mode->vdisplay > sizes.surface_height)
820 sizes.surface_height = desired_mode->vdisplay;
825 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
826 /* hmm everyone went away - assume VGA cable just fell out
827 and will come back later. */
828 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
829 sizes.fb_width = sizes.surface_width = 1024;
830 sizes.fb_height = sizes.surface_height = 768;
833 /* push down into drivers */
834 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
838 info = fb_helper->fbdev;
840 /* set the fb pointer */
841 for (i = 0; i < fb_helper->crtc_count; i++) {
842 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
846 info->var.pixclock = 0;
847 if (register_framebuffer(info) < 0) {
851 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
855 drm_fb_helper_set_par(info);
858 /* Switch back to kernel console on panic */
859 /* multi card linked list maybe */
860 if (list_empty(&kernel_fb_helper_list)) {
861 printk(KERN_INFO "registered panic notifier\n");
862 atomic_notifier_chain_register(&panic_notifier_list,
864 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
867 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
871 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
873 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
876 info->fix.type = FB_TYPE_PACKED_PIXELS;
877 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
879 info->fix.type_aux = 0;
880 info->fix.xpanstep = 1; /* doing it in hw */
881 info->fix.ypanstep = 1; /* doing it in hw */
882 info->fix.ywrapstep = 0;
883 info->fix.accel = FB_ACCEL_NONE;
884 info->fix.type_aux = 0;
886 info->fix.line_length = pitch;
889 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
891 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
892 uint32_t fb_width, uint32_t fb_height)
894 struct drm_framebuffer *fb = fb_helper->fb;
895 info->pseudo_palette = fb_helper->pseudo_palette;
896 info->var.xres_virtual = fb->width;
897 info->var.yres_virtual = fb->height;
898 info->var.bits_per_pixel = fb->bits_per_pixel;
899 info->var.xoffset = 0;
900 info->var.yoffset = 0;
901 info->var.activate = FB_ACTIVATE_NOW;
902 info->var.height = -1;
903 info->var.width = -1;
907 info->var.red.offset = 0;
908 info->var.green.offset = 0;
909 info->var.blue.offset = 0;
910 info->var.red.length = 8; /* 8bit DAC */
911 info->var.green.length = 8;
912 info->var.blue.length = 8;
913 info->var.transp.offset = 0;
914 info->var.transp.length = 0;
917 info->var.red.offset = 10;
918 info->var.green.offset = 5;
919 info->var.blue.offset = 0;
920 info->var.red.length = 5;
921 info->var.green.length = 5;
922 info->var.blue.length = 5;
923 info->var.transp.offset = 15;
924 info->var.transp.length = 1;
927 info->var.red.offset = 11;
928 info->var.green.offset = 5;
929 info->var.blue.offset = 0;
930 info->var.red.length = 5;
931 info->var.green.length = 6;
932 info->var.blue.length = 5;
933 info->var.transp.offset = 0;
936 info->var.red.offset = 16;
937 info->var.green.offset = 8;
938 info->var.blue.offset = 0;
939 info->var.red.length = 8;
940 info->var.green.length = 8;
941 info->var.blue.length = 8;
942 info->var.transp.offset = 0;
943 info->var.transp.length = 0;
946 info->var.red.offset = 16;
947 info->var.green.offset = 8;
948 info->var.blue.offset = 0;
949 info->var.red.length = 8;
950 info->var.green.length = 8;
951 info->var.blue.length = 8;
952 info->var.transp.offset = 24;
953 info->var.transp.length = 8;
959 info->var.xres = fb_width;
960 info->var.yres = fb_height;
962 EXPORT_SYMBOL(drm_fb_helper_fill_var);
964 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
968 struct drm_connector *connector;
972 for (i = 0; i < fb_helper->connector_count; i++) {
973 connector = fb_helper->connector_info[i]->connector;
974 count += connector->funcs->fill_modes(connector, maxX, maxY);
980 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
982 struct drm_display_mode *mode;
984 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
985 if (drm_mode_width(mode) > width ||
986 drm_mode_height(mode) > height)
988 if (mode->type & DRM_MODE_TYPE_PREFERRED)
994 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
996 struct drm_fb_helper_cmdline_mode *cmdline_mode;
997 cmdline_mode = &fb_connector->cmdline_mode;
998 return cmdline_mode->specified;
1001 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1002 int width, int height)
1004 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1005 struct drm_display_mode *mode = NULL;
1007 cmdline_mode = &fb_helper_conn->cmdline_mode;
1008 if (cmdline_mode->specified == false)
1011 /* attempt to find a matching mode in the list of modes
1012 * we have gotten so far, if not add a CVT mode that conforms
1014 if (cmdline_mode->rb || cmdline_mode->margins)
1017 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1018 /* check width/height */
1019 if (mode->hdisplay != cmdline_mode->xres ||
1020 mode->vdisplay != cmdline_mode->yres)
1023 if (cmdline_mode->refresh_specified) {
1024 if (mode->vrefresh != cmdline_mode->refresh)
1028 if (cmdline_mode->interlace) {
1029 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1036 mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1038 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1039 cmdline_mode->rb, cmdline_mode->interlace,
1040 cmdline_mode->margins);
1041 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1042 list_add(&mode->head, &fb_helper_conn->connector->modes);
1046 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1051 enable = connector->status == connector_status_connected;
1053 enable = connector->status != connector_status_disconnected;
1058 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1061 bool any_enabled = false;
1062 struct drm_connector *connector;
1065 for (i = 0; i < fb_helper->connector_count; i++) {
1066 connector = fb_helper->connector_info[i]->connector;
1067 enabled[i] = drm_connector_enabled(connector, true);
1068 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1069 enabled[i] ? "yes" : "no");
1070 any_enabled |= enabled[i];
1076 for (i = 0; i < fb_helper->connector_count; i++) {
1077 connector = fb_helper->connector_info[i]->connector;
1078 enabled[i] = drm_connector_enabled(connector, false);
1082 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1083 struct drm_display_mode **modes,
1084 bool *enabled, int width, int height)
1086 struct drm_fb_helper_connector *fb_helper_conn;
1089 for (i = 0; i < fb_helper->connector_count; i++) {
1090 fb_helper_conn = fb_helper->connector_info[i];
1092 if (enabled[i] == false)
1095 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1096 fb_helper_conn->connector->base.id);
1098 /* got for command line mode first */
1099 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1101 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1102 fb_helper_conn->connector->base.id);
1103 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1105 /* No preferred modes, pick one off the list */
1106 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1107 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1110 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;
1129 struct drm_fb_helper_connector *fb_helper_conn;
1131 if (n == fb_helper->connector_count)
1134 fb_helper_conn = fb_helper->connector_info[n];
1135 connector = fb_helper_conn->connector;
1137 best_crtcs[n] = NULL;
1139 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1140 if (modes[n] == NULL)
1143 crtcs = kzalloc(dev->mode_config.num_connector *
1144 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1149 if (connector->status == connector_status_connected)
1151 if (drm_has_cmdline_mode(fb_helper_conn))
1153 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1156 connector_funcs = connector->helper_private;
1157 encoder = connector_funcs->best_encoder(connector);
1161 /* select a crtc for this connector and then attempt to configure
1162 remaining connectors */
1163 for (c = 0; c < fb_helper->crtc_count; c++) {
1164 crtc = &fb_helper->crtc_info[c];
1166 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1170 for (o = 0; o < n; o++)
1171 if (best_crtcs[o] == crtc)
1175 /* ignore cloning for now */
1180 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1181 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1183 if (score > best_score) {
1186 memcpy(best_crtcs, crtcs,
1187 dev->mode_config.num_connector *
1188 sizeof(struct drm_fb_helper_crtc *));
1196 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1198 struct drm_device *dev = fb_helper->dev;
1199 struct drm_fb_helper_crtc **crtcs;
1200 struct drm_display_mode **modes;
1201 struct drm_encoder *encoder;
1202 struct drm_mode_set *modeset;
1207 DRM_DEBUG_KMS("\n");
1209 width = dev->mode_config.max_width;
1210 height = dev->mode_config.max_height;
1212 /* clean out all the encoder/crtc combos */
1213 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1214 encoder->crtc = NULL;
1217 crtcs = kcalloc(dev->mode_config.num_connector,
1218 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1219 modes = kcalloc(dev->mode_config.num_connector,
1220 sizeof(struct drm_display_mode *), GFP_KERNEL);
1221 enabled = kcalloc(dev->mode_config.num_connector,
1222 sizeof(bool), GFP_KERNEL);
1224 drm_enable_connectors(fb_helper, enabled);
1226 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1228 DRM_ERROR("Unable to find initial modes\n");
1230 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1232 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1234 /* need to set the modesets up here for use later */
1235 /* fill out the connector<->crtc mappings into the modesets */
1236 for (i = 0; i < fb_helper->crtc_count; i++) {
1237 modeset = &fb_helper->crtc_info[i].mode_set;
1238 modeset->num_connectors = 0;
1241 for (i = 0; i < fb_helper->connector_count; i++) {
1242 struct drm_display_mode *mode = modes[i];
1243 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1244 modeset = &fb_crtc->mode_set;
1246 if (mode && fb_crtc) {
1247 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1248 mode->name, fb_crtc->mode_set.crtc->base.id);
1249 fb_crtc->desired_mode = mode;
1251 drm_mode_destroy(dev, modeset->mode);
1252 modeset->mode = drm_mode_duplicate(dev,
1253 fb_crtc->desired_mode);
1254 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1264 * drm_helper_initial_config - setup a sane initial connector configuration
1268 * Called at init time, must take mode config lock.
1270 * Scan the CRTCs and connectors and try to put together an initial setup.
1271 * At the moment, this is a cloned configuration across all heads with
1272 * a new framebuffer object as the backing store.
1275 * Zero if everything went ok, nonzero otherwise.
1277 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1279 struct drm_device *dev = fb_helper->dev;
1282 /* disable all the possible outputs/crtcs before entering KMS mode */
1283 drm_helper_disable_unused_functions(fb_helper->dev);
1285 drm_fb_helper_parse_command_line(fb_helper);
1287 count = drm_fb_helper_probe_connector_modes(fb_helper,
1288 dev->mode_config.max_width,
1289 dev->mode_config.max_height);
1291 * we shouldn't end up with no modes here.
1294 if (fb_helper->poll_enabled) {
1295 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work,
1297 printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1299 printk(KERN_INFO "No connectors reported connected with modes\n");
1301 drm_setup_crtcs(fb_helper);
1303 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1305 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1307 /* we got a hotplug irq - need to update fbcon */
1308 void drm_helper_fb_hpd_irq_event(struct drm_fb_helper *fb_helper)
1310 /* if we don't have the fbdev registered yet do nothing */
1311 if (!fb_helper->fbdev)
1314 /* schedule a slow work asap */
1315 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
1317 EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event);
1319 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, bool polled)
1323 u32 max_width, max_height, bpp_sel;
1327 DRM_DEBUG_KMS("\n");
1329 max_width = fb_helper->fb->width;
1330 max_height = fb_helper->fb->height;
1331 bpp_sel = fb_helper->fb->bits_per_pixel;
1333 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1335 if (fb_helper->poll_enabled && !polled) {
1337 delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
1339 ret = delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 5*HZ);
1342 drm_setup_crtcs(fb_helper);
1344 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1346 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
1349 * delayed work queue execution function
1350 * - check if fbdev is actually in use on the gpu
1351 * - if not set delayed flag and repoll if necessary
1352 * - check for connector status change
1353 * - repoll if 0 modes found
1354 *- call driver output status changed notifier
1356 static void output_status_change_execute(struct slow_work *work)
1358 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
1359 struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_status_change_slow_work);
1360 struct drm_connector *connector;
1361 enum drm_connector_status old_status, status;
1362 bool repoll, changed = false;
1365 bool bound = false, crtcs_bound = false;
1366 struct drm_crtc *crtc;
1368 repoll = fb_helper->poll_enabled;
1370 /* first of all check the fbcon framebuffer is actually bound to any crtc */
1371 /* take into account that no crtc at all maybe bound */
1372 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1375 if (crtc->fb == fb_helper->fb)
1379 if (bound == false && crtcs_bound) {
1380 fb_helper->delayed_hotplug = true;
1384 for (i = 0; i < fb_helper->connector_count; i++) {
1385 connector = fb_helper->connector_info[i]->connector;
1386 old_status = connector->status;
1387 status = connector->funcs->detect(connector);
1388 if (old_status != status) {
1391 if (status == connector_status_connected && repoll) {
1392 DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1398 if (fb_helper->funcs->fb_output_status_changed)
1399 fb_helper->funcs->fb_output_status_changed(fb_helper);
1404 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1406 DRM_ERROR("delayed enqueue failed %d\n", ret);
1410 static struct slow_work_ops output_status_change_ops = {
1411 .execute = output_status_change_execute,