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
= fb_helper_conn
->connector
;
102 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
104 mode_option
= fb_mode_option
;
107 cmdline_mode
->specified
= false;
112 namelen
= strlen(name
);
113 for (i
= namelen
-1; i
>= 0; i
--) {
117 if (!refresh_specified
&& !bpp_specified
&&
119 refresh
= simple_strtol(&name
[i
+1], NULL
, 10);
120 refresh_specified
= 1;
128 if (!bpp_specified
&& !yres_specified
) {
129 bpp
= simple_strtol(&name
[i
+1], NULL
, 10);
137 if (!yres_specified
) {
138 yres
= simple_strtol(&name
[i
+1], NULL
, 10);
161 force
= DRM_FORCE_ON
;
164 if ((connector
->connector_type
!= DRM_MODE_CONNECTOR_DVII
) &&
165 (connector
->connector_type
!= DRM_MODE_CONNECTOR_HDMIB
))
166 force
= DRM_FORCE_ON
;
168 force
= DRM_FORCE_ON_DIGITAL
;
171 force
= DRM_FORCE_OFF
;
177 if (i
< 0 && yres_specified
) {
178 xres
= simple_strtol(name
, NULL
, 10);
183 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
184 drm_get_connector_name(connector
), xres
, yres
,
185 (refresh
) ? refresh
: 60, (rb
) ? " reduced blanking" :
186 "", (margins
) ? " with margins" : "", (interlace
) ?
192 case DRM_FORCE_OFF
: s
= "OFF"; break;
193 case DRM_FORCE_ON_DIGITAL
: s
= "ON - dig"; break;
195 case DRM_FORCE_ON
: s
= "ON"; break;
198 DRM_INFO("forcing %s connector %s\n",
199 drm_get_connector_name(connector
), s
);
200 connector
->force
= force
;
204 cmdline_mode
->specified
= true;
205 cmdline_mode
->xres
= xres
;
206 cmdline_mode
->yres
= yres
;
209 if (refresh_specified
) {
210 cmdline_mode
->refresh_specified
= true;
211 cmdline_mode
->refresh
= refresh
;
215 cmdline_mode
->bpp_specified
= true;
216 cmdline_mode
->bpp
= bpp
;
218 cmdline_mode
->rb
= rb
? true : false;
219 cmdline_mode
->cvt
= cvt
? true : false;
220 cmdline_mode
->interlace
= interlace
? true : false;
225 static int drm_fb_helper_parse_command_line(struct drm_fb_helper
*fb_helper
)
227 struct drm_fb_helper_connector
*fb_helper_conn
;
230 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
233 fb_helper_conn
= fb_helper
->connector_info
[i
];
235 /* do something on return - turn off connector maybe */
236 if (fb_get_options(drm_get_connector_name(fb_helper_conn
->connector
), &option
))
239 drm_fb_helper_connector_parse_command_line(fb_helper_conn
, option
);
244 int drm_fb_helper_debug_enter(struct fb_info
*info
)
246 struct drm_fb_helper
*helper
= info
->par
;
247 struct drm_crtc_helper_funcs
*funcs
;
250 if (list_empty(&kernel_fb_helper_list
))
253 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
254 for (i
= 0; i
< helper
->crtc_count
; i
++) {
255 struct drm_mode_set
*mode_set
=
256 &helper
->crtc_info
[i
].mode_set
;
258 if (!mode_set
->crtc
->enabled
)
261 funcs
= mode_set
->crtc
->helper_private
;
262 funcs
->mode_set_base_atomic(mode_set
->crtc
,
272 EXPORT_SYMBOL(drm_fb_helper_debug_enter
);
274 /* Find the real fb for a given fb helper CRTC */
275 static struct drm_framebuffer
*drm_mode_config_fb(struct drm_crtc
*crtc
)
277 struct drm_device
*dev
= crtc
->dev
;
280 list_for_each_entry(c
, &dev
->mode_config
.crtc_list
, head
) {
281 if (crtc
->base
.id
== c
->base
.id
)
288 int drm_fb_helper_debug_leave(struct fb_info
*info
)
290 struct drm_fb_helper
*helper
= info
->par
;
291 struct drm_crtc
*crtc
;
292 struct drm_crtc_helper_funcs
*funcs
;
293 struct drm_framebuffer
*fb
;
296 for (i
= 0; i
< helper
->crtc_count
; i
++) {
297 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
298 crtc
= mode_set
->crtc
;
299 funcs
= crtc
->helper_private
;
300 fb
= drm_mode_config_fb(crtc
);
306 DRM_ERROR("no fb to restore??\n");
310 funcs
->mode_set_base_atomic(mode_set
->crtc
, fb
, crtc
->x
,
316 EXPORT_SYMBOL(drm_fb_helper_debug_leave
);
318 bool drm_fb_helper_force_kernel_mode(void)
321 bool ret
, error
= false;
322 struct drm_fb_helper
*helper
;
324 if (list_empty(&kernel_fb_helper_list
))
327 list_for_each_entry(helper
, &kernel_fb_helper_list
, kernel_fb_list
) {
328 for (i
= 0; i
< helper
->crtc_count
; i
++) {
329 struct drm_mode_set
*mode_set
= &helper
->crtc_info
[i
].mode_set
;
330 ret
= drm_crtc_helper_set_config(mode_set
);
338 int drm_fb_helper_panic(struct notifier_block
*n
, unsigned long ununsed
,
341 printk(KERN_ERR
"panic occurred, switching back to text console\n");
342 return drm_fb_helper_force_kernel_mode();
345 EXPORT_SYMBOL(drm_fb_helper_panic
);
347 static struct notifier_block paniced
= {
348 .notifier_call
= drm_fb_helper_panic
,
352 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
354 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
356 void drm_fb_helper_restore(void)
359 ret
= drm_fb_helper_force_kernel_mode();
361 DRM_ERROR("Failed to restore crtc configuration\n");
363 EXPORT_SYMBOL(drm_fb_helper_restore
);
365 #ifdef CONFIG_MAGIC_SYSRQ
366 static void drm_fb_helper_restore_work_fn(struct work_struct
*ignored
)
368 drm_fb_helper_restore();
370 static DECLARE_WORK(drm_fb_helper_restore_work
, drm_fb_helper_restore_work_fn
);
372 static void drm_fb_helper_sysrq(int dummy1
, struct tty_struct
*dummy3
)
374 schedule_work(&drm_fb_helper_restore_work
);
377 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= {
378 .handler
= drm_fb_helper_sysrq
,
379 .help_msg
= "force-fb(V)",
380 .action_msg
= "Restore framebuffer console",
383 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op
= { };
386 static void drm_fb_helper_on(struct fb_info
*info
)
388 struct drm_fb_helper
*fb_helper
= info
->par
;
389 struct drm_device
*dev
= fb_helper
->dev
;
390 struct drm_crtc
*crtc
;
391 struct drm_crtc_helper_funcs
*crtc_funcs
;
392 struct drm_connector
*connector
;
393 struct drm_encoder
*encoder
;
397 * For each CRTC in this fb, turn the crtc on then,
398 * find all associated encoders and turn them on.
400 mutex_lock(&dev
->mode_config
.mutex
);
401 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
402 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
403 crtc_funcs
= crtc
->helper_private
;
408 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_ON
);
410 /* Walk the connectors & encoders on this fb turning them on */
411 for (j
= 0; j
< fb_helper
->connector_count
; j
++) {
412 connector
= fb_helper
->connector_info
[j
]->connector
;
413 connector
->dpms
= DRM_MODE_DPMS_ON
;
414 drm_connector_property_set_value(connector
,
415 dev
->mode_config
.dpms_property
,
418 /* Found a CRTC on this fb, now find encoders */
419 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
420 if (encoder
->crtc
== crtc
) {
421 struct drm_encoder_helper_funcs
*encoder_funcs
;
423 encoder_funcs
= encoder
->helper_private
;
424 encoder_funcs
->dpms(encoder
, DRM_MODE_DPMS_ON
);
428 mutex_unlock(&dev
->mode_config
.mutex
);
431 static void drm_fb_helper_off(struct fb_info
*info
, int dpms_mode
)
433 struct drm_fb_helper
*fb_helper
= info
->par
;
434 struct drm_device
*dev
= fb_helper
->dev
;
435 struct drm_crtc
*crtc
;
436 struct drm_crtc_helper_funcs
*crtc_funcs
;
437 struct drm_connector
*connector
;
438 struct drm_encoder
*encoder
;
442 * For each CRTC in this fb, find all associated encoders
443 * and turn them off, then turn off the CRTC.
445 mutex_lock(&dev
->mode_config
.mutex
);
446 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
447 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
448 crtc_funcs
= crtc
->helper_private
;
453 /* Walk the connectors on this fb and mark them off */
454 for (j
= 0; j
< fb_helper
->connector_count
; j
++) {
455 connector
= fb_helper
->connector_info
[j
]->connector
;
456 connector
->dpms
= dpms_mode
;
457 drm_connector_property_set_value(connector
,
458 dev
->mode_config
.dpms_property
,
461 /* Found a CRTC on this fb, now find encoders */
462 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
463 if (encoder
->crtc
== crtc
) {
464 struct drm_encoder_helper_funcs
*encoder_funcs
;
466 encoder_funcs
= encoder
->helper_private
;
467 encoder_funcs
->dpms(encoder
, dpms_mode
);
470 crtc_funcs
->dpms(crtc
, DRM_MODE_DPMS_OFF
);
472 mutex_unlock(&dev
->mode_config
.mutex
);
475 int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
478 /* Display: On; HSync: On, VSync: On */
479 case FB_BLANK_UNBLANK
:
480 drm_fb_helper_on(info
);
482 /* Display: Off; HSync: On, VSync: On */
483 case FB_BLANK_NORMAL
:
484 drm_fb_helper_off(info
, DRM_MODE_DPMS_STANDBY
);
486 /* Display: Off; HSync: Off, VSync: On */
487 case FB_BLANK_HSYNC_SUSPEND
:
488 drm_fb_helper_off(info
, DRM_MODE_DPMS_STANDBY
);
490 /* Display: Off; HSync: On, VSync: Off */
491 case FB_BLANK_VSYNC_SUSPEND
:
492 drm_fb_helper_off(info
, DRM_MODE_DPMS_SUSPEND
);
494 /* Display: Off; HSync: Off, VSync: Off */
495 case FB_BLANK_POWERDOWN
:
496 drm_fb_helper_off(info
, DRM_MODE_DPMS_OFF
);
501 EXPORT_SYMBOL(drm_fb_helper_blank
);
503 static void drm_fb_helper_crtc_free(struct drm_fb_helper
*helper
)
507 for (i
= 0; i
< helper
->connector_count
; i
++)
508 kfree(helper
->connector_info
[i
]);
509 kfree(helper
->connector_info
);
510 for (i
= 0; i
< helper
->crtc_count
; i
++)
511 kfree(helper
->crtc_info
[i
].mode_set
.connectors
);
512 kfree(helper
->crtc_info
);
515 int drm_fb_helper_init(struct drm_device
*dev
,
516 struct drm_fb_helper
*fb_helper
,
517 int crtc_count
, int max_conn_count
)
519 struct drm_crtc
*crtc
;
523 fb_helper
->dev
= dev
;
525 INIT_LIST_HEAD(&fb_helper
->kernel_fb_list
);
527 fb_helper
->crtc_info
= kcalloc(crtc_count
, sizeof(struct drm_fb_helper_crtc
), GFP_KERNEL
);
528 if (!fb_helper
->crtc_info
)
531 fb_helper
->crtc_count
= crtc_count
;
532 fb_helper
->connector_info
= kcalloc(dev
->mode_config
.num_connector
, sizeof(struct drm_fb_helper_connector
*), GFP_KERNEL
);
533 if (!fb_helper
->connector_info
) {
534 kfree(fb_helper
->crtc_info
);
537 fb_helper
->connector_count
= 0;
539 for (i
= 0; i
< crtc_count
; i
++) {
540 fb_helper
->crtc_info
[i
].mode_set
.connectors
=
541 kcalloc(max_conn_count
,
542 sizeof(struct drm_connector
*),
545 if (!fb_helper
->crtc_info
[i
].mode_set
.connectors
) {
549 fb_helper
->crtc_info
[i
].mode_set
.num_connectors
= 0;
553 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
554 fb_helper
->crtc_info
[i
].crtc_id
= crtc
->base
.id
;
555 fb_helper
->crtc_info
[i
].mode_set
.crtc
= crtc
;
558 fb_helper
->conn_limit
= max_conn_count
;
561 drm_fb_helper_crtc_free(fb_helper
);
564 EXPORT_SYMBOL(drm_fb_helper_init
);
566 void drm_fb_helper_fini(struct drm_fb_helper
*fb_helper
)
568 if (!list_empty(&fb_helper
->kernel_fb_list
)) {
569 list_del(&fb_helper
->kernel_fb_list
);
570 if (list_empty(&kernel_fb_helper_list
)) {
571 printk(KERN_INFO
"drm: unregistered panic notifier\n");
572 atomic_notifier_chain_unregister(&panic_notifier_list
,
574 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
578 drm_fb_helper_crtc_free(fb_helper
);
581 EXPORT_SYMBOL(drm_fb_helper_fini
);
583 static int setcolreg(struct drm_crtc
*crtc
, u16 red
, u16 green
,
584 u16 blue
, u16 regno
, struct fb_info
*info
)
586 struct drm_fb_helper
*fb_helper
= info
->par
;
587 struct drm_framebuffer
*fb
= fb_helper
->fb
;
590 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
593 /* place color in psuedopalette */
596 palette
= (u32
*)info
->pseudo_palette
;
597 red
>>= (16 - info
->var
.red
.length
);
598 green
>>= (16 - info
->var
.green
.length
);
599 blue
>>= (16 - info
->var
.blue
.length
);
600 value
= (red
<< info
->var
.red
.offset
) |
601 (green
<< info
->var
.green
.offset
) |
602 (blue
<< info
->var
.blue
.offset
);
603 palette
[regno
] = value
;
609 if (fb
->bits_per_pixel
== 16) {
612 if (fb
->depth
== 16 && regno
> 63)
614 if (fb
->depth
== 15 && regno
> 31)
617 if (fb
->depth
== 16) {
621 for (i
= 0; i
< 8; i
++)
622 fb_helper
->funcs
->gamma_set(crtc
, red
,
623 green
, blue
, pindex
+ i
);
626 fb_helper
->funcs
->gamma_get(crtc
, &r
,
630 for (i
= 0; i
< 4; i
++)
631 fb_helper
->funcs
->gamma_set(crtc
, r
,
638 fb_helper
->funcs
->gamma_set(crtc
, red
, green
, blue
, pindex
);
642 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
)
644 struct drm_fb_helper
*fb_helper
= info
->par
;
645 struct drm_crtc_helper_funcs
*crtc_funcs
;
646 u16
*red
, *green
, *blue
, *transp
;
647 struct drm_crtc
*crtc
;
651 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
652 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
653 crtc_funcs
= crtc
->helper_private
;
658 transp
= cmap
->transp
;
661 for (i
= 0; i
< cmap
->len
; i
++) {
662 u16 hred
, hgreen
, hblue
, htransp
= 0xffff;
671 rc
= setcolreg(crtc
, hred
, hgreen
, hblue
, start
++, info
);
675 crtc_funcs
->load_lut(crtc
);
679 EXPORT_SYMBOL(drm_fb_helper_setcmap
);
681 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
682 struct fb_info
*info
)
684 struct drm_fb_helper
*fb_helper
= info
->par
;
685 struct drm_framebuffer
*fb
= fb_helper
->fb
;
688 if (var
->pixclock
!= 0 || in_dbg_master())
691 /* Need to resize the fb object !!! */
692 if (var
->bits_per_pixel
> fb
->bits_per_pixel
|| var
->xres
> fb
->width
|| var
->yres
> fb
->height
) {
693 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
694 "object %dx%d-%d > %dx%d-%d\n", var
->xres
, var
->yres
, var
->bits_per_pixel
,
695 fb
->width
, fb
->height
, fb
->bits_per_pixel
);
699 switch (var
->bits_per_pixel
) {
701 depth
= (var
->green
.length
== 6) ? 16 : 15;
704 depth
= (var
->transp
.length
> 0) ? 32 : 24;
707 depth
= var
->bits_per_pixel
;
714 var
->green
.offset
= 0;
715 var
->blue
.offset
= 0;
717 var
->green
.length
= 8;
718 var
->blue
.length
= 8;
719 var
->transp
.length
= 0;
720 var
->transp
.offset
= 0;
723 var
->red
.offset
= 10;
724 var
->green
.offset
= 5;
725 var
->blue
.offset
= 0;
727 var
->green
.length
= 5;
728 var
->blue
.length
= 5;
729 var
->transp
.length
= 1;
730 var
->transp
.offset
= 15;
733 var
->red
.offset
= 11;
734 var
->green
.offset
= 5;
735 var
->blue
.offset
= 0;
737 var
->green
.length
= 6;
738 var
->blue
.length
= 5;
739 var
->transp
.length
= 0;
740 var
->transp
.offset
= 0;
743 var
->red
.offset
= 16;
744 var
->green
.offset
= 8;
745 var
->blue
.offset
= 0;
747 var
->green
.length
= 8;
748 var
->blue
.length
= 8;
749 var
->transp
.length
= 0;
750 var
->transp
.offset
= 0;
753 var
->red
.offset
= 16;
754 var
->green
.offset
= 8;
755 var
->blue
.offset
= 0;
757 var
->green
.length
= 8;
758 var
->blue
.length
= 8;
759 var
->transp
.length
= 8;
760 var
->transp
.offset
= 24;
767 EXPORT_SYMBOL(drm_fb_helper_check_var
);
769 /* this will let fbcon do the mode init */
770 int drm_fb_helper_set_par(struct fb_info
*info
)
772 struct drm_fb_helper
*fb_helper
= info
->par
;
773 struct drm_device
*dev
= fb_helper
->dev
;
774 struct fb_var_screeninfo
*var
= &info
->var
;
775 struct drm_crtc
*crtc
;
779 if (var
->pixclock
!= 0) {
780 DRM_ERROR("PIXEL CLOCK SET\n");
784 mutex_lock(&dev
->mode_config
.mutex
);
785 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
786 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
787 ret
= crtc
->funcs
->set_config(&fb_helper
->crtc_info
[i
].mode_set
);
789 mutex_unlock(&dev
->mode_config
.mutex
);
793 mutex_unlock(&dev
->mode_config
.mutex
);
795 if (fb_helper
->delayed_hotplug
) {
796 fb_helper
->delayed_hotplug
= false;
797 drm_fb_helper_hotplug_event(fb_helper
);
801 EXPORT_SYMBOL(drm_fb_helper_set_par
);
803 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
804 struct fb_info
*info
)
806 struct drm_fb_helper
*fb_helper
= info
->par
;
807 struct drm_device
*dev
= fb_helper
->dev
;
808 struct drm_mode_set
*modeset
;
809 struct drm_crtc
*crtc
;
813 mutex_lock(&dev
->mode_config
.mutex
);
814 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
815 crtc
= fb_helper
->crtc_info
[i
].mode_set
.crtc
;
817 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
819 modeset
->x
= var
->xoffset
;
820 modeset
->y
= var
->yoffset
;
822 if (modeset
->num_connectors
) {
823 ret
= crtc
->funcs
->set_config(modeset
);
825 info
->var
.xoffset
= var
->xoffset
;
826 info
->var
.yoffset
= var
->yoffset
;
830 mutex_unlock(&dev
->mode_config
.mutex
);
833 EXPORT_SYMBOL(drm_fb_helper_pan_display
);
835 int drm_fb_helper_single_fb_probe(struct drm_fb_helper
*fb_helper
,
841 struct fb_info
*info
;
842 struct drm_fb_helper_surface_size sizes
;
845 memset(&sizes
, 0, sizeof(struct drm_fb_helper_surface_size
));
846 sizes
.surface_depth
= 24;
847 sizes
.surface_bpp
= 32;
848 sizes
.fb_width
= (unsigned)-1;
849 sizes
.fb_height
= (unsigned)-1;
851 /* if driver picks 8 or 16 by default use that
852 for both depth/bpp */
853 if (preferred_bpp
!= sizes
.surface_bpp
) {
854 sizes
.surface_depth
= sizes
.surface_bpp
= preferred_bpp
;
856 /* first up get a count of crtcs now in use and new min/maxes width/heights */
857 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
858 struct drm_fb_helper_connector
*fb_helper_conn
= fb_helper
->connector_info
[i
];
859 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
861 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
863 if (cmdline_mode
->bpp_specified
) {
864 switch (cmdline_mode
->bpp
) {
866 sizes
.surface_depth
= sizes
.surface_bpp
= 8;
869 sizes
.surface_depth
= 15;
870 sizes
.surface_bpp
= 16;
873 sizes
.surface_depth
= sizes
.surface_bpp
= 16;
876 sizes
.surface_depth
= sizes
.surface_bpp
= 24;
879 sizes
.surface_depth
= 24;
880 sizes
.surface_bpp
= 32;
888 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
889 struct drm_display_mode
*desired_mode
;
890 desired_mode
= fb_helper
->crtc_info
[i
].desired_mode
;
894 gamma_size
= fb_helper
->crtc_info
[i
].mode_set
.crtc
->gamma_size
;
895 if (desired_mode
->hdisplay
< sizes
.fb_width
)
896 sizes
.fb_width
= desired_mode
->hdisplay
;
897 if (desired_mode
->vdisplay
< sizes
.fb_height
)
898 sizes
.fb_height
= desired_mode
->vdisplay
;
899 if (desired_mode
->hdisplay
> sizes
.surface_width
)
900 sizes
.surface_width
= desired_mode
->hdisplay
;
901 if (desired_mode
->vdisplay
> sizes
.surface_height
)
902 sizes
.surface_height
= desired_mode
->vdisplay
;
907 if (crtc_count
== 0 || sizes
.fb_width
== -1 || sizes
.fb_height
== -1) {
908 /* hmm everyone went away - assume VGA cable just fell out
909 and will come back later. */
910 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
911 sizes
.fb_width
= sizes
.surface_width
= 1024;
912 sizes
.fb_height
= sizes
.surface_height
= 768;
915 /* push down into drivers */
916 new_fb
= (*fb_helper
->funcs
->fb_probe
)(fb_helper
, &sizes
);
920 info
= fb_helper
->fbdev
;
922 /* set the fb pointer */
923 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
924 fb_helper
->crtc_info
[i
].mode_set
.fb
= fb_helper
->fb
;
928 info
->var
.pixclock
= 0;
929 if (register_framebuffer(info
) < 0) {
933 printk(KERN_INFO
"fb%d: %s frame buffer device\n", info
->node
,
937 drm_fb_helper_set_par(info
);
940 /* Switch back to kernel console on panic */
941 /* multi card linked list maybe */
942 if (list_empty(&kernel_fb_helper_list
)) {
943 printk(KERN_INFO
"drm: registered panic notifier\n");
944 atomic_notifier_chain_register(&panic_notifier_list
,
946 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op
);
949 list_add(&fb_helper
->kernel_fb_list
, &kernel_fb_helper_list
);
953 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe
);
955 void drm_fb_helper_fill_fix(struct fb_info
*info
, uint32_t pitch
,
958 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
959 info
->fix
.visual
= depth
== 8 ? FB_VISUAL_PSEUDOCOLOR
:
961 info
->fix
.type_aux
= 0;
962 info
->fix
.xpanstep
= 1; /* doing it in hw */
963 info
->fix
.ypanstep
= 1; /* doing it in hw */
964 info
->fix
.ywrapstep
= 0;
965 info
->fix
.accel
= FB_ACCEL_NONE
;
966 info
->fix
.type_aux
= 0;
968 info
->fix
.line_length
= pitch
;
971 EXPORT_SYMBOL(drm_fb_helper_fill_fix
);
973 void drm_fb_helper_fill_var(struct fb_info
*info
, struct drm_fb_helper
*fb_helper
,
974 uint32_t fb_width
, uint32_t fb_height
)
976 struct drm_framebuffer
*fb
= fb_helper
->fb
;
977 info
->pseudo_palette
= fb_helper
->pseudo_palette
;
978 info
->var
.xres_virtual
= fb
->width
;
979 info
->var
.yres_virtual
= fb
->height
;
980 info
->var
.bits_per_pixel
= fb
->bits_per_pixel
;
981 info
->var
.xoffset
= 0;
982 info
->var
.yoffset
= 0;
983 info
->var
.activate
= FB_ACTIVATE_NOW
;
984 info
->var
.height
= -1;
985 info
->var
.width
= -1;
989 info
->var
.red
.offset
= 0;
990 info
->var
.green
.offset
= 0;
991 info
->var
.blue
.offset
= 0;
992 info
->var
.red
.length
= 8; /* 8bit DAC */
993 info
->var
.green
.length
= 8;
994 info
->var
.blue
.length
= 8;
995 info
->var
.transp
.offset
= 0;
996 info
->var
.transp
.length
= 0;
999 info
->var
.red
.offset
= 10;
1000 info
->var
.green
.offset
= 5;
1001 info
->var
.blue
.offset
= 0;
1002 info
->var
.red
.length
= 5;
1003 info
->var
.green
.length
= 5;
1004 info
->var
.blue
.length
= 5;
1005 info
->var
.transp
.offset
= 15;
1006 info
->var
.transp
.length
= 1;
1009 info
->var
.red
.offset
= 11;
1010 info
->var
.green
.offset
= 5;
1011 info
->var
.blue
.offset
= 0;
1012 info
->var
.red
.length
= 5;
1013 info
->var
.green
.length
= 6;
1014 info
->var
.blue
.length
= 5;
1015 info
->var
.transp
.offset
= 0;
1018 info
->var
.red
.offset
= 16;
1019 info
->var
.green
.offset
= 8;
1020 info
->var
.blue
.offset
= 0;
1021 info
->var
.red
.length
= 8;
1022 info
->var
.green
.length
= 8;
1023 info
->var
.blue
.length
= 8;
1024 info
->var
.transp
.offset
= 0;
1025 info
->var
.transp
.length
= 0;
1028 info
->var
.red
.offset
= 16;
1029 info
->var
.green
.offset
= 8;
1030 info
->var
.blue
.offset
= 0;
1031 info
->var
.red
.length
= 8;
1032 info
->var
.green
.length
= 8;
1033 info
->var
.blue
.length
= 8;
1034 info
->var
.transp
.offset
= 24;
1035 info
->var
.transp
.length
= 8;
1041 info
->var
.xres
= fb_width
;
1042 info
->var
.yres
= fb_height
;
1044 EXPORT_SYMBOL(drm_fb_helper_fill_var
);
1046 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper
*fb_helper
,
1050 struct drm_connector
*connector
;
1054 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1055 connector
= fb_helper
->connector_info
[i
]->connector
;
1056 count
+= connector
->funcs
->fill_modes(connector
, maxX
, maxY
);
1062 static struct drm_display_mode
*drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
, int width
, int height
)
1064 struct drm_display_mode
*mode
;
1066 list_for_each_entry(mode
, &fb_connector
->connector
->modes
, head
) {
1067 if (drm_mode_width(mode
) > width
||
1068 drm_mode_height(mode
) > height
)
1070 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
1076 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector
*fb_connector
)
1078 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
1079 cmdline_mode
= &fb_connector
->cmdline_mode
;
1080 return cmdline_mode
->specified
;
1083 static struct drm_display_mode
*drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
,
1084 int width
, int height
)
1086 struct drm_fb_helper_cmdline_mode
*cmdline_mode
;
1087 struct drm_display_mode
*mode
= NULL
;
1089 cmdline_mode
= &fb_helper_conn
->cmdline_mode
;
1090 if (cmdline_mode
->specified
== false)
1093 /* attempt to find a matching mode in the list of modes
1094 * we have gotten so far, if not add a CVT mode that conforms
1096 if (cmdline_mode
->rb
|| cmdline_mode
->margins
)
1099 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1100 /* check width/height */
1101 if (mode
->hdisplay
!= cmdline_mode
->xres
||
1102 mode
->vdisplay
!= cmdline_mode
->yres
)
1105 if (cmdline_mode
->refresh_specified
) {
1106 if (mode
->vrefresh
!= cmdline_mode
->refresh
)
1110 if (cmdline_mode
->interlace
) {
1111 if (!(mode
->flags
& DRM_MODE_FLAG_INTERLACE
))
1118 if (cmdline_mode
->cvt
)
1119 mode
= drm_cvt_mode(fb_helper_conn
->connector
->dev
,
1120 cmdline_mode
->xres
, cmdline_mode
->yres
,
1121 cmdline_mode
->refresh_specified
? cmdline_mode
->refresh
: 60,
1122 cmdline_mode
->rb
, cmdline_mode
->interlace
,
1123 cmdline_mode
->margins
);
1125 mode
= drm_gtf_mode(fb_helper_conn
->connector
->dev
,
1126 cmdline_mode
->xres
, cmdline_mode
->yres
,
1127 cmdline_mode
->refresh_specified
? cmdline_mode
->refresh
: 60,
1128 cmdline_mode
->interlace
,
1129 cmdline_mode
->margins
);
1130 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
1131 list_add(&mode
->head
, &fb_helper_conn
->connector
->modes
);
1135 static bool drm_connector_enabled(struct drm_connector
*connector
, bool strict
)
1140 enable
= connector
->status
== connector_status_connected
;
1142 enable
= connector
->status
!= connector_status_disconnected
;
1147 static void drm_enable_connectors(struct drm_fb_helper
*fb_helper
,
1150 bool any_enabled
= false;
1151 struct drm_connector
*connector
;
1154 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1155 connector
= fb_helper
->connector_info
[i
]->connector
;
1156 enabled
[i
] = drm_connector_enabled(connector
, true);
1157 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector
->base
.id
,
1158 enabled
[i
] ? "yes" : "no");
1159 any_enabled
|= enabled
[i
];
1165 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1166 connector
= fb_helper
->connector_info
[i
]->connector
;
1167 enabled
[i
] = drm_connector_enabled(connector
, false);
1171 static bool drm_target_cloned(struct drm_fb_helper
*fb_helper
,
1172 struct drm_display_mode
**modes
,
1173 bool *enabled
, int width
, int height
)
1176 bool can_clone
= false;
1177 struct drm_fb_helper_connector
*fb_helper_conn
;
1178 struct drm_display_mode
*dmt_mode
, *mode
;
1180 /* only contemplate cloning in the single crtc case */
1181 if (fb_helper
->crtc_count
> 1)
1185 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1190 /* only contemplate cloning if more than one connector is enabled */
1194 /* check the command line or if nothing common pick 1024x768 */
1196 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1199 fb_helper_conn
= fb_helper
->connector_info
[i
];
1200 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1205 for (j
= 0; j
< i
; j
++) {
1208 if (!drm_mode_equal(modes
[j
], modes
[i
]))
1214 DRM_DEBUG_KMS("can clone using command line\n");
1218 /* try and find a 1024x768 mode on each connector */
1220 dmt_mode
= drm_mode_find_dmt(fb_helper
->dev
, 1024, 768, 60);
1222 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1227 fb_helper_conn
= fb_helper
->connector_info
[i
];
1228 list_for_each_entry(mode
, &fb_helper_conn
->connector
->modes
, head
) {
1229 if (drm_mode_equal(mode
, dmt_mode
))
1237 DRM_DEBUG_KMS("can clone using 1024x768\n");
1240 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1244 static bool drm_target_preferred(struct drm_fb_helper
*fb_helper
,
1245 struct drm_display_mode
**modes
,
1246 bool *enabled
, int width
, int height
)
1248 struct drm_fb_helper_connector
*fb_helper_conn
;
1251 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1252 fb_helper_conn
= fb_helper
->connector_info
[i
];
1254 if (enabled
[i
] == false)
1257 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1258 fb_helper_conn
->connector
->base
.id
);
1260 /* got for command line mode first */
1261 modes
[i
] = drm_pick_cmdline_mode(fb_helper_conn
, width
, height
);
1263 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1264 fb_helper_conn
->connector
->base
.id
);
1265 modes
[i
] = drm_has_preferred_mode(fb_helper_conn
, width
, height
);
1267 /* No preferred modes, pick one off the list */
1268 if (!modes
[i
] && !list_empty(&fb_helper_conn
->connector
->modes
)) {
1269 list_for_each_entry(modes
[i
], &fb_helper_conn
->connector
->modes
, head
)
1272 DRM_DEBUG_KMS("found mode %s\n", modes
[i
] ? modes
[i
]->name
:
1278 static int drm_pick_crtcs(struct drm_fb_helper
*fb_helper
,
1279 struct drm_fb_helper_crtc
**best_crtcs
,
1280 struct drm_display_mode
**modes
,
1281 int n
, int width
, int height
)
1284 struct drm_device
*dev
= fb_helper
->dev
;
1285 struct drm_connector
*connector
;
1286 struct drm_connector_helper_funcs
*connector_funcs
;
1287 struct drm_encoder
*encoder
;
1288 struct drm_fb_helper_crtc
*best_crtc
;
1289 int my_score
, best_score
, score
;
1290 struct drm_fb_helper_crtc
**crtcs
, *crtc
;
1291 struct drm_fb_helper_connector
*fb_helper_conn
;
1293 if (n
== fb_helper
->connector_count
)
1296 fb_helper_conn
= fb_helper
->connector_info
[n
];
1297 connector
= fb_helper_conn
->connector
;
1299 best_crtcs
[n
] = NULL
;
1301 best_score
= drm_pick_crtcs(fb_helper
, best_crtcs
, modes
, n
+1, width
, height
);
1302 if (modes
[n
] == NULL
)
1305 crtcs
= kzalloc(dev
->mode_config
.num_connector
*
1306 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1311 if (connector
->status
== connector_status_connected
)
1313 if (drm_has_cmdline_mode(fb_helper_conn
))
1315 if (drm_has_preferred_mode(fb_helper_conn
, width
, height
))
1318 connector_funcs
= connector
->helper_private
;
1319 encoder
= connector_funcs
->best_encoder(connector
);
1323 /* select a crtc for this connector and then attempt to configure
1324 remaining connectors */
1325 for (c
= 0; c
< fb_helper
->crtc_count
; c
++) {
1326 crtc
= &fb_helper
->crtc_info
[c
];
1328 if ((encoder
->possible_crtcs
& (1 << c
)) == 0) {
1332 for (o
= 0; o
< n
; o
++)
1333 if (best_crtcs
[o
] == crtc
)
1337 /* ignore cloning unless only a single crtc */
1338 if (fb_helper
->crtc_count
> 1)
1341 if (!drm_mode_equal(modes
[o
], modes
[n
]))
1346 memcpy(crtcs
, best_crtcs
, n
* sizeof(struct drm_fb_helper_crtc
*));
1347 score
= my_score
+ drm_pick_crtcs(fb_helper
, crtcs
, modes
, n
+ 1,
1349 if (score
> best_score
) {
1352 memcpy(best_crtcs
, crtcs
,
1353 dev
->mode_config
.num_connector
*
1354 sizeof(struct drm_fb_helper_crtc
*));
1362 static void drm_setup_crtcs(struct drm_fb_helper
*fb_helper
)
1364 struct drm_device
*dev
= fb_helper
->dev
;
1365 struct drm_fb_helper_crtc
**crtcs
;
1366 struct drm_display_mode
**modes
;
1367 struct drm_encoder
*encoder
;
1368 struct drm_mode_set
*modeset
;
1373 DRM_DEBUG_KMS("\n");
1375 width
= dev
->mode_config
.max_width
;
1376 height
= dev
->mode_config
.max_height
;
1378 /* clean out all the encoder/crtc combos */
1379 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
1380 encoder
->crtc
= NULL
;
1383 crtcs
= kcalloc(dev
->mode_config
.num_connector
,
1384 sizeof(struct drm_fb_helper_crtc
*), GFP_KERNEL
);
1385 modes
= kcalloc(dev
->mode_config
.num_connector
,
1386 sizeof(struct drm_display_mode
*), GFP_KERNEL
);
1387 enabled
= kcalloc(dev
->mode_config
.num_connector
,
1388 sizeof(bool), GFP_KERNEL
);
1390 drm_enable_connectors(fb_helper
, enabled
);
1392 ret
= drm_target_cloned(fb_helper
, modes
, enabled
, width
, height
);
1394 ret
= drm_target_preferred(fb_helper
, modes
, enabled
, width
, height
);
1396 DRM_ERROR("Unable to find initial modes\n");
1399 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width
, height
);
1401 drm_pick_crtcs(fb_helper
, crtcs
, modes
, 0, width
, height
);
1403 /* need to set the modesets up here for use later */
1404 /* fill out the connector<->crtc mappings into the modesets */
1405 for (i
= 0; i
< fb_helper
->crtc_count
; i
++) {
1406 modeset
= &fb_helper
->crtc_info
[i
].mode_set
;
1407 modeset
->num_connectors
= 0;
1410 for (i
= 0; i
< fb_helper
->connector_count
; i
++) {
1411 struct drm_display_mode
*mode
= modes
[i
];
1412 struct drm_fb_helper_crtc
*fb_crtc
= crtcs
[i
];
1413 modeset
= &fb_crtc
->mode_set
;
1415 if (mode
&& fb_crtc
) {
1416 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1417 mode
->name
, fb_crtc
->mode_set
.crtc
->base
.id
);
1418 fb_crtc
->desired_mode
= mode
;
1420 drm_mode_destroy(dev
, modeset
->mode
);
1421 modeset
->mode
= drm_mode_duplicate(dev
,
1422 fb_crtc
->desired_mode
);
1423 modeset
->connectors
[modeset
->num_connectors
++] = fb_helper
->connector_info
[i
]->connector
;
1433 * drm_helper_initial_config - setup a sane initial connector configuration
1437 * Called at init time, must take mode config lock.
1439 * Scan the CRTCs and connectors and try to put together an initial setup.
1440 * At the moment, this is a cloned configuration across all heads with
1441 * a new framebuffer object as the backing store.
1444 * Zero if everything went ok, nonzero otherwise.
1446 bool drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
)
1448 struct drm_device
*dev
= fb_helper
->dev
;
1451 /* disable all the possible outputs/crtcs before entering KMS mode */
1452 drm_helper_disable_unused_functions(fb_helper
->dev
);
1454 drm_fb_helper_parse_command_line(fb_helper
);
1456 count
= drm_fb_helper_probe_connector_modes(fb_helper
,
1457 dev
->mode_config
.max_width
,
1458 dev
->mode_config
.max_height
);
1460 * we shouldn't end up with no modes here.
1463 printk(KERN_INFO
"No connectors reported connected with modes\n");
1465 drm_setup_crtcs(fb_helper
);
1467 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1469 EXPORT_SYMBOL(drm_fb_helper_initial_config
);
1471 bool drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
1474 u32 max_width
, max_height
, bpp_sel
;
1475 bool bound
= false, crtcs_bound
= false;
1476 struct drm_crtc
*crtc
;
1481 list_for_each_entry(crtc
, &fb_helper
->dev
->mode_config
.crtc_list
, head
) {
1484 if (crtc
->fb
== fb_helper
->fb
)
1488 if (!bound
&& crtcs_bound
) {
1489 fb_helper
->delayed_hotplug
= true;
1492 DRM_DEBUG_KMS("\n");
1494 max_width
= fb_helper
->fb
->width
;
1495 max_height
= fb_helper
->fb
->height
;
1496 bpp_sel
= fb_helper
->fb
->bits_per_pixel
;
1498 count
= drm_fb_helper_probe_connector_modes(fb_helper
, max_width
,
1500 drm_setup_crtcs(fb_helper
);
1502 return drm_fb_helper_single_fb_probe(fb_helper
, bpp_sel
);
1504 EXPORT_SYMBOL(drm_fb_helper_hotplug_event
);