Merge branch 'drm-radeon-evergreen-accel' into drm-core-next
[pandora-kernel.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
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.
17  *
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
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
33 #include <linux/fb.h>
34 #include "drmP.h"
35 #include "drm_crtc.h"
36 #include "drm_fb_helper.h"
37 #include "drm_crtc_helper.h"
38
39 MODULE_AUTHOR("David Airlie, Jesse Barnes");
40 MODULE_DESCRIPTION("DRM KMS helper");
41 MODULE_LICENSE("GPL and additional rights");
42
43 static LIST_HEAD(kernel_fb_helper_list);
44
45 static struct slow_work_ops output_status_change_ops;
46
47 /* simple single crtc case helper function */
48 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
49 {
50         struct drm_device *dev = fb_helper->dev;
51         struct drm_connector *connector;
52         int i;
53
54         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
55                 struct drm_fb_helper_connector *fb_helper_connector;
56
57                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
58                 if (!fb_helper_connector)
59                         goto fail;
60
61                 fb_helper_connector->connector = connector;
62                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
63         }
64         return 0;
65 fail:
66         for (i = 0; i < fb_helper->connector_count; i++) {
67                 kfree(fb_helper->connector_info[i]);
68                 fb_helper->connector_info[i] = NULL;
69         }
70         fb_helper->connector_count = 0;
71         return -ENOMEM;
72 }
73 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
74
75 /**
76  * drm_fb_helper_connector_parse_command_line - parse command line for connector
77  * @connector - connector to parse line for
78  * @mode_option - per connector mode option
79  *
80  * This parses the connector specific then generic command lines for
81  * modes and options to configure the connector.
82  *
83  * This uses the same parameters as the fb modedb.c, except for extra
84  *      <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
85  *
86  * enable/enable Digital/disable bit at the end
87  */
88 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
89                                                        const char *mode_option)
90 {
91         const char *name;
92         unsigned int namelen;
93         int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
94         unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
95         int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
96         int i;
97         enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
98         struct drm_fb_helper_cmdline_mode *cmdline_mode;
99         struct drm_connector *connector = fb_helper_conn->connector;
100
101         if (!fb_helper_conn)
102                 return false;
103
104         cmdline_mode = &fb_helper_conn->cmdline_mode;
105         if (!mode_option)
106                 mode_option = fb_mode_option;
107
108         if (!mode_option) {
109                 cmdline_mode->specified = false;
110                 return false;
111         }
112
113         name = mode_option;
114         namelen = strlen(name);
115         for (i = namelen-1; i >= 0; i--) {
116                 switch (name[i]) {
117                 case '@':
118                         namelen = i;
119                         if (!refresh_specified && !bpp_specified &&
120                             !yres_specified) {
121                                 refresh = simple_strtol(&name[i+1], NULL, 10);
122                                 refresh_specified = 1;
123                                 if (cvt || rb)
124                                         cvt = 0;
125                         } else
126                                 goto done;
127                         break;
128                 case '-':
129                         namelen = i;
130                         if (!bpp_specified && !yres_specified) {
131                                 bpp = simple_strtol(&name[i+1], NULL, 10);
132                                 bpp_specified = 1;
133                                 if (cvt || rb)
134                                         cvt = 0;
135                         } else
136                                 goto done;
137                         break;
138                 case 'x':
139                         if (!yres_specified) {
140                                 yres = simple_strtol(&name[i+1], NULL, 10);
141                                 yres_specified = 1;
142                         } else
143                                 goto done;
144                 case '0' ... '9':
145                         break;
146                 case 'M':
147                         if (!yres_specified)
148                                 cvt = 1;
149                         break;
150                 case 'R':
151                         if (!cvt)
152                                 rb = 1;
153                         break;
154                 case 'm':
155                         if (!cvt)
156                                 margins = 1;
157                         break;
158                 case 'i':
159                         if (!cvt)
160                                 interlace = 1;
161                         break;
162                 case 'e':
163                         force = DRM_FORCE_ON;
164                         break;
165                 case 'D':
166                         if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
167                             (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
168                                 force = DRM_FORCE_ON;
169                         else
170                                 force = DRM_FORCE_ON_DIGITAL;
171                         break;
172                 case 'd':
173                         force = DRM_FORCE_OFF;
174                         break;
175                 default:
176                         goto done;
177                 }
178         }
179         if (i < 0 && yres_specified) {
180                 xres = simple_strtol(name, NULL, 10);
181                 res_specified = 1;
182         }
183 done:
184
185         DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
186                 drm_get_connector_name(connector), xres, yres,
187                 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
188                 "", (margins) ? " with margins" : "", (interlace) ?
189                 " interlaced" : "");
190
191         if (force) {
192                 const char *s;
193                 switch (force) {
194                 case DRM_FORCE_OFF: s = "OFF"; break;
195                 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
196                 default:
197                 case DRM_FORCE_ON: s = "ON"; break;
198                 }
199
200                 DRM_INFO("forcing %s connector %s\n",
201                          drm_get_connector_name(connector), s);
202                 connector->force = force;
203         }
204
205         if (res_specified) {
206                 cmdline_mode->specified = true;
207                 cmdline_mode->xres = xres;
208                 cmdline_mode->yres = yres;
209         }
210
211         if (refresh_specified) {
212                 cmdline_mode->refresh_specified = true;
213                 cmdline_mode->refresh = refresh;
214         }
215
216         if (bpp_specified) {
217                 cmdline_mode->bpp_specified = true;
218                 cmdline_mode->bpp = bpp;
219         }
220         cmdline_mode->rb = rb ? true : false;
221         cmdline_mode->cvt = cvt  ? true : false;
222         cmdline_mode->interlace = interlace ? true : false;
223
224         return true;
225 }
226
227 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
228 {
229         struct drm_fb_helper_connector *fb_helper_conn;
230         int i;
231
232         for (i = 0; i < fb_helper->connector_count; i++) {
233                 char *option = NULL;
234
235                 fb_helper_conn = fb_helper->connector_info[i];
236
237                 /* do something on return - turn off connector maybe */
238                 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
239                         continue;
240
241                 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
242         }
243         return 0;
244 }
245
246 bool drm_fb_helper_force_kernel_mode(void)
247 {
248         int i = 0;
249         bool ret, error = false;
250         struct drm_fb_helper *helper;
251
252         if (list_empty(&kernel_fb_helper_list))
253                 return false;
254
255         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
256                 for (i = 0; i < helper->crtc_count; i++) {
257                         struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
258                         ret = drm_crtc_helper_set_config(mode_set);
259                         if (ret)
260                                 error = true;
261                 }
262         }
263         return error;
264 }
265
266 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267                         void *panic_str)
268 {
269         DRM_ERROR("panic occurred, switching back to text console\n");
270         return drm_fb_helper_force_kernel_mode();
271         return 0;
272 }
273 EXPORT_SYMBOL(drm_fb_helper_panic);
274
275 static struct notifier_block paniced = {
276         .notifier_call = drm_fb_helper_panic,
277 };
278
279 /**
280  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
281  *
282  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
283  */
284 void drm_fb_helper_restore(void)
285 {
286         bool ret;
287         ret = drm_fb_helper_force_kernel_mode();
288         if (ret == true)
289                 DRM_ERROR("Failed to restore crtc configuration\n");
290 }
291 EXPORT_SYMBOL(drm_fb_helper_restore);
292
293 #ifdef CONFIG_MAGIC_SYSRQ
294 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
295 {
296         drm_fb_helper_restore();
297 }
298 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
299
300 static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
301 {
302         schedule_work(&drm_fb_helper_restore_work);
303 }
304
305 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
306         .handler = drm_fb_helper_sysrq,
307         .help_msg = "force-fb(V)",
308         .action_msg = "Restore framebuffer console",
309 };
310 #else
311 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
312 #endif
313
314 static void drm_fb_helper_on(struct fb_info *info)
315 {
316         struct drm_fb_helper *fb_helper = info->par;
317         struct drm_device *dev = fb_helper->dev;
318         struct drm_crtc *crtc;
319         struct drm_crtc_helper_funcs *crtc_funcs;
320         struct drm_encoder *encoder;
321         int i;
322
323         /*
324          * For each CRTC in this fb, turn the crtc on then,
325          * find all associated encoders and turn them on.
326          */
327         mutex_lock(&dev->mode_config.mutex);
328         for (i = 0; i < fb_helper->crtc_count; i++) {
329                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
330                 crtc_funcs = crtc->helper_private;
331
332                 if (!crtc->enabled)
333                         continue;
334
335                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
336
337
338                 /* Found a CRTC on this fb, now find encoders */
339                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
340                         if (encoder->crtc == crtc) {
341                                 struct drm_encoder_helper_funcs *encoder_funcs;
342
343                                 encoder_funcs = encoder->helper_private;
344                                 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
345                         }
346                 }
347         }
348         mutex_unlock(&dev->mode_config.mutex);
349 }
350
351 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
352 {
353         struct drm_fb_helper *fb_helper = info->par;
354         struct drm_device *dev = fb_helper->dev;
355         struct drm_crtc *crtc;
356         struct drm_crtc_helper_funcs *crtc_funcs;
357         struct drm_encoder *encoder;
358         int i;
359
360         /*
361          * For each CRTC in this fb, find all associated encoders
362          * and turn them off, then turn off the CRTC.
363          */
364         mutex_lock(&dev->mode_config.mutex);
365         for (i = 0; i < fb_helper->crtc_count; i++) {
366                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
367                 crtc_funcs = crtc->helper_private;
368
369                 if (!crtc->enabled)
370                         continue;
371
372                 /* Found a CRTC on this fb, now find encoders */
373                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
374                         if (encoder->crtc == crtc) {
375                                 struct drm_encoder_helper_funcs *encoder_funcs;
376
377                                 encoder_funcs = encoder->helper_private;
378                                 encoder_funcs->dpms(encoder, dpms_mode);
379                         }
380                 }
381                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
382         }
383         mutex_unlock(&dev->mode_config.mutex);
384 }
385
386 int drm_fb_helper_blank(int blank, struct fb_info *info)
387 {
388         switch (blank) {
389         /* Display: On; HSync: On, VSync: On */
390         case FB_BLANK_UNBLANK:
391                 drm_fb_helper_on(info);
392                 break;
393         /* Display: Off; HSync: On, VSync: On */
394         case FB_BLANK_NORMAL:
395                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
396                 break;
397         /* Display: Off; HSync: Off, VSync: On */
398         case FB_BLANK_HSYNC_SUSPEND:
399                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
400                 break;
401         /* Display: Off; HSync: On, VSync: Off */
402         case FB_BLANK_VSYNC_SUSPEND:
403                 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
404                 break;
405         /* Display: Off; HSync: Off, VSync: Off */
406         case FB_BLANK_POWERDOWN:
407                 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
408                 break;
409         }
410         return 0;
411 }
412 EXPORT_SYMBOL(drm_fb_helper_blank);
413
414 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
415 {
416         int i;
417
418         for (i = 0; i < helper->connector_count; i++)
419                 kfree(helper->connector_info[i]);
420         kfree(helper->connector_info);
421         for (i = 0; i < helper->crtc_count; i++)
422                 kfree(helper->crtc_info[i].mode_set.connectors);
423         kfree(helper->crtc_info);
424 }
425
426 int drm_fb_helper_init(struct drm_device *dev,
427                        struct drm_fb_helper *fb_helper,
428                        int crtc_count, int max_conn_count,
429                        bool polled)
430 {
431         struct drm_crtc *crtc;
432         int ret = 0;
433         int i;
434
435         fb_helper->dev = dev;
436         fb_helper->poll_enabled = polled;
437
438         slow_work_register_user(THIS_MODULE);
439         delayed_slow_work_init(&fb_helper->output_status_change_slow_work,
440                                &output_status_change_ops);
441
442         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
443
444         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
445         if (!fb_helper->crtc_info)
446                 return -ENOMEM;
447
448         fb_helper->crtc_count = crtc_count;
449         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
450         if (!fb_helper->connector_info) {
451                 kfree(fb_helper->crtc_info);
452                 return -ENOMEM;
453         }
454         fb_helper->connector_count = 0;
455
456         for (i = 0; i < crtc_count; i++) {
457                 fb_helper->crtc_info[i].mode_set.connectors =
458                         kcalloc(max_conn_count,
459                                 sizeof(struct drm_connector *),
460                                 GFP_KERNEL);
461
462                 if (!fb_helper->crtc_info[i].mode_set.connectors) {
463                         ret = -ENOMEM;
464                         goto out_free;
465                 }
466                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
467         }
468
469         i = 0;
470         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
471                 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
472                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
473                 i++;
474         }
475         fb_helper->conn_limit = max_conn_count;
476         return 0;
477 out_free:
478         drm_fb_helper_crtc_free(fb_helper);
479         return -ENOMEM;
480 }
481 EXPORT_SYMBOL(drm_fb_helper_init);
482
483 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
484 {
485         if (!list_empty(&fb_helper->kernel_fb_list)) {
486                 list_del(&fb_helper->kernel_fb_list);
487                 if (list_empty(&kernel_fb_helper_list)) {
488                         printk(KERN_INFO "unregistered panic notifier\n");
489                         atomic_notifier_chain_unregister(&panic_notifier_list,
490                                                          &paniced);
491                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
492                 }
493         }
494
495         drm_fb_helper_crtc_free(fb_helper);
496
497         delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
498         slow_work_unregister_user(THIS_MODULE);
499 }
500 EXPORT_SYMBOL(drm_fb_helper_fini);
501
502 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
503                      u16 blue, u16 regno, struct fb_info *info)
504 {
505         struct drm_fb_helper *fb_helper = info->par;
506         struct drm_framebuffer *fb = fb_helper->fb;
507         int pindex;
508
509         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
510                 u32 *palette;
511                 u32 value;
512                 /* place color in psuedopalette */
513                 if (regno > 16)
514                         return -EINVAL;
515                 palette = (u32 *)info->pseudo_palette;
516                 red >>= (16 - info->var.red.length);
517                 green >>= (16 - info->var.green.length);
518                 blue >>= (16 - info->var.blue.length);
519                 value = (red << info->var.red.offset) |
520                         (green << info->var.green.offset) |
521                         (blue << info->var.blue.offset);
522                 palette[regno] = value;
523                 return 0;
524         }
525
526         pindex = regno;
527
528         if (fb->bits_per_pixel == 16) {
529                 pindex = regno << 3;
530
531                 if (fb->depth == 16 && regno > 63)
532                         return -EINVAL;
533                 if (fb->depth == 15 && regno > 31)
534                         return -EINVAL;
535
536                 if (fb->depth == 16) {
537                         u16 r, g, b;
538                         int i;
539                         if (regno < 32) {
540                                 for (i = 0; i < 8; i++)
541                                         fb_helper->funcs->gamma_set(crtc, red,
542                                                 green, blue, pindex + i);
543                         }
544
545                         fb_helper->funcs->gamma_get(crtc, &r,
546                                                     &g, &b,
547                                                     pindex >> 1);
548
549                         for (i = 0; i < 4; i++)
550                                 fb_helper->funcs->gamma_set(crtc, r,
551                                                             green, b,
552                                                             (pindex >> 1) + i);
553                 }
554         }
555
556         if (fb->depth != 16)
557                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
558         return 0;
559 }
560
561 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
562 {
563         struct drm_fb_helper *fb_helper = info->par;
564         struct drm_crtc_helper_funcs *crtc_funcs;
565         u16 *red, *green, *blue, *transp;
566         struct drm_crtc *crtc;
567         int i, rc = 0;
568         int start;
569
570         for (i = 0; i < fb_helper->crtc_count; i++) {
571                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
572                 crtc_funcs = crtc->helper_private;
573
574                 red = cmap->red;
575                 green = cmap->green;
576                 blue = cmap->blue;
577                 transp = cmap->transp;
578                 start = cmap->start;
579
580                 for (i = 0; i < cmap->len; i++) {
581                         u16 hred, hgreen, hblue, htransp = 0xffff;
582
583                         hred = *red++;
584                         hgreen = *green++;
585                         hblue = *blue++;
586
587                         if (transp)
588                                 htransp = *transp++;
589
590                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
591                         if (rc)
592                                 return rc;
593                 }
594                 crtc_funcs->load_lut(crtc);
595         }
596         return rc;
597 }
598 EXPORT_SYMBOL(drm_fb_helper_setcmap);
599
600 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
601                             struct fb_info *info)
602 {
603         struct drm_fb_helper *fb_helper = info->par;
604         struct drm_framebuffer *fb = fb_helper->fb;
605         int depth;
606
607         if (var->pixclock != 0)
608                 return -EINVAL;
609
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);
615                 return -EINVAL;
616         }
617
618         switch (var->bits_per_pixel) {
619         case 16:
620                 depth = (var->green.length == 6) ? 16 : 15;
621                 break;
622         case 32:
623                 depth = (var->transp.length > 0) ? 32 : 24;
624                 break;
625         default:
626                 depth = var->bits_per_pixel;
627                 break;
628         }
629
630         switch (depth) {
631         case 8:
632                 var->red.offset = 0;
633                 var->green.offset = 0;
634                 var->blue.offset = 0;
635                 var->red.length = 8;
636                 var->green.length = 8;
637                 var->blue.length = 8;
638                 var->transp.length = 0;
639                 var->transp.offset = 0;
640                 break;
641         case 15:
642                 var->red.offset = 10;
643                 var->green.offset = 5;
644                 var->blue.offset = 0;
645                 var->red.length = 5;
646                 var->green.length = 5;
647                 var->blue.length = 5;
648                 var->transp.length = 1;
649                 var->transp.offset = 15;
650                 break;
651         case 16:
652                 var->red.offset = 11;
653                 var->green.offset = 5;
654                 var->blue.offset = 0;
655                 var->red.length = 5;
656                 var->green.length = 6;
657                 var->blue.length = 5;
658                 var->transp.length = 0;
659                 var->transp.offset = 0;
660                 break;
661         case 24:
662                 var->red.offset = 16;
663                 var->green.offset = 8;
664                 var->blue.offset = 0;
665                 var->red.length = 8;
666                 var->green.length = 8;
667                 var->blue.length = 8;
668                 var->transp.length = 0;
669                 var->transp.offset = 0;
670                 break;
671         case 32:
672                 var->red.offset = 16;
673                 var->green.offset = 8;
674                 var->blue.offset = 0;
675                 var->red.length = 8;
676                 var->green.length = 8;
677                 var->blue.length = 8;
678                 var->transp.length = 8;
679                 var->transp.offset = 24;
680                 break;
681         default:
682                 return -EINVAL;
683         }
684         return 0;
685 }
686 EXPORT_SYMBOL(drm_fb_helper_check_var);
687
688 /* this will let fbcon do the mode init */
689 int drm_fb_helper_set_par(struct fb_info *info)
690 {
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;
695         int ret;
696         int i;
697
698         if (var->pixclock != 0) {
699                 DRM_ERROR("PIXEL CLOCK SET\n");
700                 return -EINVAL;
701         }
702
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);
707                 if (ret) {
708                         mutex_unlock(&dev->mode_config.mutex);
709                         return ret;
710                 }
711         }
712         mutex_unlock(&dev->mode_config.mutex);
713
714         if (fb_helper->delayed_hotplug) {
715                 fb_helper->delayed_hotplug = false;
716                 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
717         }
718         return 0;
719 }
720 EXPORT_SYMBOL(drm_fb_helper_set_par);
721
722 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
723                               struct fb_info *info)
724 {
725         struct drm_fb_helper *fb_helper = info->par;
726         struct drm_device *dev = fb_helper->dev;
727         struct drm_mode_set *modeset;
728         struct drm_crtc *crtc;
729         int ret = 0;
730         int i;
731
732         mutex_lock(&dev->mode_config.mutex);
733         for (i = 0; i < fb_helper->crtc_count; i++) {
734                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
735
736                 modeset = &fb_helper->crtc_info[i].mode_set;
737
738                 modeset->x = var->xoffset;
739                 modeset->y = var->yoffset;
740
741                 if (modeset->num_connectors) {
742                         ret = crtc->funcs->set_config(modeset);
743                         if (!ret) {
744                                 info->var.xoffset = var->xoffset;
745                                 info->var.yoffset = var->yoffset;
746                         }
747                 }
748         }
749         mutex_unlock(&dev->mode_config.mutex);
750         return ret;
751 }
752 EXPORT_SYMBOL(drm_fb_helper_pan_display);
753
754 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
755                                   int preferred_bpp)
756 {
757         int new_fb = 0;
758         int crtc_count = 0;
759         int i;
760         struct fb_info *info;
761         struct drm_fb_helper_surface_size sizes;
762         int gamma_size = 0;
763
764         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
765         sizes.surface_depth = 24;
766         sizes.surface_bpp = 32;
767         sizes.fb_width = (unsigned)-1;
768         sizes.fb_height = (unsigned)-1;
769
770         /* if driver picks 8 or 16 by default use that
771            for both depth/bpp */
772         if (preferred_bpp != sizes.surface_bpp) {
773                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
774         }
775         /* first up get a count of crtcs now in use and new min/maxes width/heights */
776         for (i = 0; i < fb_helper->connector_count; i++) {
777                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
778                 struct drm_fb_helper_cmdline_mode *cmdline_mode;
779
780                 cmdline_mode = &fb_helper_conn->cmdline_mode;
781
782                 if (cmdline_mode->bpp_specified) {
783                         switch (cmdline_mode->bpp) {
784                         case 8:
785                                 sizes.surface_depth = sizes.surface_bpp = 8;
786                                 break;
787                         case 15:
788                                 sizes.surface_depth = 15;
789                                 sizes.surface_bpp = 16;
790                                 break;
791                         case 16:
792                                 sizes.surface_depth = sizes.surface_bpp = 16;
793                                 break;
794                         case 24:
795                                 sizes.surface_depth = sizes.surface_bpp = 24;
796                                 break;
797                         case 32:
798                                 sizes.surface_depth = 24;
799                                 sizes.surface_bpp = 32;
800                                 break;
801                         }
802                         break;
803                 }
804         }
805
806         crtc_count = 0;
807         for (i = 0; i < fb_helper->crtc_count; i++) {
808                 struct drm_display_mode *desired_mode;
809                 desired_mode = fb_helper->crtc_info[i].desired_mode;
810
811                 if (desired_mode) {
812                         if (gamma_size == 0)
813                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
814                         if (desired_mode->hdisplay < sizes.fb_width)
815                                 sizes.fb_width = desired_mode->hdisplay;
816                         if (desired_mode->vdisplay < sizes.fb_height)
817                                 sizes.fb_height = desired_mode->vdisplay;
818                         if (desired_mode->hdisplay > sizes.surface_width)
819                                 sizes.surface_width = desired_mode->hdisplay;
820                         if (desired_mode->vdisplay > sizes.surface_height)
821                                 sizes.surface_height = desired_mode->vdisplay;
822                         crtc_count++;
823                 }
824         }
825
826         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
827                 /* hmm everyone went away - assume VGA cable just fell out
828                    and will come back later. */
829                 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
830                 sizes.fb_width = sizes.surface_width = 1024;
831                 sizes.fb_height = sizes.surface_height = 768;
832         }
833
834         /* push down into drivers */
835         new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
836         if (new_fb < 0)
837                 return new_fb;
838
839         info = fb_helper->fbdev;
840
841         /* set the fb pointer */
842         for (i = 0; i < fb_helper->crtc_count; i++) {
843                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
844         }
845
846         if (new_fb) {
847                 info->var.pixclock = 0;
848                 if (register_framebuffer(info) < 0) {
849                         return -EINVAL;
850                 }
851
852                 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
853                        info->fix.id);
854
855         } else {
856                 drm_fb_helper_set_par(info);
857         }
858
859         /* Switch back to kernel console on panic */
860         /* multi card linked list maybe */
861         if (list_empty(&kernel_fb_helper_list)) {
862                 printk(KERN_INFO "registered panic notifier\n");
863                 atomic_notifier_chain_register(&panic_notifier_list,
864                                                &paniced);
865                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
866         }
867         if (new_fb)
868                 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
869
870         return 0;
871 }
872 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
873
874 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
875                             uint32_t depth)
876 {
877         info->fix.type = FB_TYPE_PACKED_PIXELS;
878         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
879                 FB_VISUAL_TRUECOLOR;
880         info->fix.type_aux = 0;
881         info->fix.xpanstep = 1; /* doing it in hw */
882         info->fix.ypanstep = 1; /* doing it in hw */
883         info->fix.ywrapstep = 0;
884         info->fix.accel = FB_ACCEL_NONE;
885         info->fix.type_aux = 0;
886
887         info->fix.line_length = pitch;
888         return;
889 }
890 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
891
892 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
893                             uint32_t fb_width, uint32_t fb_height)
894 {
895         struct drm_framebuffer *fb = fb_helper->fb;
896         info->pseudo_palette = fb_helper->pseudo_palette;
897         info->var.xres_virtual = fb->width;
898         info->var.yres_virtual = fb->height;
899         info->var.bits_per_pixel = fb->bits_per_pixel;
900         info->var.xoffset = 0;
901         info->var.yoffset = 0;
902         info->var.activate = FB_ACTIVATE_NOW;
903         info->var.height = -1;
904         info->var.width = -1;
905
906         switch (fb->depth) {
907         case 8:
908                 info->var.red.offset = 0;
909                 info->var.green.offset = 0;
910                 info->var.blue.offset = 0;
911                 info->var.red.length = 8; /* 8bit DAC */
912                 info->var.green.length = 8;
913                 info->var.blue.length = 8;
914                 info->var.transp.offset = 0;
915                 info->var.transp.length = 0;
916                 break;
917         case 15:
918                 info->var.red.offset = 10;
919                 info->var.green.offset = 5;
920                 info->var.blue.offset = 0;
921                 info->var.red.length = 5;
922                 info->var.green.length = 5;
923                 info->var.blue.length = 5;
924                 info->var.transp.offset = 15;
925                 info->var.transp.length = 1;
926                 break;
927         case 16:
928                 info->var.red.offset = 11;
929                 info->var.green.offset = 5;
930                 info->var.blue.offset = 0;
931                 info->var.red.length = 5;
932                 info->var.green.length = 6;
933                 info->var.blue.length = 5;
934                 info->var.transp.offset = 0;
935                 break;
936         case 24:
937                 info->var.red.offset = 16;
938                 info->var.green.offset = 8;
939                 info->var.blue.offset = 0;
940                 info->var.red.length = 8;
941                 info->var.green.length = 8;
942                 info->var.blue.length = 8;
943                 info->var.transp.offset = 0;
944                 info->var.transp.length = 0;
945                 break;
946         case 32:
947                 info->var.red.offset = 16;
948                 info->var.green.offset = 8;
949                 info->var.blue.offset = 0;
950                 info->var.red.length = 8;
951                 info->var.green.length = 8;
952                 info->var.blue.length = 8;
953                 info->var.transp.offset = 24;
954                 info->var.transp.length = 8;
955                 break;
956         default:
957                 break;
958         }
959
960         info->var.xres = fb_width;
961         info->var.yres = fb_height;
962 }
963 EXPORT_SYMBOL(drm_fb_helper_fill_var);
964
965 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
966                                                uint32_t maxX,
967                                                uint32_t maxY)
968 {
969         struct drm_connector *connector;
970         int count = 0;
971         int i;
972
973         for (i = 0; i < fb_helper->connector_count; i++) {
974                 connector = fb_helper->connector_info[i]->connector;
975                 count += connector->funcs->fill_modes(connector, maxX, maxY);
976         }
977
978         return count;
979 }
980
981 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
982 {
983         struct drm_display_mode *mode;
984
985         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
986                 if (drm_mode_width(mode) > width ||
987                     drm_mode_height(mode) > height)
988                         continue;
989                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
990                         return mode;
991         }
992         return NULL;
993 }
994
995 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
996 {
997         struct drm_fb_helper_cmdline_mode *cmdline_mode;
998         cmdline_mode = &fb_connector->cmdline_mode;
999         return cmdline_mode->specified;
1000 }
1001
1002 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1003                                                       int width, int height)
1004 {
1005         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1006         struct drm_display_mode *mode = NULL;
1007
1008         cmdline_mode = &fb_helper_conn->cmdline_mode;
1009         if (cmdline_mode->specified == false)
1010                 return mode;
1011
1012         /* attempt to find a matching mode in the list of modes
1013          *  we have gotten so far, if not add a CVT mode that conforms
1014          */
1015         if (cmdline_mode->rb || cmdline_mode->margins)
1016                 goto create_mode;
1017
1018         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1019                 /* check width/height */
1020                 if (mode->hdisplay != cmdline_mode->xres ||
1021                     mode->vdisplay != cmdline_mode->yres)
1022                         continue;
1023
1024                 if (cmdline_mode->refresh_specified) {
1025                         if (mode->vrefresh != cmdline_mode->refresh)
1026                                 continue;
1027                 }
1028
1029                 if (cmdline_mode->interlace) {
1030                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1031                                 continue;
1032                 }
1033                 return mode;
1034         }
1035
1036 create_mode:
1037         mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
1038                             cmdline_mode->yres,
1039                             cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1040                             cmdline_mode->rb, cmdline_mode->interlace,
1041                             cmdline_mode->margins);
1042         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1043         list_add(&mode->head, &fb_helper_conn->connector->modes);
1044         return mode;
1045 }
1046
1047 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1048 {
1049         bool enable;
1050
1051         if (strict) {
1052                 enable = connector->status == connector_status_connected;
1053         } else {
1054                 enable = connector->status != connector_status_disconnected;
1055         }
1056         return enable;
1057 }
1058
1059 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1060                                   bool *enabled)
1061 {
1062         bool any_enabled = false;
1063         struct drm_connector *connector;
1064         int i = 0;
1065
1066         for (i = 0; i < fb_helper->connector_count; i++) {
1067                 connector = fb_helper->connector_info[i]->connector;
1068                 enabled[i] = drm_connector_enabled(connector, true);
1069                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1070                           enabled[i] ? "yes" : "no");
1071                 any_enabled |= enabled[i];
1072         }
1073
1074         if (any_enabled)
1075                 return;
1076
1077         for (i = 0; i < fb_helper->connector_count; i++) {
1078                 connector = fb_helper->connector_info[i]->connector;
1079                 enabled[i] = drm_connector_enabled(connector, false);
1080         }
1081 }
1082
1083 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1084                                  struct drm_display_mode **modes,
1085                                  bool *enabled, int width, int height)
1086 {
1087         struct drm_fb_helper_connector *fb_helper_conn;
1088         int i;
1089
1090         for (i = 0; i < fb_helper->connector_count; i++) {
1091                 fb_helper_conn = fb_helper->connector_info[i];
1092
1093                 if (enabled[i] == false)
1094                         continue;
1095
1096                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1097                               fb_helper_conn->connector->base.id);
1098
1099                 /* got for command line mode first */
1100                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1101                 if (!modes[i]) {
1102                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1103                                       fb_helper_conn->connector->base.id);
1104                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1105                 }
1106                 /* No preferred modes, pick one off the list */
1107                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1108                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1109                                 break;
1110                 }
1111                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1112                           "none");
1113         }
1114         return true;
1115 }
1116
1117 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1118                           struct drm_fb_helper_crtc **best_crtcs,
1119                           struct drm_display_mode **modes,
1120                           int n, int width, int height)
1121 {
1122         int c, o;
1123         struct drm_device *dev = fb_helper->dev;
1124         struct drm_connector *connector;
1125         struct drm_connector_helper_funcs *connector_funcs;
1126         struct drm_encoder *encoder;
1127         struct drm_fb_helper_crtc *best_crtc;
1128         int my_score, best_score, score;
1129         struct drm_fb_helper_crtc **crtcs, *crtc;
1130         struct drm_fb_helper_connector *fb_helper_conn;
1131
1132         if (n == fb_helper->connector_count)
1133                 return 0;
1134
1135         fb_helper_conn = fb_helper->connector_info[n];
1136         connector = fb_helper_conn->connector;
1137
1138         best_crtcs[n] = NULL;
1139         best_crtc = NULL;
1140         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1141         if (modes[n] == NULL)
1142                 return best_score;
1143
1144         crtcs = kzalloc(dev->mode_config.num_connector *
1145                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1146         if (!crtcs)
1147                 return best_score;
1148
1149         my_score = 1;
1150         if (connector->status == connector_status_connected)
1151                 my_score++;
1152         if (drm_has_cmdline_mode(fb_helper_conn))
1153                 my_score++;
1154         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1155                 my_score++;
1156
1157         connector_funcs = connector->helper_private;
1158         encoder = connector_funcs->best_encoder(connector);
1159         if (!encoder)
1160                 goto out;
1161
1162         /* select a crtc for this connector and then attempt to configure
1163            remaining connectors */
1164         for (c = 0; c < fb_helper->crtc_count; c++) {
1165                 crtc = &fb_helper->crtc_info[c];
1166
1167                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1168                         continue;
1169                 }
1170
1171                 for (o = 0; o < n; o++)
1172                         if (best_crtcs[o] == crtc)
1173                                 break;
1174
1175                 if (o < n) {
1176                         /* ignore cloning for now */
1177                         continue;
1178                 }
1179
1180                 crtcs[n] = crtc;
1181                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1182                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1183                                                   width, height);
1184                 if (score > best_score) {
1185                         best_crtc = crtc;
1186                         best_score = score;
1187                         memcpy(best_crtcs, crtcs,
1188                                dev->mode_config.num_connector *
1189                                sizeof(struct drm_fb_helper_crtc *));
1190                 }
1191         }
1192 out:
1193         kfree(crtcs);
1194         return best_score;
1195 }
1196
1197 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1198 {
1199         struct drm_device *dev = fb_helper->dev;
1200         struct drm_fb_helper_crtc **crtcs;
1201         struct drm_display_mode **modes;
1202         struct drm_encoder *encoder;
1203         struct drm_mode_set *modeset;
1204         bool *enabled;
1205         int width, height;
1206         int i, ret;
1207
1208         DRM_DEBUG_KMS("\n");
1209
1210         width = dev->mode_config.max_width;
1211         height = dev->mode_config.max_height;
1212
1213         /* clean out all the encoder/crtc combos */
1214         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1215                 encoder->crtc = NULL;
1216         }
1217
1218         crtcs = kcalloc(dev->mode_config.num_connector,
1219                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1220         modes = kcalloc(dev->mode_config.num_connector,
1221                         sizeof(struct drm_display_mode *), GFP_KERNEL);
1222         enabled = kcalloc(dev->mode_config.num_connector,
1223                           sizeof(bool), GFP_KERNEL);
1224
1225         drm_enable_connectors(fb_helper, enabled);
1226
1227         ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1228         if (!ret)
1229                 DRM_ERROR("Unable to find initial modes\n");
1230
1231         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1232
1233         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1234
1235         /* need to set the modesets up here for use later */
1236         /* fill out the connector<->crtc mappings into the modesets */
1237         for (i = 0; i < fb_helper->crtc_count; i++) {
1238                 modeset = &fb_helper->crtc_info[i].mode_set;
1239                 modeset->num_connectors = 0;
1240         }
1241
1242         for (i = 0; i < fb_helper->connector_count; i++) {
1243                 struct drm_display_mode *mode = modes[i];
1244                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1245                 modeset = &fb_crtc->mode_set;
1246
1247                 if (mode && fb_crtc) {
1248                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1249                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1250                         fb_crtc->desired_mode = mode;
1251                         if (modeset->mode)
1252                                 drm_mode_destroy(dev, modeset->mode);
1253                         modeset->mode = drm_mode_duplicate(dev,
1254                                                            fb_crtc->desired_mode);
1255                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1256                 }
1257         }
1258
1259         kfree(crtcs);
1260         kfree(modes);
1261         kfree(enabled);
1262 }
1263
1264 /**
1265  * drm_helper_initial_config - setup a sane initial connector configuration
1266  * @dev: DRM device
1267  *
1268  * LOCKING:
1269  * Called at init time, must take mode config lock.
1270  *
1271  * Scan the CRTCs and connectors and try to put together an initial setup.
1272  * At the moment, this is a cloned configuration across all heads with
1273  * a new framebuffer object as the backing store.
1274  *
1275  * RETURNS:
1276  * Zero if everything went ok, nonzero otherwise.
1277  */
1278 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1279 {
1280         struct drm_device *dev = fb_helper->dev;
1281         int count = 0;
1282
1283         /* disable all the possible outputs/crtcs before entering KMS mode */
1284         drm_helper_disable_unused_functions(fb_helper->dev);
1285
1286         drm_fb_helper_parse_command_line(fb_helper);
1287
1288         count = drm_fb_helper_probe_connector_modes(fb_helper,
1289                                                     dev->mode_config.max_width,
1290                                                     dev->mode_config.max_height);
1291         /*
1292          * we shouldn't end up with no modes here.
1293          */
1294         if (count == 0) {
1295                 if (fb_helper->poll_enabled) {
1296                         delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work,
1297                                                   5*HZ);
1298                         printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1299                 } else
1300                         printk(KERN_INFO "No connectors reported connected with modes\n");
1301         }
1302         drm_setup_crtcs(fb_helper);
1303
1304         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1305 }
1306 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1307
1308 /* we got a hotplug irq - need to update fbcon */
1309 void drm_helper_fb_hpd_irq_event(struct drm_fb_helper *fb_helper)
1310 {
1311         /* if we don't have the fbdev registered yet do nothing */
1312         if (!fb_helper->fbdev)
1313                 return;
1314
1315         /* schedule a slow work asap */
1316         delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
1317 }
1318 EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event);
1319
1320 bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, bool polled)
1321 {
1322         int count = 0;
1323         int ret;
1324         u32 max_width, max_height, bpp_sel;
1325
1326         if (!fb_helper->fb)
1327                 return false;
1328         DRM_DEBUG_KMS("\n");
1329
1330         max_width = fb_helper->fb->width;
1331         max_height = fb_helper->fb->height;
1332         bpp_sel = fb_helper->fb->bits_per_pixel;
1333
1334         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1335                                                     max_height);
1336         if (fb_helper->poll_enabled && !polled) {
1337                 if (count) {
1338                         delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
1339                 } else {
1340                         ret = delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 5*HZ);
1341                 }
1342         }
1343         drm_setup_crtcs(fb_helper);
1344
1345         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1346 }
1347 EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
1348
1349 /*
1350  * delayed work queue execution function
1351  * - check if fbdev is actually in use on the gpu
1352  *   - if not set delayed flag and repoll if necessary
1353  * - check for connector status change
1354  * - repoll if 0 modes found
1355  *- call driver output status changed notifier
1356  */
1357 static void output_status_change_execute(struct slow_work *work)
1358 {
1359         struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
1360         struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_status_change_slow_work);
1361         struct drm_connector *connector;
1362         enum drm_connector_status old_status, status;
1363         bool repoll, changed = false;
1364         int ret;
1365         int i;
1366         bool bound = false, crtcs_bound = false;
1367         struct drm_crtc *crtc;
1368
1369         repoll = fb_helper->poll_enabled;
1370
1371         /* first of all check the fbcon framebuffer is actually bound to any crtc */
1372         /* take into account that no crtc at all maybe bound */
1373         list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1374                 if (crtc->fb)
1375                         crtcs_bound = true;
1376                 if (crtc->fb == fb_helper->fb)
1377                         bound = true;
1378         }
1379
1380         if (bound == false && crtcs_bound) {
1381                 fb_helper->delayed_hotplug = true;
1382                 goto requeue;
1383         }
1384
1385         for (i = 0; i < fb_helper->connector_count; i++) {
1386                 connector = fb_helper->connector_info[i]->connector;
1387                 old_status = connector->status;
1388                 status = connector->funcs->detect(connector);
1389                 if (old_status != status) {
1390                         changed = true;
1391                 }
1392                 if (status == connector_status_connected && repoll) {
1393                         DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1394                         repoll = false;
1395                 }
1396         }
1397
1398         if (changed) {
1399                 if (fb_helper->funcs->fb_output_status_changed)
1400                         fb_helper->funcs->fb_output_status_changed(fb_helper);
1401         }
1402
1403 requeue:
1404         if (repoll) {
1405                 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1406                 if (ret)
1407                         DRM_ERROR("delayed enqueue failed %d\n", ret);
1408         }
1409 }
1410
1411 static struct slow_work_ops output_status_change_ops = {
1412         .execute = output_status_change_execute,
1413 };
1414