blob: 625a2d551d6a3db6c963eb65b759e1502339c7fd [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;
Dan Carpenter09f0c482010-08-19 11:46:29 +020097 struct drm_connector *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;
Dan Carpenter09f0c482010-08-19 11:46:29 +0200101 connector = fb_helper_conn->connector;
Dave Airlie8ef86782009-09-26 06:39:00 +1000102
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000103 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000104 if (!mode_option)
105 mode_option = fb_mode_option;
106
107 if (!mode_option) {
108 cmdline_mode->specified = false;
109 return false;
110 }
111
112 name = mode_option;
113 namelen = strlen(name);
114 for (i = namelen-1; i >= 0; i--) {
115 switch (name[i]) {
116 case '@':
117 namelen = i;
118 if (!refresh_specified && !bpp_specified &&
119 !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800120 refresh = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000121 refresh_specified = 1;
122 if (cvt || rb)
123 cvt = 0;
124 } else
125 goto done;
126 break;
127 case '-':
128 namelen = i;
129 if (!bpp_specified && !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800130 bpp = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000131 bpp_specified = 1;
132 if (cvt || rb)
133 cvt = 0;
134 } else
135 goto done;
136 break;
137 case 'x':
138 if (!yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800139 yres = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000140 yres_specified = 1;
141 } else
142 goto done;
143 case '0' ... '9':
144 break;
145 case 'M':
146 if (!yres_specified)
147 cvt = 1;
148 break;
149 case 'R':
Adam Jacksonb829e012010-06-10 13:33:26 -0400150 if (cvt)
Dave Airlied50ba252009-09-23 14:44:08 +1000151 rb = 1;
152 break;
153 case 'm':
154 if (!cvt)
155 margins = 1;
156 break;
157 case 'i':
158 if (!cvt)
159 interlace = 1;
160 break;
161 case 'e':
162 force = DRM_FORCE_ON;
163 break;
164 case 'D':
Roel Kluine89a8c92009-12-31 13:06:29 +0100165 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
Dave Airlied50ba252009-09-23 14:44:08 +1000166 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
167 force = DRM_FORCE_ON;
168 else
169 force = DRM_FORCE_ON_DIGITAL;
170 break;
171 case 'd':
172 force = DRM_FORCE_OFF;
173 break;
174 default:
175 goto done;
176 }
177 }
178 if (i < 0 && yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800179 xres = simple_strtol(name, NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000180 res_specified = 1;
181 }
182done:
183
184 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
185 drm_get_connector_name(connector), xres, yres,
186 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
187 "", (margins) ? " with margins" : "", (interlace) ?
188 " interlaced" : "");
189
190 if (force) {
191 const char *s;
192 switch (force) {
193 case DRM_FORCE_OFF: s = "OFF"; break;
194 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
195 default:
196 case DRM_FORCE_ON: s = "ON"; break;
197 }
198
199 DRM_INFO("forcing %s connector %s\n",
200 drm_get_connector_name(connector), s);
201 connector->force = force;
202 }
203
204 if (res_specified) {
205 cmdline_mode->specified = true;
206 cmdline_mode->xres = xres;
207 cmdline_mode->yres = yres;
208 }
209
210 if (refresh_specified) {
211 cmdline_mode->refresh_specified = true;
212 cmdline_mode->refresh = refresh;
213 }
214
215 if (bpp_specified) {
216 cmdline_mode->bpp_specified = true;
217 cmdline_mode->bpp = bpp;
218 }
219 cmdline_mode->rb = rb ? true : false;
220 cmdline_mode->cvt = cvt ? true : false;
221 cmdline_mode->interlace = interlace ? true : false;
222
223 return true;
224}
225
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000226static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000227{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000228 struct drm_fb_helper_connector *fb_helper_conn;
229 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000230
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000231 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlied50ba252009-09-23 14:44:08 +1000232 char *option = NULL;
233
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000234 fb_helper_conn = fb_helper->connector_info[i];
235
Dave Airlied50ba252009-09-23 14:44:08 +1000236 /* do something on return - turn off connector maybe */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000237 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +1000238 continue;
239
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000240 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
Dave Airlied50ba252009-09-23 14:44:08 +1000241 }
242 return 0;
243}
244
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500245int drm_fb_helper_debug_enter(struct fb_info *info)
246{
247 struct drm_fb_helper *helper = info->par;
248 struct drm_crtc_helper_funcs *funcs;
249 int i;
250
251 if (list_empty(&kernel_fb_helper_list))
252 return false;
253
254 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
255 for (i = 0; i < helper->crtc_count; i++) {
256 struct drm_mode_set *mode_set =
257 &helper->crtc_info[i].mode_set;
258
259 if (!mode_set->crtc->enabled)
260 continue;
261
262 funcs = mode_set->crtc->helper_private;
263 funcs->mode_set_base_atomic(mode_set->crtc,
264 mode_set->fb,
265 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500266 mode_set->y,
267 1);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500268
269 }
270 }
271
272 return 0;
273}
274EXPORT_SYMBOL(drm_fb_helper_debug_enter);
275
276/* Find the real fb for a given fb helper CRTC */
277static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
278{
279 struct drm_device *dev = crtc->dev;
280 struct drm_crtc *c;
281
282 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
283 if (crtc->base.id == c->base.id)
284 return c->fb;
285 }
286
287 return NULL;
288}
289
290int drm_fb_helper_debug_leave(struct fb_info *info)
291{
292 struct drm_fb_helper *helper = info->par;
293 struct drm_crtc *crtc;
294 struct drm_crtc_helper_funcs *funcs;
295 struct drm_framebuffer *fb;
296 int i;
297
298 for (i = 0; i < helper->crtc_count; i++) {
299 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
300 crtc = mode_set->crtc;
301 funcs = crtc->helper_private;
302 fb = drm_mode_config_fb(crtc);
303
304 if (!crtc->enabled)
305 continue;
306
307 if (!fb) {
308 DRM_ERROR("no fb to restore??\n");
309 continue;
310 }
311
312 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500313 crtc->y, 0);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500314 }
315
316 return 0;
317}
318EXPORT_SYMBOL(drm_fb_helper_debug_leave);
319
Dave Airlie785b93e2009-08-28 15:46:53 +1000320bool drm_fb_helper_force_kernel_mode(void)
321{
322 int i = 0;
323 bool ret, error = false;
324 struct drm_fb_helper *helper;
325
326 if (list_empty(&kernel_fb_helper_list))
327 return false;
328
329 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
330 for (i = 0; i < helper->crtc_count; i++) {
331 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
332 ret = drm_crtc_helper_set_config(mode_set);
333 if (ret)
334 error = true;
335 }
336 }
337 return error;
338}
339
340int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
341 void *panic_str)
342{
Dave Airliefc2362a2010-06-07 12:14:54 +1000343 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000344 return drm_fb_helper_force_kernel_mode();
345 return 0;
346}
347EXPORT_SYMBOL(drm_fb_helper_panic);
348
349static struct notifier_block paniced = {
350 .notifier_call = drm_fb_helper_panic,
351};
352
353/**
354 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
355 *
356 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
357 */
358void drm_fb_helper_restore(void)
359{
360 bool ret;
361 ret = drm_fb_helper_force_kernel_mode();
362 if (ret == true)
363 DRM_ERROR("Failed to restore crtc configuration\n");
364}
365EXPORT_SYMBOL(drm_fb_helper_restore);
366
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200367#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000368static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
369{
370 drm_fb_helper_restore();
371}
372static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
373
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700374static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000375{
376 schedule_work(&drm_fb_helper_restore_work);
377}
378
379static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
380 .handler = drm_fb_helper_sysrq,
381 .help_msg = "force-fb(V)",
382 .action_msg = "Restore framebuffer console",
383};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000384#else
385static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200386#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000387
388static void drm_fb_helper_on(struct fb_info *info)
389{
390 struct drm_fb_helper *fb_helper = info->par;
391 struct drm_device *dev = fb_helper->dev;
392 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000393 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700394 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000395 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700396 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000397
398 /*
399 * For each CRTC in this fb, turn the crtc on then,
400 * find all associated encoders and turn them on.
401 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000402 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700403 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000404 crtc = fb_helper->crtc_info[i].mode_set.crtc;
405 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000406
Dave Airlie8be48d92010-03-30 05:34:14 +0000407 if (!crtc->enabled)
408 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000409
Dave Airlie8be48d92010-03-30 05:34:14 +0000410 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000411
Jesse Barnes023eb572010-07-02 10:48:08 -0700412 /* Walk the connectors & encoders on this fb turning them on */
413 for (j = 0; j < fb_helper->connector_count; j++) {
414 connector = fb_helper->connector_info[j]->connector;
415 connector->dpms = DRM_MODE_DPMS_ON;
416 drm_connector_property_set_value(connector,
417 dev->mode_config.dpms_property,
418 DRM_MODE_DPMS_ON);
419 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000420 /* Found a CRTC on this fb, now find encoders */
421 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
422 if (encoder->crtc == crtc) {
423 struct drm_encoder_helper_funcs *encoder_funcs;
424
425 encoder_funcs = encoder->helper_private;
426 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000427 }
428 }
429 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000430 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000431}
432
433static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
434{
435 struct drm_fb_helper *fb_helper = info->par;
436 struct drm_device *dev = fb_helper->dev;
437 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000438 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700439 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000440 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700441 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000442
443 /*
444 * For each CRTC in this fb, find all associated encoders
445 * and turn them off, then turn off the CRTC.
446 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000447 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700448 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000449 crtc = fb_helper->crtc_info[i].mode_set.crtc;
450 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000451
Dave Airlie8be48d92010-03-30 05:34:14 +0000452 if (!crtc->enabled)
453 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000454
Jesse Barnes023eb572010-07-02 10:48:08 -0700455 /* Walk the connectors on this fb and mark them off */
456 for (j = 0; j < fb_helper->connector_count; j++) {
457 connector = fb_helper->connector_info[j]->connector;
458 connector->dpms = dpms_mode;
459 drm_connector_property_set_value(connector,
460 dev->mode_config.dpms_property,
461 dpms_mode);
462 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000463 /* Found a CRTC on this fb, now find encoders */
464 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
465 if (encoder->crtc == crtc) {
466 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000467
Dave Airlie8be48d92010-03-30 05:34:14 +0000468 encoder_funcs = encoder->helper_private;
469 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700470 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000471 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000472 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000473 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000474 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000475}
476
477int drm_fb_helper_blank(int blank, struct fb_info *info)
478{
479 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000480 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000481 case FB_BLANK_UNBLANK:
482 drm_fb_helper_on(info);
483 break;
James Simmons731b5a12009-10-29 20:39:07 +0000484 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000485 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800486 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000487 break;
James Simmons731b5a12009-10-29 20:39:07 +0000488 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000489 case FB_BLANK_HSYNC_SUSPEND:
490 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
491 break;
James Simmons731b5a12009-10-29 20:39:07 +0000492 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000493 case FB_BLANK_VSYNC_SUSPEND:
494 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
495 break;
James Simmons731b5a12009-10-29 20:39:07 +0000496 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000497 case FB_BLANK_POWERDOWN:
498 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
499 break;
500 }
501 return 0;
502}
503EXPORT_SYMBOL(drm_fb_helper_blank);
504
505static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
506{
507 int i;
508
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000509 for (i = 0; i < helper->connector_count; i++)
510 kfree(helper->connector_info[i]);
511 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000512 for (i = 0; i < helper->crtc_count; i++)
513 kfree(helper->crtc_info[i].mode_set.connectors);
514 kfree(helper->crtc_info);
515}
516
Dave Airlie4abe3522010-03-30 05:34:18 +0000517int drm_fb_helper_init(struct drm_device *dev,
518 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000519 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000520{
Dave Airlie785b93e2009-08-28 15:46:53 +1000521 struct drm_crtc *crtc;
522 int ret = 0;
523 int i;
524
Dave Airlie4abe3522010-03-30 05:34:18 +0000525 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000526
527 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
528
529 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
530 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000531 return -ENOMEM;
532
Dave Airlie4abe3522010-03-30 05:34:18 +0000533 fb_helper->crtc_count = crtc_count;
534 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
535 if (!fb_helper->connector_info) {
536 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000537 return -ENOMEM;
538 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000539 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000540
541 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000542 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000543 kcalloc(max_conn_count,
544 sizeof(struct drm_connector *),
545 GFP_KERNEL);
546
Dave Airlie4abe3522010-03-30 05:34:18 +0000547 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000548 ret = -ENOMEM;
549 goto out_free;
550 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000551 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000552 }
553
554 i = 0;
555 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000556 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
557 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000558 i++;
559 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000560 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000561 return 0;
562out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000563 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000564 return -ENOMEM;
565}
Dave Airlie4abe3522010-03-30 05:34:18 +0000566EXPORT_SYMBOL(drm_fb_helper_init);
567
568void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
569{
570 if (!list_empty(&fb_helper->kernel_fb_list)) {
571 list_del(&fb_helper->kernel_fb_list);
572 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400573 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000574 atomic_notifier_chain_unregister(&panic_notifier_list,
575 &paniced);
576 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
577 }
578 }
579
580 drm_fb_helper_crtc_free(fb_helper);
581
Dave Airlie4abe3522010-03-30 05:34:18 +0000582}
583EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000584
Dave Airliec850cb72009-10-23 18:49:03 +1000585static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000586 u16 blue, u16 regno, struct fb_info *info)
587{
588 struct drm_fb_helper *fb_helper = info->par;
589 struct drm_framebuffer *fb = fb_helper->fb;
590 int pindex;
591
Dave Airliec850cb72009-10-23 18:49:03 +1000592 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
593 u32 *palette;
594 u32 value;
595 /* place color in psuedopalette */
596 if (regno > 16)
597 return -EINVAL;
598 palette = (u32 *)info->pseudo_palette;
599 red >>= (16 - info->var.red.length);
600 green >>= (16 - info->var.green.length);
601 blue >>= (16 - info->var.blue.length);
602 value = (red << info->var.red.offset) |
603 (green << info->var.green.offset) |
604 (blue << info->var.blue.offset);
605 palette[regno] = value;
606 return 0;
607 }
608
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000609 pindex = regno;
610
611 if (fb->bits_per_pixel == 16) {
612 pindex = regno << 3;
613
614 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000615 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000616 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000617 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000618
619 if (fb->depth == 16) {
620 u16 r, g, b;
621 int i;
622 if (regno < 32) {
623 for (i = 0; i < 8; i++)
624 fb_helper->funcs->gamma_set(crtc, red,
625 green, blue, pindex + i);
626 }
627
628 fb_helper->funcs->gamma_get(crtc, &r,
629 &g, &b,
630 pindex >> 1);
631
632 for (i = 0; i < 4; i++)
633 fb_helper->funcs->gamma_set(crtc, r,
634 green, b,
635 (pindex >> 1) + i);
636 }
637 }
638
639 if (fb->depth != 16)
640 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000641 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000642}
643
Dave Airlie068143d2009-10-05 09:58:02 +1000644int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
645{
646 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000647 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000648 u16 *red, *green, *blue, *transp;
649 struct drm_crtc *crtc;
650 int i, rc = 0;
651 int start;
652
Dave Airlie8be48d92010-03-30 05:34:14 +0000653 for (i = 0; i < fb_helper->crtc_count; i++) {
654 crtc = fb_helper->crtc_info[i].mode_set.crtc;
655 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000656
657 red = cmap->red;
658 green = cmap->green;
659 blue = cmap->blue;
660 transp = cmap->transp;
661 start = cmap->start;
662
663 for (i = 0; i < cmap->len; i++) {
664 u16 hred, hgreen, hblue, htransp = 0xffff;
665
666 hred = *red++;
667 hgreen = *green++;
668 hblue = *blue++;
669
670 if (transp)
671 htransp = *transp++;
672
Dave Airliec850cb72009-10-23 18:49:03 +1000673 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
674 if (rc)
675 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000676 }
677 crtc_funcs->load_lut(crtc);
678 }
679 return rc;
680}
681EXPORT_SYMBOL(drm_fb_helper_setcmap);
682
Dave Airlie785b93e2009-08-28 15:46:53 +1000683int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
684 struct fb_info *info)
685{
686 struct drm_fb_helper *fb_helper = info->par;
687 struct drm_framebuffer *fb = fb_helper->fb;
688 int depth;
689
Jason Wesself90ebd92010-08-05 09:22:32 -0500690 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000691 return -EINVAL;
692
693 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000694 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
695 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
696 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
697 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000698 return -EINVAL;
699 }
700
701 switch (var->bits_per_pixel) {
702 case 16:
703 depth = (var->green.length == 6) ? 16 : 15;
704 break;
705 case 32:
706 depth = (var->transp.length > 0) ? 32 : 24;
707 break;
708 default:
709 depth = var->bits_per_pixel;
710 break;
711 }
712
713 switch (depth) {
714 case 8:
715 var->red.offset = 0;
716 var->green.offset = 0;
717 var->blue.offset = 0;
718 var->red.length = 8;
719 var->green.length = 8;
720 var->blue.length = 8;
721 var->transp.length = 0;
722 var->transp.offset = 0;
723 break;
724 case 15:
725 var->red.offset = 10;
726 var->green.offset = 5;
727 var->blue.offset = 0;
728 var->red.length = 5;
729 var->green.length = 5;
730 var->blue.length = 5;
731 var->transp.length = 1;
732 var->transp.offset = 15;
733 break;
734 case 16:
735 var->red.offset = 11;
736 var->green.offset = 5;
737 var->blue.offset = 0;
738 var->red.length = 5;
739 var->green.length = 6;
740 var->blue.length = 5;
741 var->transp.length = 0;
742 var->transp.offset = 0;
743 break;
744 case 24:
745 var->red.offset = 16;
746 var->green.offset = 8;
747 var->blue.offset = 0;
748 var->red.length = 8;
749 var->green.length = 8;
750 var->blue.length = 8;
751 var->transp.length = 0;
752 var->transp.offset = 0;
753 break;
754 case 32:
755 var->red.offset = 16;
756 var->green.offset = 8;
757 var->blue.offset = 0;
758 var->red.length = 8;
759 var->green.length = 8;
760 var->blue.length = 8;
761 var->transp.length = 8;
762 var->transp.offset = 24;
763 break;
764 default:
765 return -EINVAL;
766 }
767 return 0;
768}
769EXPORT_SYMBOL(drm_fb_helper_check_var);
770
771/* this will let fbcon do the mode init */
772int drm_fb_helper_set_par(struct fb_info *info)
773{
774 struct drm_fb_helper *fb_helper = info->par;
775 struct drm_device *dev = fb_helper->dev;
776 struct fb_var_screeninfo *var = &info->var;
777 struct drm_crtc *crtc;
778 int ret;
779 int i;
780
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100781 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000782 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000783 return -EINVAL;
784 }
785
Dave Airlie8be48d92010-03-30 05:34:14 +0000786 mutex_lock(&dev->mode_config.mutex);
787 for (i = 0; i < fb_helper->crtc_count; i++) {
788 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000789 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
790 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000791 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000792 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000793 }
794 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000795 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000796
797 if (fb_helper->delayed_hotplug) {
798 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000799 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000800 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000801 return 0;
802}
803EXPORT_SYMBOL(drm_fb_helper_set_par);
804
805int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
806 struct fb_info *info)
807{
808 struct drm_fb_helper *fb_helper = info->par;
809 struct drm_device *dev = fb_helper->dev;
810 struct drm_mode_set *modeset;
811 struct drm_crtc *crtc;
812 int ret = 0;
813 int i;
814
Dave Airlie8be48d92010-03-30 05:34:14 +0000815 mutex_lock(&dev->mode_config.mutex);
816 for (i = 0; i < fb_helper->crtc_count; i++) {
817 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000818
819 modeset = &fb_helper->crtc_info[i].mode_set;
820
821 modeset->x = var->xoffset;
822 modeset->y = var->yoffset;
823
824 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000825 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000826 if (!ret) {
827 info->var.xoffset = var->xoffset;
828 info->var.yoffset = var->yoffset;
829 }
830 }
831 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000832 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000833 return ret;
834}
835EXPORT_SYMBOL(drm_fb_helper_pan_display);
836
Dave Airlie8be48d92010-03-30 05:34:14 +0000837int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
838 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000839{
Dave Airlie785b93e2009-08-28 15:46:53 +1000840 int new_fb = 0;
841 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000842 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000843 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000844 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000845 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000846
847 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
848 sizes.surface_depth = 24;
849 sizes.surface_bpp = 32;
850 sizes.fb_width = (unsigned)-1;
851 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000852
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000853 /* if driver picks 8 or 16 by default use that
854 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000855 if (preferred_bpp != sizes.surface_bpp) {
856 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000857 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000858 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000859 for (i = 0; i < fb_helper->connector_count; i++) {
860 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000861 struct drm_fb_helper_cmdline_mode *cmdline_mode;
862
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000863 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000864
865 if (cmdline_mode->bpp_specified) {
866 switch (cmdline_mode->bpp) {
867 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000868 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000869 break;
870 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000871 sizes.surface_depth = 15;
872 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000873 break;
874 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000875 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000876 break;
877 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000878 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000879 break;
880 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000881 sizes.surface_depth = 24;
882 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000883 break;
884 }
885 break;
886 }
887 }
888
Dave Airlie8be48d92010-03-30 05:34:14 +0000889 crtc_count = 0;
890 for (i = 0; i < fb_helper->crtc_count; i++) {
891 struct drm_display_mode *desired_mode;
892 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000893
Dave Airlie8be48d92010-03-30 05:34:14 +0000894 if (desired_mode) {
895 if (gamma_size == 0)
896 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
897 if (desired_mode->hdisplay < sizes.fb_width)
898 sizes.fb_width = desired_mode->hdisplay;
899 if (desired_mode->vdisplay < sizes.fb_height)
900 sizes.fb_height = desired_mode->vdisplay;
901 if (desired_mode->hdisplay > sizes.surface_width)
902 sizes.surface_width = desired_mode->hdisplay;
903 if (desired_mode->vdisplay > sizes.surface_height)
904 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000905 crtc_count++;
906 }
907 }
908
Dave Airlie38651672010-03-30 05:34:13 +0000909 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000910 /* hmm everyone went away - assume VGA cable just fell out
911 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000912 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000913 sizes.fb_width = sizes.surface_width = 1024;
914 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000915 }
916
Dave Airlie38651672010-03-30 05:34:13 +0000917 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000918 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000919 if (new_fb < 0)
920 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000921
Dave Airlie38651672010-03-30 05:34:13 +0000922 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000923
Dave Airlie8be48d92010-03-30 05:34:14 +0000924 /* set the fb pointer */
925 for (i = 0; i < fb_helper->crtc_count; i++) {
926 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000927 }
928
Dave Airlie785b93e2009-08-28 15:46:53 +1000929 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100930 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100931 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000932 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100933 }
Dave Airlie38651672010-03-30 05:34:13 +0000934
935 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
936 info->fix.id);
937
Dave Airlie785b93e2009-08-28 15:46:53 +1000938 } else {
939 drm_fb_helper_set_par(info);
940 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000941
942 /* Switch back to kernel console on panic */
943 /* multi card linked list maybe */
944 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400945 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000946 atomic_notifier_chain_register(&panic_notifier_list,
947 &paniced);
948 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
949 }
Dave Airlie38651672010-03-30 05:34:13 +0000950 if (new_fb)
951 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
952
Dave Airlie785b93e2009-08-28 15:46:53 +1000953 return 0;
954}
955EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
956
Dave Airlie068143d2009-10-05 09:58:02 +1000957void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
958 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000959{
960 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000961 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000962 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000963 info->fix.type_aux = 0;
964 info->fix.xpanstep = 1; /* doing it in hw */
965 info->fix.ypanstep = 1; /* doing it in hw */
966 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000967 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000968 info->fix.type_aux = 0;
969
970 info->fix.line_length = pitch;
971 return;
972}
973EXPORT_SYMBOL(drm_fb_helper_fill_fix);
974
Dave Airlie38651672010-03-30 05:34:13 +0000975void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000976 uint32_t fb_width, uint32_t fb_height)
977{
Dave Airlie38651672010-03-30 05:34:13 +0000978 struct drm_framebuffer *fb = fb_helper->fb;
979 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000980 info->var.xres_virtual = fb->width;
981 info->var.yres_virtual = fb->height;
982 info->var.bits_per_pixel = fb->bits_per_pixel;
983 info->var.xoffset = 0;
984 info->var.yoffset = 0;
985 info->var.activate = FB_ACTIVATE_NOW;
986 info->var.height = -1;
987 info->var.width = -1;
988
989 switch (fb->depth) {
990 case 8:
991 info->var.red.offset = 0;
992 info->var.green.offset = 0;
993 info->var.blue.offset = 0;
994 info->var.red.length = 8; /* 8bit DAC */
995 info->var.green.length = 8;
996 info->var.blue.length = 8;
997 info->var.transp.offset = 0;
998 info->var.transp.length = 0;
999 break;
1000 case 15:
1001 info->var.red.offset = 10;
1002 info->var.green.offset = 5;
1003 info->var.blue.offset = 0;
1004 info->var.red.length = 5;
1005 info->var.green.length = 5;
1006 info->var.blue.length = 5;
1007 info->var.transp.offset = 15;
1008 info->var.transp.length = 1;
1009 break;
1010 case 16:
1011 info->var.red.offset = 11;
1012 info->var.green.offset = 5;
1013 info->var.blue.offset = 0;
1014 info->var.red.length = 5;
1015 info->var.green.length = 6;
1016 info->var.blue.length = 5;
1017 info->var.transp.offset = 0;
1018 break;
1019 case 24:
1020 info->var.red.offset = 16;
1021 info->var.green.offset = 8;
1022 info->var.blue.offset = 0;
1023 info->var.red.length = 8;
1024 info->var.green.length = 8;
1025 info->var.blue.length = 8;
1026 info->var.transp.offset = 0;
1027 info->var.transp.length = 0;
1028 break;
1029 case 32:
1030 info->var.red.offset = 16;
1031 info->var.green.offset = 8;
1032 info->var.blue.offset = 0;
1033 info->var.red.length = 8;
1034 info->var.green.length = 8;
1035 info->var.blue.length = 8;
1036 info->var.transp.offset = 24;
1037 info->var.transp.length = 8;
1038 break;
1039 default:
1040 break;
1041 }
1042
1043 info->var.xres = fb_width;
1044 info->var.yres = fb_height;
1045}
1046EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001047
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001048static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1049 uint32_t maxX,
1050 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001051{
1052 struct drm_connector *connector;
1053 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001054 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001055
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001056 for (i = 0; i < fb_helper->connector_count; i++) {
1057 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001058 count += connector->funcs->fill_modes(connector, maxX, maxY);
1059 }
1060
1061 return count;
1062}
1063
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001064static 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 +00001065{
1066 struct drm_display_mode *mode;
1067
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001068 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001069 if (drm_mode_width(mode) > width ||
1070 drm_mode_height(mode) > height)
1071 continue;
1072 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1073 return mode;
1074 }
1075 return NULL;
1076}
1077
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001078static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001079{
Dave Airlie38651672010-03-30 05:34:13 +00001080 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001081 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001082 return cmdline_mode->specified;
1083}
1084
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001085static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1086 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001087{
Dave Airlie38651672010-03-30 05:34:13 +00001088 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1089 struct drm_display_mode *mode = NULL;
1090
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001091 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001092 if (cmdline_mode->specified == false)
1093 return mode;
1094
1095 /* attempt to find a matching mode in the list of modes
1096 * we have gotten so far, if not add a CVT mode that conforms
1097 */
1098 if (cmdline_mode->rb || cmdline_mode->margins)
1099 goto create_mode;
1100
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001101 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001102 /* check width/height */
1103 if (mode->hdisplay != cmdline_mode->xres ||
1104 mode->vdisplay != cmdline_mode->yres)
1105 continue;
1106
1107 if (cmdline_mode->refresh_specified) {
1108 if (mode->vrefresh != cmdline_mode->refresh)
1109 continue;
1110 }
1111
1112 if (cmdline_mode->interlace) {
1113 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1114 continue;
1115 }
1116 return mode;
1117 }
1118
1119create_mode:
Adam Jacksonb829e012010-06-10 13:33:26 -04001120 if (cmdline_mode->cvt)
1121 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1122 cmdline_mode->xres, cmdline_mode->yres,
1123 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1124 cmdline_mode->rb, cmdline_mode->interlace,
1125 cmdline_mode->margins);
1126 else
1127 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1128 cmdline_mode->xres, cmdline_mode->yres,
1129 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1130 cmdline_mode->interlace,
1131 cmdline_mode->margins);
Dave Airlie38651672010-03-30 05:34:13 +00001132 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001133 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001134 return mode;
1135}
1136
1137static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1138{
1139 bool enable;
1140
1141 if (strict) {
1142 enable = connector->status == connector_status_connected;
1143 } else {
1144 enable = connector->status != connector_status_disconnected;
1145 }
1146 return enable;
1147}
1148
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001149static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1150 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001151{
1152 bool any_enabled = false;
1153 struct drm_connector *connector;
1154 int i = 0;
1155
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001156 for (i = 0; i < fb_helper->connector_count; i++) {
1157 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001158 enabled[i] = drm_connector_enabled(connector, true);
1159 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1160 enabled[i] ? "yes" : "no");
1161 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001162 }
1163
1164 if (any_enabled)
1165 return;
1166
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167 for (i = 0; i < fb_helper->connector_count; i++) {
1168 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001169 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001170 }
1171}
1172
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001173static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1174 struct drm_display_mode **modes,
1175 bool *enabled, int width, int height)
1176{
1177 int count, i, j;
1178 bool can_clone = false;
1179 struct drm_fb_helper_connector *fb_helper_conn;
1180 struct drm_display_mode *dmt_mode, *mode;
1181
1182 /* only contemplate cloning in the single crtc case */
1183 if (fb_helper->crtc_count > 1)
1184 return false;
1185
1186 count = 0;
1187 for (i = 0; i < fb_helper->connector_count; i++) {
1188 if (enabled[i])
1189 count++;
1190 }
1191
1192 /* only contemplate cloning if more than one connector is enabled */
1193 if (count <= 1)
1194 return false;
1195
1196 /* check the command line or if nothing common pick 1024x768 */
1197 can_clone = true;
1198 for (i = 0; i < fb_helper->connector_count; i++) {
1199 if (!enabled[i])
1200 continue;
1201 fb_helper_conn = fb_helper->connector_info[i];
1202 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1203 if (!modes[i]) {
1204 can_clone = false;
1205 break;
1206 }
1207 for (j = 0; j < i; j++) {
1208 if (!enabled[j])
1209 continue;
1210 if (!drm_mode_equal(modes[j], modes[i]))
1211 can_clone = false;
1212 }
1213 }
1214
1215 if (can_clone) {
1216 DRM_DEBUG_KMS("can clone using command line\n");
1217 return true;
1218 }
1219
1220 /* try and find a 1024x768 mode on each connector */
1221 can_clone = true;
1222 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1223
1224 for (i = 0; i < fb_helper->connector_count; i++) {
1225
1226 if (!enabled[i])
1227 continue;
1228
1229 fb_helper_conn = fb_helper->connector_info[i];
1230 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1231 if (drm_mode_equal(mode, dmt_mode))
1232 modes[i] = mode;
1233 }
1234 if (!modes[i])
1235 can_clone = false;
1236 }
1237
1238 if (can_clone) {
1239 DRM_DEBUG_KMS("can clone using 1024x768\n");
1240 return true;
1241 }
1242 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1243 return false;
1244}
1245
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001246static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001247 struct drm_display_mode **modes,
1248 bool *enabled, int width, int height)
1249{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001250 struct drm_fb_helper_connector *fb_helper_conn;
1251 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001252
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001253 for (i = 0; i < fb_helper->connector_count; i++) {
1254 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001255
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001256 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001257 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001258
1259 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001260 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001261
1262 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001263 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001264 if (!modes[i]) {
1265 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001266 fb_helper_conn->connector->base.id);
1267 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001268 }
1269 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001270 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1271 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001272 break;
1273 }
1274 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1275 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001276 }
1277 return true;
1278}
1279
Dave Airlie8be48d92010-03-30 05:34:14 +00001280static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1281 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001282 struct drm_display_mode **modes,
1283 int n, int width, int height)
1284{
1285 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001286 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001287 struct drm_connector *connector;
1288 struct drm_connector_helper_funcs *connector_funcs;
1289 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001290 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001291 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001292 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001293 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001294
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001295 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001296 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001297
1298 fb_helper_conn = fb_helper->connector_info[n];
1299 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001300
1301 best_crtcs[n] = NULL;
1302 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001303 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001304 if (modes[n] == NULL)
1305 return best_score;
1306
Dave Airlie8be48d92010-03-30 05:34:14 +00001307 crtcs = kzalloc(dev->mode_config.num_connector *
1308 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001309 if (!crtcs)
1310 return best_score;
1311
1312 my_score = 1;
1313 if (connector->status == connector_status_connected)
1314 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001315 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001316 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001317 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001318 my_score++;
1319
1320 connector_funcs = connector->helper_private;
1321 encoder = connector_funcs->best_encoder(connector);
1322 if (!encoder)
1323 goto out;
1324
Dave Airlie38651672010-03-30 05:34:13 +00001325 /* select a crtc for this connector and then attempt to configure
1326 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001327 for (c = 0; c < fb_helper->crtc_count; c++) {
1328 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001329
1330 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001331 continue;
1332 }
1333
1334 for (o = 0; o < n; o++)
1335 if (best_crtcs[o] == crtc)
1336 break;
1337
1338 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001339 /* ignore cloning unless only a single crtc */
1340 if (fb_helper->crtc_count > 1)
1341 continue;
1342
1343 if (!drm_mode_equal(modes[o], modes[n]))
1344 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001345 }
1346
1347 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001348 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1349 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001350 width, height);
1351 if (score > best_score) {
1352 best_crtc = crtc;
1353 best_score = score;
1354 memcpy(best_crtcs, crtcs,
1355 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001356 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001357 }
Dave Airlie38651672010-03-30 05:34:13 +00001358 }
1359out:
1360 kfree(crtcs);
1361 return best_score;
1362}
1363
Dave Airlie8be48d92010-03-30 05:34:14 +00001364static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001365{
Dave Airlie8be48d92010-03-30 05:34:14 +00001366 struct drm_device *dev = fb_helper->dev;
1367 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001368 struct drm_display_mode **modes;
1369 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001370 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001371 bool *enabled;
1372 int width, height;
1373 int i, ret;
1374
1375 DRM_DEBUG_KMS("\n");
1376
1377 width = dev->mode_config.max_width;
1378 height = dev->mode_config.max_height;
1379
1380 /* clean out all the encoder/crtc combos */
1381 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1382 encoder->crtc = NULL;
1383 }
1384
1385 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001386 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001387 modes = kcalloc(dev->mode_config.num_connector,
1388 sizeof(struct drm_display_mode *), GFP_KERNEL);
1389 enabled = kcalloc(dev->mode_config.num_connector,
1390 sizeof(bool), GFP_KERNEL);
1391
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001392 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001393
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001394 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1395 if (!ret) {
1396 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1397 if (!ret)
1398 DRM_ERROR("Unable to find initial modes\n");
1399 }
Dave Airlie38651672010-03-30 05:34:13 +00001400
1401 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1402
Dave Airlie8be48d92010-03-30 05:34:14 +00001403 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1404
1405 /* need to set the modesets up here for use later */
1406 /* fill out the connector<->crtc mappings into the modesets */
1407 for (i = 0; i < fb_helper->crtc_count; i++) {
1408 modeset = &fb_helper->crtc_info[i].mode_set;
1409 modeset->num_connectors = 0;
1410 }
Dave Airlie38651672010-03-30 05:34:13 +00001411
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001412 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001413 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001414 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1415 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001416
Dave Airlie8be48d92010-03-30 05:34:14 +00001417 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001418 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001419 mode->name, fb_crtc->mode_set.crtc->base.id);
1420 fb_crtc->desired_mode = mode;
1421 if (modeset->mode)
1422 drm_mode_destroy(dev, modeset->mode);
1423 modeset->mode = drm_mode_duplicate(dev,
1424 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001425 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001426 }
Dave Airlie38651672010-03-30 05:34:13 +00001427 }
1428
1429 kfree(crtcs);
1430 kfree(modes);
1431 kfree(enabled);
1432}
1433
1434/**
1435 * drm_helper_initial_config - setup a sane initial connector configuration
1436 * @dev: DRM device
1437 *
1438 * LOCKING:
1439 * Called at init time, must take mode config lock.
1440 *
1441 * Scan the CRTCs and connectors and try to put together an initial setup.
1442 * At the moment, this is a cloned configuration across all heads with
1443 * a new framebuffer object as the backing store.
1444 *
1445 * RETURNS:
1446 * Zero if everything went ok, nonzero otherwise.
1447 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001448bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001449{
Dave Airlie8be48d92010-03-30 05:34:14 +00001450 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001451 int count = 0;
1452
1453 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001454 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001455
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001456 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001457
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001458 count = drm_fb_helper_probe_connector_modes(fb_helper,
1459 dev->mode_config.max_width,
1460 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001461 /*
1462 * we shouldn't end up with no modes here.
1463 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001464 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001465 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001466 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001467 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001468
Dave Airlie4abe3522010-03-30 05:34:18 +00001469 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001470}
Dave Airlie8be48d92010-03-30 05:34:14 +00001471EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001472
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001473bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001474{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001475 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001476 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001477 bool bound = false, crtcs_bound = false;
1478 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001479
1480 if (!fb_helper->fb)
1481 return false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001482
1483 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1484 if (crtc->fb)
1485 crtcs_bound = true;
1486 if (crtc->fb == fb_helper->fb)
1487 bound = true;
1488 }
1489
1490 if (!bound && crtcs_bound) {
1491 fb_helper->delayed_hotplug = true;
1492 return false;
1493 }
Dave Airlie38651672010-03-30 05:34:13 +00001494 DRM_DEBUG_KMS("\n");
1495
Dave Airlie4abe3522010-03-30 05:34:18 +00001496 max_width = fb_helper->fb->width;
1497 max_height = fb_helper->fb->height;
1498 bpp_sel = fb_helper->fb->bits_per_pixel;
1499
Dave Airlie5c4426a2010-03-30 05:34:17 +00001500 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001501 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001502 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001503
Dave Airlie4abe3522010-03-30 05:34:18 +00001504 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001505}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001506EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001507