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>
32 #include <linux/slab.h>
36 #include "drm_fb_helper.h"
37 #include "drm_crtc_helper.h"
39 MODULE_AUTHOR("David Airlie, Jesse Barnes");
40 MODULE_DESCRIPTION("DRM KMS helper");
41 MODULE_LICENSE("GPL and additional rights");
43 static LIST_HEAD(kernel_fb_helper_list);
45 /* simple single crtc case helper function */
46 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
48 struct drm_device *dev = fb_helper->dev;
49 struct drm_connector *connector;
52 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
53 struct drm_fb_helper_connector *fb_helper_connector;
55 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
56 if (!fb_helper_connector)
59 fb_helper_connector->connector = connector;
60 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
64 for (i = 0; i < fb_helper->connector_count; i++) {
65 kfree(fb_helper->connector_info[i]);
66 fb_helper->connector_info[i] = NULL;
68 fb_helper->connector_count = 0;
71 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
74 * drm_fb_helper_connector_parse_command_line - parse command line for connector
75 * @connector - connector to parse line for
76 * @mode_option - per connector mode option
78 * This parses the connector specific then generic command lines for
79 * modes and options to configure the connector.
81 * This uses the same parameters as the fb modedb.c, except for extra
82 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
84 * enable/enable Digital/disable bit at the end
86 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
87 const char *mode_option)
91 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
92 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
93 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
95 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
96 struct drm_fb_helper_cmdline_mode *cmdline_mode;
97 struct drm_connector *connector;
101 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 int drm_fb_helper_debug_enter(struct fb_info *info)
247 struct drm_fb_helper *helper = info->par;
248 struct drm_crtc_helper_funcs *funcs;
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 =
257 &helper->crtc_info[i].mode_set;
259 if (!mode_set->crtc->enabled)
262 funcs = mode_set->crtc->helper_private;
263 funcs->mode_set_base_atomic(mode_set->crtc,
273 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
275 /* Find the real fb for a given fb helper CRTC */
276 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
278 struct drm_device *dev = crtc->dev;
281 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
282 if (crtc->base.id == c->base.id)
289 int drm_fb_helper_debug_leave(struct fb_info *info)
291 struct drm_fb_helper *helper = info->par;
292 struct drm_crtc *crtc;
293 struct drm_crtc_helper_funcs *funcs;
294 struct drm_framebuffer *fb;
297 for (i = 0; i < helper->crtc_count; i++) {
298 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
299 crtc = mode_set->crtc;
300 funcs = crtc->helper_private;
301 fb = drm_mode_config_fb(crtc);
307 DRM_ERROR("no fb to restore??\n");
311 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
317 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
319 bool drm_fb_helper_force_kernel_mode(void)
322 bool ret, error = false;
323 struct drm_fb_helper *helper;
325 if (list_empty(&kernel_fb_helper_list))
328 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
329 for (i = 0; i < helper->crtc_count; i++) {
330 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
331 ret = drm_crtc_helper_set_config(mode_set);
339 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
342 printk(KERN_ERR "panic occurred, switching back to text console\n");
343 return drm_fb_helper_force_kernel_mode();
346 EXPORT_SYMBOL(drm_fb_helper_panic);
348 static struct notifier_block paniced = {
349 .notifier_call = drm_fb_helper_panic,
353 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
355 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
357 void drm_fb_helper_restore(void)
360 ret = drm_fb_helper_force_kernel_mode();
362 DRM_ERROR("Failed to restore crtc configuration\n");
364 EXPORT_SYMBOL(drm_fb_helper_restore);
366 #ifdef CONFIG_MAGIC_SYSRQ
367 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
369 drm_fb_helper_restore();
371 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
373 static void drm_fb_helper_sysrq(int dummy1)
375 schedule_work(&drm_fb_helper_restore_work);
378 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
379 .handler = drm_fb_helper_sysrq,
380 .help_msg = "force-fb(V)",
381 .action_msg = "Restore framebuffer console",
384 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
387 static void drm_fb_helper_on(struct fb_info *info)
389 struct drm_fb_helper *fb_helper = info->par;
390 struct drm_device *dev = fb_helper->dev;
391 struct drm_crtc *crtc;
392 struct drm_crtc_helper_funcs *crtc_funcs;
393 struct drm_connector *connector;
394 struct drm_encoder *encoder;
398 * For each CRTC in this fb, turn the crtc on then,
399 * find all associated encoders and turn them on.
401 mutex_lock(&dev->mode_config.mutex);
402 for (i = 0; i < fb_helper->crtc_count; i++) {
403 crtc = fb_helper->crtc_info[i].mode_set.crtc;
404 crtc_funcs = crtc->helper_private;
409 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
411 /* Walk the connectors & encoders on this fb turning them on */
412 for (j = 0; j < fb_helper->connector_count; j++) {
413 connector = fb_helper->connector_info[j]->connector;
414 connector->dpms = DRM_MODE_DPMS_ON;
415 drm_connector_property_set_value(connector,
416 dev->mode_config.dpms_property,
419 /* Found a CRTC on this fb, now find encoders */
420 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
421 if (encoder->crtc == crtc) {
422 struct drm_encoder_helper_funcs *encoder_funcs;
424 encoder_funcs = encoder->helper_private;
425 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
429 mutex_unlock(&dev->mode_config.mutex);
432 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
434 struct drm_fb_helper *fb_helper = info->par;
435 struct drm_device *dev = fb_helper->dev;
436 struct drm_crtc *crtc;
437 struct drm_crtc_helper_funcs *crtc_funcs;
438 struct drm_connector *connector;
439 struct drm_encoder *encoder;
443 * For each CRTC in this fb, find all associated encoders
444 * and turn them off, then turn off the CRTC.
446 mutex_lock(&dev->mode_config.mutex);
447 for (i = 0; i < fb_helper->crtc_count; i++) {
448 crtc = fb_helper->crtc_info[i].mode_set.crtc;
449 crtc_funcs = crtc->helper_private;
454 /* Walk the connectors on this fb and mark them off */
455 for (j = 0; j < fb_helper->connector_count; j++) {
456 connector = fb_helper->connector_info[j]->connector;
457 connector->dpms = dpms_mode;
458 drm_connector_property_set_value(connector,
459 dev->mode_config.dpms_property,
462 /* Found a CRTC on this fb, now find encoders */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
464 if (encoder->crtc == crtc) {
465 struct drm_encoder_helper_funcs *encoder_funcs;
467 encoder_funcs = encoder->helper_private;
468 encoder_funcs->dpms(encoder, dpms_mode);
471 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
473 mutex_unlock(&dev->mode_config.mutex);
476 int drm_fb_helper_blank(int blank, struct fb_info *info)
479 /* Display: On; HSync: On, VSync: On */
480 case FB_BLANK_UNBLANK:
481 drm_fb_helper_on(info);
483 /* Display: Off; HSync: On, VSync: On */
484 case FB_BLANK_NORMAL:
485 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
487 /* Display: Off; HSync: Off, VSync: On */
488 case FB_BLANK_HSYNC_SUSPEND:
489 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
491 /* Display: Off; HSync: On, VSync: Off */
492 case FB_BLANK_VSYNC_SUSPEND:
493 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
495 /* Display: Off; HSync: Off, VSync: Off */
496 case FB_BLANK_POWERDOWN:
497 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
502 EXPORT_SYMBOL(drm_fb_helper_blank);
504 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
508 for (i = 0; i < helper->connector_count; i++)
509 kfree(helper->connector_info[i]);
510 kfree(helper->connector_info);
511 for (i = 0; i < helper->crtc_count; i++)
512 kfree(helper->crtc_info[i].mode_set.connectors);
513 kfree(helper->crtc_info);
516 int drm_fb_helper_init(struct drm_device *dev,
517 struct drm_fb_helper *fb_helper,
518 int crtc_count, int max_conn_count)
520 struct drm_crtc *crtc;
524 fb_helper->dev = dev;
526 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
528 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
529 if (!fb_helper->crtc_info)
532 fb_helper->crtc_count = crtc_count;
533 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
534 if (!fb_helper->connector_info) {
535 kfree(fb_helper->crtc_info);
538 fb_helper->connector_count = 0;
540 for (i = 0; i < crtc_count; i++) {
541 fb_helper->crtc_info[i].mode_set.connectors =
542 kcalloc(max_conn_count,
543 sizeof(struct drm_connector *),
546 if (!fb_helper->crtc_info[i].mode_set.connectors) {
550 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
554 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
555 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
556 fb_helper->crtc_info[i].mode_set.crtc = crtc;
559 fb_helper->conn_limit = max_conn_count;
562 drm_fb_helper_crtc_free(fb_helper);
565 EXPORT_SYMBOL(drm_fb_helper_init);
567 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
569 if (!list_empty(&fb_helper->kernel_fb_list)) {
570 list_del(&fb_helper->kernel_fb_list);
571 if (list_empty(&kernel_fb_helper_list)) {
572 printk(KERN_INFO "drm: unregistered panic notifier\n");
573 atomic_notifier_chain_unregister(&panic_notifier_list,
575 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
579 drm_fb_helper_crtc_free(fb_helper);
582 EXPORT_SYMBOL(drm_fb_helper_fini);
584 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
585 u16 blue, u16 regno, struct fb_info *info)
587 struct drm_fb_helper *fb_helper = info->par;
588 struct drm_framebuffer *fb = fb_helper->fb;
591 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
594 /* place color in psuedopalette */
597 palette = (u32 *)info->pseudo_palette;
598 red >>= (16 - info->var.red.length);
599 green >>= (16 - info->var.green.length);
600 blue >>= (16 - info->var.blue.length);
601 value = (red << info->var.red.offset) |
602 (green << info->var.green.offset) |
603 (blue << info->var.blue.offset);
604 palette[regno] = value;
610 if (fb->bits_per_pixel == 16) {
613 if (fb->depth == 16 && regno > 63)
615 if (fb->depth == 15 && regno > 31)
618 if (fb->depth == 16) {
622 for (i = 0; i < 8; i++)
623 fb_helper->funcs->gamma_set(crtc, red,
624 green, blue, pindex + i);
627 fb_helper->funcs->gamma_get(crtc, &r,
631 for (i = 0; i < 4; i++)
632 fb_helper->funcs->gamma_set(crtc, r,
639 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
643 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
645 struct drm_fb_helper *fb_helper = info->par;
646 struct drm_crtc_helper_funcs *crtc_funcs;
647 u16 *red, *green, *blue, *transp;
648 struct drm_crtc *crtc;
652 for (i = 0; i < fb_helper->crtc_count; i++) {
653 crtc = fb_helper->crtc_info[i].mode_set.crtc;
654 crtc_funcs = crtc->helper_private;
659 transp = cmap->transp;
662 for (i = 0; i < cmap->len; i++) {
663 u16 hred, hgreen, hblue, htransp = 0xffff;
672 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
676 crtc_funcs->load_lut(crtc);
680 EXPORT_SYMBOL(drm_fb_helper_setcmap);
682 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
683 struct fb_info *info)
685 struct drm_fb_helper *fb_helper = info->par;
686 struct drm_framebuffer *fb = fb_helper->fb;
689 if (var->pixclock != 0 || in_dbg_master())
692 /* Need to resize the fb object !!! */
693 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
694 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
695 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
696 fb->width, fb->height, fb->bits_per_pixel);
700 switch (var->bits_per_pixel) {
702 depth = (var->green.length == 6) ? 16 : 15;
705 depth = (var->transp.length > 0) ? 32 : 24;
708 depth = var->bits_per_pixel;
715 var->green.offset = 0;
716 var->blue.offset = 0;
718 var->green.length = 8;
719 var->blue.length = 8;
720 var->transp.length = 0;
721 var->transp.offset = 0;
724 var->red.offset = 10;
725 var->green.offset = 5;
726 var->blue.offset = 0;
728 var->green.length = 5;
729 var->blue.length = 5;
730 var->transp.length = 1;
731 var->transp.offset = 15;
734 var->red.offset = 11;
735 var->green.offset = 5;
736 var->blue.offset = 0;
738 var->green.length = 6;
739 var->blue.length = 5;
740 var->transp.length = 0;
741 var->transp.offset = 0;
744 var->red.offset = 16;
745 var->green.offset = 8;
746 var->blue.offset = 0;
748 var->green.length = 8;
749 var->blue.length = 8;
750 var->transp.length = 0;
751 var->transp.offset = 0;
754 var->red.offset = 16;
755 var->green.offset = 8;
756 var->blue.offset = 0;
758 var->green.length = 8;
759 var->blue.length = 8;
760 var->transp.length = 8;
761 var->transp.offset = 24;
768 EXPORT_SYMBOL(drm_fb_helper_check_var);
770 /* this will let fbcon do the mode init */
771 int drm_fb_helper_set_par(struct fb_info *info)
773 struct drm_fb_helper *fb_helper = info->par;
774 struct drm_device *dev = fb_helper->dev;
775 struct fb_var_screeninfo *var = &info->var;
776 struct drm_crtc *crtc;
780 if (var->pixclock != 0) {
781 DRM_ERROR("PIXEL CLOCK SET\n");
785 mutex_lock(&dev->mode_config.mutex);
786 for (i = 0; i < fb_helper->crtc_count; i++) {
787 crtc = fb_helper->crtc_info[i].mode_set.crtc;
788 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
790 mutex_unlock(&dev->mode_config.mutex);
794 mutex_unlock(&dev->mode_config.mutex);
796 if (fb_helper->delayed_hotplug) {
797 fb_helper->delayed_hotplug = false;
798 drm_fb_helper_hotplug_event(fb_helper);
802 EXPORT_SYMBOL(drm_fb_helper_set_par);
804 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
805 struct fb_info *info)
807 struct drm_fb_helper *fb_helper = info->par;
808 struct drm_device *dev = fb_helper->dev;
809 struct drm_mode_set *modeset;
810 struct drm_crtc *crtc;
814 mutex_lock(&dev->mode_config.mutex);
815 for (i = 0; i < fb_helper->crtc_count; i++) {
816 crtc = fb_helper->crtc_info[i].mode_set.crtc;
818 modeset = &fb_helper->crtc_info[i].mode_set;
820 modeset->x = var->xoffset;
821 modeset->y = var->yoffset;
823 if (modeset->num_connectors) {
824 ret = crtc->funcs->set_config(modeset);
826 info->var.xoffset = var->xoffset;
827 info->var.yoffset = var->yoffset;
831 mutex_unlock(&dev->mode_config.mutex);
834 EXPORT_SYMBOL(drm_fb_helper_pan_display);
836 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
842 struct fb_info *info;
843 struct drm_fb_helper_surface_size sizes;
846 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
847 sizes.surface_depth = 24;
848 sizes.surface_bpp = 32;
849 sizes.fb_width = (unsigned)-1;
850 sizes.fb_height = (unsigned)-1;
852 /* if driver picks 8 or 16 by default use that
853 for both depth/bpp */
854 if (preferred_bpp != sizes.surface_bpp) {
855 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
857 /* first up get a count of crtcs now in use and new min/maxes width/heights */
858 for (i = 0; i < fb_helper->connector_count; i++) {
859 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
860 struct drm_fb_helper_cmdline_mode *cmdline_mode;
862 cmdline_mode = &fb_helper_conn->cmdline_mode;
864 if (cmdline_mode->bpp_specified) {
865 switch (cmdline_mode->bpp) {
867 sizes.surface_depth = sizes.surface_bpp = 8;
870 sizes.surface_depth = 15;
871 sizes.surface_bpp = 16;
874 sizes.surface_depth = sizes.surface_bpp = 16;
877 sizes.surface_depth = sizes.surface_bpp = 24;
880 sizes.surface_depth = 24;
881 sizes.surface_bpp = 32;
889 for (i = 0; i < fb_helper->crtc_count; i++) {
890 struct drm_display_mode *desired_mode;
891 desired_mode = fb_helper->crtc_info[i].desired_mode;
895 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
896 if (desired_mode->hdisplay < sizes.fb_width)
897 sizes.fb_width = desired_mode->hdisplay;
898 if (desired_mode->vdisplay < sizes.fb_height)
899 sizes.fb_height = desired_mode->vdisplay;
900 if (desired_mode->hdisplay > sizes.surface_width)
901 sizes.surface_width = desired_mode->hdisplay;
902 if (desired_mode->vdisplay > sizes.surface_height)
903 sizes.surface_height = desired_mode->vdisplay;
908 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
909 /* hmm everyone went away - assume VGA cable just fell out
910 and will come back later. */
911 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
912 sizes.fb_width = sizes.surface_width = 1024;
913 sizes.fb_height = sizes.surface_height = 768;
916 /* push down into drivers */
917 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
921 info = fb_helper->fbdev;
923 /* set the fb pointer */
924 for (i = 0; i < fb_helper->crtc_count; i++) {
925 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
929 info->var.pixclock = 0;
930 if (register_framebuffer(info) < 0) {
934 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
938 drm_fb_helper_set_par(info);
941 /* Switch back to kernel console on panic */
942 /* multi card linked list maybe */
943 if (list_empty(&kernel_fb_helper_list)) {
944 printk(KERN_INFO "drm: registered panic notifier\n");
945 atomic_notifier_chain_register(&panic_notifier_list,
947 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
950 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
954 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
956 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
959 info->fix.type = FB_TYPE_PACKED_PIXELS;
960 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
962 info->fix.type_aux = 0;
963 info->fix.xpanstep = 1; /* doing it in hw */
964 info->fix.ypanstep = 1; /* doing it in hw */
965 info->fix.ywrapstep = 0;
966 info->fix.accel = FB_ACCEL_NONE;
967 info->fix.type_aux = 0;
969 info->fix.line_length = pitch;
972 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
974 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
975 uint32_t fb_width, uint32_t fb_height)
977 struct drm_framebuffer *fb = fb_helper->fb;
978 info->pseudo_palette = fb_helper->pseudo_palette;
979 info->var.xres_virtual = fb->width;
980 info->var.yres_virtual = fb->height;
981 info->var.bits_per_pixel = fb->bits_per_pixel;
982 info->var.xoffset = 0;
983 info->var.yoffset = 0;
984 info->var.activate = FB_ACTIVATE_NOW;
985 info->var.height = -1;
986 info->var.width = -1;
990 info->var.red.offset = 0;
991 info->var.green.offset = 0;
992 info->var.blue.offset = 0;
993 info->var.red.length = 8; /* 8bit DAC */
994 info->var.green.length = 8;
995 info->var.blue.length = 8;
996 info->var.transp.offset = 0;
997 info->var.transp.length = 0;
1000 info->var.red.offset = 10;
1001 info->var.green.offset = 5;
1002 info->var.blue.offset = 0;
1003 info->var.red.length = 5;
1004 info->var.green.length = 5;
1005 info->var.blue.length = 5;
1006 info->var.transp.offset = 15;
1007 info->var.transp.length = 1;
1010 info->var.red.offset = 11;
1011 info->var.green.offset = 5;
1012 info->var.blue.offset = 0;
1013 info->var.red.length = 5;
1014 info->var.green.length = 6;
1015 info->var.blue.length = 5;
1016 info->var.transp.offset = 0;
1019 info->var.red.offset = 16;
1020 info->var.green.offset = 8;
1021 info->var.blue.offset = 0;
1022 info->var.red.length = 8;
1023 info->var.green.length = 8;
1024 info->var.blue.length = 8;
1025 info->var.transp.offset = 0;
1026 info->var.transp.length = 0;
1029 info->var.red.offset = 16;
1030 info->var.green.offset = 8;
1031 info->var.blue.offset = 0;
1032 info->var.red.length = 8;
1033 info->var.green.length = 8;
1034 info->var.blue.length = 8;
1035 info->var.transp.offset = 24;
1036 info->var.transp.length = 8;
1042 info->var.xres = fb_width;
1043 info->var.yres = fb_height;
1045 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1047 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1051 struct drm_connector *connector;
1055 for (i = 0; i < fb_helper->connector_count; i++) {
1056 connector = fb_helper->connector_info[i]->connector;
1057 count += connector->funcs->fill_modes(connector, maxX, maxY);
1063 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1065 struct drm_display_mode *mode;
1067 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1068 if (drm_mode_width(mode) > width ||
1069 drm_mode_height(mode) > height)
1071 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1077 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1079 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1080 cmdline_mode = &fb_connector->cmdline_mode;
1081 return cmdline_mode->specified;
1084 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1085 int width, int height)
1087 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1088 struct drm_display_mode *mode = NULL;
1090 cmdline_mode = &fb_helper_conn->cmdline_mode;
1091 if (cmdline_mode->specified == false)
1094 /* attempt to find a matching mode in the list of modes
1095 * we have gotten so far, if not add a CVT mode that conforms
1097 if (cmdline_mode->rb || cmdline_mode->margins)
1100 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1101 /* check width/height */
1102 if (mode->hdisplay != cmdline_mode->xres ||
1103 mode->vdisplay != cmdline_mode->yres)
1106 if (cmdline_mode->refresh_specified) {
1107 if (mode->vrefresh != cmdline_mode->refresh)
1111 if (cmdline_mode->interlace) {
1112 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1119 if (cmdline_mode->cvt)
1120 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1121 cmdline_mode->xres, cmdline_mode->yres,
1122 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1123 cmdline_mode->rb, cmdline_mode->interlace,
1124 cmdline_mode->margins);
1126 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1127 cmdline_mode->xres, cmdline_mode->yres,
1128 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1129 cmdline_mode->interlace,
1130 cmdline_mode->margins);
1131 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1132 list_add(&mode->head, &fb_helper_conn->connector->modes);
1136 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1141 enable = connector->status == connector_status_connected;
1143 enable = connector->status != connector_status_disconnected;
1148 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1151 bool any_enabled = false;
1152 struct drm_connector *connector;
1155 for (i = 0; i < fb_helper->connector_count; i++) {
1156 connector = fb_helper->connector_info[i]->connector;
1157 enabled[i] = drm_connector_enabled(connector, true);
1158 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1159 enabled[i] ? "yes" : "no");
1160 any_enabled |= enabled[i];
1166 for (i = 0; i < fb_helper->connector_count; i++) {
1167 connector = fb_helper->connector_info[i]->connector;
1168 enabled[i] = drm_connector_enabled(connector, false);
1172 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1173 struct drm_display_mode **modes,
1174 bool *enabled, int width, int height)
1177 bool can_clone = false;
1178 struct drm_fb_helper_connector *fb_helper_conn;
1179 struct drm_display_mode *dmt_mode, *mode;
1181 /* only contemplate cloning in the single crtc case */
1182 if (fb_helper->crtc_count > 1)
1186 for (i = 0; i < fb_helper->connector_count; i++) {
1191 /* only contemplate cloning if more than one connector is enabled */
1195 /* check the command line or if nothing common pick 1024x768 */
1197 for (i = 0; i < fb_helper->connector_count; i++) {
1200 fb_helper_conn = fb_helper->connector_info[i];
1201 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1206 for (j = 0; j < i; j++) {
1209 if (!drm_mode_equal(modes[j], modes[i]))
1215 DRM_DEBUG_KMS("can clone using command line\n");
1219 /* try and find a 1024x768 mode on each connector */
1221 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1223 for (i = 0; i < fb_helper->connector_count; i++) {
1228 fb_helper_conn = fb_helper->connector_info[i];
1229 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1230 if (drm_mode_equal(mode, dmt_mode))
1238 DRM_DEBUG_KMS("can clone using 1024x768\n");
1241 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1245 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1246 struct drm_display_mode **modes,
1247 bool *enabled, int width, int height)
1249 struct drm_fb_helper_connector *fb_helper_conn;
1252 for (i = 0; i < fb_helper->connector_count; i++) {
1253 fb_helper_conn = fb_helper->connector_info[i];
1255 if (enabled[i] == false)
1258 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1259 fb_helper_conn->connector->base.id);
1261 /* got for command line mode first */
1262 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1264 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1265 fb_helper_conn->connector->base.id);
1266 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1268 /* No preferred modes, pick one off the list */
1269 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1270 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1273 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1279 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1280 struct drm_fb_helper_crtc **best_crtcs,
1281 struct drm_display_mode **modes,
1282 int n, int width, int height)
1285 struct drm_device *dev = fb_helper->dev;
1286 struct drm_connector *connector;
1287 struct drm_connector_helper_funcs *connector_funcs;
1288 struct drm_encoder *encoder;
1289 struct drm_fb_helper_crtc *best_crtc;
1290 int my_score, best_score, score;
1291 struct drm_fb_helper_crtc **crtcs, *crtc;
1292 struct drm_fb_helper_connector *fb_helper_conn;
1294 if (n == fb_helper->connector_count)
1297 fb_helper_conn = fb_helper->connector_info[n];
1298 connector = fb_helper_conn->connector;
1300 best_crtcs[n] = NULL;
1302 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1303 if (modes[n] == NULL)
1306 crtcs = kzalloc(dev->mode_config.num_connector *
1307 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1312 if (connector->status == connector_status_connected)
1314 if (drm_has_cmdline_mode(fb_helper_conn))
1316 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1319 connector_funcs = connector->helper_private;
1320 encoder = connector_funcs->best_encoder(connector);
1324 /* select a crtc for this connector and then attempt to configure
1325 remaining connectors */
1326 for (c = 0; c < fb_helper->crtc_count; c++) {
1327 crtc = &fb_helper->crtc_info[c];
1329 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1333 for (o = 0; o < n; o++)
1334 if (best_crtcs[o] == crtc)
1338 /* ignore cloning unless only a single crtc */
1339 if (fb_helper->crtc_count > 1)
1342 if (!drm_mode_equal(modes[o], modes[n]))
1347 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1348 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1350 if (score > best_score) {
1353 memcpy(best_crtcs, crtcs,
1354 dev->mode_config.num_connector *
1355 sizeof(struct drm_fb_helper_crtc *));
1363 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1365 struct drm_device *dev = fb_helper->dev;
1366 struct drm_fb_helper_crtc **crtcs;
1367 struct drm_display_mode **modes;
1368 struct drm_encoder *encoder;
1369 struct drm_mode_set *modeset;
1374 DRM_DEBUG_KMS("\n");
1376 width = dev->mode_config.max_width;
1377 height = dev->mode_config.max_height;
1379 /* clean out all the encoder/crtc combos */
1380 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1381 encoder->crtc = NULL;
1384 crtcs = kcalloc(dev->mode_config.num_connector,
1385 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1386 modes = kcalloc(dev->mode_config.num_connector,
1387 sizeof(struct drm_display_mode *), GFP_KERNEL);
1388 enabled = kcalloc(dev->mode_config.num_connector,
1389 sizeof(bool), GFP_KERNEL);
1391 drm_enable_connectors(fb_helper, enabled);
1393 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1395 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1397 DRM_ERROR("Unable to find initial modes\n");
1400 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1402 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1404 /* need to set the modesets up here for use later */
1405 /* fill out the connector<->crtc mappings into the modesets */
1406 for (i = 0; i < fb_helper->crtc_count; i++) {
1407 modeset = &fb_helper->crtc_info[i].mode_set;
1408 modeset->num_connectors = 0;
1411 for (i = 0; i < fb_helper->connector_count; i++) {
1412 struct drm_display_mode *mode = modes[i];
1413 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1414 modeset = &fb_crtc->mode_set;
1416 if (mode && fb_crtc) {
1417 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1418 mode->name, fb_crtc->mode_set.crtc->base.id);
1419 fb_crtc->desired_mode = mode;
1421 drm_mode_destroy(dev, modeset->mode);
1422 modeset->mode = drm_mode_duplicate(dev,
1423 fb_crtc->desired_mode);
1424 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1434 * drm_helper_initial_config - setup a sane initial connector configuration
1438 * Called at init time, must take mode config lock.
1440 * Scan the CRTCs and connectors and try to put together an initial setup.
1441 * At the moment, this is a cloned configuration across all heads with
1442 * a new framebuffer object as the backing store.
1445 * Zero if everything went ok, nonzero otherwise.
1447 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1449 struct drm_device *dev = fb_helper->dev;
1452 /* disable all the possible outputs/crtcs before entering KMS mode */
1453 drm_helper_disable_unused_functions(fb_helper->dev);
1455 drm_fb_helper_parse_command_line(fb_helper);
1457 count = drm_fb_helper_probe_connector_modes(fb_helper,
1458 dev->mode_config.max_width,
1459 dev->mode_config.max_height);
1461 * we shouldn't end up with no modes here.
1464 printk(KERN_INFO "No connectors reported connected with modes\n");
1466 drm_setup_crtcs(fb_helper);
1468 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1470 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1472 bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1475 u32 max_width, max_height, bpp_sel;
1476 bool bound = false, crtcs_bound = false;
1477 struct drm_crtc *crtc;
1482 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1485 if (crtc->fb == fb_helper->fb)
1489 if (!bound && crtcs_bound) {
1490 fb_helper->delayed_hotplug = true;
1493 DRM_DEBUG_KMS("\n");
1495 max_width = fb_helper->fb->width;
1496 max_height = fb_helper->fb->height;
1497 bpp_sel = fb_helper->fb->bits_per_pixel;
1499 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1501 drm_setup_crtcs(fb_helper);
1503 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1505 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);