blob: de82e201d6826d8ac0e422b22a2d0a93f8312d82 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#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
Dave Airlie6fcefd52009-09-08 11:08:32 +100039MODULE_AUTHOR("David Airlie, Jesse Barnes");
40MODULE_DESCRIPTION("DRM KMS helper");
41MODULE_LICENSE("GPL and additional rights");
42
Dave Airlie785b93e2009-08-28 15:46:53 +100043static LIST_HEAD(kernel_fb_helper_list);
44
Dave Airlie0b4c0f32010-03-30 05:34:15 +000045/* simple single crtc case helper function */
46int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100047{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000048 struct drm_device *dev = fb_helper->dev;
49 struct drm_connector *connector;
50 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100051
Dave Airlie0b4c0f32010-03-30 05:34:15 +000052 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;
58
59 fb_helper_connector->connector = connector;
60 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
61 }
Dave Airlied50ba252009-09-23 14:44:08 +100062 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000063fail:
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;
Dave Airlied50ba252009-09-23 14:44:08 +100070}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000071EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100072
Dave Airlied50ba252009-09-23 14:44:08 +100073/**
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 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000086static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlied50ba252009-09-23 14:44:08 +100087 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;
Dave Airlie8ef86782009-09-26 06:39:00 +100096 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000097 struct drm_connector *connector = fb_helper_conn->connector;
Dave Airlied50ba252009-09-23 14:44:08 +100098
Dave Airlie0b4c0f32010-03-30 05:34:15 +000099 if (!fb_helper_conn)
Dave Airlie8ef86782009-09-26 06:39:00 +1000100 return false;
101
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000102 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000103 if (!mode_option)
104 mode_option = fb_mode_option;
105
106 if (!mode_option) {
107 cmdline_mode->specified = false;
108 return false;
109 }
110
111 name = mode_option;
112 namelen = strlen(name);
113 for (i = namelen-1; i >= 0; i--) {
114 switch (name[i]) {
115 case '@':
116 namelen = i;
117 if (!refresh_specified && !bpp_specified &&
118 !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800119 refresh = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000120 refresh_specified = 1;
121 if (cvt || rb)
122 cvt = 0;
123 } else
124 goto done;
125 break;
126 case '-':
127 namelen = i;
128 if (!bpp_specified && !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800129 bpp = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000130 bpp_specified = 1;
131 if (cvt || rb)
132 cvt = 0;
133 } else
134 goto done;
135 break;
136 case 'x':
137 if (!yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800138 yres = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000139 yres_specified = 1;
140 } else
141 goto done;
142 case '0' ... '9':
143 break;
144 case 'M':
145 if (!yres_specified)
146 cvt = 1;
147 break;
148 case 'R':
Adam Jacksonb829e012010-06-10 13:33:26 -0400149 if (cvt)
Dave Airlied50ba252009-09-23 14:44:08 +1000150 rb = 1;
151 break;
152 case 'm':
153 if (!cvt)
154 margins = 1;
155 break;
156 case 'i':
157 if (!cvt)
158 interlace = 1;
159 break;
160 case 'e':
161 force = DRM_FORCE_ON;
162 break;
163 case 'D':
Roel Kluine89a8c92009-12-31 13:06:29 +0100164 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
Dave Airlied50ba252009-09-23 14:44:08 +1000165 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
166 force = DRM_FORCE_ON;
167 else
168 force = DRM_FORCE_ON_DIGITAL;
169 break;
170 case 'd':
171 force = DRM_FORCE_OFF;
172 break;
173 default:
174 goto done;
175 }
176 }
177 if (i < 0 && yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800178 xres = simple_strtol(name, NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000179 res_specified = 1;
180 }
181done:
182
183 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
184 drm_get_connector_name(connector), xres, yres,
185 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
186 "", (margins) ? " with margins" : "", (interlace) ?
187 " interlaced" : "");
188
189 if (force) {
190 const char *s;
191 switch (force) {
192 case DRM_FORCE_OFF: s = "OFF"; break;
193 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
194 default:
195 case DRM_FORCE_ON: s = "ON"; break;
196 }
197
198 DRM_INFO("forcing %s connector %s\n",
199 drm_get_connector_name(connector), s);
200 connector->force = force;
201 }
202
203 if (res_specified) {
204 cmdline_mode->specified = true;
205 cmdline_mode->xres = xres;
206 cmdline_mode->yres = yres;
207 }
208
209 if (refresh_specified) {
210 cmdline_mode->refresh_specified = true;
211 cmdline_mode->refresh = refresh;
212 }
213
214 if (bpp_specified) {
215 cmdline_mode->bpp_specified = true;
216 cmdline_mode->bpp = bpp;
217 }
218 cmdline_mode->rb = rb ? true : false;
219 cmdline_mode->cvt = cvt ? true : false;
220 cmdline_mode->interlace = interlace ? true : false;
221
222 return true;
223}
224
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000225static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000226{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000227 struct drm_fb_helper_connector *fb_helper_conn;
228 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000229
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000230 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlied50ba252009-09-23 14:44:08 +1000231 char *option = NULL;
232
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000233 fb_helper_conn = fb_helper->connector_info[i];
234
Dave Airlied50ba252009-09-23 14:44:08 +1000235 /* do something on return - turn off connector maybe */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000236 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +1000237 continue;
238
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000239 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
Dave Airlied50ba252009-09-23 14:44:08 +1000240 }
241 return 0;
242}
243
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500244int drm_fb_helper_debug_enter(struct fb_info *info)
245{
246 struct drm_fb_helper *helper = info->par;
247 struct drm_crtc_helper_funcs *funcs;
248 int i;
249
250 if (list_empty(&kernel_fb_helper_list))
251 return false;
252
253 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
254 for (i = 0; i < helper->crtc_count; i++) {
255 struct drm_mode_set *mode_set =
256 &helper->crtc_info[i].mode_set;
257
258 if (!mode_set->crtc->enabled)
259 continue;
260
261 funcs = mode_set->crtc->helper_private;
262 funcs->mode_set_base_atomic(mode_set->crtc,
263 mode_set->fb,
264 mode_set->x,
265 mode_set->y);
266
267 }
268 }
269
270 return 0;
271}
272EXPORT_SYMBOL(drm_fb_helper_debug_enter);
273
274/* Find the real fb for a given fb helper CRTC */
275static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
276{
277 struct drm_device *dev = crtc->dev;
278 struct drm_crtc *c;
279
280 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
281 if (crtc->base.id == c->base.id)
282 return c->fb;
283 }
284
285 return NULL;
286}
287
288int drm_fb_helper_debug_leave(struct fb_info *info)
289{
290 struct drm_fb_helper *helper = info->par;
291 struct drm_crtc *crtc;
292 struct drm_crtc_helper_funcs *funcs;
293 struct drm_framebuffer *fb;
294 int i;
295
296 for (i = 0; i < helper->crtc_count; i++) {
297 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
298 crtc = mode_set->crtc;
299 funcs = crtc->helper_private;
300 fb = drm_mode_config_fb(crtc);
301
302 if (!crtc->enabled)
303 continue;
304
305 if (!fb) {
306 DRM_ERROR("no fb to restore??\n");
307 continue;
308 }
309
310 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
311 crtc->y);
312 }
313
314 return 0;
315}
316EXPORT_SYMBOL(drm_fb_helper_debug_leave);
317
Dave Airlie785b93e2009-08-28 15:46:53 +1000318bool drm_fb_helper_force_kernel_mode(void)
319{
320 int i = 0;
321 bool ret, error = false;
322 struct drm_fb_helper *helper;
323
324 if (list_empty(&kernel_fb_helper_list))
325 return false;
326
327 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
328 for (i = 0; i < helper->crtc_count; i++) {
329 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
330 ret = drm_crtc_helper_set_config(mode_set);
331 if (ret)
332 error = true;
333 }
334 }
335 return error;
336}
337
338int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
339 void *panic_str)
340{
Dave Airliefc2362a2010-06-07 12:14:54 +1000341 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000342 return drm_fb_helper_force_kernel_mode();
343 return 0;
344}
345EXPORT_SYMBOL(drm_fb_helper_panic);
346
347static struct notifier_block paniced = {
348 .notifier_call = drm_fb_helper_panic,
349};
350
351/**
352 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
353 *
354 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
355 */
356void drm_fb_helper_restore(void)
357{
358 bool ret;
359 ret = drm_fb_helper_force_kernel_mode();
360 if (ret == true)
361 DRM_ERROR("Failed to restore crtc configuration\n");
362}
363EXPORT_SYMBOL(drm_fb_helper_restore);
364
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200365#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000366static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
367{
368 drm_fb_helper_restore();
369}
370static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
371
372static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
373{
374 schedule_work(&drm_fb_helper_restore_work);
375}
376
377static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
378 .handler = drm_fb_helper_sysrq,
379 .help_msg = "force-fb(V)",
380 .action_msg = "Restore framebuffer console",
381};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000382#else
383static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200384#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000385
386static void drm_fb_helper_on(struct fb_info *info)
387{
388 struct drm_fb_helper *fb_helper = info->par;
389 struct drm_device *dev = fb_helper->dev;
390 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000391 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700392 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000393 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700394 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000395
396 /*
397 * For each CRTC in this fb, turn the crtc on then,
398 * find all associated encoders and turn them on.
399 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000400 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700401 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000402 crtc = fb_helper->crtc_info[i].mode_set.crtc;
403 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000404
Dave Airlie8be48d92010-03-30 05:34:14 +0000405 if (!crtc->enabled)
406 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000407
Dave Airlie8be48d92010-03-30 05:34:14 +0000408 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000409
Jesse Barnes023eb572010-07-02 10:48:08 -0700410 /* Walk the connectors & encoders on this fb turning them on */
411 for (j = 0; j < fb_helper->connector_count; j++) {
412 connector = fb_helper->connector_info[j]->connector;
413 connector->dpms = DRM_MODE_DPMS_ON;
414 drm_connector_property_set_value(connector,
415 dev->mode_config.dpms_property,
416 DRM_MODE_DPMS_ON);
417 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000418 /* Found a CRTC on this fb, now find encoders */
419 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
420 if (encoder->crtc == crtc) {
421 struct drm_encoder_helper_funcs *encoder_funcs;
422
423 encoder_funcs = encoder->helper_private;
424 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000425 }
426 }
427 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000428 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000429}
430
431static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
432{
433 struct drm_fb_helper *fb_helper = info->par;
434 struct drm_device *dev = fb_helper->dev;
435 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000436 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700437 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000438 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700439 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000440
441 /*
442 * For each CRTC in this fb, find all associated encoders
443 * and turn them off, then turn off the CRTC.
444 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000445 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700446 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000447 crtc = fb_helper->crtc_info[i].mode_set.crtc;
448 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000449
Dave Airlie8be48d92010-03-30 05:34:14 +0000450 if (!crtc->enabled)
451 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000452
Jesse Barnes023eb572010-07-02 10:48:08 -0700453 /* Walk the connectors on this fb and mark them off */
454 for (j = 0; j < fb_helper->connector_count; j++) {
455 connector = fb_helper->connector_info[j]->connector;
456 connector->dpms = dpms_mode;
457 drm_connector_property_set_value(connector,
458 dev->mode_config.dpms_property,
459 dpms_mode);
460 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000461 /* Found a CRTC on this fb, now find encoders */
462 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
463 if (encoder->crtc == crtc) {
464 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000465
Dave Airlie8be48d92010-03-30 05:34:14 +0000466 encoder_funcs = encoder->helper_private;
467 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700468 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000469 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000470 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000471 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000472 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000473}
474
475int drm_fb_helper_blank(int blank, struct fb_info *info)
476{
477 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000478 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000479 case FB_BLANK_UNBLANK:
480 drm_fb_helper_on(info);
481 break;
James Simmons731b5a12009-10-29 20:39:07 +0000482 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000483 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800484 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000485 break;
James Simmons731b5a12009-10-29 20:39:07 +0000486 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000487 case FB_BLANK_HSYNC_SUSPEND:
488 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
489 break;
James Simmons731b5a12009-10-29 20:39:07 +0000490 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000491 case FB_BLANK_VSYNC_SUSPEND:
492 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
493 break;
James Simmons731b5a12009-10-29 20:39:07 +0000494 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000495 case FB_BLANK_POWERDOWN:
496 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
497 break;
498 }
499 return 0;
500}
501EXPORT_SYMBOL(drm_fb_helper_blank);
502
503static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
504{
505 int i;
506
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000507 for (i = 0; i < helper->connector_count; i++)
508 kfree(helper->connector_info[i]);
509 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000510 for (i = 0; i < helper->crtc_count; i++)
511 kfree(helper->crtc_info[i].mode_set.connectors);
512 kfree(helper->crtc_info);
513}
514
Dave Airlie4abe3522010-03-30 05:34:18 +0000515int drm_fb_helper_init(struct drm_device *dev,
516 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000517 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000518{
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 struct drm_crtc *crtc;
520 int ret = 0;
521 int i;
522
Dave Airlie4abe3522010-03-30 05:34:18 +0000523 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000524
525 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
526
527 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
528 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000529 return -ENOMEM;
530
Dave Airlie4abe3522010-03-30 05:34:18 +0000531 fb_helper->crtc_count = crtc_count;
532 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
533 if (!fb_helper->connector_info) {
534 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000535 return -ENOMEM;
536 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000537 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000538
539 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000540 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000541 kcalloc(max_conn_count,
542 sizeof(struct drm_connector *),
543 GFP_KERNEL);
544
Dave Airlie4abe3522010-03-30 05:34:18 +0000545 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000546 ret = -ENOMEM;
547 goto out_free;
548 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000549 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000550 }
551
552 i = 0;
553 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000554 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
555 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000556 i++;
557 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000558 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000559 return 0;
560out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000561 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000562 return -ENOMEM;
563}
Dave Airlie4abe3522010-03-30 05:34:18 +0000564EXPORT_SYMBOL(drm_fb_helper_init);
565
566void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
567{
568 if (!list_empty(&fb_helper->kernel_fb_list)) {
569 list_del(&fb_helper->kernel_fb_list);
570 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400571 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000572 atomic_notifier_chain_unregister(&panic_notifier_list,
573 &paniced);
574 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
575 }
576 }
577
578 drm_fb_helper_crtc_free(fb_helper);
579
Dave Airlie4abe3522010-03-30 05:34:18 +0000580}
581EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000582
Dave Airliec850cb72009-10-23 18:49:03 +1000583static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000584 u16 blue, u16 regno, struct fb_info *info)
585{
586 struct drm_fb_helper *fb_helper = info->par;
587 struct drm_framebuffer *fb = fb_helper->fb;
588 int pindex;
589
Dave Airliec850cb72009-10-23 18:49:03 +1000590 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
591 u32 *palette;
592 u32 value;
593 /* place color in psuedopalette */
594 if (regno > 16)
595 return -EINVAL;
596 palette = (u32 *)info->pseudo_palette;
597 red >>= (16 - info->var.red.length);
598 green >>= (16 - info->var.green.length);
599 blue >>= (16 - info->var.blue.length);
600 value = (red << info->var.red.offset) |
601 (green << info->var.green.offset) |
602 (blue << info->var.blue.offset);
603 palette[regno] = value;
604 return 0;
605 }
606
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000607 pindex = regno;
608
609 if (fb->bits_per_pixel == 16) {
610 pindex = regno << 3;
611
612 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000613 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000614 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000615 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000616
617 if (fb->depth == 16) {
618 u16 r, g, b;
619 int i;
620 if (regno < 32) {
621 for (i = 0; i < 8; i++)
622 fb_helper->funcs->gamma_set(crtc, red,
623 green, blue, pindex + i);
624 }
625
626 fb_helper->funcs->gamma_get(crtc, &r,
627 &g, &b,
628 pindex >> 1);
629
630 for (i = 0; i < 4; i++)
631 fb_helper->funcs->gamma_set(crtc, r,
632 green, b,
633 (pindex >> 1) + i);
634 }
635 }
636
637 if (fb->depth != 16)
638 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000639 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000640}
641
Dave Airlie068143d2009-10-05 09:58:02 +1000642int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
643{
644 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000645 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000646 u16 *red, *green, *blue, *transp;
647 struct drm_crtc *crtc;
648 int i, rc = 0;
649 int start;
650
Dave Airlie8be48d92010-03-30 05:34:14 +0000651 for (i = 0; i < fb_helper->crtc_count; i++) {
652 crtc = fb_helper->crtc_info[i].mode_set.crtc;
653 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000654
655 red = cmap->red;
656 green = cmap->green;
657 blue = cmap->blue;
658 transp = cmap->transp;
659 start = cmap->start;
660
661 for (i = 0; i < cmap->len; i++) {
662 u16 hred, hgreen, hblue, htransp = 0xffff;
663
664 hred = *red++;
665 hgreen = *green++;
666 hblue = *blue++;
667
668 if (transp)
669 htransp = *transp++;
670
Dave Airliec850cb72009-10-23 18:49:03 +1000671 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
672 if (rc)
673 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000674 }
675 crtc_funcs->load_lut(crtc);
676 }
677 return rc;
678}
679EXPORT_SYMBOL(drm_fb_helper_setcmap);
680
Dave Airlie785b93e2009-08-28 15:46:53 +1000681int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
682 struct fb_info *info)
683{
684 struct drm_fb_helper *fb_helper = info->par;
685 struct drm_framebuffer *fb = fb_helper->fb;
686 int depth;
687
Jason Wesself90ebd92010-08-05 09:22:32 -0500688 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000689 return -EINVAL;
690
691 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000692 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
693 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
694 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
695 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000696 return -EINVAL;
697 }
698
699 switch (var->bits_per_pixel) {
700 case 16:
701 depth = (var->green.length == 6) ? 16 : 15;
702 break;
703 case 32:
704 depth = (var->transp.length > 0) ? 32 : 24;
705 break;
706 default:
707 depth = var->bits_per_pixel;
708 break;
709 }
710
711 switch (depth) {
712 case 8:
713 var->red.offset = 0;
714 var->green.offset = 0;
715 var->blue.offset = 0;
716 var->red.length = 8;
717 var->green.length = 8;
718 var->blue.length = 8;
719 var->transp.length = 0;
720 var->transp.offset = 0;
721 break;
722 case 15:
723 var->red.offset = 10;
724 var->green.offset = 5;
725 var->blue.offset = 0;
726 var->red.length = 5;
727 var->green.length = 5;
728 var->blue.length = 5;
729 var->transp.length = 1;
730 var->transp.offset = 15;
731 break;
732 case 16:
733 var->red.offset = 11;
734 var->green.offset = 5;
735 var->blue.offset = 0;
736 var->red.length = 5;
737 var->green.length = 6;
738 var->blue.length = 5;
739 var->transp.length = 0;
740 var->transp.offset = 0;
741 break;
742 case 24:
743 var->red.offset = 16;
744 var->green.offset = 8;
745 var->blue.offset = 0;
746 var->red.length = 8;
747 var->green.length = 8;
748 var->blue.length = 8;
749 var->transp.length = 0;
750 var->transp.offset = 0;
751 break;
752 case 32:
753 var->red.offset = 16;
754 var->green.offset = 8;
755 var->blue.offset = 0;
756 var->red.length = 8;
757 var->green.length = 8;
758 var->blue.length = 8;
759 var->transp.length = 8;
760 var->transp.offset = 24;
761 break;
762 default:
763 return -EINVAL;
764 }
765 return 0;
766}
767EXPORT_SYMBOL(drm_fb_helper_check_var);
768
769/* this will let fbcon do the mode init */
770int drm_fb_helper_set_par(struct fb_info *info)
771{
772 struct drm_fb_helper *fb_helper = info->par;
773 struct drm_device *dev = fb_helper->dev;
774 struct fb_var_screeninfo *var = &info->var;
775 struct drm_crtc *crtc;
776 int ret;
777 int i;
778
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100779 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000780 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000781 return -EINVAL;
782 }
783
Dave Airlie8be48d92010-03-30 05:34:14 +0000784 mutex_lock(&dev->mode_config.mutex);
785 for (i = 0; i < fb_helper->crtc_count; i++) {
786 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000787 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
788 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000789 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000790 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000791 }
792 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000793 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000794
795 if (fb_helper->delayed_hotplug) {
796 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000797 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000798 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000799 return 0;
800}
801EXPORT_SYMBOL(drm_fb_helper_set_par);
802
803int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
804 struct fb_info *info)
805{
806 struct drm_fb_helper *fb_helper = info->par;
807 struct drm_device *dev = fb_helper->dev;
808 struct drm_mode_set *modeset;
809 struct drm_crtc *crtc;
810 int ret = 0;
811 int i;
812
Dave Airlie8be48d92010-03-30 05:34:14 +0000813 mutex_lock(&dev->mode_config.mutex);
814 for (i = 0; i < fb_helper->crtc_count; i++) {
815 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000816
817 modeset = &fb_helper->crtc_info[i].mode_set;
818
819 modeset->x = var->xoffset;
820 modeset->y = var->yoffset;
821
822 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000823 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 if (!ret) {
825 info->var.xoffset = var->xoffset;
826 info->var.yoffset = var->yoffset;
827 }
828 }
829 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000830 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000831 return ret;
832}
833EXPORT_SYMBOL(drm_fb_helper_pan_display);
834
Dave Airlie8be48d92010-03-30 05:34:14 +0000835int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
836 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000837{
Dave Airlie785b93e2009-08-28 15:46:53 +1000838 int new_fb = 0;
839 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000840 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000841 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000842 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000843 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000844
845 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
846 sizes.surface_depth = 24;
847 sizes.surface_bpp = 32;
848 sizes.fb_width = (unsigned)-1;
849 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000850
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000851 /* if driver picks 8 or 16 by default use that
852 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000853 if (preferred_bpp != sizes.surface_bpp) {
854 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000855 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000856 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000857 for (i = 0; i < fb_helper->connector_count; i++) {
858 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000859 struct drm_fb_helper_cmdline_mode *cmdline_mode;
860
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000861 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000862
863 if (cmdline_mode->bpp_specified) {
864 switch (cmdline_mode->bpp) {
865 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000866 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000867 break;
868 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000869 sizes.surface_depth = 15;
870 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000871 break;
872 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000873 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000874 break;
875 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000876 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000877 break;
878 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000879 sizes.surface_depth = 24;
880 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000881 break;
882 }
883 break;
884 }
885 }
886
Dave Airlie8be48d92010-03-30 05:34:14 +0000887 crtc_count = 0;
888 for (i = 0; i < fb_helper->crtc_count; i++) {
889 struct drm_display_mode *desired_mode;
890 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000891
Dave Airlie8be48d92010-03-30 05:34:14 +0000892 if (desired_mode) {
893 if (gamma_size == 0)
894 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
895 if (desired_mode->hdisplay < sizes.fb_width)
896 sizes.fb_width = desired_mode->hdisplay;
897 if (desired_mode->vdisplay < sizes.fb_height)
898 sizes.fb_height = desired_mode->vdisplay;
899 if (desired_mode->hdisplay > sizes.surface_width)
900 sizes.surface_width = desired_mode->hdisplay;
901 if (desired_mode->vdisplay > sizes.surface_height)
902 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000903 crtc_count++;
904 }
905 }
906
Dave Airlie38651672010-03-30 05:34:13 +0000907 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000908 /* hmm everyone went away - assume VGA cable just fell out
909 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000910 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000911 sizes.fb_width = sizes.surface_width = 1024;
912 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000913 }
914
Dave Airlie38651672010-03-30 05:34:13 +0000915 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000916 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000917 if (new_fb < 0)
918 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000919
Dave Airlie38651672010-03-30 05:34:13 +0000920 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000921
Dave Airlie8be48d92010-03-30 05:34:14 +0000922 /* set the fb pointer */
923 for (i = 0; i < fb_helper->crtc_count; i++) {
924 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000925 }
926
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100928 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100929 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000930 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100931 }
Dave Airlie38651672010-03-30 05:34:13 +0000932
933 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
934 info->fix.id);
935
Dave Airlie785b93e2009-08-28 15:46:53 +1000936 } else {
937 drm_fb_helper_set_par(info);
938 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000939
940 /* Switch back to kernel console on panic */
941 /* multi card linked list maybe */
942 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400943 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000944 atomic_notifier_chain_register(&panic_notifier_list,
945 &paniced);
946 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
947 }
Dave Airlie38651672010-03-30 05:34:13 +0000948 if (new_fb)
949 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
950
Dave Airlie785b93e2009-08-28 15:46:53 +1000951 return 0;
952}
953EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
954
Dave Airlie068143d2009-10-05 09:58:02 +1000955void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
956 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000957{
958 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000959 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000960 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000961 info->fix.type_aux = 0;
962 info->fix.xpanstep = 1; /* doing it in hw */
963 info->fix.ypanstep = 1; /* doing it in hw */
964 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000965 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000966 info->fix.type_aux = 0;
967
968 info->fix.line_length = pitch;
969 return;
970}
971EXPORT_SYMBOL(drm_fb_helper_fill_fix);
972
Dave Airlie38651672010-03-30 05:34:13 +0000973void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000974 uint32_t fb_width, uint32_t fb_height)
975{
Dave Airlie38651672010-03-30 05:34:13 +0000976 struct drm_framebuffer *fb = fb_helper->fb;
977 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000978 info->var.xres_virtual = fb->width;
979 info->var.yres_virtual = fb->height;
980 info->var.bits_per_pixel = fb->bits_per_pixel;
981 info->var.xoffset = 0;
982 info->var.yoffset = 0;
983 info->var.activate = FB_ACTIVATE_NOW;
984 info->var.height = -1;
985 info->var.width = -1;
986
987 switch (fb->depth) {
988 case 8:
989 info->var.red.offset = 0;
990 info->var.green.offset = 0;
991 info->var.blue.offset = 0;
992 info->var.red.length = 8; /* 8bit DAC */
993 info->var.green.length = 8;
994 info->var.blue.length = 8;
995 info->var.transp.offset = 0;
996 info->var.transp.length = 0;
997 break;
998 case 15:
999 info->var.red.offset = 10;
1000 info->var.green.offset = 5;
1001 info->var.blue.offset = 0;
1002 info->var.red.length = 5;
1003 info->var.green.length = 5;
1004 info->var.blue.length = 5;
1005 info->var.transp.offset = 15;
1006 info->var.transp.length = 1;
1007 break;
1008 case 16:
1009 info->var.red.offset = 11;
1010 info->var.green.offset = 5;
1011 info->var.blue.offset = 0;
1012 info->var.red.length = 5;
1013 info->var.green.length = 6;
1014 info->var.blue.length = 5;
1015 info->var.transp.offset = 0;
1016 break;
1017 case 24:
1018 info->var.red.offset = 16;
1019 info->var.green.offset = 8;
1020 info->var.blue.offset = 0;
1021 info->var.red.length = 8;
1022 info->var.green.length = 8;
1023 info->var.blue.length = 8;
1024 info->var.transp.offset = 0;
1025 info->var.transp.length = 0;
1026 break;
1027 case 32:
1028 info->var.red.offset = 16;
1029 info->var.green.offset = 8;
1030 info->var.blue.offset = 0;
1031 info->var.red.length = 8;
1032 info->var.green.length = 8;
1033 info->var.blue.length = 8;
1034 info->var.transp.offset = 24;
1035 info->var.transp.length = 8;
1036 break;
1037 default:
1038 break;
1039 }
1040
1041 info->var.xres = fb_width;
1042 info->var.yres = fb_height;
1043}
1044EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001045
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001046static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1047 uint32_t maxX,
1048 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001049{
1050 struct drm_connector *connector;
1051 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001052 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001053
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001054 for (i = 0; i < fb_helper->connector_count; i++) {
1055 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001056 count += connector->funcs->fill_modes(connector, maxX, maxY);
1057 }
1058
1059 return count;
1060}
1061
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001062static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001063{
1064 struct drm_display_mode *mode;
1065
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001066 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001067 if (drm_mode_width(mode) > width ||
1068 drm_mode_height(mode) > height)
1069 continue;
1070 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1071 return mode;
1072 }
1073 return NULL;
1074}
1075
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001076static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001077{
Dave Airlie38651672010-03-30 05:34:13 +00001078 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001079 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001080 return cmdline_mode->specified;
1081}
1082
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001083static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1084 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001085{
Dave Airlie38651672010-03-30 05:34:13 +00001086 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1087 struct drm_display_mode *mode = NULL;
1088
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001089 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001090 if (cmdline_mode->specified == false)
1091 return mode;
1092
1093 /* attempt to find a matching mode in the list of modes
1094 * we have gotten so far, if not add a CVT mode that conforms
1095 */
1096 if (cmdline_mode->rb || cmdline_mode->margins)
1097 goto create_mode;
1098
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001099 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001100 /* check width/height */
1101 if (mode->hdisplay != cmdline_mode->xres ||
1102 mode->vdisplay != cmdline_mode->yres)
1103 continue;
1104
1105 if (cmdline_mode->refresh_specified) {
1106 if (mode->vrefresh != cmdline_mode->refresh)
1107 continue;
1108 }
1109
1110 if (cmdline_mode->interlace) {
1111 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1112 continue;
1113 }
1114 return mode;
1115 }
1116
1117create_mode:
Adam Jacksonb829e012010-06-10 13:33:26 -04001118 if (cmdline_mode->cvt)
1119 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1120 cmdline_mode->xres, cmdline_mode->yres,
1121 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1122 cmdline_mode->rb, cmdline_mode->interlace,
1123 cmdline_mode->margins);
1124 else
1125 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1126 cmdline_mode->xres, cmdline_mode->yres,
1127 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1128 cmdline_mode->interlace,
1129 cmdline_mode->margins);
Dave Airlie38651672010-03-30 05:34:13 +00001130 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001131 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001132 return mode;
1133}
1134
1135static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1136{
1137 bool enable;
1138
1139 if (strict) {
1140 enable = connector->status == connector_status_connected;
1141 } else {
1142 enable = connector->status != connector_status_disconnected;
1143 }
1144 return enable;
1145}
1146
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001147static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1148 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001149{
1150 bool any_enabled = false;
1151 struct drm_connector *connector;
1152 int i = 0;
1153
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001154 for (i = 0; i < fb_helper->connector_count; i++) {
1155 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001156 enabled[i] = drm_connector_enabled(connector, true);
1157 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1158 enabled[i] ? "yes" : "no");
1159 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001160 }
1161
1162 if (any_enabled)
1163 return;
1164
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001165 for (i = 0; i < fb_helper->connector_count; i++) {
1166 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001167 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001168 }
1169}
1170
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001171static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1172 struct drm_display_mode **modes,
1173 bool *enabled, int width, int height)
1174{
1175 int count, i, j;
1176 bool can_clone = false;
1177 struct drm_fb_helper_connector *fb_helper_conn;
1178 struct drm_display_mode *dmt_mode, *mode;
1179
1180 /* only contemplate cloning in the single crtc case */
1181 if (fb_helper->crtc_count > 1)
1182 return false;
1183
1184 count = 0;
1185 for (i = 0; i < fb_helper->connector_count; i++) {
1186 if (enabled[i])
1187 count++;
1188 }
1189
1190 /* only contemplate cloning if more than one connector is enabled */
1191 if (count <= 1)
1192 return false;
1193
1194 /* check the command line or if nothing common pick 1024x768 */
1195 can_clone = true;
1196 for (i = 0; i < fb_helper->connector_count; i++) {
1197 if (!enabled[i])
1198 continue;
1199 fb_helper_conn = fb_helper->connector_info[i];
1200 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1201 if (!modes[i]) {
1202 can_clone = false;
1203 break;
1204 }
1205 for (j = 0; j < i; j++) {
1206 if (!enabled[j])
1207 continue;
1208 if (!drm_mode_equal(modes[j], modes[i]))
1209 can_clone = false;
1210 }
1211 }
1212
1213 if (can_clone) {
1214 DRM_DEBUG_KMS("can clone using command line\n");
1215 return true;
1216 }
1217
1218 /* try and find a 1024x768 mode on each connector */
1219 can_clone = true;
1220 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1221
1222 for (i = 0; i < fb_helper->connector_count; i++) {
1223
1224 if (!enabled[i])
1225 continue;
1226
1227 fb_helper_conn = fb_helper->connector_info[i];
1228 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1229 if (drm_mode_equal(mode, dmt_mode))
1230 modes[i] = mode;
1231 }
1232 if (!modes[i])
1233 can_clone = false;
1234 }
1235
1236 if (can_clone) {
1237 DRM_DEBUG_KMS("can clone using 1024x768\n");
1238 return true;
1239 }
1240 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1241 return false;
1242}
1243
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001244static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001245 struct drm_display_mode **modes,
1246 bool *enabled, int width, int height)
1247{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001248 struct drm_fb_helper_connector *fb_helper_conn;
1249 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001250
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001251 for (i = 0; i < fb_helper->connector_count; i++) {
1252 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001253
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001254 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001255 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001256
1257 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001258 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001259
1260 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001261 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001262 if (!modes[i]) {
1263 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001264 fb_helper_conn->connector->base.id);
1265 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001266 }
1267 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001268 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1269 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001270 break;
1271 }
1272 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1273 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001274 }
1275 return true;
1276}
1277
Dave Airlie8be48d92010-03-30 05:34:14 +00001278static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1279 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001280 struct drm_display_mode **modes,
1281 int n, int width, int height)
1282{
1283 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001284 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001285 struct drm_connector *connector;
1286 struct drm_connector_helper_funcs *connector_funcs;
1287 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001288 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001289 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001290 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001291 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001292
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001293 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001294 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001295
1296 fb_helper_conn = fb_helper->connector_info[n];
1297 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001298
1299 best_crtcs[n] = NULL;
1300 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001301 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001302 if (modes[n] == NULL)
1303 return best_score;
1304
Dave Airlie8be48d92010-03-30 05:34:14 +00001305 crtcs = kzalloc(dev->mode_config.num_connector *
1306 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001307 if (!crtcs)
1308 return best_score;
1309
1310 my_score = 1;
1311 if (connector->status == connector_status_connected)
1312 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001313 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001314 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001315 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001316 my_score++;
1317
1318 connector_funcs = connector->helper_private;
1319 encoder = connector_funcs->best_encoder(connector);
1320 if (!encoder)
1321 goto out;
1322
Dave Airlie38651672010-03-30 05:34:13 +00001323 /* select a crtc for this connector and then attempt to configure
1324 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001325 for (c = 0; c < fb_helper->crtc_count; c++) {
1326 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001327
1328 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001329 continue;
1330 }
1331
1332 for (o = 0; o < n; o++)
1333 if (best_crtcs[o] == crtc)
1334 break;
1335
1336 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001337 /* ignore cloning unless only a single crtc */
1338 if (fb_helper->crtc_count > 1)
1339 continue;
1340
1341 if (!drm_mode_equal(modes[o], modes[n]))
1342 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001343 }
1344
1345 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001346 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1347 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001348 width, height);
1349 if (score > best_score) {
1350 best_crtc = crtc;
1351 best_score = score;
1352 memcpy(best_crtcs, crtcs,
1353 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001354 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001355 }
Dave Airlie38651672010-03-30 05:34:13 +00001356 }
1357out:
1358 kfree(crtcs);
1359 return best_score;
1360}
1361
Dave Airlie8be48d92010-03-30 05:34:14 +00001362static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001363{
Dave Airlie8be48d92010-03-30 05:34:14 +00001364 struct drm_device *dev = fb_helper->dev;
1365 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001366 struct drm_display_mode **modes;
1367 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001368 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001369 bool *enabled;
1370 int width, height;
1371 int i, ret;
1372
1373 DRM_DEBUG_KMS("\n");
1374
1375 width = dev->mode_config.max_width;
1376 height = dev->mode_config.max_height;
1377
1378 /* clean out all the encoder/crtc combos */
1379 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1380 encoder->crtc = NULL;
1381 }
1382
1383 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001384 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001385 modes = kcalloc(dev->mode_config.num_connector,
1386 sizeof(struct drm_display_mode *), GFP_KERNEL);
1387 enabled = kcalloc(dev->mode_config.num_connector,
1388 sizeof(bool), GFP_KERNEL);
1389
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001390 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001391
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001392 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1393 if (!ret) {
1394 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1395 if (!ret)
1396 DRM_ERROR("Unable to find initial modes\n");
1397 }
Dave Airlie38651672010-03-30 05:34:13 +00001398
1399 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1400
Dave Airlie8be48d92010-03-30 05:34:14 +00001401 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1402
1403 /* need to set the modesets up here for use later */
1404 /* fill out the connector<->crtc mappings into the modesets */
1405 for (i = 0; i < fb_helper->crtc_count; i++) {
1406 modeset = &fb_helper->crtc_info[i].mode_set;
1407 modeset->num_connectors = 0;
1408 }
Dave Airlie38651672010-03-30 05:34:13 +00001409
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001410 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001411 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001412 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1413 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001414
Dave Airlie8be48d92010-03-30 05:34:14 +00001415 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001416 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001417 mode->name, fb_crtc->mode_set.crtc->base.id);
1418 fb_crtc->desired_mode = mode;
1419 if (modeset->mode)
1420 drm_mode_destroy(dev, modeset->mode);
1421 modeset->mode = drm_mode_duplicate(dev,
1422 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001423 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001424 }
Dave Airlie38651672010-03-30 05:34:13 +00001425 }
1426
1427 kfree(crtcs);
1428 kfree(modes);
1429 kfree(enabled);
1430}
1431
1432/**
1433 * drm_helper_initial_config - setup a sane initial connector configuration
1434 * @dev: DRM device
1435 *
1436 * LOCKING:
1437 * Called at init time, must take mode config lock.
1438 *
1439 * Scan the CRTCs and connectors and try to put together an initial setup.
1440 * At the moment, this is a cloned configuration across all heads with
1441 * a new framebuffer object as the backing store.
1442 *
1443 * RETURNS:
1444 * Zero if everything went ok, nonzero otherwise.
1445 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001446bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001447{
Dave Airlie8be48d92010-03-30 05:34:14 +00001448 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001449 int count = 0;
1450
1451 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001452 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001453
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001454 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001455
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001456 count = drm_fb_helper_probe_connector_modes(fb_helper,
1457 dev->mode_config.max_width,
1458 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001459 /*
1460 * we shouldn't end up with no modes here.
1461 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001462 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001463 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001464 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001465 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001466
Dave Airlie4abe3522010-03-30 05:34:18 +00001467 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001468}
Dave Airlie8be48d92010-03-30 05:34:14 +00001469EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001470
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001471bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001472{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001473 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001474 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001475 bool bound = false, crtcs_bound = false;
1476 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001477
1478 if (!fb_helper->fb)
1479 return false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001480
1481 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1482 if (crtc->fb)
1483 crtcs_bound = true;
1484 if (crtc->fb == fb_helper->fb)
1485 bound = true;
1486 }
1487
1488 if (!bound && crtcs_bound) {
1489 fb_helper->delayed_hotplug = true;
1490 return false;
1491 }
Dave Airlie38651672010-03-30 05:34:13 +00001492 DRM_DEBUG_KMS("\n");
1493
Dave Airlie4abe3522010-03-30 05:34:18 +00001494 max_width = fb_helper->fb->width;
1495 max_height = fb_helper->fb->height;
1496 bpp_sel = fb_helper->fb->bits_per_pixel;
1497
Dave Airlie5c4426a2010-03-30 05:34:17 +00001498 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001499 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001500 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001501
Dave Airlie4abe3522010-03-30 05:34:18 +00001502 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001503}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001504EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001505