blob: 8208e190faaad22df581bca1bea1917d95f365a1 [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
Jason Wessel99231022010-10-13 14:09:43 -0500245static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
246{
247 uint16_t *r_base, *g_base, *b_base;
248 int i;
249
250 r_base = crtc->gamma_store;
251 g_base = r_base + crtc->gamma_size;
252 b_base = g_base + crtc->gamma_size;
253
254 for (i = 0; i < crtc->gamma_size; i++)
255 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
256}
257
258static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
259{
260 uint16_t *r_base, *g_base, *b_base;
261
262 r_base = crtc->gamma_store;
263 g_base = r_base + crtc->gamma_size;
264 b_base = g_base + crtc->gamma_size;
265
266 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
267}
268
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500269int drm_fb_helper_debug_enter(struct fb_info *info)
270{
271 struct drm_fb_helper *helper = info->par;
272 struct drm_crtc_helper_funcs *funcs;
273 int i;
274
275 if (list_empty(&kernel_fb_helper_list))
276 return false;
277
278 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
279 for (i = 0; i < helper->crtc_count; i++) {
280 struct drm_mode_set *mode_set =
281 &helper->crtc_info[i].mode_set;
282
283 if (!mode_set->crtc->enabled)
284 continue;
285
286 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500287 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500288 funcs->mode_set_base_atomic(mode_set->crtc,
289 mode_set->fb,
290 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500291 mode_set->y,
292 1);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500293
294 }
295 }
296
297 return 0;
298}
299EXPORT_SYMBOL(drm_fb_helper_debug_enter);
300
301/* Find the real fb for a given fb helper CRTC */
302static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
303{
304 struct drm_device *dev = crtc->dev;
305 struct drm_crtc *c;
306
307 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
308 if (crtc->base.id == c->base.id)
309 return c->fb;
310 }
311
312 return NULL;
313}
314
315int drm_fb_helper_debug_leave(struct fb_info *info)
316{
317 struct drm_fb_helper *helper = info->par;
318 struct drm_crtc *crtc;
319 struct drm_crtc_helper_funcs *funcs;
320 struct drm_framebuffer *fb;
321 int i;
322
323 for (i = 0; i < helper->crtc_count; i++) {
324 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
325 crtc = mode_set->crtc;
326 funcs = crtc->helper_private;
327 fb = drm_mode_config_fb(crtc);
328
329 if (!crtc->enabled)
330 continue;
331
332 if (!fb) {
333 DRM_ERROR("no fb to restore??\n");
334 continue;
335 }
336
Jason Wessel99231022010-10-13 14:09:43 -0500337 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500338 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500339 crtc->y, 0);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500340 }
341
342 return 0;
343}
344EXPORT_SYMBOL(drm_fb_helper_debug_leave);
345
Dave Airlie785b93e2009-08-28 15:46:53 +1000346bool drm_fb_helper_force_kernel_mode(void)
347{
348 int i = 0;
349 bool ret, error = false;
350 struct drm_fb_helper *helper;
351
352 if (list_empty(&kernel_fb_helper_list))
353 return false;
354
355 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
356 for (i = 0; i < helper->crtc_count; i++) {
357 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
358 ret = drm_crtc_helper_set_config(mode_set);
359 if (ret)
360 error = true;
361 }
362 }
363 return error;
364}
365
366int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
367 void *panic_str)
368{
Dave Airliefc2362a2010-06-07 12:14:54 +1000369 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000370 return drm_fb_helper_force_kernel_mode();
371 return 0;
372}
373EXPORT_SYMBOL(drm_fb_helper_panic);
374
375static struct notifier_block paniced = {
376 .notifier_call = drm_fb_helper_panic,
377};
378
379/**
380 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
381 *
382 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
383 */
384void drm_fb_helper_restore(void)
385{
386 bool ret;
387 ret = drm_fb_helper_force_kernel_mode();
388 if (ret == true)
389 DRM_ERROR("Failed to restore crtc configuration\n");
390}
391EXPORT_SYMBOL(drm_fb_helper_restore);
392
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200393#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000394static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
395{
396 drm_fb_helper_restore();
397}
398static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
399
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700400static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000401{
402 schedule_work(&drm_fb_helper_restore_work);
403}
404
405static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
406 .handler = drm_fb_helper_sysrq,
407 .help_msg = "force-fb(V)",
408 .action_msg = "Restore framebuffer console",
409};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000410#else
411static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200412#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000413
414static void drm_fb_helper_on(struct fb_info *info)
415{
416 struct drm_fb_helper *fb_helper = info->par;
417 struct drm_device *dev = fb_helper->dev;
418 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000419 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700420 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000421 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700422 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000423
424 /*
425 * For each CRTC in this fb, turn the crtc on then,
426 * find all associated encoders and turn them on.
427 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000428 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700429 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000430 crtc = fb_helper->crtc_info[i].mode_set.crtc;
431 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000432
Dave Airlie8be48d92010-03-30 05:34:14 +0000433 if (!crtc->enabled)
434 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000435
Dave Airlie8be48d92010-03-30 05:34:14 +0000436 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000437
Jesse Barnes023eb572010-07-02 10:48:08 -0700438 /* Walk the connectors & encoders on this fb turning them on */
439 for (j = 0; j < fb_helper->connector_count; j++) {
440 connector = fb_helper->connector_info[j]->connector;
441 connector->dpms = DRM_MODE_DPMS_ON;
442 drm_connector_property_set_value(connector,
443 dev->mode_config.dpms_property,
444 DRM_MODE_DPMS_ON);
445 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000446 /* Found a CRTC on this fb, now find encoders */
447 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
448 if (encoder->crtc == crtc) {
449 struct drm_encoder_helper_funcs *encoder_funcs;
450
451 encoder_funcs = encoder->helper_private;
452 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000453 }
454 }
455 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000456 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000457}
458
459static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
460{
461 struct drm_fb_helper *fb_helper = info->par;
462 struct drm_device *dev = fb_helper->dev;
463 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000464 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700465 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000466 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700467 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000468
469 /*
470 * For each CRTC in this fb, find all associated encoders
471 * and turn them off, then turn off the CRTC.
472 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000473 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700474 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000475 crtc = fb_helper->crtc_info[i].mode_set.crtc;
476 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000477
Dave Airlie8be48d92010-03-30 05:34:14 +0000478 if (!crtc->enabled)
479 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000480
Jesse Barnes023eb572010-07-02 10:48:08 -0700481 /* Walk the connectors on this fb and mark them off */
482 for (j = 0; j < fb_helper->connector_count; j++) {
483 connector = fb_helper->connector_info[j]->connector;
484 connector->dpms = dpms_mode;
485 drm_connector_property_set_value(connector,
486 dev->mode_config.dpms_property,
487 dpms_mode);
488 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000489 /* Found a CRTC on this fb, now find encoders */
490 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
491 if (encoder->crtc == crtc) {
492 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000493
Dave Airlie8be48d92010-03-30 05:34:14 +0000494 encoder_funcs = encoder->helper_private;
495 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700496 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000497 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000498 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000499 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000500 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000501}
502
503int drm_fb_helper_blank(int blank, struct fb_info *info)
504{
505 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000506 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000507 case FB_BLANK_UNBLANK:
508 drm_fb_helper_on(info);
509 break;
James Simmons731b5a12009-10-29 20:39:07 +0000510 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000511 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800512 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000513 break;
James Simmons731b5a12009-10-29 20:39:07 +0000514 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000515 case FB_BLANK_HSYNC_SUSPEND:
516 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
517 break;
James Simmons731b5a12009-10-29 20:39:07 +0000518 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 case FB_BLANK_VSYNC_SUSPEND:
520 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
521 break;
James Simmons731b5a12009-10-29 20:39:07 +0000522 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000523 case FB_BLANK_POWERDOWN:
524 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
525 break;
526 }
527 return 0;
528}
529EXPORT_SYMBOL(drm_fb_helper_blank);
530
531static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
532{
533 int i;
534
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000535 for (i = 0; i < helper->connector_count; i++)
536 kfree(helper->connector_info[i]);
537 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000538 for (i = 0; i < helper->crtc_count; i++)
539 kfree(helper->crtc_info[i].mode_set.connectors);
540 kfree(helper->crtc_info);
541}
542
Dave Airlie4abe3522010-03-30 05:34:18 +0000543int drm_fb_helper_init(struct drm_device *dev,
544 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000545 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000546{
Dave Airlie785b93e2009-08-28 15:46:53 +1000547 struct drm_crtc *crtc;
548 int ret = 0;
549 int i;
550
Dave Airlie4abe3522010-03-30 05:34:18 +0000551 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000552
553 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
554
555 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
556 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000557 return -ENOMEM;
558
Dave Airlie4abe3522010-03-30 05:34:18 +0000559 fb_helper->crtc_count = crtc_count;
560 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
561 if (!fb_helper->connector_info) {
562 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000563 return -ENOMEM;
564 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000565 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000566
567 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000568 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000569 kcalloc(max_conn_count,
570 sizeof(struct drm_connector *),
571 GFP_KERNEL);
572
Dave Airlie4abe3522010-03-30 05:34:18 +0000573 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000574 ret = -ENOMEM;
575 goto out_free;
576 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000577 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000578 }
579
580 i = 0;
581 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000582 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
583 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000584 i++;
585 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000586 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000587 return 0;
588out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000589 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000590 return -ENOMEM;
591}
Dave Airlie4abe3522010-03-30 05:34:18 +0000592EXPORT_SYMBOL(drm_fb_helper_init);
593
594void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
595{
596 if (!list_empty(&fb_helper->kernel_fb_list)) {
597 list_del(&fb_helper->kernel_fb_list);
598 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400599 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000600 atomic_notifier_chain_unregister(&panic_notifier_list,
601 &paniced);
602 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
603 }
604 }
605
606 drm_fb_helper_crtc_free(fb_helper);
607
Dave Airlie4abe3522010-03-30 05:34:18 +0000608}
609EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000610
Dave Airliec850cb72009-10-23 18:49:03 +1000611static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000612 u16 blue, u16 regno, struct fb_info *info)
613{
614 struct drm_fb_helper *fb_helper = info->par;
615 struct drm_framebuffer *fb = fb_helper->fb;
616 int pindex;
617
Dave Airliec850cb72009-10-23 18:49:03 +1000618 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
619 u32 *palette;
620 u32 value;
621 /* place color in psuedopalette */
622 if (regno > 16)
623 return -EINVAL;
624 palette = (u32 *)info->pseudo_palette;
625 red >>= (16 - info->var.red.length);
626 green >>= (16 - info->var.green.length);
627 blue >>= (16 - info->var.blue.length);
628 value = (red << info->var.red.offset) |
629 (green << info->var.green.offset) |
630 (blue << info->var.blue.offset);
631 palette[regno] = value;
632 return 0;
633 }
634
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000635 pindex = regno;
636
637 if (fb->bits_per_pixel == 16) {
638 pindex = regno << 3;
639
640 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000641 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000642 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000643 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000644
645 if (fb->depth == 16) {
646 u16 r, g, b;
647 int i;
648 if (regno < 32) {
649 for (i = 0; i < 8; i++)
650 fb_helper->funcs->gamma_set(crtc, red,
651 green, blue, pindex + i);
652 }
653
654 fb_helper->funcs->gamma_get(crtc, &r,
655 &g, &b,
656 pindex >> 1);
657
658 for (i = 0; i < 4; i++)
659 fb_helper->funcs->gamma_set(crtc, r,
660 green, b,
661 (pindex >> 1) + i);
662 }
663 }
664
665 if (fb->depth != 16)
666 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000667 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000668}
669
Dave Airlie068143d2009-10-05 09:58:02 +1000670int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
671{
672 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000673 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000674 u16 *red, *green, *blue, *transp;
675 struct drm_crtc *crtc;
676 int i, rc = 0;
677 int start;
678
Dave Airlie8be48d92010-03-30 05:34:14 +0000679 for (i = 0; i < fb_helper->crtc_count; i++) {
680 crtc = fb_helper->crtc_info[i].mode_set.crtc;
681 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000682
683 red = cmap->red;
684 green = cmap->green;
685 blue = cmap->blue;
686 transp = cmap->transp;
687 start = cmap->start;
688
689 for (i = 0; i < cmap->len; i++) {
690 u16 hred, hgreen, hblue, htransp = 0xffff;
691
692 hred = *red++;
693 hgreen = *green++;
694 hblue = *blue++;
695
696 if (transp)
697 htransp = *transp++;
698
Dave Airliec850cb72009-10-23 18:49:03 +1000699 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
700 if (rc)
701 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000702 }
703 crtc_funcs->load_lut(crtc);
704 }
705 return rc;
706}
707EXPORT_SYMBOL(drm_fb_helper_setcmap);
708
Dave Airlie785b93e2009-08-28 15:46:53 +1000709int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
710 struct fb_info *info)
711{
712 struct drm_fb_helper *fb_helper = info->par;
713 struct drm_framebuffer *fb = fb_helper->fb;
714 int depth;
715
Jason Wesself90ebd92010-08-05 09:22:32 -0500716 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000717 return -EINVAL;
718
719 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000720 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
721 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
722 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
723 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000724 return -EINVAL;
725 }
726
727 switch (var->bits_per_pixel) {
728 case 16:
729 depth = (var->green.length == 6) ? 16 : 15;
730 break;
731 case 32:
732 depth = (var->transp.length > 0) ? 32 : 24;
733 break;
734 default:
735 depth = var->bits_per_pixel;
736 break;
737 }
738
739 switch (depth) {
740 case 8:
741 var->red.offset = 0;
742 var->green.offset = 0;
743 var->blue.offset = 0;
744 var->red.length = 8;
745 var->green.length = 8;
746 var->blue.length = 8;
747 var->transp.length = 0;
748 var->transp.offset = 0;
749 break;
750 case 15:
751 var->red.offset = 10;
752 var->green.offset = 5;
753 var->blue.offset = 0;
754 var->red.length = 5;
755 var->green.length = 5;
756 var->blue.length = 5;
757 var->transp.length = 1;
758 var->transp.offset = 15;
759 break;
760 case 16:
761 var->red.offset = 11;
762 var->green.offset = 5;
763 var->blue.offset = 0;
764 var->red.length = 5;
765 var->green.length = 6;
766 var->blue.length = 5;
767 var->transp.length = 0;
768 var->transp.offset = 0;
769 break;
770 case 24:
771 var->red.offset = 16;
772 var->green.offset = 8;
773 var->blue.offset = 0;
774 var->red.length = 8;
775 var->green.length = 8;
776 var->blue.length = 8;
777 var->transp.length = 0;
778 var->transp.offset = 0;
779 break;
780 case 32:
781 var->red.offset = 16;
782 var->green.offset = 8;
783 var->blue.offset = 0;
784 var->red.length = 8;
785 var->green.length = 8;
786 var->blue.length = 8;
787 var->transp.length = 8;
788 var->transp.offset = 24;
789 break;
790 default:
791 return -EINVAL;
792 }
793 return 0;
794}
795EXPORT_SYMBOL(drm_fb_helper_check_var);
796
797/* this will let fbcon do the mode init */
798int drm_fb_helper_set_par(struct fb_info *info)
799{
800 struct drm_fb_helper *fb_helper = info->par;
801 struct drm_device *dev = fb_helper->dev;
802 struct fb_var_screeninfo *var = &info->var;
803 struct drm_crtc *crtc;
804 int ret;
805 int i;
806
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100807 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000808 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000809 return -EINVAL;
810 }
811
Dave Airlie8be48d92010-03-30 05:34:14 +0000812 mutex_lock(&dev->mode_config.mutex);
813 for (i = 0; i < fb_helper->crtc_count; i++) {
814 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000815 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
816 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000817 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000818 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000819 }
820 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000821 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000822
823 if (fb_helper->delayed_hotplug) {
824 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000825 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000826 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000827 return 0;
828}
829EXPORT_SYMBOL(drm_fb_helper_set_par);
830
831int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
832 struct fb_info *info)
833{
834 struct drm_fb_helper *fb_helper = info->par;
835 struct drm_device *dev = fb_helper->dev;
836 struct drm_mode_set *modeset;
837 struct drm_crtc *crtc;
838 int ret = 0;
839 int i;
840
Dave Airlie8be48d92010-03-30 05:34:14 +0000841 mutex_lock(&dev->mode_config.mutex);
842 for (i = 0; i < fb_helper->crtc_count; i++) {
843 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000844
845 modeset = &fb_helper->crtc_info[i].mode_set;
846
847 modeset->x = var->xoffset;
848 modeset->y = var->yoffset;
849
850 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000851 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000852 if (!ret) {
853 info->var.xoffset = var->xoffset;
854 info->var.yoffset = var->yoffset;
855 }
856 }
857 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000858 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000859 return ret;
860}
861EXPORT_SYMBOL(drm_fb_helper_pan_display);
862
Dave Airlie8be48d92010-03-30 05:34:14 +0000863int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
864 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000865{
Dave Airlie785b93e2009-08-28 15:46:53 +1000866 int new_fb = 0;
867 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000868 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000869 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000870 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000871 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000872
873 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
874 sizes.surface_depth = 24;
875 sizes.surface_bpp = 32;
876 sizes.fb_width = (unsigned)-1;
877 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000878
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000879 /* if driver picks 8 or 16 by default use that
880 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000881 if (preferred_bpp != sizes.surface_bpp) {
882 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000883 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000884 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000885 for (i = 0; i < fb_helper->connector_count; i++) {
886 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000887 struct drm_fb_helper_cmdline_mode *cmdline_mode;
888
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000889 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000890
891 if (cmdline_mode->bpp_specified) {
892 switch (cmdline_mode->bpp) {
893 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000894 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000895 break;
896 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000897 sizes.surface_depth = 15;
898 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000899 break;
900 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000901 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000902 break;
903 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000904 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000905 break;
906 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000907 sizes.surface_depth = 24;
908 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000909 break;
910 }
911 break;
912 }
913 }
914
Dave Airlie8be48d92010-03-30 05:34:14 +0000915 crtc_count = 0;
916 for (i = 0; i < fb_helper->crtc_count; i++) {
917 struct drm_display_mode *desired_mode;
918 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000919
Dave Airlie8be48d92010-03-30 05:34:14 +0000920 if (desired_mode) {
921 if (gamma_size == 0)
922 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
923 if (desired_mode->hdisplay < sizes.fb_width)
924 sizes.fb_width = desired_mode->hdisplay;
925 if (desired_mode->vdisplay < sizes.fb_height)
926 sizes.fb_height = desired_mode->vdisplay;
927 if (desired_mode->hdisplay > sizes.surface_width)
928 sizes.surface_width = desired_mode->hdisplay;
929 if (desired_mode->vdisplay > sizes.surface_height)
930 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000931 crtc_count++;
932 }
933 }
934
Dave Airlie38651672010-03-30 05:34:13 +0000935 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000936 /* hmm everyone went away - assume VGA cable just fell out
937 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000938 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000939 sizes.fb_width = sizes.surface_width = 1024;
940 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000941 }
942
Dave Airlie38651672010-03-30 05:34:13 +0000943 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000944 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000945 if (new_fb < 0)
946 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000947
Dave Airlie38651672010-03-30 05:34:13 +0000948 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000949
Dave Airlie8be48d92010-03-30 05:34:14 +0000950 /* set the fb pointer */
951 for (i = 0; i < fb_helper->crtc_count; i++) {
952 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000953 }
954
Dave Airlie785b93e2009-08-28 15:46:53 +1000955 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100956 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100957 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000958 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100959 }
Dave Airlie38651672010-03-30 05:34:13 +0000960
961 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
962 info->fix.id);
963
Dave Airlie785b93e2009-08-28 15:46:53 +1000964 } else {
965 drm_fb_helper_set_par(info);
966 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000967
968 /* Switch back to kernel console on panic */
969 /* multi card linked list maybe */
970 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400971 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000972 atomic_notifier_chain_register(&panic_notifier_list,
973 &paniced);
974 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
975 }
Dave Airlie38651672010-03-30 05:34:13 +0000976 if (new_fb)
977 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
978
Dave Airlie785b93e2009-08-28 15:46:53 +1000979 return 0;
980}
981EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
982
Dave Airlie068143d2009-10-05 09:58:02 +1000983void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
984 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000985{
986 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000987 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000988 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000989 info->fix.type_aux = 0;
990 info->fix.xpanstep = 1; /* doing it in hw */
991 info->fix.ypanstep = 1; /* doing it in hw */
992 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000993 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000994 info->fix.type_aux = 0;
995
996 info->fix.line_length = pitch;
997 return;
998}
999EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1000
Dave Airlie38651672010-03-30 05:34:13 +00001001void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001002 uint32_t fb_width, uint32_t fb_height)
1003{
Dave Airlie38651672010-03-30 05:34:13 +00001004 struct drm_framebuffer *fb = fb_helper->fb;
1005 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001006 info->var.xres_virtual = fb->width;
1007 info->var.yres_virtual = fb->height;
1008 info->var.bits_per_pixel = fb->bits_per_pixel;
1009 info->var.xoffset = 0;
1010 info->var.yoffset = 0;
1011 info->var.activate = FB_ACTIVATE_NOW;
1012 info->var.height = -1;
1013 info->var.width = -1;
1014
1015 switch (fb->depth) {
1016 case 8:
1017 info->var.red.offset = 0;
1018 info->var.green.offset = 0;
1019 info->var.blue.offset = 0;
1020 info->var.red.length = 8; /* 8bit DAC */
1021 info->var.green.length = 8;
1022 info->var.blue.length = 8;
1023 info->var.transp.offset = 0;
1024 info->var.transp.length = 0;
1025 break;
1026 case 15:
1027 info->var.red.offset = 10;
1028 info->var.green.offset = 5;
1029 info->var.blue.offset = 0;
1030 info->var.red.length = 5;
1031 info->var.green.length = 5;
1032 info->var.blue.length = 5;
1033 info->var.transp.offset = 15;
1034 info->var.transp.length = 1;
1035 break;
1036 case 16:
1037 info->var.red.offset = 11;
1038 info->var.green.offset = 5;
1039 info->var.blue.offset = 0;
1040 info->var.red.length = 5;
1041 info->var.green.length = 6;
1042 info->var.blue.length = 5;
1043 info->var.transp.offset = 0;
1044 break;
1045 case 24:
1046 info->var.red.offset = 16;
1047 info->var.green.offset = 8;
1048 info->var.blue.offset = 0;
1049 info->var.red.length = 8;
1050 info->var.green.length = 8;
1051 info->var.blue.length = 8;
1052 info->var.transp.offset = 0;
1053 info->var.transp.length = 0;
1054 break;
1055 case 32:
1056 info->var.red.offset = 16;
1057 info->var.green.offset = 8;
1058 info->var.blue.offset = 0;
1059 info->var.red.length = 8;
1060 info->var.green.length = 8;
1061 info->var.blue.length = 8;
1062 info->var.transp.offset = 24;
1063 info->var.transp.length = 8;
1064 break;
1065 default:
1066 break;
1067 }
1068
1069 info->var.xres = fb_width;
1070 info->var.yres = fb_height;
1071}
1072EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001073
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001074static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1075 uint32_t maxX,
1076 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001077{
1078 struct drm_connector *connector;
1079 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001080 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001081
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001082 for (i = 0; i < fb_helper->connector_count; i++) {
1083 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001084 count += connector->funcs->fill_modes(connector, maxX, maxY);
1085 }
1086
1087 return count;
1088}
1089
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001090static 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 +00001091{
1092 struct drm_display_mode *mode;
1093
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001094 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001095 if (drm_mode_width(mode) > width ||
1096 drm_mode_height(mode) > height)
1097 continue;
1098 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1099 return mode;
1100 }
1101 return NULL;
1102}
1103
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001104static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001105{
Dave Airlie38651672010-03-30 05:34:13 +00001106 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001107 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001108 return cmdline_mode->specified;
1109}
1110
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001111static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1112 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001113{
Dave Airlie38651672010-03-30 05:34:13 +00001114 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1115 struct drm_display_mode *mode = NULL;
1116
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001117 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001118 if (cmdline_mode->specified == false)
1119 return mode;
1120
1121 /* attempt to find a matching mode in the list of modes
1122 * we have gotten so far, if not add a CVT mode that conforms
1123 */
1124 if (cmdline_mode->rb || cmdline_mode->margins)
1125 goto create_mode;
1126
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001127 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001128 /* check width/height */
1129 if (mode->hdisplay != cmdline_mode->xres ||
1130 mode->vdisplay != cmdline_mode->yres)
1131 continue;
1132
1133 if (cmdline_mode->refresh_specified) {
1134 if (mode->vrefresh != cmdline_mode->refresh)
1135 continue;
1136 }
1137
1138 if (cmdline_mode->interlace) {
1139 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1140 continue;
1141 }
1142 return mode;
1143 }
1144
1145create_mode:
Adam Jacksonb829e012010-06-10 13:33:26 -04001146 if (cmdline_mode->cvt)
1147 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1148 cmdline_mode->xres, cmdline_mode->yres,
1149 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1150 cmdline_mode->rb, cmdline_mode->interlace,
1151 cmdline_mode->margins);
1152 else
1153 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1154 cmdline_mode->xres, cmdline_mode->yres,
1155 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1156 cmdline_mode->interlace,
1157 cmdline_mode->margins);
Dave Airlie38651672010-03-30 05:34:13 +00001158 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001159 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001160 return mode;
1161}
1162
1163static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1164{
1165 bool enable;
1166
1167 if (strict) {
1168 enable = connector->status == connector_status_connected;
1169 } else {
1170 enable = connector->status != connector_status_disconnected;
1171 }
1172 return enable;
1173}
1174
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001175static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1176 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001177{
1178 bool any_enabled = false;
1179 struct drm_connector *connector;
1180 int i = 0;
1181
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001182 for (i = 0; i < fb_helper->connector_count; i++) {
1183 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001184 enabled[i] = drm_connector_enabled(connector, true);
1185 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1186 enabled[i] ? "yes" : "no");
1187 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001188 }
1189
1190 if (any_enabled)
1191 return;
1192
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001193 for (i = 0; i < fb_helper->connector_count; i++) {
1194 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001195 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001196 }
1197}
1198
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001199static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1200 struct drm_display_mode **modes,
1201 bool *enabled, int width, int height)
1202{
1203 int count, i, j;
1204 bool can_clone = false;
1205 struct drm_fb_helper_connector *fb_helper_conn;
1206 struct drm_display_mode *dmt_mode, *mode;
1207
1208 /* only contemplate cloning in the single crtc case */
1209 if (fb_helper->crtc_count > 1)
1210 return false;
1211
1212 count = 0;
1213 for (i = 0; i < fb_helper->connector_count; i++) {
1214 if (enabled[i])
1215 count++;
1216 }
1217
1218 /* only contemplate cloning if more than one connector is enabled */
1219 if (count <= 1)
1220 return false;
1221
1222 /* check the command line or if nothing common pick 1024x768 */
1223 can_clone = true;
1224 for (i = 0; i < fb_helper->connector_count; i++) {
1225 if (!enabled[i])
1226 continue;
1227 fb_helper_conn = fb_helper->connector_info[i];
1228 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1229 if (!modes[i]) {
1230 can_clone = false;
1231 break;
1232 }
1233 for (j = 0; j < i; j++) {
1234 if (!enabled[j])
1235 continue;
1236 if (!drm_mode_equal(modes[j], modes[i]))
1237 can_clone = false;
1238 }
1239 }
1240
1241 if (can_clone) {
1242 DRM_DEBUG_KMS("can clone using command line\n");
1243 return true;
1244 }
1245
1246 /* try and find a 1024x768 mode on each connector */
1247 can_clone = true;
1248 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1249
1250 for (i = 0; i < fb_helper->connector_count; i++) {
1251
1252 if (!enabled[i])
1253 continue;
1254
1255 fb_helper_conn = fb_helper->connector_info[i];
1256 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1257 if (drm_mode_equal(mode, dmt_mode))
1258 modes[i] = mode;
1259 }
1260 if (!modes[i])
1261 can_clone = false;
1262 }
1263
1264 if (can_clone) {
1265 DRM_DEBUG_KMS("can clone using 1024x768\n");
1266 return true;
1267 }
1268 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1269 return false;
1270}
1271
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001272static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001273 struct drm_display_mode **modes,
1274 bool *enabled, int width, int height)
1275{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001276 struct drm_fb_helper_connector *fb_helper_conn;
1277 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001278
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001279 for (i = 0; i < fb_helper->connector_count; i++) {
1280 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001281
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001282 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001283 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001284
1285 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001286 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001287
1288 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001289 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001290 if (!modes[i]) {
1291 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001292 fb_helper_conn->connector->base.id);
1293 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001294 }
1295 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001296 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1297 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001298 break;
1299 }
1300 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1301 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001302 }
1303 return true;
1304}
1305
Dave Airlie8be48d92010-03-30 05:34:14 +00001306static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1307 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001308 struct drm_display_mode **modes,
1309 int n, int width, int height)
1310{
1311 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001312 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001313 struct drm_connector *connector;
1314 struct drm_connector_helper_funcs *connector_funcs;
1315 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001316 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001317 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001318 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001319 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001320
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001321 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001322 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001323
1324 fb_helper_conn = fb_helper->connector_info[n];
1325 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001326
1327 best_crtcs[n] = NULL;
1328 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001329 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001330 if (modes[n] == NULL)
1331 return best_score;
1332
Dave Airlie8be48d92010-03-30 05:34:14 +00001333 crtcs = kzalloc(dev->mode_config.num_connector *
1334 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001335 if (!crtcs)
1336 return best_score;
1337
1338 my_score = 1;
1339 if (connector->status == connector_status_connected)
1340 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001341 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001342 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001343 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001344 my_score++;
1345
1346 connector_funcs = connector->helper_private;
1347 encoder = connector_funcs->best_encoder(connector);
1348 if (!encoder)
1349 goto out;
1350
Dave Airlie38651672010-03-30 05:34:13 +00001351 /* select a crtc for this connector and then attempt to configure
1352 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001353 for (c = 0; c < fb_helper->crtc_count; c++) {
1354 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001355
1356 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001357 continue;
1358 }
1359
1360 for (o = 0; o < n; o++)
1361 if (best_crtcs[o] == crtc)
1362 break;
1363
1364 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001365 /* ignore cloning unless only a single crtc */
1366 if (fb_helper->crtc_count > 1)
1367 continue;
1368
1369 if (!drm_mode_equal(modes[o], modes[n]))
1370 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001371 }
1372
1373 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001374 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1375 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001376 width, height);
1377 if (score > best_score) {
1378 best_crtc = crtc;
1379 best_score = score;
1380 memcpy(best_crtcs, crtcs,
1381 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001382 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001383 }
Dave Airlie38651672010-03-30 05:34:13 +00001384 }
1385out:
1386 kfree(crtcs);
1387 return best_score;
1388}
1389
Dave Airlie8be48d92010-03-30 05:34:14 +00001390static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001391{
Dave Airlie8be48d92010-03-30 05:34:14 +00001392 struct drm_device *dev = fb_helper->dev;
1393 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001394 struct drm_display_mode **modes;
1395 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001396 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001397 bool *enabled;
1398 int width, height;
1399 int i, ret;
1400
1401 DRM_DEBUG_KMS("\n");
1402
1403 width = dev->mode_config.max_width;
1404 height = dev->mode_config.max_height;
1405
1406 /* clean out all the encoder/crtc combos */
1407 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1408 encoder->crtc = NULL;
1409 }
1410
1411 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001412 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001413 modes = kcalloc(dev->mode_config.num_connector,
1414 sizeof(struct drm_display_mode *), GFP_KERNEL);
1415 enabled = kcalloc(dev->mode_config.num_connector,
1416 sizeof(bool), GFP_KERNEL);
1417
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001418 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001419
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001420 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1421 if (!ret) {
1422 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1423 if (!ret)
1424 DRM_ERROR("Unable to find initial modes\n");
1425 }
Dave Airlie38651672010-03-30 05:34:13 +00001426
1427 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1428
Dave Airlie8be48d92010-03-30 05:34:14 +00001429 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1430
1431 /* need to set the modesets up here for use later */
1432 /* fill out the connector<->crtc mappings into the modesets */
1433 for (i = 0; i < fb_helper->crtc_count; i++) {
1434 modeset = &fb_helper->crtc_info[i].mode_set;
1435 modeset->num_connectors = 0;
1436 }
Dave Airlie38651672010-03-30 05:34:13 +00001437
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001438 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001439 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001440 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1441 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001442
Dave Airlie8be48d92010-03-30 05:34:14 +00001443 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001444 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001445 mode->name, fb_crtc->mode_set.crtc->base.id);
1446 fb_crtc->desired_mode = mode;
1447 if (modeset->mode)
1448 drm_mode_destroy(dev, modeset->mode);
1449 modeset->mode = drm_mode_duplicate(dev,
1450 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001451 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001452 }
Dave Airlie38651672010-03-30 05:34:13 +00001453 }
1454
1455 kfree(crtcs);
1456 kfree(modes);
1457 kfree(enabled);
1458}
1459
1460/**
1461 * drm_helper_initial_config - setup a sane initial connector configuration
1462 * @dev: DRM device
1463 *
1464 * LOCKING:
1465 * Called at init time, must take mode config lock.
1466 *
1467 * Scan the CRTCs and connectors and try to put together an initial setup.
1468 * At the moment, this is a cloned configuration across all heads with
1469 * a new framebuffer object as the backing store.
1470 *
1471 * RETURNS:
1472 * Zero if everything went ok, nonzero otherwise.
1473 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001474bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001475{
Dave Airlie8be48d92010-03-30 05:34:14 +00001476 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001477 int count = 0;
1478
1479 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001480 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001481
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001482 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001483
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001484 count = drm_fb_helper_probe_connector_modes(fb_helper,
1485 dev->mode_config.max_width,
1486 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001487 /*
1488 * we shouldn't end up with no modes here.
1489 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001490 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001491 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001492 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001493 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001494
Dave Airlie4abe3522010-03-30 05:34:18 +00001495 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001496}
Dave Airlie8be48d92010-03-30 05:34:14 +00001497EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001498
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001499bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001500{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001501 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001502 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001503 bool bound = false, crtcs_bound = false;
1504 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001505
1506 if (!fb_helper->fb)
1507 return false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001508
1509 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1510 if (crtc->fb)
1511 crtcs_bound = true;
1512 if (crtc->fb == fb_helper->fb)
1513 bound = true;
1514 }
1515
1516 if (!bound && crtcs_bound) {
1517 fb_helper->delayed_hotplug = true;
1518 return false;
1519 }
Dave Airlie38651672010-03-30 05:34:13 +00001520 DRM_DEBUG_KMS("\n");
1521
Dave Airlie4abe3522010-03-30 05:34:18 +00001522 max_width = fb_helper->fb->width;
1523 max_height = fb_helper->fb->height;
1524 bpp_sel = fb_helper->fb->bits_per_pixel;
1525
Dave Airlie5c4426a2010-03-30 05:34:17 +00001526 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001527 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001528 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001529
Dave Airlie4abe3522010-03-30 05:34:18 +00001530 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001531}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001532EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001533