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 /* simple single crtc case helper function */
45 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
47 struct drm_device *dev = fb_helper->dev;
48 struct drm_connector *connector;
51 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
52 struct drm_fb_helper_connector *fb_helper_connector;
54 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
55 if (!fb_helper_connector)
58 fb_helper_connector->connector = connector;
59 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
63 for (i = 0; i < fb_helper->connector_count; i++) {
64 kfree(fb_helper->connector_info[i]);
65 fb_helper->connector_info[i] = NULL;
67 fb_helper->connector_count = 0;
70 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
73 * drm_fb_helper_connector_parse_command_line - parse command line for connector
74 * @connector - connector to parse line for
75 * @mode_option - per connector mode option
77 * This parses the connector specific then generic command lines for
78 * modes and options to configure the connector.
80 * This uses the same parameters as the fb modedb.c, except for extra
81 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
83 * enable/enable Digital/disable bit at the end
85 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
86 const char *mode_option)
90 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
91 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
92 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
94 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
95 struct drm_fb_helper_cmdline_mode *cmdline_mode;
96 struct drm_connector *connector = fb_helper_conn->connector;
101 cmdline_mode = &fb_helper_conn->cmdline_mode;
103 mode_option = fb_mode_option;
106 cmdline_mode->specified = false;
111 namelen = strlen(name);
112 for (i = namelen-1; i >= 0; i--) {
116 if (!refresh_specified && !bpp_specified &&
118 refresh = simple_strtol(&name[i+1], NULL, 10);
119 refresh_specified = 1;
127 if (!bpp_specified && !yres_specified) {
128 bpp = simple_strtol(&name[i+1], NULL, 10);
136 if (!yres_specified) {
137 yres = simple_strtol(&name[i+1], NULL, 10);
160 force = DRM_FORCE_ON;
163 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
164 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
165 force = DRM_FORCE_ON;
167 force = DRM_FORCE_ON_DIGITAL;
170 force = DRM_FORCE_OFF;
176 if (i < 0 && yres_specified) {
177 xres = simple_strtol(name, NULL, 10);
182 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
183 drm_get_connector_name(connector), xres, yres,
184 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
185 "", (margins) ? " with margins" : "", (interlace) ?
191 case DRM_FORCE_OFF: s = "OFF"; break;
192 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
194 case DRM_FORCE_ON: s = "ON"; break;
197 DRM_INFO("forcing %s connector %s\n",
198 drm_get_connector_name(connector), s);
199 connector->force = force;
203 cmdline_mode->specified = true;
204 cmdline_mode->xres = xres;
205 cmdline_mode->yres = yres;
208 if (refresh_specified) {
209 cmdline_mode->refresh_specified = true;
210 cmdline_mode->refresh = refresh;
214 cmdline_mode->bpp_specified = true;
215 cmdline_mode->bpp = bpp;
217 cmdline_mode->rb = rb ? true : false;
218 cmdline_mode->cvt = cvt ? true : false;
219 cmdline_mode->interlace = interlace ? true : false;
224 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
226 struct drm_fb_helper_connector *fb_helper_conn;
229 for (i = 0; i < fb_helper->connector_count; i++) {
232 fb_helper_conn = fb_helper->connector_info[i];
234 /* do something on return - turn off connector maybe */
235 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
238 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
243 bool drm_fb_helper_force_kernel_mode(void)
246 bool ret, error = false;
247 struct drm_fb_helper *helper;
249 if (list_empty(&kernel_fb_helper_list))
252 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
253 for (i = 0; i < helper->crtc_count; i++) {
254 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
255 ret = drm_crtc_helper_set_config(mode_set);
263 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
266 DRM_ERROR("panic occurred, switching back to text console\n");
267 return drm_fb_helper_force_kernel_mode();
270 EXPORT_SYMBOL(drm_fb_helper_panic);
272 static struct notifier_block paniced = {
273 .notifier_call = drm_fb_helper_panic,
277 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
279 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
281 void drm_fb_helper_restore(void)
284 ret = drm_fb_helper_force_kernel_mode();
286 DRM_ERROR("Failed to restore crtc configuration\n");
288 EXPORT_SYMBOL(drm_fb_helper_restore);
290 #ifdef CONFIG_MAGIC_SYSRQ
291 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
293 drm_fb_helper_restore();
295 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
297 static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
299 schedule_work(&drm_fb_helper_restore_work);
302 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
303 .handler = drm_fb_helper_sysrq,
304 .help_msg = "force-fb(V)",
305 .action_msg = "Restore framebuffer console",
308 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
311 static void drm_fb_helper_on(struct fb_info *info)
313 struct drm_fb_helper *fb_helper = info->par;
314 struct drm_device *dev = fb_helper->dev;
315 struct drm_crtc *crtc;
316 struct drm_crtc_helper_funcs *crtc_funcs;
317 struct drm_encoder *encoder;
321 * For each CRTC in this fb, turn the crtc on then,
322 * find all associated encoders and turn them on.
324 mutex_lock(&dev->mode_config.mutex);
325 for (i = 0; i < fb_helper->crtc_count; i++) {
326 crtc = fb_helper->crtc_info[i].mode_set.crtc;
327 crtc_funcs = crtc->helper_private;
332 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
335 /* Found a CRTC on this fb, now find encoders */
336 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
337 if (encoder->crtc == crtc) {
338 struct drm_encoder_helper_funcs *encoder_funcs;
340 encoder_funcs = encoder->helper_private;
341 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
345 mutex_unlock(&dev->mode_config.mutex);
348 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
350 struct drm_fb_helper *fb_helper = info->par;
351 struct drm_device *dev = fb_helper->dev;
352 struct drm_crtc *crtc;
353 struct drm_crtc_helper_funcs *crtc_funcs;
354 struct drm_encoder *encoder;
358 * For each CRTC in this fb, find all associated encoders
359 * and turn them off, then turn off the CRTC.
361 mutex_lock(&dev->mode_config.mutex);
362 for (i = 0; i < fb_helper->crtc_count; i++) {
363 crtc = fb_helper->crtc_info[i].mode_set.crtc;
364 crtc_funcs = crtc->helper_private;
369 /* Found a CRTC on this fb, now find encoders */
370 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
371 if (encoder->crtc == crtc) {
372 struct drm_encoder_helper_funcs *encoder_funcs;
374 encoder_funcs = encoder->helper_private;
375 encoder_funcs->dpms(encoder, dpms_mode);
378 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
380 mutex_unlock(&dev->mode_config.mutex);
383 int drm_fb_helper_blank(int blank, struct fb_info *info)
386 /* Display: On; HSync: On, VSync: On */
387 case FB_BLANK_UNBLANK:
388 drm_fb_helper_on(info);
390 /* Display: Off; HSync: On, VSync: On */
391 case FB_BLANK_NORMAL:
392 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
394 /* Display: Off; HSync: Off, VSync: On */
395 case FB_BLANK_HSYNC_SUSPEND:
396 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
398 /* Display: Off; HSync: On, VSync: Off */
399 case FB_BLANK_VSYNC_SUSPEND:
400 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
402 /* Display: Off; HSync: Off, VSync: Off */
403 case FB_BLANK_POWERDOWN:
404 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
409 EXPORT_SYMBOL(drm_fb_helper_blank);
411 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
415 for (i = 0; i < helper->connector_count; i++)
416 kfree(helper->connector_info[i]);
417 kfree(helper->connector_info);
418 for (i = 0; i < helper->crtc_count; i++)
419 kfree(helper->crtc_info[i].mode_set.connectors);
420 kfree(helper->crtc_info);
423 int drm_fb_helper_init_crtc_count(struct drm_device *dev,
424 struct drm_fb_helper *helper,
425 int crtc_count, int max_conn_count)
427 struct drm_crtc *crtc;
431 INIT_LIST_HEAD(&helper->kernel_fb_list);
433 helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
434 if (!helper->crtc_info)
436 helper->crtc_count = crtc_count;
438 helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
439 if (!helper->connector_info) {
440 kfree(helper->crtc_info);
443 helper->connector_count = 0;
445 for (i = 0; i < crtc_count; i++) {
446 helper->crtc_info[i].mode_set.connectors =
447 kcalloc(max_conn_count,
448 sizeof(struct drm_connector *),
451 if (!helper->crtc_info[i].mode_set.connectors) {
455 helper->crtc_info[i].mode_set.num_connectors = 0;
459 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
460 helper->crtc_info[i].crtc_id = crtc->base.id;
461 helper->crtc_info[i].mode_set.crtc = crtc;
464 helper->conn_limit = max_conn_count;
467 drm_fb_helper_crtc_free(helper);
470 EXPORT_SYMBOL(drm_fb_helper_init_crtc_count);
472 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
473 u16 blue, u16 regno, struct fb_info *info)
475 struct drm_fb_helper *fb_helper = info->par;
476 struct drm_framebuffer *fb = fb_helper->fb;
479 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
482 /* place color in psuedopalette */
485 palette = (u32 *)info->pseudo_palette;
486 red >>= (16 - info->var.red.length);
487 green >>= (16 - info->var.green.length);
488 blue >>= (16 - info->var.blue.length);
489 value = (red << info->var.red.offset) |
490 (green << info->var.green.offset) |
491 (blue << info->var.blue.offset);
492 palette[regno] = value;
498 if (fb->bits_per_pixel == 16) {
501 if (fb->depth == 16 && regno > 63)
503 if (fb->depth == 15 && regno > 31)
506 if (fb->depth == 16) {
510 for (i = 0; i < 8; i++)
511 fb_helper->funcs->gamma_set(crtc, red,
512 green, blue, pindex + i);
515 fb_helper->funcs->gamma_get(crtc, &r,
519 for (i = 0; i < 4; i++)
520 fb_helper->funcs->gamma_set(crtc, r,
527 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
531 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
533 struct drm_fb_helper *fb_helper = info->par;
534 struct drm_crtc_helper_funcs *crtc_funcs;
535 u16 *red, *green, *blue, *transp;
536 struct drm_crtc *crtc;
540 for (i = 0; i < fb_helper->crtc_count; i++) {
541 crtc = fb_helper->crtc_info[i].mode_set.crtc;
542 crtc_funcs = crtc->helper_private;
547 transp = cmap->transp;
550 for (i = 0; i < cmap->len; i++) {
551 u16 hred, hgreen, hblue, htransp = 0xffff;
560 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
564 crtc_funcs->load_lut(crtc);
568 EXPORT_SYMBOL(drm_fb_helper_setcmap);
570 int drm_fb_helper_setcolreg(unsigned regno,
575 struct fb_info *info)
577 struct drm_fb_helper *fb_helper = info->par;
578 struct drm_crtc *crtc;
579 struct drm_crtc_helper_funcs *crtc_funcs;
586 for (i = 0; i < fb_helper->crtc_count; i++) {
587 crtc = fb_helper->crtc_info[i].mode_set.crtc;
588 crtc_funcs = crtc->helper_private;
590 ret = setcolreg(crtc, red, green, blue, regno, info);
594 crtc_funcs->load_lut(crtc);
598 EXPORT_SYMBOL(drm_fb_helper_setcolreg);
600 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
601 struct fb_info *info)
603 struct drm_fb_helper *fb_helper = info->par;
604 struct drm_framebuffer *fb = fb_helper->fb;
607 if (var->pixclock != 0)
610 /* Need to resize the fb object !!! */
611 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
612 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
613 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
614 fb->width, fb->height, fb->bits_per_pixel);
618 switch (var->bits_per_pixel) {
620 depth = (var->green.length == 6) ? 16 : 15;
623 depth = (var->transp.length > 0) ? 32 : 24;
626 depth = var->bits_per_pixel;
633 var->green.offset = 0;
634 var->blue.offset = 0;
636 var->green.length = 8;
637 var->blue.length = 8;
638 var->transp.length = 0;
639 var->transp.offset = 0;
642 var->red.offset = 10;
643 var->green.offset = 5;
644 var->blue.offset = 0;
646 var->green.length = 5;
647 var->blue.length = 5;
648 var->transp.length = 1;
649 var->transp.offset = 15;
652 var->red.offset = 11;
653 var->green.offset = 5;
654 var->blue.offset = 0;
656 var->green.length = 6;
657 var->blue.length = 5;
658 var->transp.length = 0;
659 var->transp.offset = 0;
662 var->red.offset = 16;
663 var->green.offset = 8;
664 var->blue.offset = 0;
666 var->green.length = 8;
667 var->blue.length = 8;
668 var->transp.length = 0;
669 var->transp.offset = 0;
672 var->red.offset = 16;
673 var->green.offset = 8;
674 var->blue.offset = 0;
676 var->green.length = 8;
677 var->blue.length = 8;
678 var->transp.length = 8;
679 var->transp.offset = 24;
686 EXPORT_SYMBOL(drm_fb_helper_check_var);
688 /* this will let fbcon do the mode init */
689 int drm_fb_helper_set_par(struct fb_info *info)
691 struct drm_fb_helper *fb_helper = info->par;
692 struct drm_device *dev = fb_helper->dev;
693 struct fb_var_screeninfo *var = &info->var;
694 struct drm_crtc *crtc;
698 if (var->pixclock != 0) {
699 DRM_ERROR("PIXEL CLOCK SET\n");
703 mutex_lock(&dev->mode_config.mutex);
704 for (i = 0; i < fb_helper->crtc_count; i++) {
705 crtc = fb_helper->crtc_info[i].mode_set.crtc;
706 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
708 mutex_unlock(&dev->mode_config.mutex);
712 mutex_unlock(&dev->mode_config.mutex);
715 EXPORT_SYMBOL(drm_fb_helper_set_par);
717 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
718 struct fb_info *info)
720 struct drm_fb_helper *fb_helper = info->par;
721 struct drm_device *dev = fb_helper->dev;
722 struct drm_mode_set *modeset;
723 struct drm_crtc *crtc;
727 mutex_lock(&dev->mode_config.mutex);
728 for (i = 0; i < fb_helper->crtc_count; i++) {
729 crtc = fb_helper->crtc_info[i].mode_set.crtc;
731 modeset = &fb_helper->crtc_info[i].mode_set;
733 modeset->x = var->xoffset;
734 modeset->y = var->yoffset;
736 if (modeset->num_connectors) {
737 ret = crtc->funcs->set_config(modeset);
739 info->var.xoffset = var->xoffset;
740 info->var.yoffset = var->yoffset;
744 mutex_unlock(&dev->mode_config.mutex);
747 EXPORT_SYMBOL(drm_fb_helper_pan_display);
749 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
755 struct fb_info *info;
756 struct drm_fb_helper_surface_size sizes;
759 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
760 sizes.surface_depth = 24;
761 sizes.surface_bpp = 32;
762 sizes.fb_width = (unsigned)-1;
763 sizes.fb_height = (unsigned)-1;
765 /* if driver picks 8 or 16 by default use that
766 for both depth/bpp */
767 if (preferred_bpp != sizes.surface_bpp) {
768 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
770 /* first up get a count of crtcs now in use and new min/maxes width/heights */
771 for (i = 0; i < fb_helper->connector_count; i++) {
772 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
773 struct drm_fb_helper_cmdline_mode *cmdline_mode;
775 cmdline_mode = &fb_helper_conn->cmdline_mode;
777 if (cmdline_mode->bpp_specified) {
778 switch (cmdline_mode->bpp) {
780 sizes.surface_depth = sizes.surface_bpp = 8;
783 sizes.surface_depth = 15;
784 sizes.surface_bpp = 16;
787 sizes.surface_depth = sizes.surface_bpp = 16;
790 sizes.surface_depth = sizes.surface_bpp = 24;
793 sizes.surface_depth = 24;
794 sizes.surface_bpp = 32;
802 for (i = 0; i < fb_helper->crtc_count; i++) {
803 struct drm_display_mode *desired_mode;
804 desired_mode = fb_helper->crtc_info[i].desired_mode;
808 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
809 if (desired_mode->hdisplay < sizes.fb_width)
810 sizes.fb_width = desired_mode->hdisplay;
811 if (desired_mode->vdisplay < sizes.fb_height)
812 sizes.fb_height = desired_mode->vdisplay;
813 if (desired_mode->hdisplay > sizes.surface_width)
814 sizes.surface_width = desired_mode->hdisplay;
815 if (desired_mode->vdisplay > sizes.surface_height)
816 sizes.surface_height = desired_mode->vdisplay;
821 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
822 /* hmm everyone went away - assume VGA cable just fell out
823 and will come back later. */
827 /* push down into drivers */
828 new_fb = (*fb_helper->fb_probe)(fb_helper, &sizes);
832 info = fb_helper->fbdev;
834 /* set the fb pointer */
835 for (i = 0; i < fb_helper->crtc_count; i++) {
836 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
840 info->var.pixclock = 0;
841 ret = fb_alloc_cmap(&info->cmap, gamma_size, 0);
844 if (register_framebuffer(info) < 0) {
845 fb_dealloc_cmap(&info->cmap);
849 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
853 drm_fb_helper_set_par(info);
856 /* Switch back to kernel console on panic */
857 /* multi card linked list maybe */
858 if (list_empty(&kernel_fb_helper_list)) {
859 printk(KERN_INFO "registered panic notifier\n");
860 atomic_notifier_chain_register(&panic_notifier_list,
862 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
865 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
869 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
871 void drm_fb_helper_free(struct drm_fb_helper *helper)
873 if (!list_empty(&helper->kernel_fb_list)) {
874 list_del(&helper->kernel_fb_list);
875 if (list_empty(&kernel_fb_helper_list)) {
876 printk(KERN_INFO "unregistered panic notifier\n");
877 atomic_notifier_chain_unregister(&panic_notifier_list,
879 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
882 drm_fb_helper_crtc_free(helper);
883 if (helper->fbdev->cmap.len)
884 fb_dealloc_cmap(&helper->fbdev->cmap);
886 EXPORT_SYMBOL(drm_fb_helper_free);
888 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
891 info->fix.type = FB_TYPE_PACKED_PIXELS;
892 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
894 info->fix.type_aux = 0;
895 info->fix.xpanstep = 1; /* doing it in hw */
896 info->fix.ypanstep = 1; /* doing it in hw */
897 info->fix.ywrapstep = 0;
898 info->fix.accel = FB_ACCEL_NONE;
899 info->fix.type_aux = 0;
901 info->fix.line_length = pitch;
904 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
906 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
907 uint32_t fb_width, uint32_t fb_height)
909 struct drm_framebuffer *fb = fb_helper->fb;
910 info->pseudo_palette = fb_helper->pseudo_palette;
911 info->var.xres_virtual = fb->width;
912 info->var.yres_virtual = fb->height;
913 info->var.bits_per_pixel = fb->bits_per_pixel;
914 info->var.xoffset = 0;
915 info->var.yoffset = 0;
916 info->var.activate = FB_ACTIVATE_NOW;
917 info->var.height = -1;
918 info->var.width = -1;
922 info->var.red.offset = 0;
923 info->var.green.offset = 0;
924 info->var.blue.offset = 0;
925 info->var.red.length = 8; /* 8bit DAC */
926 info->var.green.length = 8;
927 info->var.blue.length = 8;
928 info->var.transp.offset = 0;
929 info->var.transp.length = 0;
932 info->var.red.offset = 10;
933 info->var.green.offset = 5;
934 info->var.blue.offset = 0;
935 info->var.red.length = 5;
936 info->var.green.length = 5;
937 info->var.blue.length = 5;
938 info->var.transp.offset = 15;
939 info->var.transp.length = 1;
942 info->var.red.offset = 11;
943 info->var.green.offset = 5;
944 info->var.blue.offset = 0;
945 info->var.red.length = 5;
946 info->var.green.length = 6;
947 info->var.blue.length = 5;
948 info->var.transp.offset = 0;
951 info->var.red.offset = 16;
952 info->var.green.offset = 8;
953 info->var.blue.offset = 0;
954 info->var.red.length = 8;
955 info->var.green.length = 8;
956 info->var.blue.length = 8;
957 info->var.transp.offset = 0;
958 info->var.transp.length = 0;
961 info->var.red.offset = 16;
962 info->var.green.offset = 8;
963 info->var.blue.offset = 0;
964 info->var.red.length = 8;
965 info->var.green.length = 8;
966 info->var.blue.length = 8;
967 info->var.transp.offset = 24;
968 info->var.transp.length = 8;
974 info->var.xres = fb_width;
975 info->var.yres = fb_height;
977 EXPORT_SYMBOL(drm_fb_helper_fill_var);
979 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
983 struct drm_connector *connector;
987 for (i = 0; i < fb_helper->connector_count; i++) {
988 connector = fb_helper->connector_info[i]->connector;
989 count += connector->funcs->fill_modes(connector, maxX, maxY);
995 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
997 struct drm_display_mode *mode;
999 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1000 if (drm_mode_width(mode) > width ||
1001 drm_mode_height(mode) > height)
1003 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1009 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1011 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1012 cmdline_mode = &fb_connector->cmdline_mode;
1013 return cmdline_mode->specified;
1016 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1017 int width, int height)
1019 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1020 struct drm_display_mode *mode = NULL;
1022 cmdline_mode = &fb_helper_conn->cmdline_mode;
1023 if (cmdline_mode->specified == false)
1026 /* attempt to find a matching mode in the list of modes
1027 * we have gotten so far, if not add a CVT mode that conforms
1029 if (cmdline_mode->rb || cmdline_mode->margins)
1032 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1033 /* check width/height */
1034 if (mode->hdisplay != cmdline_mode->xres ||
1035 mode->vdisplay != cmdline_mode->yres)
1038 if (cmdline_mode->refresh_specified) {
1039 if (mode->vrefresh != cmdline_mode->refresh)
1043 if (cmdline_mode->interlace) {
1044 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1051 mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1053 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1054 cmdline_mode->rb, cmdline_mode->interlace,
1055 cmdline_mode->margins);
1056 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1057 list_add(&mode->head, &fb_helper_conn->connector->modes);
1061 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1066 enable = connector->status == connector_status_connected;
1068 enable = connector->status != connector_status_disconnected;
1073 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1076 bool any_enabled = false;
1077 struct drm_connector *connector;
1080 for (i = 0; i < fb_helper->connector_count; i++) {
1081 connector = fb_helper->connector_info[i]->connector;
1082 enabled[i] = drm_connector_enabled(connector, true);
1083 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1084 enabled[i] ? "yes" : "no");
1085 any_enabled |= enabled[i];
1091 for (i = 0; i < fb_helper->connector_count; i++) {
1092 connector = fb_helper->connector_info[i]->connector;
1093 enabled[i] = drm_connector_enabled(connector, false);
1097 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1098 struct drm_display_mode **modes,
1099 bool *enabled, int width, int height)
1101 struct drm_fb_helper_connector *fb_helper_conn;
1104 for (i = 0; i < fb_helper->connector_count; i++) {
1105 fb_helper_conn = fb_helper->connector_info[i];
1107 if (enabled[i] == false)
1110 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1111 fb_helper_conn->connector->base.id);
1113 /* got for command line mode first */
1114 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1116 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1117 fb_helper_conn->connector->base.id);
1118 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1120 /* No preferred modes, pick one off the list */
1121 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1122 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1125 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1131 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1132 struct drm_fb_helper_crtc **best_crtcs,
1133 struct drm_display_mode **modes,
1134 int n, int width, int height)
1137 struct drm_device *dev = fb_helper->dev;
1138 struct drm_connector *connector;
1139 struct drm_connector_helper_funcs *connector_funcs;
1140 struct drm_encoder *encoder;
1141 struct drm_fb_helper_crtc *best_crtc;
1142 int my_score, best_score, score;
1143 struct drm_fb_helper_crtc **crtcs, *crtc;
1144 struct drm_fb_helper_connector *fb_helper_conn;
1146 if (n == fb_helper->connector_count)
1149 fb_helper_conn = fb_helper->connector_info[n];
1150 connector = fb_helper_conn->connector;
1152 best_crtcs[n] = NULL;
1154 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1155 if (modes[n] == NULL)
1158 crtcs = kzalloc(dev->mode_config.num_connector *
1159 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1164 if (connector->status == connector_status_connected)
1166 if (drm_has_cmdline_mode(fb_helper_conn))
1168 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1171 connector_funcs = connector->helper_private;
1172 encoder = connector_funcs->best_encoder(connector);
1176 /* select a crtc for this connector and then attempt to configure
1177 remaining connectors */
1178 for (c = 0; c < fb_helper->crtc_count; c++) {
1179 crtc = &fb_helper->crtc_info[c];
1181 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1185 for (o = 0; o < n; o++)
1186 if (best_crtcs[o] == crtc)
1190 /* ignore cloning for now */
1195 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1196 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1198 if (score > best_score) {
1201 memcpy(best_crtcs, crtcs,
1202 dev->mode_config.num_connector *
1203 sizeof(struct drm_fb_helper_crtc *));
1211 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1213 struct drm_device *dev = fb_helper->dev;
1214 struct drm_fb_helper_crtc **crtcs;
1215 struct drm_display_mode **modes;
1216 struct drm_encoder *encoder;
1217 struct drm_mode_set *modeset;
1222 DRM_DEBUG_KMS("\n");
1224 width = dev->mode_config.max_width;
1225 height = dev->mode_config.max_height;
1227 /* clean out all the encoder/crtc combos */
1228 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1229 encoder->crtc = NULL;
1232 crtcs = kcalloc(dev->mode_config.num_connector,
1233 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1234 modes = kcalloc(dev->mode_config.num_connector,
1235 sizeof(struct drm_display_mode *), GFP_KERNEL);
1236 enabled = kcalloc(dev->mode_config.num_connector,
1237 sizeof(bool), GFP_KERNEL);
1239 drm_enable_connectors(fb_helper, enabled);
1241 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1243 DRM_ERROR("Unable to find initial modes\n");
1245 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1247 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1249 /* need to set the modesets up here for use later */
1250 /* fill out the connector<->crtc mappings into the modesets */
1251 for (i = 0; i < fb_helper->crtc_count; i++) {
1252 modeset = &fb_helper->crtc_info[i].mode_set;
1253 modeset->num_connectors = 0;
1256 for (i = 0; i < fb_helper->connector_count; i++) {
1257 struct drm_display_mode *mode = modes[i];
1258 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1259 modeset = &fb_crtc->mode_set;
1261 if (mode && fb_crtc) {
1262 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1263 mode->name, fb_crtc->mode_set.crtc->base.id);
1264 fb_crtc->desired_mode = mode;
1266 drm_mode_destroy(dev, modeset->mode);
1267 modeset->mode = drm_mode_duplicate(dev,
1268 fb_crtc->desired_mode);
1269 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1279 * drm_helper_initial_config - setup a sane initial connector configuration
1283 * Called at init time, must take mode config lock.
1285 * Scan the CRTCs and connectors and try to put together an initial setup.
1286 * At the moment, this is a cloned configuration across all heads with
1287 * a new framebuffer object as the backing store.
1290 * Zero if everything went ok, nonzero otherwise.
1292 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
1294 struct drm_device *dev = fb_helper->dev;
1297 /* disable all the possible outputs/crtcs before entering KMS mode */
1298 drm_helper_disable_unused_functions(fb_helper->dev);
1300 drm_fb_helper_parse_command_line(fb_helper);
1302 count = drm_fb_helper_probe_connector_modes(fb_helper,
1303 dev->mode_config.max_width,
1304 dev->mode_config.max_height);
1307 * we shouldn't end up with no modes here.
1310 printk(KERN_INFO "No connectors reported connected with modes\n");
1312 drm_setup_crtcs(fb_helper);
1316 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1318 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper,
1319 u32 max_width, u32 max_height)
1321 DRM_DEBUG_KMS("\n");
1323 drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1326 drm_setup_crtcs(fb_helper);
1330 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);