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