]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/drm_fb_helper.c
625a2d551d6a3db6c963eb65b759e1502339c7fd
[mv-sheeva.git] / drivers / gpu / drm / drm_fb_helper.c
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #include <linux/kernel.h>
31 #include <linux/sysrq.h>
32 #include <linux/slab.h>
33 #include <linux/fb.h>
34 #include "drmP.h"
35 #include "drm_crtc.h"
36 #include "drm_fb_helper.h"
37 #include "drm_crtc_helper.h"
38
39 MODULE_AUTHOR("David Airlie, Jesse Barnes");
40 MODULE_DESCRIPTION("DRM KMS helper");
41 MODULE_LICENSE("GPL and additional rights");
42
43 static LIST_HEAD(kernel_fb_helper_list);
44
45 /* simple single crtc case helper function */
46 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
47 {
48         struct drm_device *dev = fb_helper->dev;
49         struct drm_connector *connector;
50         int i;
51
52         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
53                 struct drm_fb_helper_connector *fb_helper_connector;
54
55                 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
56                 if (!fb_helper_connector)
57                         goto fail;
58
59                 fb_helper_connector->connector = connector;
60                 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
61         }
62         return 0;
63 fail:
64         for (i = 0; i < fb_helper->connector_count; i++) {
65                 kfree(fb_helper->connector_info[i]);
66                 fb_helper->connector_info[i] = NULL;
67         }
68         fb_helper->connector_count = 0;
69         return -ENOMEM;
70 }
71 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
72
73 /**
74  * drm_fb_helper_connector_parse_command_line - parse command line for connector
75  * @connector - connector to parse line for
76  * @mode_option - per connector mode option
77  *
78  * This parses the connector specific then generic command lines for
79  * modes and options to configure the connector.
80  *
81  * This uses the same parameters as the fb modedb.c, except for extra
82  *      <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
83  *
84  * enable/enable Digital/disable bit at the end
85  */
86 static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
87                                                        const char *mode_option)
88 {
89         const char *name;
90         unsigned int namelen;
91         int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
92         unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
93         int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
94         int i;
95         enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
96         struct drm_fb_helper_cmdline_mode *cmdline_mode;
97         struct drm_connector *connector;
98
99         if (!fb_helper_conn)
100                 return false;
101         connector = fb_helper_conn->connector;
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 int drm_fb_helper_debug_enter(struct fb_info *info)
246 {
247         struct drm_fb_helper *helper = info->par;
248         struct drm_crtc_helper_funcs *funcs;
249         int i;
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 =
257                                 &helper->crtc_info[i].mode_set;
258
259                         if (!mode_set->crtc->enabled)
260                                 continue;
261
262                         funcs = mode_set->crtc->helper_private;
263                         funcs->mode_set_base_atomic(mode_set->crtc,
264                                                     mode_set->fb,
265                                                     mode_set->x,
266                                                     mode_set->y,
267                                                     1);
268
269                 }
270         }
271
272         return 0;
273 }
274 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
275
276 /* Find the real fb for a given fb helper CRTC */
277 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
278 {
279         struct drm_device *dev = crtc->dev;
280         struct drm_crtc *c;
281
282         list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
283                 if (crtc->base.id == c->base.id)
284                         return c->fb;
285         }
286
287         return NULL;
288 }
289
290 int drm_fb_helper_debug_leave(struct fb_info *info)
291 {
292         struct drm_fb_helper *helper = info->par;
293         struct drm_crtc *crtc;
294         struct drm_crtc_helper_funcs *funcs;
295         struct drm_framebuffer *fb;
296         int i;
297
298         for (i = 0; i < helper->crtc_count; i++) {
299                 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
300                 crtc = mode_set->crtc;
301                 funcs = crtc->helper_private;
302                 fb = drm_mode_config_fb(crtc);
303
304                 if (!crtc->enabled)
305                         continue;
306
307                 if (!fb) {
308                         DRM_ERROR("no fb to restore??\n");
309                         continue;
310                 }
311
312                 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
313                                             crtc->y, 0);
314         }
315
316         return 0;
317 }
318 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
319
320 bool drm_fb_helper_force_kernel_mode(void)
321 {
322         int i = 0;
323         bool ret, error = false;
324         struct drm_fb_helper *helper;
325
326         if (list_empty(&kernel_fb_helper_list))
327                 return false;
328
329         list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
330                 for (i = 0; i < helper->crtc_count; i++) {
331                         struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
332                         ret = drm_crtc_helper_set_config(mode_set);
333                         if (ret)
334                                 error = true;
335                 }
336         }
337         return error;
338 }
339
340 int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
341                         void *panic_str)
342 {
343         printk(KERN_ERR "panic occurred, switching back to text console\n");
344         return drm_fb_helper_force_kernel_mode();
345         return 0;
346 }
347 EXPORT_SYMBOL(drm_fb_helper_panic);
348
349 static struct notifier_block paniced = {
350         .notifier_call = drm_fb_helper_panic,
351 };
352
353 /**
354  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
355  *
356  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
357  */
358 void drm_fb_helper_restore(void)
359 {
360         bool ret;
361         ret = drm_fb_helper_force_kernel_mode();
362         if (ret == true)
363                 DRM_ERROR("Failed to restore crtc configuration\n");
364 }
365 EXPORT_SYMBOL(drm_fb_helper_restore);
366
367 #ifdef CONFIG_MAGIC_SYSRQ
368 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
369 {
370         drm_fb_helper_restore();
371 }
372 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
373
374 static void drm_fb_helper_sysrq(int dummy1)
375 {
376         schedule_work(&drm_fb_helper_restore_work);
377 }
378
379 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
380         .handler = drm_fb_helper_sysrq,
381         .help_msg = "force-fb(V)",
382         .action_msg = "Restore framebuffer console",
383 };
384 #else
385 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
386 #endif
387
388 static void drm_fb_helper_on(struct fb_info *info)
389 {
390         struct drm_fb_helper *fb_helper = info->par;
391         struct drm_device *dev = fb_helper->dev;
392         struct drm_crtc *crtc;
393         struct drm_crtc_helper_funcs *crtc_funcs;
394         struct drm_connector *connector;
395         struct drm_encoder *encoder;
396         int i, j;
397
398         /*
399          * For each CRTC in this fb, turn the crtc on then,
400          * find all associated encoders and turn them on.
401          */
402         mutex_lock(&dev->mode_config.mutex);
403         for (i = 0; i < fb_helper->crtc_count; i++) {
404                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
405                 crtc_funcs = crtc->helper_private;
406
407                 if (!crtc->enabled)
408                         continue;
409
410                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
411
412                 /* Walk the connectors & encoders on this fb turning them on */
413                 for (j = 0; j < fb_helper->connector_count; j++) {
414                         connector = fb_helper->connector_info[j]->connector;
415                         connector->dpms = DRM_MODE_DPMS_ON;
416                         drm_connector_property_set_value(connector,
417                                                          dev->mode_config.dpms_property,
418                                                          DRM_MODE_DPMS_ON);
419                 }
420                 /* Found a CRTC on this fb, now find encoders */
421                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
422                         if (encoder->crtc == crtc) {
423                                 struct drm_encoder_helper_funcs *encoder_funcs;
424
425                                 encoder_funcs = encoder->helper_private;
426                                 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
427                         }
428                 }
429         }
430         mutex_unlock(&dev->mode_config.mutex);
431 }
432
433 static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
434 {
435         struct drm_fb_helper *fb_helper = info->par;
436         struct drm_device *dev = fb_helper->dev;
437         struct drm_crtc *crtc;
438         struct drm_crtc_helper_funcs *crtc_funcs;
439         struct drm_connector *connector;
440         struct drm_encoder *encoder;
441         int i, j;
442
443         /*
444          * For each CRTC in this fb, find all associated encoders
445          * and turn them off, then turn off the CRTC.
446          */
447         mutex_lock(&dev->mode_config.mutex);
448         for (i = 0; i < fb_helper->crtc_count; i++) {
449                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
450                 crtc_funcs = crtc->helper_private;
451
452                 if (!crtc->enabled)
453                         continue;
454
455                 /* Walk the connectors on this fb and mark them off */
456                 for (j = 0; j < fb_helper->connector_count; j++) {
457                         connector = fb_helper->connector_info[j]->connector;
458                         connector->dpms = dpms_mode;
459                         drm_connector_property_set_value(connector,
460                                                          dev->mode_config.dpms_property,
461                                                          dpms_mode);
462                 }
463                 /* Found a CRTC on this fb, now find encoders */
464                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
465                         if (encoder->crtc == crtc) {
466                                 struct drm_encoder_helper_funcs *encoder_funcs;
467
468                                 encoder_funcs = encoder->helper_private;
469                                 encoder_funcs->dpms(encoder, dpms_mode);
470                         }
471                 }
472                 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
473         }
474         mutex_unlock(&dev->mode_config.mutex);
475 }
476
477 int drm_fb_helper_blank(int blank, struct fb_info *info)
478 {
479         switch (blank) {
480         /* Display: On; HSync: On, VSync: On */
481         case FB_BLANK_UNBLANK:
482                 drm_fb_helper_on(info);
483                 break;
484         /* Display: Off; HSync: On, VSync: On */
485         case FB_BLANK_NORMAL:
486                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
487                 break;
488         /* Display: Off; HSync: Off, VSync: On */
489         case FB_BLANK_HSYNC_SUSPEND:
490                 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
491                 break;
492         /* Display: Off; HSync: On, VSync: Off */
493         case FB_BLANK_VSYNC_SUSPEND:
494                 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
495                 break;
496         /* Display: Off; HSync: Off, VSync: Off */
497         case FB_BLANK_POWERDOWN:
498                 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
499                 break;
500         }
501         return 0;
502 }
503 EXPORT_SYMBOL(drm_fb_helper_blank);
504
505 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
506 {
507         int i;
508
509         for (i = 0; i < helper->connector_count; i++)
510                 kfree(helper->connector_info[i]);
511         kfree(helper->connector_info);
512         for (i = 0; i < helper->crtc_count; i++)
513                 kfree(helper->crtc_info[i].mode_set.connectors);
514         kfree(helper->crtc_info);
515 }
516
517 int drm_fb_helper_init(struct drm_device *dev,
518                        struct drm_fb_helper *fb_helper,
519                        int crtc_count, int max_conn_count)
520 {
521         struct drm_crtc *crtc;
522         int ret = 0;
523         int i;
524
525         fb_helper->dev = dev;
526
527         INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
528
529         fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
530         if (!fb_helper->crtc_info)
531                 return -ENOMEM;
532
533         fb_helper->crtc_count = crtc_count;
534         fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
535         if (!fb_helper->connector_info) {
536                 kfree(fb_helper->crtc_info);
537                 return -ENOMEM;
538         }
539         fb_helper->connector_count = 0;
540
541         for (i = 0; i < crtc_count; i++) {
542                 fb_helper->crtc_info[i].mode_set.connectors =
543                         kcalloc(max_conn_count,
544                                 sizeof(struct drm_connector *),
545                                 GFP_KERNEL);
546
547                 if (!fb_helper->crtc_info[i].mode_set.connectors) {
548                         ret = -ENOMEM;
549                         goto out_free;
550                 }
551                 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
552         }
553
554         i = 0;
555         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
556                 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
557                 fb_helper->crtc_info[i].mode_set.crtc = crtc;
558                 i++;
559         }
560         fb_helper->conn_limit = max_conn_count;
561         return 0;
562 out_free:
563         drm_fb_helper_crtc_free(fb_helper);
564         return -ENOMEM;
565 }
566 EXPORT_SYMBOL(drm_fb_helper_init);
567
568 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
569 {
570         if (!list_empty(&fb_helper->kernel_fb_list)) {
571                 list_del(&fb_helper->kernel_fb_list);
572                 if (list_empty(&kernel_fb_helper_list)) {
573                         printk(KERN_INFO "drm: unregistered panic notifier\n");
574                         atomic_notifier_chain_unregister(&panic_notifier_list,
575                                                          &paniced);
576                         unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
577                 }
578         }
579
580         drm_fb_helper_crtc_free(fb_helper);
581
582 }
583 EXPORT_SYMBOL(drm_fb_helper_fini);
584
585 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
586                      u16 blue, u16 regno, struct fb_info *info)
587 {
588         struct drm_fb_helper *fb_helper = info->par;
589         struct drm_framebuffer *fb = fb_helper->fb;
590         int pindex;
591
592         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
593                 u32 *palette;
594                 u32 value;
595                 /* place color in psuedopalette */
596                 if (regno > 16)
597                         return -EINVAL;
598                 palette = (u32 *)info->pseudo_palette;
599                 red >>= (16 - info->var.red.length);
600                 green >>= (16 - info->var.green.length);
601                 blue >>= (16 - info->var.blue.length);
602                 value = (red << info->var.red.offset) |
603                         (green << info->var.green.offset) |
604                         (blue << info->var.blue.offset);
605                 palette[regno] = value;
606                 return 0;
607         }
608
609         pindex = regno;
610
611         if (fb->bits_per_pixel == 16) {
612                 pindex = regno << 3;
613
614                 if (fb->depth == 16 && regno > 63)
615                         return -EINVAL;
616                 if (fb->depth == 15 && regno > 31)
617                         return -EINVAL;
618
619                 if (fb->depth == 16) {
620                         u16 r, g, b;
621                         int i;
622                         if (regno < 32) {
623                                 for (i = 0; i < 8; i++)
624                                         fb_helper->funcs->gamma_set(crtc, red,
625                                                 green, blue, pindex + i);
626                         }
627
628                         fb_helper->funcs->gamma_get(crtc, &r,
629                                                     &g, &b,
630                                                     pindex >> 1);
631
632                         for (i = 0; i < 4; i++)
633                                 fb_helper->funcs->gamma_set(crtc, r,
634                                                             green, b,
635                                                             (pindex >> 1) + i);
636                 }
637         }
638
639         if (fb->depth != 16)
640                 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
641         return 0;
642 }
643
644 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
645 {
646         struct drm_fb_helper *fb_helper = info->par;
647         struct drm_crtc_helper_funcs *crtc_funcs;
648         u16 *red, *green, *blue, *transp;
649         struct drm_crtc *crtc;
650         int i, rc = 0;
651         int start;
652
653         for (i = 0; i < fb_helper->crtc_count; i++) {
654                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
655                 crtc_funcs = crtc->helper_private;
656
657                 red = cmap->red;
658                 green = cmap->green;
659                 blue = cmap->blue;
660                 transp = cmap->transp;
661                 start = cmap->start;
662
663                 for (i = 0; i < cmap->len; i++) {
664                         u16 hred, hgreen, hblue, htransp = 0xffff;
665
666                         hred = *red++;
667                         hgreen = *green++;
668                         hblue = *blue++;
669
670                         if (transp)
671                                 htransp = *transp++;
672
673                         rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
674                         if (rc)
675                                 return rc;
676                 }
677                 crtc_funcs->load_lut(crtc);
678         }
679         return rc;
680 }
681 EXPORT_SYMBOL(drm_fb_helper_setcmap);
682
683 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
684                             struct fb_info *info)
685 {
686         struct drm_fb_helper *fb_helper = info->par;
687         struct drm_framebuffer *fb = fb_helper->fb;
688         int depth;
689
690         if (var->pixclock != 0 || in_dbg_master())
691                 return -EINVAL;
692
693         /* Need to resize the fb object !!! */
694         if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
695                 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
696                           "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
697                           fb->width, fb->height, fb->bits_per_pixel);
698                 return -EINVAL;
699         }
700
701         switch (var->bits_per_pixel) {
702         case 16:
703                 depth = (var->green.length == 6) ? 16 : 15;
704                 break;
705         case 32:
706                 depth = (var->transp.length > 0) ? 32 : 24;
707                 break;
708         default:
709                 depth = var->bits_per_pixel;
710                 break;
711         }
712
713         switch (depth) {
714         case 8:
715                 var->red.offset = 0;
716                 var->green.offset = 0;
717                 var->blue.offset = 0;
718                 var->red.length = 8;
719                 var->green.length = 8;
720                 var->blue.length = 8;
721                 var->transp.length = 0;
722                 var->transp.offset = 0;
723                 break;
724         case 15:
725                 var->red.offset = 10;
726                 var->green.offset = 5;
727                 var->blue.offset = 0;
728                 var->red.length = 5;
729                 var->green.length = 5;
730                 var->blue.length = 5;
731                 var->transp.length = 1;
732                 var->transp.offset = 15;
733                 break;
734         case 16:
735                 var->red.offset = 11;
736                 var->green.offset = 5;
737                 var->blue.offset = 0;
738                 var->red.length = 5;
739                 var->green.length = 6;
740                 var->blue.length = 5;
741                 var->transp.length = 0;
742                 var->transp.offset = 0;
743                 break;
744         case 24:
745                 var->red.offset = 16;
746                 var->green.offset = 8;
747                 var->blue.offset = 0;
748                 var->red.length = 8;
749                 var->green.length = 8;
750                 var->blue.length = 8;
751                 var->transp.length = 0;
752                 var->transp.offset = 0;
753                 break;
754         case 32:
755                 var->red.offset = 16;
756                 var->green.offset = 8;
757                 var->blue.offset = 0;
758                 var->red.length = 8;
759                 var->green.length = 8;
760                 var->blue.length = 8;
761                 var->transp.length = 8;
762                 var->transp.offset = 24;
763                 break;
764         default:
765                 return -EINVAL;
766         }
767         return 0;
768 }
769 EXPORT_SYMBOL(drm_fb_helper_check_var);
770
771 /* this will let fbcon do the mode init */
772 int drm_fb_helper_set_par(struct fb_info *info)
773 {
774         struct drm_fb_helper *fb_helper = info->par;
775         struct drm_device *dev = fb_helper->dev;
776         struct fb_var_screeninfo *var = &info->var;
777         struct drm_crtc *crtc;
778         int ret;
779         int i;
780
781         if (var->pixclock != 0) {
782                 DRM_ERROR("PIXEL CLOCK SET\n");
783                 return -EINVAL;
784         }
785
786         mutex_lock(&dev->mode_config.mutex);
787         for (i = 0; i < fb_helper->crtc_count; i++) {
788                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
789                 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
790                 if (ret) {
791                         mutex_unlock(&dev->mode_config.mutex);
792                         return ret;
793                 }
794         }
795         mutex_unlock(&dev->mode_config.mutex);
796
797         if (fb_helper->delayed_hotplug) {
798                 fb_helper->delayed_hotplug = false;
799                 drm_fb_helper_hotplug_event(fb_helper);
800         }
801         return 0;
802 }
803 EXPORT_SYMBOL(drm_fb_helper_set_par);
804
805 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
806                               struct fb_info *info)
807 {
808         struct drm_fb_helper *fb_helper = info->par;
809         struct drm_device *dev = fb_helper->dev;
810         struct drm_mode_set *modeset;
811         struct drm_crtc *crtc;
812         int ret = 0;
813         int i;
814
815         mutex_lock(&dev->mode_config.mutex);
816         for (i = 0; i < fb_helper->crtc_count; i++) {
817                 crtc = fb_helper->crtc_info[i].mode_set.crtc;
818
819                 modeset = &fb_helper->crtc_info[i].mode_set;
820
821                 modeset->x = var->xoffset;
822                 modeset->y = var->yoffset;
823
824                 if (modeset->num_connectors) {
825                         ret = crtc->funcs->set_config(modeset);
826                         if (!ret) {
827                                 info->var.xoffset = var->xoffset;
828                                 info->var.yoffset = var->yoffset;
829                         }
830                 }
831         }
832         mutex_unlock(&dev->mode_config.mutex);
833         return ret;
834 }
835 EXPORT_SYMBOL(drm_fb_helper_pan_display);
836
837 int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
838                                   int preferred_bpp)
839 {
840         int new_fb = 0;
841         int crtc_count = 0;
842         int i;
843         struct fb_info *info;
844         struct drm_fb_helper_surface_size sizes;
845         int gamma_size = 0;
846
847         memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
848         sizes.surface_depth = 24;
849         sizes.surface_bpp = 32;
850         sizes.fb_width = (unsigned)-1;
851         sizes.fb_height = (unsigned)-1;
852
853         /* if driver picks 8 or 16 by default use that
854            for both depth/bpp */
855         if (preferred_bpp != sizes.surface_bpp) {
856                 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
857         }
858         /* first up get a count of crtcs now in use and new min/maxes width/heights */
859         for (i = 0; i < fb_helper->connector_count; i++) {
860                 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
861                 struct drm_fb_helper_cmdline_mode *cmdline_mode;
862
863                 cmdline_mode = &fb_helper_conn->cmdline_mode;
864
865                 if (cmdline_mode->bpp_specified) {
866                         switch (cmdline_mode->bpp) {
867                         case 8:
868                                 sizes.surface_depth = sizes.surface_bpp = 8;
869                                 break;
870                         case 15:
871                                 sizes.surface_depth = 15;
872                                 sizes.surface_bpp = 16;
873                                 break;
874                         case 16:
875                                 sizes.surface_depth = sizes.surface_bpp = 16;
876                                 break;
877                         case 24:
878                                 sizes.surface_depth = sizes.surface_bpp = 24;
879                                 break;
880                         case 32:
881                                 sizes.surface_depth = 24;
882                                 sizes.surface_bpp = 32;
883                                 break;
884                         }
885                         break;
886                 }
887         }
888
889         crtc_count = 0;
890         for (i = 0; i < fb_helper->crtc_count; i++) {
891                 struct drm_display_mode *desired_mode;
892                 desired_mode = fb_helper->crtc_info[i].desired_mode;
893
894                 if (desired_mode) {
895                         if (gamma_size == 0)
896                                 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
897                         if (desired_mode->hdisplay < sizes.fb_width)
898                                 sizes.fb_width = desired_mode->hdisplay;
899                         if (desired_mode->vdisplay < sizes.fb_height)
900                                 sizes.fb_height = desired_mode->vdisplay;
901                         if (desired_mode->hdisplay > sizes.surface_width)
902                                 sizes.surface_width = desired_mode->hdisplay;
903                         if (desired_mode->vdisplay > sizes.surface_height)
904                                 sizes.surface_height = desired_mode->vdisplay;
905                         crtc_count++;
906                 }
907         }
908
909         if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
910                 /* hmm everyone went away - assume VGA cable just fell out
911                    and will come back later. */
912                 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
913                 sizes.fb_width = sizes.surface_width = 1024;
914                 sizes.fb_height = sizes.surface_height = 768;
915         }
916
917         /* push down into drivers */
918         new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
919         if (new_fb < 0)
920                 return new_fb;
921
922         info = fb_helper->fbdev;
923
924         /* set the fb pointer */
925         for (i = 0; i < fb_helper->crtc_count; i++) {
926                 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
927         }
928
929         if (new_fb) {
930                 info->var.pixclock = 0;
931                 if (register_framebuffer(info) < 0) {
932                         return -EINVAL;
933                 }
934
935                 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
936                        info->fix.id);
937
938         } else {
939                 drm_fb_helper_set_par(info);
940         }
941
942         /* Switch back to kernel console on panic */
943         /* multi card linked list maybe */
944         if (list_empty(&kernel_fb_helper_list)) {
945                 printk(KERN_INFO "drm: registered panic notifier\n");
946                 atomic_notifier_chain_register(&panic_notifier_list,
947                                                &paniced);
948                 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
949         }
950         if (new_fb)
951                 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
952
953         return 0;
954 }
955 EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
956
957 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
958                             uint32_t depth)
959 {
960         info->fix.type = FB_TYPE_PACKED_PIXELS;
961         info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
962                 FB_VISUAL_TRUECOLOR;
963         info->fix.type_aux = 0;
964         info->fix.xpanstep = 1; /* doing it in hw */
965         info->fix.ypanstep = 1; /* doing it in hw */
966         info->fix.ywrapstep = 0;
967         info->fix.accel = FB_ACCEL_NONE;
968         info->fix.type_aux = 0;
969
970         info->fix.line_length = pitch;
971         return;
972 }
973 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
974
975 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
976                             uint32_t fb_width, uint32_t fb_height)
977 {
978         struct drm_framebuffer *fb = fb_helper->fb;
979         info->pseudo_palette = fb_helper->pseudo_palette;
980         info->var.xres_virtual = fb->width;
981         info->var.yres_virtual = fb->height;
982         info->var.bits_per_pixel = fb->bits_per_pixel;
983         info->var.xoffset = 0;
984         info->var.yoffset = 0;
985         info->var.activate = FB_ACTIVATE_NOW;
986         info->var.height = -1;
987         info->var.width = -1;
988
989         switch (fb->depth) {
990         case 8:
991                 info->var.red.offset = 0;
992                 info->var.green.offset = 0;
993                 info->var.blue.offset = 0;
994                 info->var.red.length = 8; /* 8bit DAC */
995                 info->var.green.length = 8;
996                 info->var.blue.length = 8;
997                 info->var.transp.offset = 0;
998                 info->var.transp.length = 0;
999                 break;
1000         case 15:
1001                 info->var.red.offset = 10;
1002                 info->var.green.offset = 5;
1003                 info->var.blue.offset = 0;
1004                 info->var.red.length = 5;
1005                 info->var.green.length = 5;
1006                 info->var.blue.length = 5;
1007                 info->var.transp.offset = 15;
1008                 info->var.transp.length = 1;
1009                 break;
1010         case 16:
1011                 info->var.red.offset = 11;
1012                 info->var.green.offset = 5;
1013                 info->var.blue.offset = 0;
1014                 info->var.red.length = 5;
1015                 info->var.green.length = 6;
1016                 info->var.blue.length = 5;
1017                 info->var.transp.offset = 0;
1018                 break;
1019         case 24:
1020                 info->var.red.offset = 16;
1021                 info->var.green.offset = 8;
1022                 info->var.blue.offset = 0;
1023                 info->var.red.length = 8;
1024                 info->var.green.length = 8;
1025                 info->var.blue.length = 8;
1026                 info->var.transp.offset = 0;
1027                 info->var.transp.length = 0;
1028                 break;
1029         case 32:
1030                 info->var.red.offset = 16;
1031                 info->var.green.offset = 8;
1032                 info->var.blue.offset = 0;
1033                 info->var.red.length = 8;
1034                 info->var.green.length = 8;
1035                 info->var.blue.length = 8;
1036                 info->var.transp.offset = 24;
1037                 info->var.transp.length = 8;
1038                 break;
1039         default:
1040                 break;
1041         }
1042
1043         info->var.xres = fb_width;
1044         info->var.yres = fb_height;
1045 }
1046 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1047
1048 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1049                                                uint32_t maxX,
1050                                                uint32_t maxY)
1051 {
1052         struct drm_connector *connector;
1053         int count = 0;
1054         int i;
1055
1056         for (i = 0; i < fb_helper->connector_count; i++) {
1057                 connector = fb_helper->connector_info[i]->connector;
1058                 count += connector->funcs->fill_modes(connector, maxX, maxY);
1059         }
1060
1061         return count;
1062 }
1063
1064 static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1065 {
1066         struct drm_display_mode *mode;
1067
1068         list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1069                 if (drm_mode_width(mode) > width ||
1070                     drm_mode_height(mode) > height)
1071                         continue;
1072                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1073                         return mode;
1074         }
1075         return NULL;
1076 }
1077
1078 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1079 {
1080         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1081         cmdline_mode = &fb_connector->cmdline_mode;
1082         return cmdline_mode->specified;
1083 }
1084
1085 static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1086                                                       int width, int height)
1087 {
1088         struct drm_fb_helper_cmdline_mode *cmdline_mode;
1089         struct drm_display_mode *mode = NULL;
1090
1091         cmdline_mode = &fb_helper_conn->cmdline_mode;
1092         if (cmdline_mode->specified == false)
1093                 return mode;
1094
1095         /* attempt to find a matching mode in the list of modes
1096          *  we have gotten so far, if not add a CVT mode that conforms
1097          */
1098         if (cmdline_mode->rb || cmdline_mode->margins)
1099                 goto create_mode;
1100
1101         list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1102                 /* check width/height */
1103                 if (mode->hdisplay != cmdline_mode->xres ||
1104                     mode->vdisplay != cmdline_mode->yres)
1105                         continue;
1106
1107                 if (cmdline_mode->refresh_specified) {
1108                         if (mode->vrefresh != cmdline_mode->refresh)
1109                                 continue;
1110                 }
1111
1112                 if (cmdline_mode->interlace) {
1113                         if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1114                                 continue;
1115                 }
1116                 return mode;
1117         }
1118
1119 create_mode:
1120         if (cmdline_mode->cvt)
1121                 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1122                                     cmdline_mode->xres, cmdline_mode->yres,
1123                                     cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1124                                     cmdline_mode->rb, cmdline_mode->interlace,
1125                                     cmdline_mode->margins);
1126         else
1127                 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1128                                     cmdline_mode->xres, cmdline_mode->yres,
1129                                     cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1130                                     cmdline_mode->interlace,
1131                                     cmdline_mode->margins);
1132         drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1133         list_add(&mode->head, &fb_helper_conn->connector->modes);
1134         return mode;
1135 }
1136
1137 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1138 {
1139         bool enable;
1140
1141         if (strict) {
1142                 enable = connector->status == connector_status_connected;
1143         } else {
1144                 enable = connector->status != connector_status_disconnected;
1145         }
1146         return enable;
1147 }
1148
1149 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1150                                   bool *enabled)
1151 {
1152         bool any_enabled = false;
1153         struct drm_connector *connector;
1154         int i = 0;
1155
1156         for (i = 0; i < fb_helper->connector_count; i++) {
1157                 connector = fb_helper->connector_info[i]->connector;
1158                 enabled[i] = drm_connector_enabled(connector, true);
1159                 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1160                           enabled[i] ? "yes" : "no");
1161                 any_enabled |= enabled[i];
1162         }
1163
1164         if (any_enabled)
1165                 return;
1166
1167         for (i = 0; i < fb_helper->connector_count; i++) {
1168                 connector = fb_helper->connector_info[i]->connector;
1169                 enabled[i] = drm_connector_enabled(connector, false);
1170         }
1171 }
1172
1173 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1174                               struct drm_display_mode **modes,
1175                               bool *enabled, int width, int height)
1176 {
1177         int count, i, j;
1178         bool can_clone = false;
1179         struct drm_fb_helper_connector *fb_helper_conn;
1180         struct drm_display_mode *dmt_mode, *mode;
1181
1182         /* only contemplate cloning in the single crtc case */
1183         if (fb_helper->crtc_count > 1)
1184                 return false;
1185
1186         count = 0;
1187         for (i = 0; i < fb_helper->connector_count; i++) {
1188                 if (enabled[i])
1189                         count++;
1190         }
1191
1192         /* only contemplate cloning if more than one connector is enabled */
1193         if (count <= 1)
1194                 return false;
1195
1196         /* check the command line or if nothing common pick 1024x768 */
1197         can_clone = true;
1198         for (i = 0; i < fb_helper->connector_count; i++) {
1199                 if (!enabled[i])
1200                         continue;
1201                 fb_helper_conn = fb_helper->connector_info[i];
1202                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1203                 if (!modes[i]) {
1204                         can_clone = false;
1205                         break;
1206                 }
1207                 for (j = 0; j < i; j++) {
1208                         if (!enabled[j])
1209                                 continue;
1210                         if (!drm_mode_equal(modes[j], modes[i]))
1211                                 can_clone = false;
1212                 }
1213         }
1214
1215         if (can_clone) {
1216                 DRM_DEBUG_KMS("can clone using command line\n");
1217                 return true;
1218         }
1219
1220         /* try and find a 1024x768 mode on each connector */
1221         can_clone = true;
1222         dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1223
1224         for (i = 0; i < fb_helper->connector_count; i++) {
1225
1226                 if (!enabled[i])
1227                         continue;
1228
1229                 fb_helper_conn = fb_helper->connector_info[i];
1230                 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1231                         if (drm_mode_equal(mode, dmt_mode))
1232                                 modes[i] = mode;
1233                 }
1234                 if (!modes[i])
1235                         can_clone = false;
1236         }
1237
1238         if (can_clone) {
1239                 DRM_DEBUG_KMS("can clone using 1024x768\n");
1240                 return true;
1241         }
1242         DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1243         return false;
1244 }
1245
1246 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1247                                  struct drm_display_mode **modes,
1248                                  bool *enabled, int width, int height)
1249 {
1250         struct drm_fb_helper_connector *fb_helper_conn;
1251         int i;
1252
1253         for (i = 0; i < fb_helper->connector_count; i++) {
1254                 fb_helper_conn = fb_helper->connector_info[i];
1255
1256                 if (enabled[i] == false)
1257                         continue;
1258
1259                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1260                               fb_helper_conn->connector->base.id);
1261
1262                 /* got for command line mode first */
1263                 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1264                 if (!modes[i]) {
1265                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1266                                       fb_helper_conn->connector->base.id);
1267                         modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1268                 }
1269                 /* No preferred modes, pick one off the list */
1270                 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1271                         list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1272                                 break;
1273                 }
1274                 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1275                           "none");
1276         }
1277         return true;
1278 }
1279
1280 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1281                           struct drm_fb_helper_crtc **best_crtcs,
1282                           struct drm_display_mode **modes,
1283                           int n, int width, int height)
1284 {
1285         int c, o;
1286         struct drm_device *dev = fb_helper->dev;
1287         struct drm_connector *connector;
1288         struct drm_connector_helper_funcs *connector_funcs;
1289         struct drm_encoder *encoder;
1290         struct drm_fb_helper_crtc *best_crtc;
1291         int my_score, best_score, score;
1292         struct drm_fb_helper_crtc **crtcs, *crtc;
1293         struct drm_fb_helper_connector *fb_helper_conn;
1294
1295         if (n == fb_helper->connector_count)
1296                 return 0;
1297
1298         fb_helper_conn = fb_helper->connector_info[n];
1299         connector = fb_helper_conn->connector;
1300
1301         best_crtcs[n] = NULL;
1302         best_crtc = NULL;
1303         best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1304         if (modes[n] == NULL)
1305                 return best_score;
1306
1307         crtcs = kzalloc(dev->mode_config.num_connector *
1308                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1309         if (!crtcs)
1310                 return best_score;
1311
1312         my_score = 1;
1313         if (connector->status == connector_status_connected)
1314                 my_score++;
1315         if (drm_has_cmdline_mode(fb_helper_conn))
1316                 my_score++;
1317         if (drm_has_preferred_mode(fb_helper_conn, width, height))
1318                 my_score++;
1319
1320         connector_funcs = connector->helper_private;
1321         encoder = connector_funcs->best_encoder(connector);
1322         if (!encoder)
1323                 goto out;
1324
1325         /* select a crtc for this connector and then attempt to configure
1326            remaining connectors */
1327         for (c = 0; c < fb_helper->crtc_count; c++) {
1328                 crtc = &fb_helper->crtc_info[c];
1329
1330                 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1331                         continue;
1332                 }
1333
1334                 for (o = 0; o < n; o++)
1335                         if (best_crtcs[o] == crtc)
1336                                 break;
1337
1338                 if (o < n) {
1339                         /* ignore cloning unless only a single crtc */
1340                         if (fb_helper->crtc_count > 1)
1341                                 continue;
1342
1343                         if (!drm_mode_equal(modes[o], modes[n]))
1344                                 continue;
1345                 }
1346
1347                 crtcs[n] = crtc;
1348                 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1349                 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1350                                                   width, height);
1351                 if (score > best_score) {
1352                         best_crtc = crtc;
1353                         best_score = score;
1354                         memcpy(best_crtcs, crtcs,
1355                                dev->mode_config.num_connector *
1356                                sizeof(struct drm_fb_helper_crtc *));
1357                 }
1358         }
1359 out:
1360         kfree(crtcs);
1361         return best_score;
1362 }
1363
1364 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1365 {
1366         struct drm_device *dev = fb_helper->dev;
1367         struct drm_fb_helper_crtc **crtcs;
1368         struct drm_display_mode **modes;
1369         struct drm_encoder *encoder;
1370         struct drm_mode_set *modeset;
1371         bool *enabled;
1372         int width, height;
1373         int i, ret;
1374
1375         DRM_DEBUG_KMS("\n");
1376
1377         width = dev->mode_config.max_width;
1378         height = dev->mode_config.max_height;
1379
1380         /* clean out all the encoder/crtc combos */
1381         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1382                 encoder->crtc = NULL;
1383         }
1384
1385         crtcs = kcalloc(dev->mode_config.num_connector,
1386                         sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1387         modes = kcalloc(dev->mode_config.num_connector,
1388                         sizeof(struct drm_display_mode *), GFP_KERNEL);
1389         enabled = kcalloc(dev->mode_config.num_connector,
1390                           sizeof(bool), GFP_KERNEL);
1391
1392         drm_enable_connectors(fb_helper, enabled);
1393
1394         ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1395         if (!ret) {
1396                 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1397                 if (!ret)
1398                         DRM_ERROR("Unable to find initial modes\n");
1399         }
1400
1401         DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1402
1403         drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1404
1405         /* need to set the modesets up here for use later */
1406         /* fill out the connector<->crtc mappings into the modesets */
1407         for (i = 0; i < fb_helper->crtc_count; i++) {
1408                 modeset = &fb_helper->crtc_info[i].mode_set;
1409                 modeset->num_connectors = 0;
1410         }
1411
1412         for (i = 0; i < fb_helper->connector_count; i++) {
1413                 struct drm_display_mode *mode = modes[i];
1414                 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1415                 modeset = &fb_crtc->mode_set;
1416
1417                 if (mode && fb_crtc) {
1418                         DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1419                                       mode->name, fb_crtc->mode_set.crtc->base.id);
1420                         fb_crtc->desired_mode = mode;
1421                         if (modeset->mode)
1422                                 drm_mode_destroy(dev, modeset->mode);
1423                         modeset->mode = drm_mode_duplicate(dev,
1424                                                            fb_crtc->desired_mode);
1425                         modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1426                 }
1427         }
1428
1429         kfree(crtcs);
1430         kfree(modes);
1431         kfree(enabled);
1432 }
1433
1434 /**
1435  * drm_helper_initial_config - setup a sane initial connector configuration
1436  * @dev: DRM device
1437  *
1438  * LOCKING:
1439  * Called at init time, must take mode config lock.
1440  *
1441  * Scan the CRTCs and connectors and try to put together an initial setup.
1442  * At the moment, this is a cloned configuration across all heads with
1443  * a new framebuffer object as the backing store.
1444  *
1445  * RETURNS:
1446  * Zero if everything went ok, nonzero otherwise.
1447  */
1448 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1449 {
1450         struct drm_device *dev = fb_helper->dev;
1451         int count = 0;
1452
1453         /* disable all the possible outputs/crtcs before entering KMS mode */
1454         drm_helper_disable_unused_functions(fb_helper->dev);
1455
1456         drm_fb_helper_parse_command_line(fb_helper);
1457
1458         count = drm_fb_helper_probe_connector_modes(fb_helper,
1459                                                     dev->mode_config.max_width,
1460                                                     dev->mode_config.max_height);
1461         /*
1462          * we shouldn't end up with no modes here.
1463          */
1464         if (count == 0) {
1465                 printk(KERN_INFO "No connectors reported connected with modes\n");
1466         }
1467         drm_setup_crtcs(fb_helper);
1468
1469         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1470 }
1471 EXPORT_SYMBOL(drm_fb_helper_initial_config);
1472
1473 bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1474 {
1475         int count = 0;
1476         u32 max_width, max_height, bpp_sel;
1477         bool bound = false, crtcs_bound = false;
1478         struct drm_crtc *crtc;
1479
1480         if (!fb_helper->fb)
1481                 return false;
1482
1483         list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1484                 if (crtc->fb)
1485                         crtcs_bound = true;
1486                 if (crtc->fb == fb_helper->fb)
1487                         bound = true;
1488         }
1489
1490         if (!bound && crtcs_bound) {
1491                 fb_helper->delayed_hotplug = true;
1492                 return false;
1493         }
1494         DRM_DEBUG_KMS("\n");
1495
1496         max_width = fb_helper->fb->width;
1497         max_height = fb_helper->fb->height;
1498         bpp_sel = fb_helper->fb->bits_per_pixel;
1499
1500         count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1501                                                     max_height);
1502         drm_setup_crtcs(fb_helper);
1503
1504         return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1505 }
1506 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1507