blob: 6a5e403f9aa160b54caad1bca22ba93dd27415e6 [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,
266 mode_set->y);
267
268 }
269 }
270
271 return 0;
272}
273EXPORT_SYMBOL(drm_fb_helper_debug_enter);
274
275/* Find the real fb for a given fb helper CRTC */
276static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
277{
278 struct drm_device *dev = crtc->dev;
279 struct drm_crtc *c;
280
281 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
282 if (crtc->base.id == c->base.id)
283 return c->fb;
284 }
285
286 return NULL;
287}
288
289int drm_fb_helper_debug_leave(struct fb_info *info)
290{
291 struct drm_fb_helper *helper = info->par;
292 struct drm_crtc *crtc;
293 struct drm_crtc_helper_funcs *funcs;
294 struct drm_framebuffer *fb;
295 int i;
296
297 for (i = 0; i < helper->crtc_count; i++) {
298 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
299 crtc = mode_set->crtc;
300 funcs = crtc->helper_private;
301 fb = drm_mode_config_fb(crtc);
302
303 if (!crtc->enabled)
304 continue;
305
306 if (!fb) {
307 DRM_ERROR("no fb to restore??\n");
308 continue;
309 }
310
311 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
312 crtc->y);
313 }
314
315 return 0;
316}
317EXPORT_SYMBOL(drm_fb_helper_debug_leave);
318
Dave Airlie785b93e2009-08-28 15:46:53 +1000319bool drm_fb_helper_force_kernel_mode(void)
320{
321 int i = 0;
322 bool ret, error = false;
323 struct drm_fb_helper *helper;
324
325 if (list_empty(&kernel_fb_helper_list))
326 return false;
327
328 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
329 for (i = 0; i < helper->crtc_count; i++) {
330 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
331 ret = drm_crtc_helper_set_config(mode_set);
332 if (ret)
333 error = true;
334 }
335 }
336 return error;
337}
338
339int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
340 void *panic_str)
341{
Dave Airliefc2362a2010-06-07 12:14:54 +1000342 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000343 return drm_fb_helper_force_kernel_mode();
344 return 0;
345}
346EXPORT_SYMBOL(drm_fb_helper_panic);
347
348static struct notifier_block paniced = {
349 .notifier_call = drm_fb_helper_panic,
350};
351
352/**
353 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
354 *
355 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
356 */
357void drm_fb_helper_restore(void)
358{
359 bool ret;
360 ret = drm_fb_helper_force_kernel_mode();
361 if (ret == true)
362 DRM_ERROR("Failed to restore crtc configuration\n");
363}
364EXPORT_SYMBOL(drm_fb_helper_restore);
365
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200366#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000367static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
368{
369 drm_fb_helper_restore();
370}
371static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
372
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700373static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000374{
375 schedule_work(&drm_fb_helper_restore_work);
376}
377
378static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
379 .handler = drm_fb_helper_sysrq,
380 .help_msg = "force-fb(V)",
381 .action_msg = "Restore framebuffer console",
382};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000383#else
384static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200385#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000386
387static void drm_fb_helper_on(struct fb_info *info)
388{
389 struct drm_fb_helper *fb_helper = info->par;
390 struct drm_device *dev = fb_helper->dev;
391 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000392 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700393 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700395 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000396
397 /*
398 * For each CRTC in this fb, turn the crtc on then,
399 * find all associated encoders and turn them on.
400 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000401 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700402 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000403 crtc = fb_helper->crtc_info[i].mode_set.crtc;
404 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000405
Dave Airlie8be48d92010-03-30 05:34:14 +0000406 if (!crtc->enabled)
407 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000408
Dave Airlie8be48d92010-03-30 05:34:14 +0000409 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000410
Jesse Barnes023eb572010-07-02 10:48:08 -0700411 /* Walk the connectors & encoders on this fb turning them on */
412 for (j = 0; j < fb_helper->connector_count; j++) {
413 connector = fb_helper->connector_info[j]->connector;
414 connector->dpms = DRM_MODE_DPMS_ON;
415 drm_connector_property_set_value(connector,
416 dev->mode_config.dpms_property,
417 DRM_MODE_DPMS_ON);
418 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000419 /* Found a CRTC on this fb, now find encoders */
420 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
421 if (encoder->crtc == crtc) {
422 struct drm_encoder_helper_funcs *encoder_funcs;
423
424 encoder_funcs = encoder->helper_private;
425 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000426 }
427 }
428 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000429 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000430}
431
432static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
433{
434 struct drm_fb_helper *fb_helper = info->par;
435 struct drm_device *dev = fb_helper->dev;
436 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000437 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700438 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000439 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700440 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000441
442 /*
443 * For each CRTC in this fb, find all associated encoders
444 * and turn them off, then turn off the CRTC.
445 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000446 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700447 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000448 crtc = fb_helper->crtc_info[i].mode_set.crtc;
449 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000450
Dave Airlie8be48d92010-03-30 05:34:14 +0000451 if (!crtc->enabled)
452 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000453
Jesse Barnes023eb572010-07-02 10:48:08 -0700454 /* Walk the connectors on this fb and mark them off */
455 for (j = 0; j < fb_helper->connector_count; j++) {
456 connector = fb_helper->connector_info[j]->connector;
457 connector->dpms = dpms_mode;
458 drm_connector_property_set_value(connector,
459 dev->mode_config.dpms_property,
460 dpms_mode);
461 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000462 /* Found a CRTC on this fb, now find encoders */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
464 if (encoder->crtc == crtc) {
465 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000466
Dave Airlie8be48d92010-03-30 05:34:14 +0000467 encoder_funcs = encoder->helper_private;
468 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700469 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000470 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000471 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000472 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000473 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000474}
475
476int drm_fb_helper_blank(int blank, struct fb_info *info)
477{
478 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000479 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000480 case FB_BLANK_UNBLANK:
481 drm_fb_helper_on(info);
482 break;
James Simmons731b5a12009-10-29 20:39:07 +0000483 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000484 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800485 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000486 break;
James Simmons731b5a12009-10-29 20:39:07 +0000487 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000488 case FB_BLANK_HSYNC_SUSPEND:
489 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
490 break;
James Simmons731b5a12009-10-29 20:39:07 +0000491 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000492 case FB_BLANK_VSYNC_SUSPEND:
493 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
494 break;
James Simmons731b5a12009-10-29 20:39:07 +0000495 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000496 case FB_BLANK_POWERDOWN:
497 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
498 break;
499 }
500 return 0;
501}
502EXPORT_SYMBOL(drm_fb_helper_blank);
503
504static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
505{
506 int i;
507
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000508 for (i = 0; i < helper->connector_count; i++)
509 kfree(helper->connector_info[i]);
510 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000511 for (i = 0; i < helper->crtc_count; i++)
512 kfree(helper->crtc_info[i].mode_set.connectors);
513 kfree(helper->crtc_info);
514}
515
Dave Airlie4abe3522010-03-30 05:34:18 +0000516int drm_fb_helper_init(struct drm_device *dev,
517 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000518 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000519{
Dave Airlie785b93e2009-08-28 15:46:53 +1000520 struct drm_crtc *crtc;
521 int ret = 0;
522 int i;
523
Dave Airlie4abe3522010-03-30 05:34:18 +0000524 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000525
526 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
527
528 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
529 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000530 return -ENOMEM;
531
Dave Airlie4abe3522010-03-30 05:34:18 +0000532 fb_helper->crtc_count = crtc_count;
533 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
534 if (!fb_helper->connector_info) {
535 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000536 return -ENOMEM;
537 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000538 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000539
540 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000541 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000542 kcalloc(max_conn_count,
543 sizeof(struct drm_connector *),
544 GFP_KERNEL);
545
Dave Airlie4abe3522010-03-30 05:34:18 +0000546 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000547 ret = -ENOMEM;
548 goto out_free;
549 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000550 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000551 }
552
553 i = 0;
554 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000555 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
556 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000557 i++;
558 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000559 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000560 return 0;
561out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000562 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000563 return -ENOMEM;
564}
Dave Airlie4abe3522010-03-30 05:34:18 +0000565EXPORT_SYMBOL(drm_fb_helper_init);
566
567void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
568{
569 if (!list_empty(&fb_helper->kernel_fb_list)) {
570 list_del(&fb_helper->kernel_fb_list);
571 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400572 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000573 atomic_notifier_chain_unregister(&panic_notifier_list,
574 &paniced);
575 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
576 }
577 }
578
579 drm_fb_helper_crtc_free(fb_helper);
580
Dave Airlie4abe3522010-03-30 05:34:18 +0000581}
582EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000583
Dave Airliec850cb72009-10-23 18:49:03 +1000584static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000585 u16 blue, u16 regno, struct fb_info *info)
586{
587 struct drm_fb_helper *fb_helper = info->par;
588 struct drm_framebuffer *fb = fb_helper->fb;
589 int pindex;
590
Dave Airliec850cb72009-10-23 18:49:03 +1000591 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
592 u32 *palette;
593 u32 value;
594 /* place color in psuedopalette */
595 if (regno > 16)
596 return -EINVAL;
597 palette = (u32 *)info->pseudo_palette;
598 red >>= (16 - info->var.red.length);
599 green >>= (16 - info->var.green.length);
600 blue >>= (16 - info->var.blue.length);
601 value = (red << info->var.red.offset) |
602 (green << info->var.green.offset) |
603 (blue << info->var.blue.offset);
604 palette[regno] = value;
605 return 0;
606 }
607
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000608 pindex = regno;
609
610 if (fb->bits_per_pixel == 16) {
611 pindex = regno << 3;
612
613 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000614 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000615 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000616 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000617
618 if (fb->depth == 16) {
619 u16 r, g, b;
620 int i;
621 if (regno < 32) {
622 for (i = 0; i < 8; i++)
623 fb_helper->funcs->gamma_set(crtc, red,
624 green, blue, pindex + i);
625 }
626
627 fb_helper->funcs->gamma_get(crtc, &r,
628 &g, &b,
629 pindex >> 1);
630
631 for (i = 0; i < 4; i++)
632 fb_helper->funcs->gamma_set(crtc, r,
633 green, b,
634 (pindex >> 1) + i);
635 }
636 }
637
638 if (fb->depth != 16)
639 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000640 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000641}
642
Dave Airlie068143d2009-10-05 09:58:02 +1000643int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
644{
645 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000646 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000647 u16 *red, *green, *blue, *transp;
648 struct drm_crtc *crtc;
649 int i, rc = 0;
650 int start;
651
Dave Airlie8be48d92010-03-30 05:34:14 +0000652 for (i = 0; i < fb_helper->crtc_count; i++) {
653 crtc = fb_helper->crtc_info[i].mode_set.crtc;
654 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000655
656 red = cmap->red;
657 green = cmap->green;
658 blue = cmap->blue;
659 transp = cmap->transp;
660 start = cmap->start;
661
662 for (i = 0; i < cmap->len; i++) {
663 u16 hred, hgreen, hblue, htransp = 0xffff;
664
665 hred = *red++;
666 hgreen = *green++;
667 hblue = *blue++;
668
669 if (transp)
670 htransp = *transp++;
671
Dave Airliec850cb72009-10-23 18:49:03 +1000672 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
673 if (rc)
674 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000675 }
676 crtc_funcs->load_lut(crtc);
677 }
678 return rc;
679}
680EXPORT_SYMBOL(drm_fb_helper_setcmap);
681
Dave Airlie785b93e2009-08-28 15:46:53 +1000682int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
683 struct fb_info *info)
684{
685 struct drm_fb_helper *fb_helper = info->par;
686 struct drm_framebuffer *fb = fb_helper->fb;
687 int depth;
688
Jason Wesself90ebd92010-08-05 09:22:32 -0500689 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000690 return -EINVAL;
691
692 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000693 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
694 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
695 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
696 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000697 return -EINVAL;
698 }
699
700 switch (var->bits_per_pixel) {
701 case 16:
702 depth = (var->green.length == 6) ? 16 : 15;
703 break;
704 case 32:
705 depth = (var->transp.length > 0) ? 32 : 24;
706 break;
707 default:
708 depth = var->bits_per_pixel;
709 break;
710 }
711
712 switch (depth) {
713 case 8:
714 var->red.offset = 0;
715 var->green.offset = 0;
716 var->blue.offset = 0;
717 var->red.length = 8;
718 var->green.length = 8;
719 var->blue.length = 8;
720 var->transp.length = 0;
721 var->transp.offset = 0;
722 break;
723 case 15:
724 var->red.offset = 10;
725 var->green.offset = 5;
726 var->blue.offset = 0;
727 var->red.length = 5;
728 var->green.length = 5;
729 var->blue.length = 5;
730 var->transp.length = 1;
731 var->transp.offset = 15;
732 break;
733 case 16:
734 var->red.offset = 11;
735 var->green.offset = 5;
736 var->blue.offset = 0;
737 var->red.length = 5;
738 var->green.length = 6;
739 var->blue.length = 5;
740 var->transp.length = 0;
741 var->transp.offset = 0;
742 break;
743 case 24:
744 var->red.offset = 16;
745 var->green.offset = 8;
746 var->blue.offset = 0;
747 var->red.length = 8;
748 var->green.length = 8;
749 var->blue.length = 8;
750 var->transp.length = 0;
751 var->transp.offset = 0;
752 break;
753 case 32:
754 var->red.offset = 16;
755 var->green.offset = 8;
756 var->blue.offset = 0;
757 var->red.length = 8;
758 var->green.length = 8;
759 var->blue.length = 8;
760 var->transp.length = 8;
761 var->transp.offset = 24;
762 break;
763 default:
764 return -EINVAL;
765 }
766 return 0;
767}
768EXPORT_SYMBOL(drm_fb_helper_check_var);
769
770/* this will let fbcon do the mode init */
771int drm_fb_helper_set_par(struct fb_info *info)
772{
773 struct drm_fb_helper *fb_helper = info->par;
774 struct drm_device *dev = fb_helper->dev;
775 struct fb_var_screeninfo *var = &info->var;
776 struct drm_crtc *crtc;
777 int ret;
778 int i;
779
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100780 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000781 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000782 return -EINVAL;
783 }
784
Dave Airlie8be48d92010-03-30 05:34:14 +0000785 mutex_lock(&dev->mode_config.mutex);
786 for (i = 0; i < fb_helper->crtc_count; i++) {
787 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000788 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
789 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000790 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000791 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000792 }
793 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000794 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000795
796 if (fb_helper->delayed_hotplug) {
797 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000798 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000799 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000800 return 0;
801}
802EXPORT_SYMBOL(drm_fb_helper_set_par);
803
804int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
805 struct fb_info *info)
806{
807 struct drm_fb_helper *fb_helper = info->par;
808 struct drm_device *dev = fb_helper->dev;
809 struct drm_mode_set *modeset;
810 struct drm_crtc *crtc;
811 int ret = 0;
812 int i;
813
Dave Airlie8be48d92010-03-30 05:34:14 +0000814 mutex_lock(&dev->mode_config.mutex);
815 for (i = 0; i < fb_helper->crtc_count; i++) {
816 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000817
818 modeset = &fb_helper->crtc_info[i].mode_set;
819
820 modeset->x = var->xoffset;
821 modeset->y = var->yoffset;
822
823 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000825 if (!ret) {
826 info->var.xoffset = var->xoffset;
827 info->var.yoffset = var->yoffset;
828 }
829 }
830 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000831 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 return ret;
833}
834EXPORT_SYMBOL(drm_fb_helper_pan_display);
835
Dave Airlie8be48d92010-03-30 05:34:14 +0000836int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
837 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000838{
Dave Airlie785b93e2009-08-28 15:46:53 +1000839 int new_fb = 0;
840 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000841 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000842 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000843 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000844 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000845
846 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
847 sizes.surface_depth = 24;
848 sizes.surface_bpp = 32;
849 sizes.fb_width = (unsigned)-1;
850 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000851
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000852 /* if driver picks 8 or 16 by default use that
853 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000854 if (preferred_bpp != sizes.surface_bpp) {
855 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000856 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000857 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000858 for (i = 0; i < fb_helper->connector_count; i++) {
859 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000860 struct drm_fb_helper_cmdline_mode *cmdline_mode;
861
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000862 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000863
864 if (cmdline_mode->bpp_specified) {
865 switch (cmdline_mode->bpp) {
866 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000867 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000868 break;
869 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000870 sizes.surface_depth = 15;
871 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000872 break;
873 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000874 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000875 break;
876 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000877 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000878 break;
879 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000880 sizes.surface_depth = 24;
881 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000882 break;
883 }
884 break;
885 }
886 }
887
Dave Airlie8be48d92010-03-30 05:34:14 +0000888 crtc_count = 0;
889 for (i = 0; i < fb_helper->crtc_count; i++) {
890 struct drm_display_mode *desired_mode;
891 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000892
Dave Airlie8be48d92010-03-30 05:34:14 +0000893 if (desired_mode) {
894 if (gamma_size == 0)
895 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
896 if (desired_mode->hdisplay < sizes.fb_width)
897 sizes.fb_width = desired_mode->hdisplay;
898 if (desired_mode->vdisplay < sizes.fb_height)
899 sizes.fb_height = desired_mode->vdisplay;
900 if (desired_mode->hdisplay > sizes.surface_width)
901 sizes.surface_width = desired_mode->hdisplay;
902 if (desired_mode->vdisplay > sizes.surface_height)
903 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000904 crtc_count++;
905 }
906 }
907
Dave Airlie38651672010-03-30 05:34:13 +0000908 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000909 /* hmm everyone went away - assume VGA cable just fell out
910 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000911 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000912 sizes.fb_width = sizes.surface_width = 1024;
913 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000914 }
915
Dave Airlie38651672010-03-30 05:34:13 +0000916 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000917 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000918 if (new_fb < 0)
919 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000920
Dave Airlie38651672010-03-30 05:34:13 +0000921 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000922
Dave Airlie8be48d92010-03-30 05:34:14 +0000923 /* set the fb pointer */
924 for (i = 0; i < fb_helper->crtc_count; i++) {
925 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000926 }
927
Dave Airlie785b93e2009-08-28 15:46:53 +1000928 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100929 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100930 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000931 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100932 }
Dave Airlie38651672010-03-30 05:34:13 +0000933
934 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
935 info->fix.id);
936
Dave Airlie785b93e2009-08-28 15:46:53 +1000937 } else {
938 drm_fb_helper_set_par(info);
939 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000940
941 /* Switch back to kernel console on panic */
942 /* multi card linked list maybe */
943 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400944 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000945 atomic_notifier_chain_register(&panic_notifier_list,
946 &paniced);
947 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
948 }
Dave Airlie38651672010-03-30 05:34:13 +0000949 if (new_fb)
950 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
951
Dave Airlie785b93e2009-08-28 15:46:53 +1000952 return 0;
953}
954EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
955
Dave Airlie068143d2009-10-05 09:58:02 +1000956void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
957 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000958{
959 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000960 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000961 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000962 info->fix.type_aux = 0;
963 info->fix.xpanstep = 1; /* doing it in hw */
964 info->fix.ypanstep = 1; /* doing it in hw */
965 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000966 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000967 info->fix.type_aux = 0;
968
969 info->fix.line_length = pitch;
970 return;
971}
972EXPORT_SYMBOL(drm_fb_helper_fill_fix);
973
Dave Airlie38651672010-03-30 05:34:13 +0000974void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000975 uint32_t fb_width, uint32_t fb_height)
976{
Dave Airlie38651672010-03-30 05:34:13 +0000977 struct drm_framebuffer *fb = fb_helper->fb;
978 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000979 info->var.xres_virtual = fb->width;
980 info->var.yres_virtual = fb->height;
981 info->var.bits_per_pixel = fb->bits_per_pixel;
982 info->var.xoffset = 0;
983 info->var.yoffset = 0;
984 info->var.activate = FB_ACTIVATE_NOW;
985 info->var.height = -1;
986 info->var.width = -1;
987
988 switch (fb->depth) {
989 case 8:
990 info->var.red.offset = 0;
991 info->var.green.offset = 0;
992 info->var.blue.offset = 0;
993 info->var.red.length = 8; /* 8bit DAC */
994 info->var.green.length = 8;
995 info->var.blue.length = 8;
996 info->var.transp.offset = 0;
997 info->var.transp.length = 0;
998 break;
999 case 15:
1000 info->var.red.offset = 10;
1001 info->var.green.offset = 5;
1002 info->var.blue.offset = 0;
1003 info->var.red.length = 5;
1004 info->var.green.length = 5;
1005 info->var.blue.length = 5;
1006 info->var.transp.offset = 15;
1007 info->var.transp.length = 1;
1008 break;
1009 case 16:
1010 info->var.red.offset = 11;
1011 info->var.green.offset = 5;
1012 info->var.blue.offset = 0;
1013 info->var.red.length = 5;
1014 info->var.green.length = 6;
1015 info->var.blue.length = 5;
1016 info->var.transp.offset = 0;
1017 break;
1018 case 24:
1019 info->var.red.offset = 16;
1020 info->var.green.offset = 8;
1021 info->var.blue.offset = 0;
1022 info->var.red.length = 8;
1023 info->var.green.length = 8;
1024 info->var.blue.length = 8;
1025 info->var.transp.offset = 0;
1026 info->var.transp.length = 0;
1027 break;
1028 case 32:
1029 info->var.red.offset = 16;
1030 info->var.green.offset = 8;
1031 info->var.blue.offset = 0;
1032 info->var.red.length = 8;
1033 info->var.green.length = 8;
1034 info->var.blue.length = 8;
1035 info->var.transp.offset = 24;
1036 info->var.transp.length = 8;
1037 break;
1038 default:
1039 break;
1040 }
1041
1042 info->var.xres = fb_width;
1043 info->var.yres = fb_height;
1044}
1045EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001046
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001047static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1048 uint32_t maxX,
1049 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001050{
1051 struct drm_connector *connector;
1052 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001053 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001054
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001055 for (i = 0; i < fb_helper->connector_count; i++) {
1056 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001057 count += connector->funcs->fill_modes(connector, maxX, maxY);
1058 }
1059
1060 return count;
1061}
1062
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001063static 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 +00001064{
1065 struct drm_display_mode *mode;
1066
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001067 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001068 if (drm_mode_width(mode) > width ||
1069 drm_mode_height(mode) > height)
1070 continue;
1071 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1072 return mode;
1073 }
1074 return NULL;
1075}
1076
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001077static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001078{
Dave Airlie38651672010-03-30 05:34:13 +00001079 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001080 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001081 return cmdline_mode->specified;
1082}
1083
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001084static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1085 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001086{
Dave Airlie38651672010-03-30 05:34:13 +00001087 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1088 struct drm_display_mode *mode = NULL;
1089
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001090 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001091 if (cmdline_mode->specified == false)
1092 return mode;
1093
1094 /* attempt to find a matching mode in the list of modes
1095 * we have gotten so far, if not add a CVT mode that conforms
1096 */
1097 if (cmdline_mode->rb || cmdline_mode->margins)
1098 goto create_mode;
1099
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001100 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001101 /* check width/height */
1102 if (mode->hdisplay != cmdline_mode->xres ||
1103 mode->vdisplay != cmdline_mode->yres)
1104 continue;
1105
1106 if (cmdline_mode->refresh_specified) {
1107 if (mode->vrefresh != cmdline_mode->refresh)
1108 continue;
1109 }
1110
1111 if (cmdline_mode->interlace) {
1112 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1113 continue;
1114 }
1115 return mode;
1116 }
1117
1118create_mode:
Adam Jacksonb829e012010-06-10 13:33:26 -04001119 if (cmdline_mode->cvt)
1120 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1121 cmdline_mode->xres, cmdline_mode->yres,
1122 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1123 cmdline_mode->rb, cmdline_mode->interlace,
1124 cmdline_mode->margins);
1125 else
1126 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1127 cmdline_mode->xres, cmdline_mode->yres,
1128 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1129 cmdline_mode->interlace,
1130 cmdline_mode->margins);
Dave Airlie38651672010-03-30 05:34:13 +00001131 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001132 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001133 return mode;
1134}
1135
1136static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1137{
1138 bool enable;
1139
1140 if (strict) {
1141 enable = connector->status == connector_status_connected;
1142 } else {
1143 enable = connector->status != connector_status_disconnected;
1144 }
1145 return enable;
1146}
1147
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001148static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1149 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001150{
1151 bool any_enabled = false;
1152 struct drm_connector *connector;
1153 int i = 0;
1154
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001155 for (i = 0; i < fb_helper->connector_count; i++) {
1156 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001157 enabled[i] = drm_connector_enabled(connector, true);
1158 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1159 enabled[i] ? "yes" : "no");
1160 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001161 }
1162
1163 if (any_enabled)
1164 return;
1165
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001166 for (i = 0; i < fb_helper->connector_count; i++) {
1167 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001168 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001169 }
1170}
1171
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001172static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1173 struct drm_display_mode **modes,
1174 bool *enabled, int width, int height)
1175{
1176 int count, i, j;
1177 bool can_clone = false;
1178 struct drm_fb_helper_connector *fb_helper_conn;
1179 struct drm_display_mode *dmt_mode, *mode;
1180
1181 /* only contemplate cloning in the single crtc case */
1182 if (fb_helper->crtc_count > 1)
1183 return false;
1184
1185 count = 0;
1186 for (i = 0; i < fb_helper->connector_count; i++) {
1187 if (enabled[i])
1188 count++;
1189 }
1190
1191 /* only contemplate cloning if more than one connector is enabled */
1192 if (count <= 1)
1193 return false;
1194
1195 /* check the command line or if nothing common pick 1024x768 */
1196 can_clone = true;
1197 for (i = 0; i < fb_helper->connector_count; i++) {
1198 if (!enabled[i])
1199 continue;
1200 fb_helper_conn = fb_helper->connector_info[i];
1201 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1202 if (!modes[i]) {
1203 can_clone = false;
1204 break;
1205 }
1206 for (j = 0; j < i; j++) {
1207 if (!enabled[j])
1208 continue;
1209 if (!drm_mode_equal(modes[j], modes[i]))
1210 can_clone = false;
1211 }
1212 }
1213
1214 if (can_clone) {
1215 DRM_DEBUG_KMS("can clone using command line\n");
1216 return true;
1217 }
1218
1219 /* try and find a 1024x768 mode on each connector */
1220 can_clone = true;
1221 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1222
1223 for (i = 0; i < fb_helper->connector_count; i++) {
1224
1225 if (!enabled[i])
1226 continue;
1227
1228 fb_helper_conn = fb_helper->connector_info[i];
1229 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1230 if (drm_mode_equal(mode, dmt_mode))
1231 modes[i] = mode;
1232 }
1233 if (!modes[i])
1234 can_clone = false;
1235 }
1236
1237 if (can_clone) {
1238 DRM_DEBUG_KMS("can clone using 1024x768\n");
1239 return true;
1240 }
1241 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1242 return false;
1243}
1244
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001245static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001246 struct drm_display_mode **modes,
1247 bool *enabled, int width, int height)
1248{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001249 struct drm_fb_helper_connector *fb_helper_conn;
1250 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001251
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001252 for (i = 0; i < fb_helper->connector_count; i++) {
1253 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001254
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001255 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001256 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001257
1258 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001259 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001260
1261 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001262 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001263 if (!modes[i]) {
1264 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001265 fb_helper_conn->connector->base.id);
1266 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001267 }
1268 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001269 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1270 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001271 break;
1272 }
1273 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1274 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001275 }
1276 return true;
1277}
1278
Dave Airlie8be48d92010-03-30 05:34:14 +00001279static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1280 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001281 struct drm_display_mode **modes,
1282 int n, int width, int height)
1283{
1284 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001285 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001286 struct drm_connector *connector;
1287 struct drm_connector_helper_funcs *connector_funcs;
1288 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001289 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001290 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001291 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001292 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001293
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001294 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001295 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001296
1297 fb_helper_conn = fb_helper->connector_info[n];
1298 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001299
1300 best_crtcs[n] = NULL;
1301 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001302 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001303 if (modes[n] == NULL)
1304 return best_score;
1305
Dave Airlie8be48d92010-03-30 05:34:14 +00001306 crtcs = kzalloc(dev->mode_config.num_connector *
1307 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001308 if (!crtcs)
1309 return best_score;
1310
1311 my_score = 1;
1312 if (connector->status == connector_status_connected)
1313 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001314 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001315 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001316 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001317 my_score++;
1318
1319 connector_funcs = connector->helper_private;
1320 encoder = connector_funcs->best_encoder(connector);
1321 if (!encoder)
1322 goto out;
1323
Dave Airlie38651672010-03-30 05:34:13 +00001324 /* select a crtc for this connector and then attempt to configure
1325 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001326 for (c = 0; c < fb_helper->crtc_count; c++) {
1327 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001328
1329 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001330 continue;
1331 }
1332
1333 for (o = 0; o < n; o++)
1334 if (best_crtcs[o] == crtc)
1335 break;
1336
1337 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001338 /* ignore cloning unless only a single crtc */
1339 if (fb_helper->crtc_count > 1)
1340 continue;
1341
1342 if (!drm_mode_equal(modes[o], modes[n]))
1343 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001344 }
1345
1346 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001347 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1348 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001349 width, height);
1350 if (score > best_score) {
1351 best_crtc = crtc;
1352 best_score = score;
1353 memcpy(best_crtcs, crtcs,
1354 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001355 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001356 }
Dave Airlie38651672010-03-30 05:34:13 +00001357 }
1358out:
1359 kfree(crtcs);
1360 return best_score;
1361}
1362
Dave Airlie8be48d92010-03-30 05:34:14 +00001363static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001364{
Dave Airlie8be48d92010-03-30 05:34:14 +00001365 struct drm_device *dev = fb_helper->dev;
1366 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001367 struct drm_display_mode **modes;
1368 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001369 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001370 bool *enabled;
1371 int width, height;
1372 int i, ret;
1373
1374 DRM_DEBUG_KMS("\n");
1375
1376 width = dev->mode_config.max_width;
1377 height = dev->mode_config.max_height;
1378
1379 /* clean out all the encoder/crtc combos */
1380 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1381 encoder->crtc = NULL;
1382 }
1383
1384 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001385 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001386 modes = kcalloc(dev->mode_config.num_connector,
1387 sizeof(struct drm_display_mode *), GFP_KERNEL);
1388 enabled = kcalloc(dev->mode_config.num_connector,
1389 sizeof(bool), GFP_KERNEL);
1390
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001391 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001392
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001393 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1394 if (!ret) {
1395 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1396 if (!ret)
1397 DRM_ERROR("Unable to find initial modes\n");
1398 }
Dave Airlie38651672010-03-30 05:34:13 +00001399
1400 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1401
Dave Airlie8be48d92010-03-30 05:34:14 +00001402 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1403
1404 /* need to set the modesets up here for use later */
1405 /* fill out the connector<->crtc mappings into the modesets */
1406 for (i = 0; i < fb_helper->crtc_count; i++) {
1407 modeset = &fb_helper->crtc_info[i].mode_set;
1408 modeset->num_connectors = 0;
1409 }
Dave Airlie38651672010-03-30 05:34:13 +00001410
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001411 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001412 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001413 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1414 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001415
Dave Airlie8be48d92010-03-30 05:34:14 +00001416 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001417 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001418 mode->name, fb_crtc->mode_set.crtc->base.id);
1419 fb_crtc->desired_mode = mode;
1420 if (modeset->mode)
1421 drm_mode_destroy(dev, modeset->mode);
1422 modeset->mode = drm_mode_duplicate(dev,
1423 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001424 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001425 }
Dave Airlie38651672010-03-30 05:34:13 +00001426 }
1427
1428 kfree(crtcs);
1429 kfree(modes);
1430 kfree(enabled);
1431}
1432
1433/**
1434 * drm_helper_initial_config - setup a sane initial connector configuration
1435 * @dev: DRM device
1436 *
1437 * LOCKING:
1438 * Called at init time, must take mode config lock.
1439 *
1440 * Scan the CRTCs and connectors and try to put together an initial setup.
1441 * At the moment, this is a cloned configuration across all heads with
1442 * a new framebuffer object as the backing store.
1443 *
1444 * RETURNS:
1445 * Zero if everything went ok, nonzero otherwise.
1446 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001447bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001448{
Dave Airlie8be48d92010-03-30 05:34:14 +00001449 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001450 int count = 0;
1451
1452 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001453 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001454
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001455 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001456
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001457 count = drm_fb_helper_probe_connector_modes(fb_helper,
1458 dev->mode_config.max_width,
1459 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001460 /*
1461 * we shouldn't end up with no modes here.
1462 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001463 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001464 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001465 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001466 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001467
Dave Airlie4abe3522010-03-30 05:34:18 +00001468 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001469}
Dave Airlie8be48d92010-03-30 05:34:14 +00001470EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001471
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001472bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001473{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001474 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001475 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001476 bool bound = false, crtcs_bound = false;
1477 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001478
1479 if (!fb_helper->fb)
1480 return false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001481
1482 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1483 if (crtc->fb)
1484 crtcs_bound = true;
1485 if (crtc->fb == fb_helper->fb)
1486 bound = true;
1487 }
1488
1489 if (!bound && crtcs_bound) {
1490 fb_helper->delayed_hotplug = true;
1491 return false;
1492 }
Dave Airlie38651672010-03-30 05:34:13 +00001493 DRM_DEBUG_KMS("\n");
1494
Dave Airlie4abe3522010-03-30 05:34:18 +00001495 max_width = fb_helper->fb->width;
1496 max_height = fb_helper->fb->height;
1497 bpp_sel = fb_helper->fb->bits_per_pixel;
1498
Dave Airlie5c4426a2010-03-30 05:34:17 +00001499 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001500 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001501 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001502
Dave Airlie4abe3522010-03-30 05:34:18 +00001503 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001504}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001505EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001506