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. */
824 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
825 sizes.fb_width = sizes.surface_width = 1024;
826 sizes.fb_height = sizes.surface_height = 768;
829 /* push down into drivers */
830 new_fb = (*fb_helper->fb_probe)(fb_helper, &sizes);
834 info = fb_helper->fbdev;
836 /* set the fb pointer */
837 for (i = 0; i < fb_helper->crtc_count; i++) {
838 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
842 info->var.pixclock = 0;
843 ret = fb_alloc_cmap(&info->cmap, gamma_size, 0);
846 if (register_framebuffer(info) < 0) {
847 fb_dealloc_cmap(&info->cmap);
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_free(struct drm_fb_helper *helper)
875 if (!list_empty(&helper->kernel_fb_list)) {
876 list_del(&helper->kernel_fb_list);
877 if (list_empty(&kernel_fb_helper_list)) {
878 printk(KERN_INFO "unregistered panic notifier\n");
879 atomic_notifier_chain_unregister(&panic_notifier_list,
881 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
884 drm_fb_helper_crtc_free(helper);
885 if (helper->fbdev->cmap.len)
886 fb_dealloc_cmap(&helper->fbdev->cmap);
888 EXPORT_SYMBOL(drm_fb_helper_free);
890 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
893 info->fix.type = FB_TYPE_PACKED_PIXELS;
894 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
896 info->fix.type_aux = 0;
897 info->fix.xpanstep = 1; /* doing it in hw */
898 info->fix.ypanstep = 1; /* doing it in hw */
899 info->fix.ywrapstep = 0;
900 info->fix.accel = FB_ACCEL_NONE;
901 info->fix.type_aux = 0;
903 info->fix.line_length = pitch;
906 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
908 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
909 uint32_t fb_width, uint32_t fb_height)
911 struct drm_framebuffer *fb = fb_helper->fb;
912 info->pseudo_palette = fb_helper->pseudo_palette;
913 info->var.xres_virtual = fb->width;
914 info->var.yres_virtual = fb->height;
915 info->var.bits_per_pixel = fb->bits_per_pixel;
916 info->var.xoffset = 0;
917 info->var.yoffset = 0;
918 info->var.activate = FB_ACTIVATE_NOW;
919 info->var.height = -1;
920 info->var.width = -1;
924 info->var.red.offset = 0;
925 info->var.green.offset = 0;
926 info->var.blue.offset = 0;
927 info->var.red.length = 8; /* 8bit DAC */
928 info->var.green.length = 8;
929 info->var.blue.length = 8;
930 info->var.transp.offset = 0;
931 info->var.transp.length = 0;
934 info->var.red.offset = 10;
935 info->var.green.offset = 5;
936 info->var.blue.offset = 0;
937 info->var.red.length = 5;
938 info->var.green.length = 5;
939 info->var.blue.length = 5;
940 info->var.transp.offset = 15;
941 info->var.transp.length = 1;
944 info->var.red.offset = 11;
945 info->var.green.offset = 5;
946 info->var.blue.offset = 0;
947 info->var.red.length = 5;
948 info->var.green.length = 6;
949 info->var.blue.length = 5;
950 info->var.transp.offset = 0;
953 info->var.red.offset = 16;
954 info->var.green.offset = 8;
955 info->var.blue.offset = 0;
956 info->var.red.length = 8;
957 info->var.green.length = 8;
958 info->var.blue.length = 8;
959 info->var.transp.offset = 0;
960 info->var.transp.length = 0;
963 info->var.red.offset = 16;
964 info->var.green.offset = 8;
965 info->var.blue.offset = 0;
966 info->var.red.length = 8;
967 info->var.green.length = 8;
968 info->var.blue.length = 8;
969 info->var.transp.offset = 24;
970 info->var.transp.length = 8;
976 info->var.xres = fb_width;
977 info->var.yres = fb_height;
979 EXPORT_SYMBOL(drm_fb_helper_fill_var);
981 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
985 struct drm_connector *connector;
989 for (i = 0; i < fb_helper->connector_count; i++) {
990 connector = fb_helper->connector_info[i]->connector;
991 count += connector->funcs->fill_modes(connector, maxX, maxY);
997 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
999 struct drm_display_mode *mode;
1001 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1002 if (drm_mode_width(mode) > width ||
1003 drm_mode_height(mode) > height)
1005 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1011 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1013 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1014 cmdline_mode = &fb_connector->cmdline_mode;
1015 return cmdline_mode->specified;
1018 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1019 int width, int height)
1021 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1022 struct drm_display_mode *mode = NULL;
1024 cmdline_mode = &fb_helper_conn->cmdline_mode;
1025 if (cmdline_mode->specified == false)
1028 /* attempt to find a matching mode in the list of modes
1029 * we have gotten so far, if not add a CVT mode that conforms
1031 if (cmdline_mode->rb || cmdline_mode->margins)
1034 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1035 /* check width/height */
1036 if (mode->hdisplay != cmdline_mode->xres ||
1037 mode->vdisplay != cmdline_mode->yres)
1040 if (cmdline_mode->refresh_specified) {
1041 if (mode->vrefresh != cmdline_mode->refresh)
1045 if (cmdline_mode->interlace) {
1046 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1053 mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1055 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1056 cmdline_mode->rb, cmdline_mode->interlace,
1057 cmdline_mode->margins);
1058 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1059 list_add(&mode->head, &fb_helper_conn->connector->modes);
1063 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1068 enable = connector->status == connector_status_connected;
1070 enable = connector->status != connector_status_disconnected;
1075 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1078 bool any_enabled = false;
1079 struct drm_connector *connector;
1082 for (i = 0; i < fb_helper->connector_count; i++) {
1083 connector = fb_helper->connector_info[i]->connector;
1084 enabled[i] = drm_connector_enabled(connector, true);
1085 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1086 enabled[i] ? "yes" : "no");
1087 any_enabled |= enabled[i];
1093 for (i = 0; i < fb_helper->connector_count; i++) {
1094 connector = fb_helper->connector_info[i]->connector;
1095 enabled[i] = drm_connector_enabled(connector, false);
1099 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1100 struct drm_display_mode **modes,
1101 bool *enabled, int width, int height)
1103 struct drm_fb_helper_connector *fb_helper_conn;
1106 for (i = 0; i < fb_helper->connector_count; i++) {
1107 fb_helper_conn = fb_helper->connector_info[i];
1109 if (enabled[i] == false)
1112 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1113 fb_helper_conn->connector->base.id);
1115 /* got for command line mode first */
1116 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1118 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1119 fb_helper_conn->connector->base.id);
1120 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1122 /* No preferred modes, pick one off the list */
1123 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1124 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1127 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1133 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1134 struct drm_fb_helper_crtc **best_crtcs,
1135 struct drm_display_mode **modes,
1136 int n, int width, int height)
1139 struct drm_device *dev = fb_helper->dev;
1140 struct drm_connector *connector;
1141 struct drm_connector_helper_funcs *connector_funcs;
1142 struct drm_encoder *encoder;
1143 struct drm_fb_helper_crtc *best_crtc;
1144 int my_score, best_score, score;
1145 struct drm_fb_helper_crtc **crtcs, *crtc;
1146 struct drm_fb_helper_connector *fb_helper_conn;
1148 if (n == fb_helper->connector_count)
1151 fb_helper_conn = fb_helper->connector_info[n];
1152 connector = fb_helper_conn->connector;
1154 best_crtcs[n] = NULL;
1156 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1157 if (modes[n] == NULL)
1160 crtcs = kzalloc(dev->mode_config.num_connector *
1161 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1166 if (connector->status == connector_status_connected)
1168 if (drm_has_cmdline_mode(fb_helper_conn))
1170 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1173 connector_funcs = connector->helper_private;
1174 encoder = connector_funcs->best_encoder(connector);
1178 /* select a crtc for this connector and then attempt to configure
1179 remaining connectors */
1180 for (c = 0; c < fb_helper->crtc_count; c++) {
1181 crtc = &fb_helper->crtc_info[c];
1183 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1187 for (o = 0; o < n; o++)
1188 if (best_crtcs[o] == crtc)
1192 /* ignore cloning for now */
1197 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1198 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1200 if (score > best_score) {
1203 memcpy(best_crtcs, crtcs,
1204 dev->mode_config.num_connector *
1205 sizeof(struct drm_fb_helper_crtc *));
1213 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1215 struct drm_device *dev = fb_helper->dev;
1216 struct drm_fb_helper_crtc **crtcs;
1217 struct drm_display_mode **modes;
1218 struct drm_encoder *encoder;
1219 struct drm_mode_set *modeset;
1224 DRM_DEBUG_KMS("\n");
1226 width = dev->mode_config.max_width;
1227 height = dev->mode_config.max_height;
1229 /* clean out all the encoder/crtc combos */
1230 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1231 encoder->crtc = NULL;
1234 crtcs = kcalloc(dev->mode_config.num_connector,
1235 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1236 modes = kcalloc(dev->mode_config.num_connector,
1237 sizeof(struct drm_display_mode *), GFP_KERNEL);
1238 enabled = kcalloc(dev->mode_config.num_connector,
1239 sizeof(bool), GFP_KERNEL);
1241 drm_enable_connectors(fb_helper, enabled);
1243 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1245 DRM_ERROR("Unable to find initial modes\n");
1247 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1249 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1251 /* need to set the modesets up here for use later */
1252 /* fill out the connector<->crtc mappings into the modesets */
1253 for (i = 0; i < fb_helper->crtc_count; i++) {
1254 modeset = &fb_helper->crtc_info[i].mode_set;
1255 modeset->num_connectors = 0;
1258 for (i = 0; i < fb_helper->connector_count; i++) {
1259 struct drm_display_mode *mode = modes[i];
1260 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1261 modeset = &fb_crtc->mode_set;
1263 if (mode && fb_crtc) {
1264 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1265 mode->name, fb_crtc->mode_set.crtc->base.id);
1266 fb_crtc->desired_mode = mode;
1268 drm_mode_destroy(dev, modeset->mode);
1269 modeset->mode = drm_mode_duplicate(dev,
1270 fb_crtc->desired_mode);
1271 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1281 * drm_helper_initial_config - setup a sane initial connector configuration
1285 * Called at init time, must take mode config lock.
1287 * Scan the CRTCs and connectors and try to put together an initial setup.
1288 * At the moment, this is a cloned configuration across all heads with
1289 * a new framebuffer object as the backing store.
1292 * Zero if everything went ok, nonzero otherwise.
1294 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
1296 struct drm_device *dev = fb_helper->dev;
1299 /* disable all the possible outputs/crtcs before entering KMS mode */
1300 drm_helper_disable_unused_functions(fb_helper->dev);
1302 drm_fb_helper_parse_command_line(fb_helper);
1304 count = drm_fb_helper_probe_connector_modes(fb_helper,
1305 dev->mode_config.max_width,
1306 dev->mode_config.max_height);
1309 * we shouldn't end up with no modes here.
1312 if (fb_helper->poll_enabled) {
1313 delayed_slow_work_enqueue(&fb_helper->output_poll_slow_work,
1315 printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1317 printk(KERN_INFO "No connectors reported connected with modes\n");
1319 drm_setup_crtcs(fb_helper);
1323 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1325 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper,
1326 u32 max_width, u32 max_height, bool polled)
1330 DRM_DEBUG_KMS("\n");
1332 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1334 if (fb_helper->poll_enabled && !polled) {
1336 delayed_slow_work_cancel(&fb_helper->output_poll_slow_work);
1338 ret = delayed_slow_work_enqueue(&fb_helper->output_poll_slow_work, 5*HZ);
1341 drm_setup_crtcs(fb_helper);
1345 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
1347 static void output_poll_execute(struct slow_work *work)
1349 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
1350 struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_poll_slow_work);
1351 struct drm_device *dev = fb_helper->dev;
1352 struct drm_connector *connector;
1353 enum drm_connector_status old_status, status;
1354 bool repoll = true, changed = false;
1357 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1358 old_status = connector->status;
1359 status = connector->funcs->detect(connector);
1360 if (old_status != status) {
1362 /* something changed */
1364 if (status == connector_status_connected) {
1365 DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1371 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1373 DRM_ERROR("delayed enqueue failed %d\n", ret);
1377 if (fb_helper->fb_poll_changed)
1378 fb_helper->fb_poll_changed(fb_helper);
1382 struct slow_work_ops output_poll_ops = {
1383 .execute = output_poll_execute,
1386 void drm_fb_helper_poll_init(struct drm_fb_helper *fb_helper)
1390 ret = slow_work_register_user(THIS_MODULE);
1392 delayed_slow_work_init(&fb_helper->output_poll_slow_work, &output_poll_ops);
1393 fb_helper->poll_enabled = true;
1395 EXPORT_SYMBOL(drm_fb_helper_poll_init);
1397 void drm_fb_helper_poll_fini(struct drm_fb_helper *fb_helper)
1399 delayed_slow_work_cancel(&fb_helper->output_poll_slow_work);
1400 slow_work_unregister_user(THIS_MODULE);
1402 EXPORT_SYMBOL(drm_fb_helper_poll_fini);