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