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