blob: 11d7a72c22d9141697697db81b375e15a8a15f0b [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,
Jason Wessel21c74a82010-10-13 14:09:44 -0500292 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500293 }
294 }
295
296 return 0;
297}
298EXPORT_SYMBOL(drm_fb_helper_debug_enter);
299
300/* Find the real fb for a given fb helper CRTC */
301static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
302{
303 struct drm_device *dev = crtc->dev;
304 struct drm_crtc *c;
305
306 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
307 if (crtc->base.id == c->base.id)
308 return c->fb;
309 }
310
311 return NULL;
312}
313
314int drm_fb_helper_debug_leave(struct fb_info *info)
315{
316 struct drm_fb_helper *helper = info->par;
317 struct drm_crtc *crtc;
318 struct drm_crtc_helper_funcs *funcs;
319 struct drm_framebuffer *fb;
320 int i;
321
322 for (i = 0; i < helper->crtc_count; i++) {
323 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
324 crtc = mode_set->crtc;
325 funcs = crtc->helper_private;
326 fb = drm_mode_config_fb(crtc);
327
328 if (!crtc->enabled)
329 continue;
330
331 if (!fb) {
332 DRM_ERROR("no fb to restore??\n");
333 continue;
334 }
335
Jason Wessel99231022010-10-13 14:09:43 -0500336 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500337 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500338 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500339 }
340
341 return 0;
342}
343EXPORT_SYMBOL(drm_fb_helper_debug_leave);
344
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100345bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
346{
347 bool error = false;
348 int i, ret;
349 for (i = 0; i < fb_helper->crtc_count; i++) {
350 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
351 ret = drm_crtc_helper_set_config(mode_set);
352 if (ret)
353 error = true;
354 }
355 return error;
356}
357EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
358
Dave Airlie785b93e2009-08-28 15:46:53 +1000359bool drm_fb_helper_force_kernel_mode(void)
360{
Dave Airlie785b93e2009-08-28 15:46:53 +1000361 bool ret, error = false;
362 struct drm_fb_helper *helper;
363
364 if (list_empty(&kernel_fb_helper_list))
365 return false;
366
367 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100368 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
369 continue;
370
371 ret = drm_fb_helper_restore_fbdev_mode(helper);
372 if (ret)
373 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000374 }
375 return error;
376}
377
378int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
379 void *panic_str)
380{
Dave Airliefc2362a2010-06-07 12:14:54 +1000381 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000382 return drm_fb_helper_force_kernel_mode();
383 return 0;
384}
385EXPORT_SYMBOL(drm_fb_helper_panic);
386
387static struct notifier_block paniced = {
388 .notifier_call = drm_fb_helper_panic,
389};
390
391/**
392 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
393 *
394 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
395 */
396void drm_fb_helper_restore(void)
397{
398 bool ret;
399 ret = drm_fb_helper_force_kernel_mode();
400 if (ret == true)
401 DRM_ERROR("Failed to restore crtc configuration\n");
402}
403EXPORT_SYMBOL(drm_fb_helper_restore);
404
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200405#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000406static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
407{
408 drm_fb_helper_restore();
409}
410static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
411
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700412static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000413{
414 schedule_work(&drm_fb_helper_restore_work);
415}
416
417static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
418 .handler = drm_fb_helper_sysrq,
419 .help_msg = "force-fb(V)",
420 .action_msg = "Restore framebuffer console",
421};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000422#else
423static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200424#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000425
426static void drm_fb_helper_on(struct fb_info *info)
427{
428 struct drm_fb_helper *fb_helper = info->par;
429 struct drm_device *dev = fb_helper->dev;
430 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000431 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700432 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000433 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700434 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000435
436 /*
437 * For each CRTC in this fb, turn the crtc on then,
438 * find all associated encoders and turn them on.
439 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000440 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700441 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000442 crtc = fb_helper->crtc_info[i].mode_set.crtc;
443 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000444
Dave Airlie8be48d92010-03-30 05:34:14 +0000445 if (!crtc->enabled)
446 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000447
Dave Airlie8be48d92010-03-30 05:34:14 +0000448 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000449
Jesse Barnes023eb572010-07-02 10:48:08 -0700450 /* Walk the connectors & encoders on this fb turning them on */
451 for (j = 0; j < fb_helper->connector_count; j++) {
452 connector = fb_helper->connector_info[j]->connector;
453 connector->dpms = DRM_MODE_DPMS_ON;
454 drm_connector_property_set_value(connector,
455 dev->mode_config.dpms_property,
456 DRM_MODE_DPMS_ON);
457 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000458 /* Found a CRTC on this fb, now find encoders */
459 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
460 if (encoder->crtc == crtc) {
461 struct drm_encoder_helper_funcs *encoder_funcs;
462
463 encoder_funcs = encoder->helper_private;
464 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000465 }
466 }
467 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000468 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000469}
470
471static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
472{
473 struct drm_fb_helper *fb_helper = info->par;
474 struct drm_device *dev = fb_helper->dev;
475 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000476 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700477 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000478 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700479 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000480
481 /*
482 * For each CRTC in this fb, find all associated encoders
483 * and turn them off, then turn off the CRTC.
484 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000485 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700486 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000487 crtc = fb_helper->crtc_info[i].mode_set.crtc;
488 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000489
Dave Airlie8be48d92010-03-30 05:34:14 +0000490 if (!crtc->enabled)
491 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000492
Jesse Barnes023eb572010-07-02 10:48:08 -0700493 /* Walk the connectors on this fb and mark them off */
494 for (j = 0; j < fb_helper->connector_count; j++) {
495 connector = fb_helper->connector_info[j]->connector;
496 connector->dpms = dpms_mode;
497 drm_connector_property_set_value(connector,
498 dev->mode_config.dpms_property,
499 dpms_mode);
500 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000501 /* Found a CRTC on this fb, now find encoders */
502 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
503 if (encoder->crtc == crtc) {
504 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000505
Dave Airlie8be48d92010-03-30 05:34:14 +0000506 encoder_funcs = encoder->helper_private;
507 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700508 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000509 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000510 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000511 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000512 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000513}
514
515int drm_fb_helper_blank(int blank, struct fb_info *info)
516{
517 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000518 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000519 case FB_BLANK_UNBLANK:
520 drm_fb_helper_on(info);
521 break;
James Simmons731b5a12009-10-29 20:39:07 +0000522 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000523 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800524 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000525 break;
James Simmons731b5a12009-10-29 20:39:07 +0000526 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000527 case FB_BLANK_HSYNC_SUSPEND:
528 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
529 break;
James Simmons731b5a12009-10-29 20:39:07 +0000530 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000531 case FB_BLANK_VSYNC_SUSPEND:
532 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
533 break;
James Simmons731b5a12009-10-29 20:39:07 +0000534 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000535 case FB_BLANK_POWERDOWN:
536 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
537 break;
538 }
539 return 0;
540}
541EXPORT_SYMBOL(drm_fb_helper_blank);
542
543static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
544{
545 int i;
546
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000547 for (i = 0; i < helper->connector_count; i++)
548 kfree(helper->connector_info[i]);
549 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000550 for (i = 0; i < helper->crtc_count; i++)
551 kfree(helper->crtc_info[i].mode_set.connectors);
552 kfree(helper->crtc_info);
553}
554
Dave Airlie4abe3522010-03-30 05:34:18 +0000555int drm_fb_helper_init(struct drm_device *dev,
556 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000557 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000558{
Dave Airlie785b93e2009-08-28 15:46:53 +1000559 struct drm_crtc *crtc;
560 int ret = 0;
561 int i;
562
Dave Airlie4abe3522010-03-30 05:34:18 +0000563 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000564
565 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
566
567 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
568 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000569 return -ENOMEM;
570
Dave Airlie4abe3522010-03-30 05:34:18 +0000571 fb_helper->crtc_count = crtc_count;
572 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
573 if (!fb_helper->connector_info) {
574 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000575 return -ENOMEM;
576 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000577 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000578
579 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000580 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000581 kcalloc(max_conn_count,
582 sizeof(struct drm_connector *),
583 GFP_KERNEL);
584
Dave Airlie4abe3522010-03-30 05:34:18 +0000585 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000586 ret = -ENOMEM;
587 goto out_free;
588 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000589 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000590 }
591
592 i = 0;
593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000594 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
595 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000596 i++;
597 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000598 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000599 return 0;
600out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000601 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000602 return -ENOMEM;
603}
Dave Airlie4abe3522010-03-30 05:34:18 +0000604EXPORT_SYMBOL(drm_fb_helper_init);
605
606void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
607{
608 if (!list_empty(&fb_helper->kernel_fb_list)) {
609 list_del(&fb_helper->kernel_fb_list);
610 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400611 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000612 atomic_notifier_chain_unregister(&panic_notifier_list,
613 &paniced);
614 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
615 }
616 }
617
618 drm_fb_helper_crtc_free(fb_helper);
619
Dave Airlie4abe3522010-03-30 05:34:18 +0000620}
621EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000622
Dave Airliec850cb72009-10-23 18:49:03 +1000623static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000624 u16 blue, u16 regno, struct fb_info *info)
625{
626 struct drm_fb_helper *fb_helper = info->par;
627 struct drm_framebuffer *fb = fb_helper->fb;
628 int pindex;
629
Dave Airliec850cb72009-10-23 18:49:03 +1000630 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
631 u32 *palette;
632 u32 value;
633 /* place color in psuedopalette */
634 if (regno > 16)
635 return -EINVAL;
636 palette = (u32 *)info->pseudo_palette;
637 red >>= (16 - info->var.red.length);
638 green >>= (16 - info->var.green.length);
639 blue >>= (16 - info->var.blue.length);
640 value = (red << info->var.red.offset) |
641 (green << info->var.green.offset) |
642 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000643 if (info->var.transp.length > 0) {
644 u32 mask = (1 << info->var.transp.length) - 1;
645 mask <<= info->var.transp.offset;
646 value |= mask;
647 }
Dave Airliec850cb72009-10-23 18:49:03 +1000648 palette[regno] = value;
649 return 0;
650 }
651
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000652 pindex = regno;
653
654 if (fb->bits_per_pixel == 16) {
655 pindex = regno << 3;
656
657 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000658 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000659 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000660 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000661
662 if (fb->depth == 16) {
663 u16 r, g, b;
664 int i;
665 if (regno < 32) {
666 for (i = 0; i < 8; i++)
667 fb_helper->funcs->gamma_set(crtc, red,
668 green, blue, pindex + i);
669 }
670
671 fb_helper->funcs->gamma_get(crtc, &r,
672 &g, &b,
673 pindex >> 1);
674
675 for (i = 0; i < 4; i++)
676 fb_helper->funcs->gamma_set(crtc, r,
677 green, b,
678 (pindex >> 1) + i);
679 }
680 }
681
682 if (fb->depth != 16)
683 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000684 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000685}
686
Dave Airlie068143d2009-10-05 09:58:02 +1000687int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
688{
689 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000690 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000691 u16 *red, *green, *blue, *transp;
692 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100693 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000694 int start;
695
Dave Airlie8be48d92010-03-30 05:34:14 +0000696 for (i = 0; i < fb_helper->crtc_count; i++) {
697 crtc = fb_helper->crtc_info[i].mode_set.crtc;
698 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000699
700 red = cmap->red;
701 green = cmap->green;
702 blue = cmap->blue;
703 transp = cmap->transp;
704 start = cmap->start;
705
roel062ac622011-03-07 18:00:34 +0100706 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000707 u16 hred, hgreen, hblue, htransp = 0xffff;
708
709 hred = *red++;
710 hgreen = *green++;
711 hblue = *blue++;
712
713 if (transp)
714 htransp = *transp++;
715
Dave Airliec850cb72009-10-23 18:49:03 +1000716 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
717 if (rc)
718 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000719 }
720 crtc_funcs->load_lut(crtc);
721 }
722 return rc;
723}
724EXPORT_SYMBOL(drm_fb_helper_setcmap);
725
Dave Airlie785b93e2009-08-28 15:46:53 +1000726int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
727 struct fb_info *info)
728{
729 struct drm_fb_helper *fb_helper = info->par;
730 struct drm_framebuffer *fb = fb_helper->fb;
731 int depth;
732
Jason Wesself90ebd92010-08-05 09:22:32 -0500733 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000734 return -EINVAL;
735
736 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000737 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
738 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
739 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
740 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000741 return -EINVAL;
742 }
743
744 switch (var->bits_per_pixel) {
745 case 16:
746 depth = (var->green.length == 6) ? 16 : 15;
747 break;
748 case 32:
749 depth = (var->transp.length > 0) ? 32 : 24;
750 break;
751 default:
752 depth = var->bits_per_pixel;
753 break;
754 }
755
756 switch (depth) {
757 case 8:
758 var->red.offset = 0;
759 var->green.offset = 0;
760 var->blue.offset = 0;
761 var->red.length = 8;
762 var->green.length = 8;
763 var->blue.length = 8;
764 var->transp.length = 0;
765 var->transp.offset = 0;
766 break;
767 case 15:
768 var->red.offset = 10;
769 var->green.offset = 5;
770 var->blue.offset = 0;
771 var->red.length = 5;
772 var->green.length = 5;
773 var->blue.length = 5;
774 var->transp.length = 1;
775 var->transp.offset = 15;
776 break;
777 case 16:
778 var->red.offset = 11;
779 var->green.offset = 5;
780 var->blue.offset = 0;
781 var->red.length = 5;
782 var->green.length = 6;
783 var->blue.length = 5;
784 var->transp.length = 0;
785 var->transp.offset = 0;
786 break;
787 case 24:
788 var->red.offset = 16;
789 var->green.offset = 8;
790 var->blue.offset = 0;
791 var->red.length = 8;
792 var->green.length = 8;
793 var->blue.length = 8;
794 var->transp.length = 0;
795 var->transp.offset = 0;
796 break;
797 case 32:
798 var->red.offset = 16;
799 var->green.offset = 8;
800 var->blue.offset = 0;
801 var->red.length = 8;
802 var->green.length = 8;
803 var->blue.length = 8;
804 var->transp.length = 8;
805 var->transp.offset = 24;
806 break;
807 default:
808 return -EINVAL;
809 }
810 return 0;
811}
812EXPORT_SYMBOL(drm_fb_helper_check_var);
813
814/* this will let fbcon do the mode init */
815int drm_fb_helper_set_par(struct fb_info *info)
816{
817 struct drm_fb_helper *fb_helper = info->par;
818 struct drm_device *dev = fb_helper->dev;
819 struct fb_var_screeninfo *var = &info->var;
820 struct drm_crtc *crtc;
821 int ret;
822 int i;
823
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100824 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000825 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000826 return -EINVAL;
827 }
828
Dave Airlie8be48d92010-03-30 05:34:14 +0000829 mutex_lock(&dev->mode_config.mutex);
830 for (i = 0; i < fb_helper->crtc_count; i++) {
831 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000832 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
833 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000834 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000835 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000836 }
837 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000838 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000839
840 if (fb_helper->delayed_hotplug) {
841 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000842 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000843 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000844 return 0;
845}
846EXPORT_SYMBOL(drm_fb_helper_set_par);
847
848int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
849 struct fb_info *info)
850{
851 struct drm_fb_helper *fb_helper = info->par;
852 struct drm_device *dev = fb_helper->dev;
853 struct drm_mode_set *modeset;
854 struct drm_crtc *crtc;
855 int ret = 0;
856 int i;
857
Dave Airlie8be48d92010-03-30 05:34:14 +0000858 mutex_lock(&dev->mode_config.mutex);
859 for (i = 0; i < fb_helper->crtc_count; i++) {
860 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000861
862 modeset = &fb_helper->crtc_info[i].mode_set;
863
864 modeset->x = var->xoffset;
865 modeset->y = var->yoffset;
866
867 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000868 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000869 if (!ret) {
870 info->var.xoffset = var->xoffset;
871 info->var.yoffset = var->yoffset;
872 }
873 }
874 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000875 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000876 return ret;
877}
878EXPORT_SYMBOL(drm_fb_helper_pan_display);
879
Dave Airlie8be48d92010-03-30 05:34:14 +0000880int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
881 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000882{
Dave Airlie785b93e2009-08-28 15:46:53 +1000883 int new_fb = 0;
884 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000885 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000886 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000887 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000888 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000889
890 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
891 sizes.surface_depth = 24;
892 sizes.surface_bpp = 32;
893 sizes.fb_width = (unsigned)-1;
894 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000895
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000896 /* if driver picks 8 or 16 by default use that
897 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000898 if (preferred_bpp != sizes.surface_bpp) {
899 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000900 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000901 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000902 for (i = 0; i < fb_helper->connector_count; i++) {
903 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000904 struct drm_fb_helper_cmdline_mode *cmdline_mode;
905
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000906 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000907
908 if (cmdline_mode->bpp_specified) {
909 switch (cmdline_mode->bpp) {
910 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000911 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000912 break;
913 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000914 sizes.surface_depth = 15;
915 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000916 break;
917 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000918 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000919 break;
920 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000921 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000922 break;
923 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000924 sizes.surface_depth = 24;
925 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000926 break;
927 }
928 break;
929 }
930 }
931
Dave Airlie8be48d92010-03-30 05:34:14 +0000932 crtc_count = 0;
933 for (i = 0; i < fb_helper->crtc_count; i++) {
934 struct drm_display_mode *desired_mode;
935 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000936
Dave Airlie8be48d92010-03-30 05:34:14 +0000937 if (desired_mode) {
938 if (gamma_size == 0)
939 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
940 if (desired_mode->hdisplay < sizes.fb_width)
941 sizes.fb_width = desired_mode->hdisplay;
942 if (desired_mode->vdisplay < sizes.fb_height)
943 sizes.fb_height = desired_mode->vdisplay;
944 if (desired_mode->hdisplay > sizes.surface_width)
945 sizes.surface_width = desired_mode->hdisplay;
946 if (desired_mode->vdisplay > sizes.surface_height)
947 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000948 crtc_count++;
949 }
950 }
951
Dave Airlie38651672010-03-30 05:34:13 +0000952 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000953 /* hmm everyone went away - assume VGA cable just fell out
954 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000955 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000956 sizes.fb_width = sizes.surface_width = 1024;
957 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000958 }
959
Dave Airlie38651672010-03-30 05:34:13 +0000960 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000961 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000962 if (new_fb < 0)
963 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000964
Dave Airlie38651672010-03-30 05:34:13 +0000965 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000966
Dave Airlie8be48d92010-03-30 05:34:14 +0000967 /* set the fb pointer */
968 for (i = 0; i < fb_helper->crtc_count; i++) {
969 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000970 }
971
Dave Airlie785b93e2009-08-28 15:46:53 +1000972 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100973 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100974 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000975 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100976 }
Dave Airlie38651672010-03-30 05:34:13 +0000977
978 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
979 info->fix.id);
980
Dave Airlie785b93e2009-08-28 15:46:53 +1000981 } else {
982 drm_fb_helper_set_par(info);
983 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000984
985 /* Switch back to kernel console on panic */
986 /* multi card linked list maybe */
987 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400988 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000989 atomic_notifier_chain_register(&panic_notifier_list,
990 &paniced);
991 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
992 }
Dave Airlie38651672010-03-30 05:34:13 +0000993 if (new_fb)
994 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
995
Dave Airlie785b93e2009-08-28 15:46:53 +1000996 return 0;
997}
998EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
999
Dave Airlie3632ef82011-01-15 09:27:00 +10001000void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1001 uint32_t depth)
1002{
1003 info->fix.type = FB_TYPE_PACKED_PIXELS;
1004 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1005 FB_VISUAL_TRUECOLOR;
1006 info->fix.mmio_start = 0;
1007 info->fix.mmio_len = 0;
1008 info->fix.type_aux = 0;
1009 info->fix.xpanstep = 1; /* doing it in hw */
1010 info->fix.ypanstep = 1; /* doing it in hw */
1011 info->fix.ywrapstep = 0;
1012 info->fix.accel = FB_ACCEL_NONE;
1013 info->fix.type_aux = 0;
1014
1015 info->fix.line_length = pitch;
1016 return;
1017}
1018EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1019
Dave Airlie38651672010-03-30 05:34:13 +00001020void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001021 uint32_t fb_width, uint32_t fb_height)
1022{
Dave Airlie38651672010-03-30 05:34:13 +00001023 struct drm_framebuffer *fb = fb_helper->fb;
1024 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001025 info->var.xres_virtual = fb->width;
1026 info->var.yres_virtual = fb->height;
1027 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001028 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001029 info->var.xoffset = 0;
1030 info->var.yoffset = 0;
1031 info->var.activate = FB_ACTIVATE_NOW;
1032 info->var.height = -1;
1033 info->var.width = -1;
1034
1035 switch (fb->depth) {
1036 case 8:
1037 info->var.red.offset = 0;
1038 info->var.green.offset = 0;
1039 info->var.blue.offset = 0;
1040 info->var.red.length = 8; /* 8bit DAC */
1041 info->var.green.length = 8;
1042 info->var.blue.length = 8;
1043 info->var.transp.offset = 0;
1044 info->var.transp.length = 0;
1045 break;
1046 case 15:
1047 info->var.red.offset = 10;
1048 info->var.green.offset = 5;
1049 info->var.blue.offset = 0;
1050 info->var.red.length = 5;
1051 info->var.green.length = 5;
1052 info->var.blue.length = 5;
1053 info->var.transp.offset = 15;
1054 info->var.transp.length = 1;
1055 break;
1056 case 16:
1057 info->var.red.offset = 11;
1058 info->var.green.offset = 5;
1059 info->var.blue.offset = 0;
1060 info->var.red.length = 5;
1061 info->var.green.length = 6;
1062 info->var.blue.length = 5;
1063 info->var.transp.offset = 0;
1064 break;
1065 case 24:
1066 info->var.red.offset = 16;
1067 info->var.green.offset = 8;
1068 info->var.blue.offset = 0;
1069 info->var.red.length = 8;
1070 info->var.green.length = 8;
1071 info->var.blue.length = 8;
1072 info->var.transp.offset = 0;
1073 info->var.transp.length = 0;
1074 break;
1075 case 32:
1076 info->var.red.offset = 16;
1077 info->var.green.offset = 8;
1078 info->var.blue.offset = 0;
1079 info->var.red.length = 8;
1080 info->var.green.length = 8;
1081 info->var.blue.length = 8;
1082 info->var.transp.offset = 24;
1083 info->var.transp.length = 8;
1084 break;
1085 default:
1086 break;
1087 }
1088
1089 info->var.xres = fb_width;
1090 info->var.yres = fb_height;
1091}
1092EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001093
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001094static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1095 uint32_t maxX,
1096 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001097{
1098 struct drm_connector *connector;
1099 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001100 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001101
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001102 for (i = 0; i < fb_helper->connector_count; i++) {
1103 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001104 count += connector->funcs->fill_modes(connector, maxX, maxY);
1105 }
1106
1107 return count;
1108}
1109
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001110static 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 +00001111{
1112 struct drm_display_mode *mode;
1113
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001114 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001115 if (drm_mode_width(mode) > width ||
1116 drm_mode_height(mode) > height)
1117 continue;
1118 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1119 return mode;
1120 }
1121 return NULL;
1122}
1123
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001124static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001125{
Dave Airlie38651672010-03-30 05:34:13 +00001126 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001127 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001128 return cmdline_mode->specified;
1129}
1130
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001131static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1132 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001133{
Dave Airlie38651672010-03-30 05:34:13 +00001134 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1135 struct drm_display_mode *mode = NULL;
1136
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001137 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001138 if (cmdline_mode->specified == false)
1139 return mode;
1140
1141 /* attempt to find a matching mode in the list of modes
1142 * we have gotten so far, if not add a CVT mode that conforms
1143 */
1144 if (cmdline_mode->rb || cmdline_mode->margins)
1145 goto create_mode;
1146
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001147 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001148 /* check width/height */
1149 if (mode->hdisplay != cmdline_mode->xres ||
1150 mode->vdisplay != cmdline_mode->yres)
1151 continue;
1152
1153 if (cmdline_mode->refresh_specified) {
1154 if (mode->vrefresh != cmdline_mode->refresh)
1155 continue;
1156 }
1157
1158 if (cmdline_mode->interlace) {
1159 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1160 continue;
1161 }
1162 return mode;
1163 }
1164
1165create_mode:
Adam Jacksonb829e012010-06-10 13:33:26 -04001166 if (cmdline_mode->cvt)
1167 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1168 cmdline_mode->xres, cmdline_mode->yres,
1169 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1170 cmdline_mode->rb, cmdline_mode->interlace,
1171 cmdline_mode->margins);
1172 else
1173 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1174 cmdline_mode->xres, cmdline_mode->yres,
1175 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1176 cmdline_mode->interlace,
1177 cmdline_mode->margins);
Dave Airlie38651672010-03-30 05:34:13 +00001178 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001179 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001180 return mode;
1181}
1182
1183static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1184{
1185 bool enable;
1186
1187 if (strict) {
1188 enable = connector->status == connector_status_connected;
1189 } else {
1190 enable = connector->status != connector_status_disconnected;
1191 }
1192 return enable;
1193}
1194
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001195static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1196 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001197{
1198 bool any_enabled = false;
1199 struct drm_connector *connector;
1200 int i = 0;
1201
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001202 for (i = 0; i < fb_helper->connector_count; i++) {
1203 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001204 enabled[i] = drm_connector_enabled(connector, true);
1205 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1206 enabled[i] ? "yes" : "no");
1207 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001208 }
1209
1210 if (any_enabled)
1211 return;
1212
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001213 for (i = 0; i < fb_helper->connector_count; i++) {
1214 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001215 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001216 }
1217}
1218
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001219static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1220 struct drm_display_mode **modes,
1221 bool *enabled, int width, int height)
1222{
1223 int count, i, j;
1224 bool can_clone = false;
1225 struct drm_fb_helper_connector *fb_helper_conn;
1226 struct drm_display_mode *dmt_mode, *mode;
1227
1228 /* only contemplate cloning in the single crtc case */
1229 if (fb_helper->crtc_count > 1)
1230 return false;
1231
1232 count = 0;
1233 for (i = 0; i < fb_helper->connector_count; i++) {
1234 if (enabled[i])
1235 count++;
1236 }
1237
1238 /* only contemplate cloning if more than one connector is enabled */
1239 if (count <= 1)
1240 return false;
1241
1242 /* check the command line or if nothing common pick 1024x768 */
1243 can_clone = true;
1244 for (i = 0; i < fb_helper->connector_count; i++) {
1245 if (!enabled[i])
1246 continue;
1247 fb_helper_conn = fb_helper->connector_info[i];
1248 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1249 if (!modes[i]) {
1250 can_clone = false;
1251 break;
1252 }
1253 for (j = 0; j < i; j++) {
1254 if (!enabled[j])
1255 continue;
1256 if (!drm_mode_equal(modes[j], modes[i]))
1257 can_clone = false;
1258 }
1259 }
1260
1261 if (can_clone) {
1262 DRM_DEBUG_KMS("can clone using command line\n");
1263 return true;
1264 }
1265
1266 /* try and find a 1024x768 mode on each connector */
1267 can_clone = true;
1268 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1269
1270 for (i = 0; i < fb_helper->connector_count; i++) {
1271
1272 if (!enabled[i])
1273 continue;
1274
1275 fb_helper_conn = fb_helper->connector_info[i];
1276 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1277 if (drm_mode_equal(mode, dmt_mode))
1278 modes[i] = mode;
1279 }
1280 if (!modes[i])
1281 can_clone = false;
1282 }
1283
1284 if (can_clone) {
1285 DRM_DEBUG_KMS("can clone using 1024x768\n");
1286 return true;
1287 }
1288 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1289 return false;
1290}
1291
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001292static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001293 struct drm_display_mode **modes,
1294 bool *enabled, int width, int height)
1295{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001296 struct drm_fb_helper_connector *fb_helper_conn;
1297 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001298
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001299 for (i = 0; i < fb_helper->connector_count; i++) {
1300 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001301
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001302 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001303 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001304
1305 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001306 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001307
1308 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001309 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001310 if (!modes[i]) {
1311 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001312 fb_helper_conn->connector->base.id);
1313 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001314 }
1315 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001316 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1317 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001318 break;
1319 }
1320 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1321 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001322 }
1323 return true;
1324}
1325
Dave Airlie8be48d92010-03-30 05:34:14 +00001326static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1327 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001328 struct drm_display_mode **modes,
1329 int n, int width, int height)
1330{
1331 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001332 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001333 struct drm_connector *connector;
1334 struct drm_connector_helper_funcs *connector_funcs;
1335 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001336 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001337 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001338 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001339 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001340
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001341 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001342 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001343
1344 fb_helper_conn = fb_helper->connector_info[n];
1345 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001346
1347 best_crtcs[n] = NULL;
1348 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001349 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001350 if (modes[n] == NULL)
1351 return best_score;
1352
Dave Airlie8be48d92010-03-30 05:34:14 +00001353 crtcs = kzalloc(dev->mode_config.num_connector *
1354 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001355 if (!crtcs)
1356 return best_score;
1357
1358 my_score = 1;
1359 if (connector->status == connector_status_connected)
1360 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001361 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001362 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001363 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001364 my_score++;
1365
1366 connector_funcs = connector->helper_private;
1367 encoder = connector_funcs->best_encoder(connector);
1368 if (!encoder)
1369 goto out;
1370
Dave Airlie38651672010-03-30 05:34:13 +00001371 /* select a crtc for this connector and then attempt to configure
1372 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001373 for (c = 0; c < fb_helper->crtc_count; c++) {
1374 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001375
1376 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001377 continue;
1378 }
1379
1380 for (o = 0; o < n; o++)
1381 if (best_crtcs[o] == crtc)
1382 break;
1383
1384 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001385 /* ignore cloning unless only a single crtc */
1386 if (fb_helper->crtc_count > 1)
1387 continue;
1388
1389 if (!drm_mode_equal(modes[o], modes[n]))
1390 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001391 }
1392
1393 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001394 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1395 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001396 width, height);
1397 if (score > best_score) {
1398 best_crtc = crtc;
1399 best_score = score;
1400 memcpy(best_crtcs, crtcs,
1401 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001402 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001403 }
Dave Airlie38651672010-03-30 05:34:13 +00001404 }
1405out:
1406 kfree(crtcs);
1407 return best_score;
1408}
1409
Dave Airlie8be48d92010-03-30 05:34:14 +00001410static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001411{
Dave Airlie8be48d92010-03-30 05:34:14 +00001412 struct drm_device *dev = fb_helper->dev;
1413 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001414 struct drm_display_mode **modes;
1415 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001416 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001417 bool *enabled;
1418 int width, height;
1419 int i, ret;
1420
1421 DRM_DEBUG_KMS("\n");
1422
1423 width = dev->mode_config.max_width;
1424 height = dev->mode_config.max_height;
1425
1426 /* clean out all the encoder/crtc combos */
1427 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1428 encoder->crtc = NULL;
1429 }
1430
1431 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001432 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001433 modes = kcalloc(dev->mode_config.num_connector,
1434 sizeof(struct drm_display_mode *), GFP_KERNEL);
1435 enabled = kcalloc(dev->mode_config.num_connector,
1436 sizeof(bool), GFP_KERNEL);
1437
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001438 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001439
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001440 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1441 if (!ret) {
1442 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1443 if (!ret)
1444 DRM_ERROR("Unable to find initial modes\n");
1445 }
Dave Airlie38651672010-03-30 05:34:13 +00001446
1447 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1448
Dave Airlie8be48d92010-03-30 05:34:14 +00001449 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1450
1451 /* need to set the modesets up here for use later */
1452 /* fill out the connector<->crtc mappings into the modesets */
1453 for (i = 0; i < fb_helper->crtc_count; i++) {
1454 modeset = &fb_helper->crtc_info[i].mode_set;
1455 modeset->num_connectors = 0;
1456 }
Dave Airlie38651672010-03-30 05:34:13 +00001457
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001458 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001459 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001460 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1461 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001462
Dave Airlie8be48d92010-03-30 05:34:14 +00001463 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001464 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001465 mode->name, fb_crtc->mode_set.crtc->base.id);
1466 fb_crtc->desired_mode = mode;
1467 if (modeset->mode)
1468 drm_mode_destroy(dev, modeset->mode);
1469 modeset->mode = drm_mode_duplicate(dev,
1470 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001471 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001472 }
Dave Airlie38651672010-03-30 05:34:13 +00001473 }
1474
1475 kfree(crtcs);
1476 kfree(modes);
1477 kfree(enabled);
1478}
1479
1480/**
1481 * drm_helper_initial_config - setup a sane initial connector configuration
1482 * @dev: DRM device
1483 *
1484 * LOCKING:
1485 * Called at init time, must take mode config lock.
1486 *
1487 * Scan the CRTCs and connectors and try to put together an initial setup.
1488 * At the moment, this is a cloned configuration across all heads with
1489 * a new framebuffer object as the backing store.
1490 *
1491 * RETURNS:
1492 * Zero if everything went ok, nonzero otherwise.
1493 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001494bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001495{
Dave Airlie8be48d92010-03-30 05:34:14 +00001496 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001497 int count = 0;
1498
1499 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001500 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001501
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001502 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001503
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001504 count = drm_fb_helper_probe_connector_modes(fb_helper,
1505 dev->mode_config.max_width,
1506 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001507 /*
1508 * we shouldn't end up with no modes here.
1509 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001510 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001511 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001512 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001513 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001514
Dave Airlie4abe3522010-03-30 05:34:18 +00001515 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001516}
Dave Airlie8be48d92010-03-30 05:34:14 +00001517EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001518
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001519bool drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001520{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001521 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001522 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001523 bool bound = false, crtcs_bound = false;
1524 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001525
1526 if (!fb_helper->fb)
1527 return false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001528
1529 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1530 if (crtc->fb)
1531 crtcs_bound = true;
1532 if (crtc->fb == fb_helper->fb)
1533 bound = true;
1534 }
1535
1536 if (!bound && crtcs_bound) {
1537 fb_helper->delayed_hotplug = true;
1538 return false;
1539 }
Dave Airlie38651672010-03-30 05:34:13 +00001540 DRM_DEBUG_KMS("\n");
1541
Dave Airlie4abe3522010-03-30 05:34:18 +00001542 max_width = fb_helper->fb->width;
1543 max_height = fb_helper->fb->height;
1544 bpp_sel = fb_helper->fb->bits_per_pixel;
1545
Dave Airlie5c4426a2010-03-30 05:34:17 +00001546 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001547 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001548 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001549
Dave Airlie4abe3522010-03-30 05:34:18 +00001550 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001551}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001552EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001553
David Rientjes6a108a12011-01-20 14:44:16 -08001554/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001555 * but the module doesn't depend on any fb console symbols. At least
1556 * attempt to load fbcon to avoid leaving the system without a usable console.
1557 */
David Rientjes6a108a12011-01-20 14:44:16 -08001558#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001559static int __init drm_fb_helper_modinit(void)
1560{
1561 const char *name = "fbcon";
1562 struct module *fbcon;
1563
1564 mutex_lock(&module_mutex);
1565 fbcon = find_module(name);
1566 mutex_unlock(&module_mutex);
1567
1568 if (!fbcon)
1569 request_module_nowait(name);
1570 return 0;
1571}
1572
1573module_init(drm_fb_helper_modinit);
1574#endif