blob: 055b5be7887720922c668df87dd9d86a01ba29ca [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
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 */
Andy Shevchenko3b40a442010-02-02 14:40:32 -080030#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100031#include <linux/sysrq.h>
32#include <linux/fb.h>
33#include "drmP.h"
34#include "drm_crtc.h"
35#include "drm_fb_helper.h"
36#include "drm_crtc_helper.h"
37
Dave Airlie6fcefd52009-09-08 11:08:32 +100038MODULE_AUTHOR("David Airlie, Jesse Barnes");
39MODULE_DESCRIPTION("DRM KMS helper");
40MODULE_LICENSE("GPL and additional rights");
41
Dave Airlie785b93e2009-08-28 15:46:53 +100042static LIST_HEAD(kernel_fb_helper_list);
43
Dave Airlied50ba252009-09-23 14:44:08 +100044int drm_fb_helper_add_connector(struct drm_connector *connector)
45{
46 connector->fb_helper_private = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
47 if (!connector->fb_helper_private)
48 return -ENOMEM;
49
50 return 0;
Dave Airlied50ba252009-09-23 14:44:08 +100051}
52EXPORT_SYMBOL(drm_fb_helper_add_connector);
53
Dave Airlied50ba252009-09-23 14:44:08 +100054/**
55 * drm_fb_helper_connector_parse_command_line - parse command line for connector
56 * @connector - connector to parse line for
57 * @mode_option - per connector mode option
58 *
59 * This parses the connector specific then generic command lines for
60 * modes and options to configure the connector.
61 *
62 * This uses the same parameters as the fb modedb.c, except for extra
63 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
64 *
65 * enable/enable Digital/disable bit at the end
66 */
67static bool drm_fb_helper_connector_parse_command_line(struct drm_connector *connector,
68 const char *mode_option)
69{
70 const char *name;
71 unsigned int namelen;
72 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
73 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
74 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
75 int i;
76 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
77 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
Dave Airlie8ef86782009-09-26 06:39:00 +100078 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +100079
Dave Airlie8ef86782009-09-26 06:39:00 +100080 if (!fb_help_conn)
81 return false;
82
83 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +100084 if (!mode_option)
85 mode_option = fb_mode_option;
86
87 if (!mode_option) {
88 cmdline_mode->specified = false;
89 return false;
90 }
91
92 name = mode_option;
93 namelen = strlen(name);
94 for (i = namelen-1; i >= 0; i--) {
95 switch (name[i]) {
96 case '@':
97 namelen = i;
98 if (!refresh_specified && !bpp_specified &&
99 !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800100 refresh = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000101 refresh_specified = 1;
102 if (cvt || rb)
103 cvt = 0;
104 } else
105 goto done;
106 break;
107 case '-':
108 namelen = i;
109 if (!bpp_specified && !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800110 bpp = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000111 bpp_specified = 1;
112 if (cvt || rb)
113 cvt = 0;
114 } else
115 goto done;
116 break;
117 case 'x':
118 if (!yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800119 yres = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000120 yres_specified = 1;
121 } else
122 goto done;
123 case '0' ... '9':
124 break;
125 case 'M':
126 if (!yres_specified)
127 cvt = 1;
128 break;
129 case 'R':
130 if (!cvt)
131 rb = 1;
132 break;
133 case 'm':
134 if (!cvt)
135 margins = 1;
136 break;
137 case 'i':
138 if (!cvt)
139 interlace = 1;
140 break;
141 case 'e':
142 force = DRM_FORCE_ON;
143 break;
144 case 'D':
Roel Kluine89a8c92009-12-31 13:06:29 +0100145 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
Dave Airlied50ba252009-09-23 14:44:08 +1000146 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
147 force = DRM_FORCE_ON;
148 else
149 force = DRM_FORCE_ON_DIGITAL;
150 break;
151 case 'd':
152 force = DRM_FORCE_OFF;
153 break;
154 default:
155 goto done;
156 }
157 }
158 if (i < 0 && yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800159 xres = simple_strtol(name, NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000160 res_specified = 1;
161 }
162done:
163
164 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
165 drm_get_connector_name(connector), xres, yres,
166 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
167 "", (margins) ? " with margins" : "", (interlace) ?
168 " interlaced" : "");
169
170 if (force) {
171 const char *s;
172 switch (force) {
173 case DRM_FORCE_OFF: s = "OFF"; break;
174 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
175 default:
176 case DRM_FORCE_ON: s = "ON"; break;
177 }
178
179 DRM_INFO("forcing %s connector %s\n",
180 drm_get_connector_name(connector), s);
181 connector->force = force;
182 }
183
184 if (res_specified) {
185 cmdline_mode->specified = true;
186 cmdline_mode->xres = xres;
187 cmdline_mode->yres = yres;
188 }
189
190 if (refresh_specified) {
191 cmdline_mode->refresh_specified = true;
192 cmdline_mode->refresh = refresh;
193 }
194
195 if (bpp_specified) {
196 cmdline_mode->bpp_specified = true;
197 cmdline_mode->bpp = bpp;
198 }
199 cmdline_mode->rb = rb ? true : false;
200 cmdline_mode->cvt = cvt ? true : false;
201 cmdline_mode->interlace = interlace ? true : false;
202
203 return true;
204}
205
206int drm_fb_helper_parse_command_line(struct drm_device *dev)
207{
208 struct drm_connector *connector;
209
210 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
211 char *option = NULL;
212
213 /* do something on return - turn off connector maybe */
214 if (fb_get_options(drm_get_connector_name(connector), &option))
215 continue;
216
217 drm_fb_helper_connector_parse_command_line(connector, option);
218 }
219 return 0;
220}
221
Dave Airlie785b93e2009-08-28 15:46:53 +1000222bool drm_fb_helper_force_kernel_mode(void)
223{
224 int i = 0;
225 bool ret, error = false;
226 struct drm_fb_helper *helper;
227
228 if (list_empty(&kernel_fb_helper_list))
229 return false;
230
231 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
232 for (i = 0; i < helper->crtc_count; i++) {
233 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
234 ret = drm_crtc_helper_set_config(mode_set);
235 if (ret)
236 error = true;
237 }
238 }
239 return error;
240}
241
242int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
243 void *panic_str)
244{
245 DRM_ERROR("panic occurred, switching back to text console\n");
246 return drm_fb_helper_force_kernel_mode();
247 return 0;
248}
249EXPORT_SYMBOL(drm_fb_helper_panic);
250
251static struct notifier_block paniced = {
252 .notifier_call = drm_fb_helper_panic,
253};
254
255/**
256 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
257 *
258 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
259 */
260void drm_fb_helper_restore(void)
261{
262 bool ret;
263 ret = drm_fb_helper_force_kernel_mode();
264 if (ret == true)
265 DRM_ERROR("Failed to restore crtc configuration\n");
266}
267EXPORT_SYMBOL(drm_fb_helper_restore);
268
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200269#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000270static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
271{
272 drm_fb_helper_restore();
273}
274static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
275
276static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
277{
278 schedule_work(&drm_fb_helper_restore_work);
279}
280
281static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
282 .handler = drm_fb_helper_sysrq,
283 .help_msg = "force-fb(V)",
284 .action_msg = "Restore framebuffer console",
285};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000286#else
287static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200288#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000289
290static void drm_fb_helper_on(struct fb_info *info)
291{
292 struct drm_fb_helper *fb_helper = info->par;
293 struct drm_device *dev = fb_helper->dev;
294 struct drm_crtc *crtc;
295 struct drm_encoder *encoder;
296 int i;
297
298 /*
299 * For each CRTC in this fb, turn the crtc on then,
300 * find all associated encoders and turn them on.
301 */
Jesse Barnese87b2c42009-09-17 18:14:41 -0700302 for (i = 0; i < fb_helper->crtc_count; i++) {
303 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
304 struct drm_crtc_helper_funcs *crtc_funcs =
305 crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000306
Jesse Barnese87b2c42009-09-17 18:14:41 -0700307 /* Only mess with CRTCs in this fb */
308 if (crtc->base.id != fb_helper->crtc_info[i].crtc_id ||
309 !crtc->enabled)
310 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000311
Jesse Barnese87b2c42009-09-17 18:14:41 -0700312 mutex_lock(&dev->mode_config.mutex);
313 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
314 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000315
Jesse Barnese87b2c42009-09-17 18:14:41 -0700316 /* Found a CRTC on this fb, now find encoders */
317 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
318 if (encoder->crtc == crtc) {
319 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000320
Jesse Barnese87b2c42009-09-17 18:14:41 -0700321 encoder_funcs = encoder->helper_private;
322 mutex_lock(&dev->mode_config.mutex);
323 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
324 mutex_unlock(&dev->mode_config.mutex);
325 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000326 }
327 }
328 }
329}
330
331static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
332{
333 struct drm_fb_helper *fb_helper = info->par;
334 struct drm_device *dev = fb_helper->dev;
335 struct drm_crtc *crtc;
336 struct drm_encoder *encoder;
337 int i;
338
339 /*
340 * For each CRTC in this fb, find all associated encoders
341 * and turn them off, then turn off the CRTC.
342 */
Jesse Barnese87b2c42009-09-17 18:14:41 -0700343 for (i = 0; i < fb_helper->crtc_count; i++) {
344 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
345 struct drm_crtc_helper_funcs *crtc_funcs =
346 crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000347
Jesse Barnese87b2c42009-09-17 18:14:41 -0700348 /* Only mess with CRTCs in this fb */
349 if (crtc->base.id != fb_helper->crtc_info[i].crtc_id ||
350 !crtc->enabled)
351 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000352
Jesse Barnese87b2c42009-09-17 18:14:41 -0700353 /* Found a CRTC on this fb, now find encoders */
354 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
355 if (encoder->crtc == crtc) {
356 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000357
Jesse Barnese87b2c42009-09-17 18:14:41 -0700358 encoder_funcs = encoder->helper_private;
359 mutex_lock(&dev->mode_config.mutex);
360 encoder_funcs->dpms(encoder, dpms_mode);
361 mutex_unlock(&dev->mode_config.mutex);
362 }
363 }
James Simmons731b5a12009-10-29 20:39:07 +0000364 mutex_lock(&dev->mode_config.mutex);
365 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
366 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000367 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000368 }
369}
370
371int drm_fb_helper_blank(int blank, struct fb_info *info)
372{
373 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000374 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000375 case FB_BLANK_UNBLANK:
376 drm_fb_helper_on(info);
377 break;
James Simmons731b5a12009-10-29 20:39:07 +0000378 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000379 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800380 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000381 break;
James Simmons731b5a12009-10-29 20:39:07 +0000382 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000383 case FB_BLANK_HSYNC_SUSPEND:
384 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
385 break;
James Simmons731b5a12009-10-29 20:39:07 +0000386 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000387 case FB_BLANK_VSYNC_SUSPEND:
388 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
389 break;
James Simmons731b5a12009-10-29 20:39:07 +0000390 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000391 case FB_BLANK_POWERDOWN:
392 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
393 break;
394 }
395 return 0;
396}
397EXPORT_SYMBOL(drm_fb_helper_blank);
398
399static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
400{
401 int i;
402
403 for (i = 0; i < helper->crtc_count; i++)
404 kfree(helper->crtc_info[i].mode_set.connectors);
405 kfree(helper->crtc_info);
406}
407
408int drm_fb_helper_init_crtc_count(struct drm_fb_helper *helper, int crtc_count, int max_conn_count)
409{
410 struct drm_device *dev = helper->dev;
411 struct drm_crtc *crtc;
412 int ret = 0;
413 int i;
414
415 helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
416 if (!helper->crtc_info)
417 return -ENOMEM;
418
419 helper->crtc_count = crtc_count;
420
421 for (i = 0; i < crtc_count; i++) {
422 helper->crtc_info[i].mode_set.connectors =
423 kcalloc(max_conn_count,
424 sizeof(struct drm_connector *),
425 GFP_KERNEL);
426
427 if (!helper->crtc_info[i].mode_set.connectors) {
428 ret = -ENOMEM;
429 goto out_free;
430 }
431 helper->crtc_info[i].mode_set.num_connectors = 0;
432 }
433
434 i = 0;
435 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
436 helper->crtc_info[i].crtc_id = crtc->base.id;
437 helper->crtc_info[i].mode_set.crtc = crtc;
438 i++;
439 }
440 helper->conn_limit = max_conn_count;
441 return 0;
442out_free:
443 drm_fb_helper_crtc_free(helper);
444 return -ENOMEM;
445}
446EXPORT_SYMBOL(drm_fb_helper_init_crtc_count);
447
Dave Airliec850cb72009-10-23 18:49:03 +1000448static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000449 u16 blue, u16 regno, struct fb_info *info)
450{
451 struct drm_fb_helper *fb_helper = info->par;
452 struct drm_framebuffer *fb = fb_helper->fb;
453 int pindex;
454
Dave Airliec850cb72009-10-23 18:49:03 +1000455 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
456 u32 *palette;
457 u32 value;
458 /* place color in psuedopalette */
459 if (regno > 16)
460 return -EINVAL;
461 palette = (u32 *)info->pseudo_palette;
462 red >>= (16 - info->var.red.length);
463 green >>= (16 - info->var.green.length);
464 blue >>= (16 - info->var.blue.length);
465 value = (red << info->var.red.offset) |
466 (green << info->var.green.offset) |
467 (blue << info->var.blue.offset);
468 palette[regno] = value;
469 return 0;
470 }
471
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000472 pindex = regno;
473
474 if (fb->bits_per_pixel == 16) {
475 pindex = regno << 3;
476
477 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000478 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000479 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000480 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000481
482 if (fb->depth == 16) {
483 u16 r, g, b;
484 int i;
485 if (regno < 32) {
486 for (i = 0; i < 8; i++)
487 fb_helper->funcs->gamma_set(crtc, red,
488 green, blue, pindex + i);
489 }
490
491 fb_helper->funcs->gamma_get(crtc, &r,
492 &g, &b,
493 pindex >> 1);
494
495 for (i = 0; i < 4; i++)
496 fb_helper->funcs->gamma_set(crtc, r,
497 green, b,
498 (pindex >> 1) + i);
499 }
500 }
501
502 if (fb->depth != 16)
503 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000504 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000505}
506
Dave Airlie068143d2009-10-05 09:58:02 +1000507int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
508{
509 struct drm_fb_helper *fb_helper = info->par;
510 struct drm_device *dev = fb_helper->dev;
511 u16 *red, *green, *blue, *transp;
512 struct drm_crtc *crtc;
513 int i, rc = 0;
514 int start;
515
516 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
517 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
518 for (i = 0; i < fb_helper->crtc_count; i++) {
519 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
520 break;
521 }
522 if (i == fb_helper->crtc_count)
523 continue;
524
525 red = cmap->red;
526 green = cmap->green;
527 blue = cmap->blue;
528 transp = cmap->transp;
529 start = cmap->start;
530
531 for (i = 0; i < cmap->len; i++) {
532 u16 hred, hgreen, hblue, htransp = 0xffff;
533
534 hred = *red++;
535 hgreen = *green++;
536 hblue = *blue++;
537
538 if (transp)
539 htransp = *transp++;
540
Dave Airliec850cb72009-10-23 18:49:03 +1000541 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
542 if (rc)
543 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000544 }
545 crtc_funcs->load_lut(crtc);
546 }
547 return rc;
548}
549EXPORT_SYMBOL(drm_fb_helper_setcmap);
550
Dave Airlie785b93e2009-08-28 15:46:53 +1000551int drm_fb_helper_setcolreg(unsigned regno,
552 unsigned red,
553 unsigned green,
554 unsigned blue,
555 unsigned transp,
556 struct fb_info *info)
557{
558 struct drm_fb_helper *fb_helper = info->par;
559 struct drm_device *dev = fb_helper->dev;
560 struct drm_crtc *crtc;
561 int i;
Dave Airliec850cb72009-10-23 18:49:03 +1000562 int ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000563
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000564 if (regno > 255)
565 return 1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000566
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000567 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
568 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000569 for (i = 0; i < fb_helper->crtc_count; i++) {
570 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
571 break;
572 }
573 if (i == fb_helper->crtc_count)
574 continue;
575
Dave Airliec850cb72009-10-23 18:49:03 +1000576 ret = setcolreg(crtc, red, green, blue, regno, info);
577 if (ret)
578 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000579
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000580 crtc_funcs->load_lut(crtc);
Dave Airlie785b93e2009-08-28 15:46:53 +1000581 }
582 return 0;
583}
584EXPORT_SYMBOL(drm_fb_helper_setcolreg);
585
586int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
587 struct fb_info *info)
588{
589 struct drm_fb_helper *fb_helper = info->par;
590 struct drm_framebuffer *fb = fb_helper->fb;
591 int depth;
592
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100593 if (var->pixclock != 0)
Dave Airlie785b93e2009-08-28 15:46:53 +1000594 return -EINVAL;
595
596 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000597 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
598 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
599 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
600 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000601 return -EINVAL;
602 }
603
604 switch (var->bits_per_pixel) {
605 case 16:
606 depth = (var->green.length == 6) ? 16 : 15;
607 break;
608 case 32:
609 depth = (var->transp.length > 0) ? 32 : 24;
610 break;
611 default:
612 depth = var->bits_per_pixel;
613 break;
614 }
615
616 switch (depth) {
617 case 8:
618 var->red.offset = 0;
619 var->green.offset = 0;
620 var->blue.offset = 0;
621 var->red.length = 8;
622 var->green.length = 8;
623 var->blue.length = 8;
624 var->transp.length = 0;
625 var->transp.offset = 0;
626 break;
627 case 15:
628 var->red.offset = 10;
629 var->green.offset = 5;
630 var->blue.offset = 0;
631 var->red.length = 5;
632 var->green.length = 5;
633 var->blue.length = 5;
634 var->transp.length = 1;
635 var->transp.offset = 15;
636 break;
637 case 16:
638 var->red.offset = 11;
639 var->green.offset = 5;
640 var->blue.offset = 0;
641 var->red.length = 5;
642 var->green.length = 6;
643 var->blue.length = 5;
644 var->transp.length = 0;
645 var->transp.offset = 0;
646 break;
647 case 24:
648 var->red.offset = 16;
649 var->green.offset = 8;
650 var->blue.offset = 0;
651 var->red.length = 8;
652 var->green.length = 8;
653 var->blue.length = 8;
654 var->transp.length = 0;
655 var->transp.offset = 0;
656 break;
657 case 32:
658 var->red.offset = 16;
659 var->green.offset = 8;
660 var->blue.offset = 0;
661 var->red.length = 8;
662 var->green.length = 8;
663 var->blue.length = 8;
664 var->transp.length = 8;
665 var->transp.offset = 24;
666 break;
667 default:
668 return -EINVAL;
669 }
670 return 0;
671}
672EXPORT_SYMBOL(drm_fb_helper_check_var);
673
674/* this will let fbcon do the mode init */
675int drm_fb_helper_set_par(struct fb_info *info)
676{
677 struct drm_fb_helper *fb_helper = info->par;
678 struct drm_device *dev = fb_helper->dev;
679 struct fb_var_screeninfo *var = &info->var;
680 struct drm_crtc *crtc;
681 int ret;
682 int i;
683
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100684 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000685 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000686 return -EINVAL;
687 }
688
689 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
690
691 for (i = 0; i < fb_helper->crtc_count; i++) {
692 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
693 break;
694 }
695 if (i == fb_helper->crtc_count)
696 continue;
697
698 if (crtc->fb == fb_helper->crtc_info[i].mode_set.fb) {
699 mutex_lock(&dev->mode_config.mutex);
James Simmonsa2d49ae2009-10-27 21:09:24 +0000700 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
Dave Airlie785b93e2009-08-28 15:46:53 +1000701 mutex_unlock(&dev->mode_config.mutex);
702 if (ret)
703 return ret;
704 }
705 }
706 return 0;
707}
708EXPORT_SYMBOL(drm_fb_helper_set_par);
709
710int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
711 struct fb_info *info)
712{
713 struct drm_fb_helper *fb_helper = info->par;
714 struct drm_device *dev = fb_helper->dev;
715 struct drm_mode_set *modeset;
716 struct drm_crtc *crtc;
717 int ret = 0;
718 int i;
719
720 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
721 for (i = 0; i < fb_helper->crtc_count; i++) {
722 if (crtc->base.id == fb_helper->crtc_info[i].crtc_id)
723 break;
724 }
725
726 if (i == fb_helper->crtc_count)
727 continue;
728
729 modeset = &fb_helper->crtc_info[i].mode_set;
730
731 modeset->x = var->xoffset;
732 modeset->y = var->yoffset;
733
734 if (modeset->num_connectors) {
735 mutex_lock(&dev->mode_config.mutex);
736 ret = crtc->funcs->set_config(modeset);
737 mutex_unlock(&dev->mode_config.mutex);
738 if (!ret) {
739 info->var.xoffset = var->xoffset;
740 info->var.yoffset = var->yoffset;
741 }
742 }
743 }
744 return ret;
745}
746EXPORT_SYMBOL(drm_fb_helper_pan_display);
747
748int drm_fb_helper_single_fb_probe(struct drm_device *dev,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000749 int preferred_bpp,
Dave Airlie38651672010-03-30 05:34:13 +0000750 int (*fb_find_or_create)(struct drm_device *dev,
751 struct drm_fb_helper_surface_size *sizes,
752 struct drm_fb_helper **fb_ptr))
Dave Airlie785b93e2009-08-28 15:46:53 +1000753{
754 struct drm_crtc *crtc;
755 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000756 int new_fb = 0;
757 int crtc_count = 0;
758 int ret, i, conn_count = 0;
759 struct fb_info *info;
Dave Airlie785b93e2009-08-28 15:46:53 +1000760 struct drm_mode_set *modeset = NULL;
761 struct drm_fb_helper *fb_helper;
Dave Airlie38651672010-03-30 05:34:13 +0000762 struct drm_fb_helper_surface_size sizes;
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;
Dave Airlie785b93e2009-08-28 15:46:53 +1000769
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000772 if (preferred_bpp != sizes.surface_bpp) {
773 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000774 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlied50ba252009-09-23 14:44:08 +1000776 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
777 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
Dave Airlie8ef86782009-09-26 06:39:00 +1000778
779 struct drm_fb_helper_cmdline_mode *cmdline_mode;
780
781 if (!fb_help_conn)
782 continue;
783
784 cmdline_mode = &fb_help_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000785
786 if (cmdline_mode->bpp_specified) {
787 switch (cmdline_mode->bpp) {
788 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000789 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000790 break;
791 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000792 sizes.surface_depth = 15;
793 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000794 break;
795 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000796 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000797 break;
798 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000799 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000800 break;
801 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000802 sizes.surface_depth = 24;
803 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000804 break;
805 }
806 break;
807 }
808 }
809
Dave Airlie785b93e2009-08-28 15:46:53 +1000810 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
811 if (drm_helper_crtc_in_use(crtc)) {
812 if (crtc->desired_mode) {
Dave Airlie38651672010-03-30 05:34:13 +0000813 if (crtc->desired_mode->hdisplay < sizes.fb_width)
814 sizes.fb_width = crtc->desired_mode->hdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000815
Dave Airlie38651672010-03-30 05:34:13 +0000816 if (crtc->desired_mode->vdisplay < sizes.fb_height)
817 sizes.fb_height = crtc->desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000818
Dave Airlie38651672010-03-30 05:34:13 +0000819 if (crtc->desired_mode->hdisplay > sizes.surface_width)
820 sizes.surface_width = crtc->desired_mode->hdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000821
Dave Airlie38651672010-03-30 05:34:13 +0000822 if (crtc->desired_mode->vdisplay > sizes.surface_height)
823 sizes.surface_height = crtc->desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 }
825 crtc_count++;
826 }
827 }
828
Dave Airlie38651672010-03-30 05:34:13 +0000829 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000830 /* hmm everyone went away - assume VGA cable just fell out
831 and will come back later. */
832 return 0;
833 }
834
Dave Airlie38651672010-03-30 05:34:13 +0000835 /* push down into drivers */
836 new_fb = (*fb_find_or_create)(dev, &sizes,
837 &fb_helper);
838 if (new_fb < 0)
839 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000840
Dave Airlie38651672010-03-30 05:34:13 +0000841 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000842
843 crtc_count = 0;
844 /* okay we need to setup new connector sets in the crtcs */
845 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
846 modeset = &fb_helper->crtc_info[crtc_count].mode_set;
Dave Airlie38651672010-03-30 05:34:13 +0000847 modeset->fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000848 conn_count = 0;
849 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
850 if (connector->encoder)
851 if (connector->encoder->crtc == modeset->crtc) {
852 modeset->connectors[conn_count] = connector;
853 conn_count++;
854 if (conn_count > fb_helper->conn_limit)
855 BUG();
856 }
857 }
858
859 for (i = conn_count; i < fb_helper->conn_limit; i++)
860 modeset->connectors[i] = NULL;
861
862 modeset->crtc = crtc;
863 crtc_count++;
864
865 modeset->num_connectors = conn_count;
866 if (modeset->crtc->desired_mode) {
867 if (modeset->mode)
868 drm_mode_destroy(dev, modeset->mode);
869 modeset->mode = drm_mode_duplicate(dev,
870 modeset->crtc->desired_mode);
871 }
872 }
873 fb_helper->crtc_count = crtc_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000874
875 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100876 info->var.pixclock = 0;
Ben Skeggsb0a007d2009-12-08 11:15:10 +1000877 ret = fb_alloc_cmap(&info->cmap, modeset->crtc->gamma_size, 0);
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100878 if (ret)
879 return ret;
880 if (register_framebuffer(info) < 0) {
881 fb_dealloc_cmap(&info->cmap);
Dave Airlie785b93e2009-08-28 15:46:53 +1000882 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100883 }
Dave Airlie38651672010-03-30 05:34:13 +0000884
885 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
886 info->fix.id);
887
Dave Airlie785b93e2009-08-28 15:46:53 +1000888 } else {
889 drm_fb_helper_set_par(info);
890 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000891
892 /* Switch back to kernel console on panic */
893 /* multi card linked list maybe */
894 if (list_empty(&kernel_fb_helper_list)) {
895 printk(KERN_INFO "registered panic notifier\n");
896 atomic_notifier_chain_register(&panic_notifier_list,
897 &paniced);
898 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
899 }
Dave Airlie38651672010-03-30 05:34:13 +0000900 if (new_fb)
901 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
902
Dave Airlie785b93e2009-08-28 15:46:53 +1000903 return 0;
904}
905EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
906
907void drm_fb_helper_free(struct drm_fb_helper *helper)
908{
909 list_del(&helper->kernel_fb_list);
910 if (list_empty(&kernel_fb_helper_list)) {
911 printk(KERN_INFO "unregistered panic notifier\n");
912 atomic_notifier_chain_unregister(&panic_notifier_list,
913 &paniced);
914 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
915 }
916 drm_fb_helper_crtc_free(helper);
Dave Airlie38651672010-03-30 05:34:13 +0000917 fb_dealloc_cmap(&helper->fbdev->cmap);
Dave Airlie785b93e2009-08-28 15:46:53 +1000918}
919EXPORT_SYMBOL(drm_fb_helper_free);
920
Dave Airlie068143d2009-10-05 09:58:02 +1000921void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
922 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000923{
924 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000925 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000926 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 info->fix.type_aux = 0;
928 info->fix.xpanstep = 1; /* doing it in hw */
929 info->fix.ypanstep = 1; /* doing it in hw */
930 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000931 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000932 info->fix.type_aux = 0;
933
934 info->fix.line_length = pitch;
935 return;
936}
937EXPORT_SYMBOL(drm_fb_helper_fill_fix);
938
Dave Airlie38651672010-03-30 05:34:13 +0000939void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000940 uint32_t fb_width, uint32_t fb_height)
941{
Dave Airlie38651672010-03-30 05:34:13 +0000942 struct drm_framebuffer *fb = fb_helper->fb;
943 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000944 info->var.xres_virtual = fb->width;
945 info->var.yres_virtual = fb->height;
946 info->var.bits_per_pixel = fb->bits_per_pixel;
947 info->var.xoffset = 0;
948 info->var.yoffset = 0;
949 info->var.activate = FB_ACTIVATE_NOW;
950 info->var.height = -1;
951 info->var.width = -1;
952
953 switch (fb->depth) {
954 case 8:
955 info->var.red.offset = 0;
956 info->var.green.offset = 0;
957 info->var.blue.offset = 0;
958 info->var.red.length = 8; /* 8bit DAC */
959 info->var.green.length = 8;
960 info->var.blue.length = 8;
961 info->var.transp.offset = 0;
962 info->var.transp.length = 0;
963 break;
964 case 15:
965 info->var.red.offset = 10;
966 info->var.green.offset = 5;
967 info->var.blue.offset = 0;
968 info->var.red.length = 5;
969 info->var.green.length = 5;
970 info->var.blue.length = 5;
971 info->var.transp.offset = 15;
972 info->var.transp.length = 1;
973 break;
974 case 16:
975 info->var.red.offset = 11;
976 info->var.green.offset = 5;
977 info->var.blue.offset = 0;
978 info->var.red.length = 5;
979 info->var.green.length = 6;
980 info->var.blue.length = 5;
981 info->var.transp.offset = 0;
982 break;
983 case 24:
984 info->var.red.offset = 16;
985 info->var.green.offset = 8;
986 info->var.blue.offset = 0;
987 info->var.red.length = 8;
988 info->var.green.length = 8;
989 info->var.blue.length = 8;
990 info->var.transp.offset = 0;
991 info->var.transp.length = 0;
992 break;
993 case 32:
994 info->var.red.offset = 16;
995 info->var.green.offset = 8;
996 info->var.blue.offset = 0;
997 info->var.red.length = 8;
998 info->var.green.length = 8;
999 info->var.blue.length = 8;
1000 info->var.transp.offset = 24;
1001 info->var.transp.length = 8;
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 info->var.xres = fb_width;
1008 info->var.yres = fb_height;
1009}
1010EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001011
1012static int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
1013 uint32_t maxY)
1014{
1015 struct drm_connector *connector;
1016 int count = 0;
1017
1018 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1019 count += connector->funcs->fill_modes(connector, maxX, maxY);
1020 }
1021
1022 return count;
1023}
1024
1025static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
1026{
1027 struct drm_display_mode *mode;
1028
1029 list_for_each_entry(mode, &connector->modes, head) {
1030 if (drm_mode_width(mode) > width ||
1031 drm_mode_height(mode) > height)
1032 continue;
1033 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1034 return mode;
1035 }
1036 return NULL;
1037}
1038
1039static bool drm_has_cmdline_mode(struct drm_connector *connector)
1040{
1041 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
1042 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1043
1044 if (!fb_help_conn)
1045 return false;
1046
1047 cmdline_mode = &fb_help_conn->cmdline_mode;
1048 return cmdline_mode->specified;
1049}
1050
1051static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_connector *connector, int width, int height)
1052{
1053 struct drm_fb_helper_connector *fb_help_conn = connector->fb_helper_private;
1054 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1055 struct drm_display_mode *mode = NULL;
1056
1057 if (!fb_help_conn)
1058 return mode;
1059
1060 cmdline_mode = &fb_help_conn->cmdline_mode;
1061 if (cmdline_mode->specified == false)
1062 return mode;
1063
1064 /* attempt to find a matching mode in the list of modes
1065 * we have gotten so far, if not add a CVT mode that conforms
1066 */
1067 if (cmdline_mode->rb || cmdline_mode->margins)
1068 goto create_mode;
1069
1070 list_for_each_entry(mode, &connector->modes, head) {
1071 /* check width/height */
1072 if (mode->hdisplay != cmdline_mode->xres ||
1073 mode->vdisplay != cmdline_mode->yres)
1074 continue;
1075
1076 if (cmdline_mode->refresh_specified) {
1077 if (mode->vrefresh != cmdline_mode->refresh)
1078 continue;
1079 }
1080
1081 if (cmdline_mode->interlace) {
1082 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1083 continue;
1084 }
1085 return mode;
1086 }
1087
1088create_mode:
1089 mode = drm_cvt_mode(connector->dev, cmdline_mode->xres,
1090 cmdline_mode->yres,
1091 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1092 cmdline_mode->rb, cmdline_mode->interlace,
1093 cmdline_mode->margins);
1094 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1095 list_add(&mode->head, &connector->modes);
1096 return mode;
1097}
1098
1099static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1100{
1101 bool enable;
1102
1103 if (strict) {
1104 enable = connector->status == connector_status_connected;
1105 } else {
1106 enable = connector->status != connector_status_disconnected;
1107 }
1108 return enable;
1109}
1110
1111static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
1112{
1113 bool any_enabled = false;
1114 struct drm_connector *connector;
1115 int i = 0;
1116
1117 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1118 enabled[i] = drm_connector_enabled(connector, true);
1119 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1120 enabled[i] ? "yes" : "no");
1121 any_enabled |= enabled[i];
1122 i++;
1123 }
1124
1125 if (any_enabled)
1126 return;
1127
1128 i = 0;
1129 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1130 enabled[i] = drm_connector_enabled(connector, false);
1131 i++;
1132 }
1133}
1134
1135static bool drm_target_preferred(struct drm_device *dev,
1136 struct drm_display_mode **modes,
1137 bool *enabled, int width, int height)
1138{
1139 struct drm_connector *connector;
1140 int i = 0;
1141
1142 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1143
1144 if (enabled[i] == false) {
1145 i++;
1146 continue;
1147 }
1148
1149 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1150 connector->base.id);
1151
1152 /* got for command line mode first */
1153 modes[i] = drm_pick_cmdline_mode(connector, width, height);
1154 if (!modes[i]) {
1155 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1156 connector->base.id);
1157 modes[i] = drm_has_preferred_mode(connector, width, height);
1158 }
1159 /* No preferred modes, pick one off the list */
1160 if (!modes[i] && !list_empty(&connector->modes)) {
1161 list_for_each_entry(modes[i], &connector->modes, head)
1162 break;
1163 }
1164 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1165 "none");
1166 i++;
1167 }
1168 return true;
1169}
1170
1171static int drm_pick_crtcs(struct drm_device *dev,
1172 struct drm_crtc **best_crtcs,
1173 struct drm_display_mode **modes,
1174 int n, int width, int height)
1175{
1176 int c, o;
1177 struct drm_connector *connector;
1178 struct drm_connector_helper_funcs *connector_funcs;
1179 struct drm_encoder *encoder;
1180 struct drm_crtc *best_crtc;
1181 int my_score, best_score, score;
1182 struct drm_crtc **crtcs, *crtc;
1183
1184 if (n == dev->mode_config.num_connector)
1185 return 0;
1186 c = 0;
1187 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1188 if (c == n)
1189 break;
1190 c++;
1191 }
1192
1193 best_crtcs[n] = NULL;
1194 best_crtc = NULL;
1195 best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
1196 if (modes[n] == NULL)
1197 return best_score;
1198
1199 crtcs = kmalloc(dev->mode_config.num_connector *
1200 sizeof(struct drm_crtc *), GFP_KERNEL);
1201 if (!crtcs)
1202 return best_score;
1203
1204 my_score = 1;
1205 if (connector->status == connector_status_connected)
1206 my_score++;
1207 if (drm_has_cmdline_mode(connector))
1208 my_score++;
1209 if (drm_has_preferred_mode(connector, width, height))
1210 my_score++;
1211
1212 connector_funcs = connector->helper_private;
1213 encoder = connector_funcs->best_encoder(connector);
1214 if (!encoder)
1215 goto out;
1216
1217 connector->encoder = encoder;
1218
1219 /* select a crtc for this connector and then attempt to configure
1220 remaining connectors */
1221 c = 0;
1222 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1223
1224 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1225 c++;
1226 continue;
1227 }
1228
1229 for (o = 0; o < n; o++)
1230 if (best_crtcs[o] == crtc)
1231 break;
1232
1233 if (o < n) {
1234 /* ignore cloning for now */
1235 c++;
1236 continue;
1237 }
1238
1239 crtcs[n] = crtc;
1240 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
1241 score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
1242 width, height);
1243 if (score > best_score) {
1244 best_crtc = crtc;
1245 best_score = score;
1246 memcpy(best_crtcs, crtcs,
1247 dev->mode_config.num_connector *
1248 sizeof(struct drm_crtc *));
1249 }
1250 c++;
1251 }
1252out:
1253 kfree(crtcs);
1254 return best_score;
1255}
1256
1257static void drm_setup_crtcs(struct drm_device *dev)
1258{
1259 struct drm_crtc **crtcs;
1260 struct drm_display_mode **modes;
1261 struct drm_encoder *encoder;
1262 struct drm_connector *connector;
1263 bool *enabled;
1264 int width, height;
1265 int i, ret;
1266
1267 DRM_DEBUG_KMS("\n");
1268
1269 width = dev->mode_config.max_width;
1270 height = dev->mode_config.max_height;
1271
1272 /* clean out all the encoder/crtc combos */
1273 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1274 encoder->crtc = NULL;
1275 }
1276
1277 crtcs = kcalloc(dev->mode_config.num_connector,
1278 sizeof(struct drm_crtc *), GFP_KERNEL);
1279 modes = kcalloc(dev->mode_config.num_connector,
1280 sizeof(struct drm_display_mode *), GFP_KERNEL);
1281 enabled = kcalloc(dev->mode_config.num_connector,
1282 sizeof(bool), GFP_KERNEL);
1283
1284 drm_enable_connectors(dev, enabled);
1285
1286 ret = drm_target_preferred(dev, modes, enabled, width, height);
1287 if (!ret)
1288 DRM_ERROR("Unable to find initial modes\n");
1289
1290 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1291
1292 drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
1293
1294 i = 0;
1295 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1296 struct drm_display_mode *mode = modes[i];
1297 struct drm_crtc *crtc = crtcs[i];
1298
1299 if (connector->encoder == NULL) {
1300 i++;
1301 continue;
1302 }
1303
1304 if (mode && crtc) {
1305 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1306 mode->name, crtc->base.id);
1307 crtc->desired_mode = mode;
1308 connector->encoder->crtc = crtc;
1309 } else {
1310 connector->encoder->crtc = NULL;
1311 connector->encoder = NULL;
1312 }
1313 i++;
1314 }
1315
1316 kfree(crtcs);
1317 kfree(modes);
1318 kfree(enabled);
1319}
1320
1321/**
1322 * drm_helper_initial_config - setup a sane initial connector configuration
1323 * @dev: DRM device
1324 *
1325 * LOCKING:
1326 * Called at init time, must take mode config lock.
1327 *
1328 * Scan the CRTCs and connectors and try to put together an initial setup.
1329 * At the moment, this is a cloned configuration across all heads with
1330 * a new framebuffer object as the backing store.
1331 *
1332 * RETURNS:
1333 * Zero if everything went ok, nonzero otherwise.
1334 */
1335bool drm_helper_initial_config(struct drm_device *dev)
1336{
1337 int count = 0;
1338
1339 /* disable all the possible outputs/crtcs before entering KMS mode */
1340 drm_helper_disable_unused_functions(dev);
1341
1342 drm_fb_helper_parse_command_line(dev);
1343
1344 count = drm_helper_probe_connector_modes(dev,
1345 dev->mode_config.max_width,
1346 dev->mode_config.max_height);
1347
1348 /*
1349 * we shouldn't end up with no modes here.
1350 */
1351 if (count == 0)
1352 printk(KERN_INFO "No connectors reported connected with modes\n");
1353
1354 drm_setup_crtcs(dev);
1355
1356 return 0;
1357}
1358EXPORT_SYMBOL(drm_helper_initial_config);
1359
1360bool drm_helper_fb_hotplug_event(struct drm_device *dev)
1361{
1362 DRM_DEBUG_KMS("\n");
1363
1364 drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
1365 dev->mode_config.max_height);
1366
1367 drm_setup_crtcs(dev);
1368
1369 return true;
1370}
1371EXPORT_SYMBOL(drm_helper_fb_hotplug_event);