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