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