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