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