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