blob: 77fec5a9383278a4190605d323a45c0b36921e11 [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>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040034#include <linux/module.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include "drmP.h"
36#include "drm_crtc.h"
37#include "drm_fb_helper.h"
38#include "drm_crtc_helper.h"
39
Dave Airlie6fcefd52009-09-08 11:08:32 +100040MODULE_AUTHOR("David Airlie, Jesse Barnes");
41MODULE_DESCRIPTION("DRM KMS helper");
42MODULE_LICENSE("GPL and additional rights");
43
Dave Airlie785b93e2009-08-28 15:46:53 +100044static LIST_HEAD(kernel_fb_helper_list);
45
Dave Airlie0b4c0f32010-03-30 05:34:15 +000046/* simple single crtc case helper function */
47int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100048{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000049 struct drm_device *dev = fb_helper->dev;
50 struct drm_connector *connector;
51 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100052
Dave Airlie0b4c0f32010-03-30 05:34:15 +000053 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
54 struct drm_fb_helper_connector *fb_helper_connector;
55
56 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
57 if (!fb_helper_connector)
58 goto fail;
59
60 fb_helper_connector->connector = connector;
61 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
62 }
Dave Airlied50ba252009-09-23 14:44:08 +100063 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000064fail:
65 for (i = 0; i < fb_helper->connector_count; i++) {
66 kfree(fb_helper->connector_info[i]);
67 fb_helper->connector_info[i] = NULL;
68 }
69 fb_helper->connector_count = 0;
70 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +100071}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000072EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100073
Dave Airlie0b4c0f32010-03-30 05:34:15 +000074static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100075{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000076 struct drm_fb_helper_connector *fb_helper_conn;
77 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100078
Dave Airlie0b4c0f32010-03-30 05:34:15 +000079 for (i = 0; i < fb_helper->connector_count; i++) {
Chris Wilson1794d252011-04-17 07:43:32 +010080 struct drm_cmdline_mode *mode;
81 struct drm_connector *connector;
Dave Airlied50ba252009-09-23 14:44:08 +100082 char *option = NULL;
83
Dave Airlie0b4c0f32010-03-30 05:34:15 +000084 fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +010085 connector = fb_helper_conn->connector;
86 mode = &fb_helper_conn->cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000087
Dave Airlied50ba252009-09-23 14:44:08 +100088 /* do something on return - turn off connector maybe */
Chris Wilson1794d252011-04-17 07:43:32 +010089 if (fb_get_options(drm_get_connector_name(connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +100090 continue;
91
Chris Wilson1794d252011-04-17 07:43:32 +010092 if (drm_mode_parse_command_line_for_connector(option,
93 connector,
94 mode)) {
95 if (mode->force) {
96 const char *s;
97 switch (mode->force) {
98 case DRM_FORCE_OFF: s = "OFF"; break;
99 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
100 default:
101 case DRM_FORCE_ON: s = "ON"; break;
102 }
103
104 DRM_INFO("forcing %s connector %s\n",
105 drm_get_connector_name(connector), s);
106 connector->force = mode->force;
107 }
108
109 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
110 drm_get_connector_name(connector),
111 mode->xres, mode->yres,
112 mode->refresh_specified ? mode->refresh : 60,
113 mode->rb ? " reduced blanking" : "",
114 mode->margins ? " with margins" : "",
115 mode->interlace ? " interlaced" : "");
116 }
117
Dave Airlied50ba252009-09-23 14:44:08 +1000118 }
119 return 0;
120}
121
Jason Wessel99231022010-10-13 14:09:43 -0500122static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
123{
124 uint16_t *r_base, *g_base, *b_base;
125 int i;
126
127 r_base = crtc->gamma_store;
128 g_base = r_base + crtc->gamma_size;
129 b_base = g_base + crtc->gamma_size;
130
131 for (i = 0; i < crtc->gamma_size; i++)
132 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
133}
134
135static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
136{
137 uint16_t *r_base, *g_base, *b_base;
138
139 r_base = crtc->gamma_store;
140 g_base = r_base + crtc->gamma_size;
141 b_base = g_base + crtc->gamma_size;
142
143 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
144}
145
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500146int drm_fb_helper_debug_enter(struct fb_info *info)
147{
148 struct drm_fb_helper *helper = info->par;
149 struct drm_crtc_helper_funcs *funcs;
150 int i;
151
152 if (list_empty(&kernel_fb_helper_list))
153 return false;
154
155 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
156 for (i = 0; i < helper->crtc_count; i++) {
157 struct drm_mode_set *mode_set =
158 &helper->crtc_info[i].mode_set;
159
160 if (!mode_set->crtc->enabled)
161 continue;
162
163 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500164 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500165 funcs->mode_set_base_atomic(mode_set->crtc,
166 mode_set->fb,
167 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500168 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500169 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500170 }
171 }
172
173 return 0;
174}
175EXPORT_SYMBOL(drm_fb_helper_debug_enter);
176
177/* Find the real fb for a given fb helper CRTC */
178static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
179{
180 struct drm_device *dev = crtc->dev;
181 struct drm_crtc *c;
182
183 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
184 if (crtc->base.id == c->base.id)
185 return c->fb;
186 }
187
188 return NULL;
189}
190
191int drm_fb_helper_debug_leave(struct fb_info *info)
192{
193 struct drm_fb_helper *helper = info->par;
194 struct drm_crtc *crtc;
195 struct drm_crtc_helper_funcs *funcs;
196 struct drm_framebuffer *fb;
197 int i;
198
199 for (i = 0; i < helper->crtc_count; i++) {
200 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
201 crtc = mode_set->crtc;
202 funcs = crtc->helper_private;
203 fb = drm_mode_config_fb(crtc);
204
205 if (!crtc->enabled)
206 continue;
207
208 if (!fb) {
209 DRM_ERROR("no fb to restore??\n");
210 continue;
211 }
212
Jason Wessel99231022010-10-13 14:09:43 -0500213 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500214 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500215 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500216 }
217
218 return 0;
219}
220EXPORT_SYMBOL(drm_fb_helper_debug_leave);
221
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100222bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
223{
224 bool error = false;
225 int i, ret;
226 for (i = 0; i < fb_helper->crtc_count; i++) {
227 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
228 ret = drm_crtc_helper_set_config(mode_set);
229 if (ret)
230 error = true;
231 }
232 return error;
233}
234EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
235
Dave Airlie785b93e2009-08-28 15:46:53 +1000236bool drm_fb_helper_force_kernel_mode(void)
237{
Dave Airlie785b93e2009-08-28 15:46:53 +1000238 bool ret, error = false;
239 struct drm_fb_helper *helper;
240
241 if (list_empty(&kernel_fb_helper_list))
242 return false;
243
244 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100245 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
246 continue;
247
248 ret = drm_fb_helper_restore_fbdev_mode(helper);
249 if (ret)
250 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000251 }
252 return error;
253}
254
255int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
256 void *panic_str)
257{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700258 /*
259 * It's a waste of time and effort to switch back to text console
260 * if the kernel should reboot before panic messages can be seen.
261 */
262 if (panic_timeout < 0)
263 return 0;
264
Dave Airliefc2362a2010-06-07 12:14:54 +1000265 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000266 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000267}
268EXPORT_SYMBOL(drm_fb_helper_panic);
269
270static struct notifier_block paniced = {
271 .notifier_call = drm_fb_helper_panic,
272};
273
274/**
275 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
276 *
277 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
278 */
279void drm_fb_helper_restore(void)
280{
281 bool ret;
282 ret = drm_fb_helper_force_kernel_mode();
283 if (ret == true)
284 DRM_ERROR("Failed to restore crtc configuration\n");
285}
286EXPORT_SYMBOL(drm_fb_helper_restore);
287
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200288#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000289static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
290{
291 drm_fb_helper_restore();
292}
293static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
294
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700295static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000296{
297 schedule_work(&drm_fb_helper_restore_work);
298}
299
300static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
301 .handler = drm_fb_helper_sysrq,
302 .help_msg = "force-fb(V)",
303 .action_msg = "Restore framebuffer console",
304};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000305#else
306static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200307#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000308
309static void drm_fb_helper_on(struct fb_info *info)
310{
311 struct drm_fb_helper *fb_helper = info->par;
312 struct drm_device *dev = fb_helper->dev;
313 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000314 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700315 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000316 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700317 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000318
319 /*
320 * For each CRTC in this fb, turn the crtc on then,
321 * find all associated encoders and turn them on.
322 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000323 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700324 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000325 crtc = fb_helper->crtc_info[i].mode_set.crtc;
326 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000327
Dave Airlie8be48d92010-03-30 05:34:14 +0000328 if (!crtc->enabled)
329 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000330
Dave Airlie8be48d92010-03-30 05:34:14 +0000331 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000332
Jesse Barnes023eb572010-07-02 10:48:08 -0700333 /* Walk the connectors & encoders on this fb turning them on */
334 for (j = 0; j < fb_helper->connector_count; j++) {
335 connector = fb_helper->connector_info[j]->connector;
336 connector->dpms = DRM_MODE_DPMS_ON;
337 drm_connector_property_set_value(connector,
338 dev->mode_config.dpms_property,
339 DRM_MODE_DPMS_ON);
340 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000341 /* Found a CRTC on this fb, now find encoders */
342 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
343 if (encoder->crtc == crtc) {
344 struct drm_encoder_helper_funcs *encoder_funcs;
345
346 encoder_funcs = encoder->helper_private;
347 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000348 }
349 }
350 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000351 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000352}
353
354static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
355{
356 struct drm_fb_helper *fb_helper = info->par;
357 struct drm_device *dev = fb_helper->dev;
358 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000359 struct drm_crtc_helper_funcs *crtc_funcs;
Jesse Barnes023eb572010-07-02 10:48:08 -0700360 struct drm_connector *connector;
Dave Airlie785b93e2009-08-28 15:46:53 +1000361 struct drm_encoder *encoder;
Jesse Barnes023eb572010-07-02 10:48:08 -0700362 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000363
364 /*
365 * For each CRTC in this fb, find all associated encoders
366 * and turn them off, then turn off the CRTC.
367 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000368 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700369 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000370 crtc = fb_helper->crtc_info[i].mode_set.crtc;
371 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000372
Dave Airlie8be48d92010-03-30 05:34:14 +0000373 if (!crtc->enabled)
374 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000375
Jesse Barnes023eb572010-07-02 10:48:08 -0700376 /* Walk the connectors on this fb and mark them off */
377 for (j = 0; j < fb_helper->connector_count; j++) {
378 connector = fb_helper->connector_info[j]->connector;
379 connector->dpms = dpms_mode;
380 drm_connector_property_set_value(connector,
381 dev->mode_config.dpms_property,
382 dpms_mode);
383 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000384 /* Found a CRTC on this fb, now find encoders */
385 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
386 if (encoder->crtc == crtc) {
387 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000388
Dave Airlie8be48d92010-03-30 05:34:14 +0000389 encoder_funcs = encoder->helper_private;
390 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700391 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000392 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000393 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000395 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000396}
397
398int drm_fb_helper_blank(int blank, struct fb_info *info)
399{
400 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000401 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000402 case FB_BLANK_UNBLANK:
403 drm_fb_helper_on(info);
404 break;
James Simmons731b5a12009-10-29 20:39:07 +0000405 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000406 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800407 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000408 break;
James Simmons731b5a12009-10-29 20:39:07 +0000409 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000410 case FB_BLANK_HSYNC_SUSPEND:
411 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
412 break;
James Simmons731b5a12009-10-29 20:39:07 +0000413 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000414 case FB_BLANK_VSYNC_SUSPEND:
415 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
416 break;
James Simmons731b5a12009-10-29 20:39:07 +0000417 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000418 case FB_BLANK_POWERDOWN:
419 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
420 break;
421 }
422 return 0;
423}
424EXPORT_SYMBOL(drm_fb_helper_blank);
425
426static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
427{
428 int i;
429
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000430 for (i = 0; i < helper->connector_count; i++)
431 kfree(helper->connector_info[i]);
432 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100433 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000434 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100435 if (helper->crtc_info[i].mode_set.mode)
436 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
437 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000438 kfree(helper->crtc_info);
439}
440
Dave Airlie4abe3522010-03-30 05:34:18 +0000441int drm_fb_helper_init(struct drm_device *dev,
442 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000443 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000444{
Dave Airlie785b93e2009-08-28 15:46:53 +1000445 struct drm_crtc *crtc;
446 int ret = 0;
447 int i;
448
Dave Airlie4abe3522010-03-30 05:34:18 +0000449 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000450
451 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
452
453 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
454 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000455 return -ENOMEM;
456
Dave Airlie4abe3522010-03-30 05:34:18 +0000457 fb_helper->crtc_count = crtc_count;
458 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
459 if (!fb_helper->connector_info) {
460 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000461 return -ENOMEM;
462 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000463 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000464
465 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000466 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000467 kcalloc(max_conn_count,
468 sizeof(struct drm_connector *),
469 GFP_KERNEL);
470
Dave Airlie4abe3522010-03-30 05:34:18 +0000471 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000472 ret = -ENOMEM;
473 goto out_free;
474 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000475 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000476 }
477
478 i = 0;
479 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000480 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
481 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000482 i++;
483 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000484 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000485 return 0;
486out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000487 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000488 return -ENOMEM;
489}
Dave Airlie4abe3522010-03-30 05:34:18 +0000490EXPORT_SYMBOL(drm_fb_helper_init);
491
492void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
493{
494 if (!list_empty(&fb_helper->kernel_fb_list)) {
495 list_del(&fb_helper->kernel_fb_list);
496 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400497 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000498 atomic_notifier_chain_unregister(&panic_notifier_list,
499 &paniced);
500 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
501 }
502 }
503
504 drm_fb_helper_crtc_free(fb_helper);
505
Dave Airlie4abe3522010-03-30 05:34:18 +0000506}
507EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000508
Dave Airliec850cb72009-10-23 18:49:03 +1000509static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000510 u16 blue, u16 regno, struct fb_info *info)
511{
512 struct drm_fb_helper *fb_helper = info->par;
513 struct drm_framebuffer *fb = fb_helper->fb;
514 int pindex;
515
Dave Airliec850cb72009-10-23 18:49:03 +1000516 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
517 u32 *palette;
518 u32 value;
519 /* place color in psuedopalette */
520 if (regno > 16)
521 return -EINVAL;
522 palette = (u32 *)info->pseudo_palette;
523 red >>= (16 - info->var.red.length);
524 green >>= (16 - info->var.green.length);
525 blue >>= (16 - info->var.blue.length);
526 value = (red << info->var.red.offset) |
527 (green << info->var.green.offset) |
528 (blue << info->var.blue.offset);
Rob Clark9da12b62011-02-16 02:45:51 +0000529 if (info->var.transp.length > 0) {
530 u32 mask = (1 << info->var.transp.length) - 1;
531 mask <<= info->var.transp.offset;
532 value |= mask;
533 }
Dave Airliec850cb72009-10-23 18:49:03 +1000534 palette[regno] = value;
535 return 0;
536 }
537
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000538 pindex = regno;
539
540 if (fb->bits_per_pixel == 16) {
541 pindex = regno << 3;
542
543 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000544 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000545 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000546 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000547
548 if (fb->depth == 16) {
549 u16 r, g, b;
550 int i;
551 if (regno < 32) {
552 for (i = 0; i < 8; i++)
553 fb_helper->funcs->gamma_set(crtc, red,
554 green, blue, pindex + i);
555 }
556
557 fb_helper->funcs->gamma_get(crtc, &r,
558 &g, &b,
559 pindex >> 1);
560
561 for (i = 0; i < 4; i++)
562 fb_helper->funcs->gamma_set(crtc, r,
563 green, b,
564 (pindex >> 1) + i);
565 }
566 }
567
568 if (fb->depth != 16)
569 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000570 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000571}
572
Dave Airlie068143d2009-10-05 09:58:02 +1000573int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
574{
575 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000576 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000577 u16 *red, *green, *blue, *transp;
578 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100579 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000580 int start;
581
Dave Airlie8be48d92010-03-30 05:34:14 +0000582 for (i = 0; i < fb_helper->crtc_count; i++) {
583 crtc = fb_helper->crtc_info[i].mode_set.crtc;
584 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000585
586 red = cmap->red;
587 green = cmap->green;
588 blue = cmap->blue;
589 transp = cmap->transp;
590 start = cmap->start;
591
roel062ac622011-03-07 18:00:34 +0100592 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000593 u16 hred, hgreen, hblue, htransp = 0xffff;
594
595 hred = *red++;
596 hgreen = *green++;
597 hblue = *blue++;
598
599 if (transp)
600 htransp = *transp++;
601
Dave Airliec850cb72009-10-23 18:49:03 +1000602 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
603 if (rc)
604 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000605 }
606 crtc_funcs->load_lut(crtc);
607 }
608 return rc;
609}
610EXPORT_SYMBOL(drm_fb_helper_setcmap);
611
Dave Airlie785b93e2009-08-28 15:46:53 +1000612int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
613 struct fb_info *info)
614{
615 struct drm_fb_helper *fb_helper = info->par;
616 struct drm_framebuffer *fb = fb_helper->fb;
617 int depth;
618
Jason Wesself90ebd92010-08-05 09:22:32 -0500619 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000620 return -EINVAL;
621
622 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000623 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
624 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
625 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
626 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000627 return -EINVAL;
628 }
629
630 switch (var->bits_per_pixel) {
631 case 16:
632 depth = (var->green.length == 6) ? 16 : 15;
633 break;
634 case 32:
635 depth = (var->transp.length > 0) ? 32 : 24;
636 break;
637 default:
638 depth = var->bits_per_pixel;
639 break;
640 }
641
642 switch (depth) {
643 case 8:
644 var->red.offset = 0;
645 var->green.offset = 0;
646 var->blue.offset = 0;
647 var->red.length = 8;
648 var->green.length = 8;
649 var->blue.length = 8;
650 var->transp.length = 0;
651 var->transp.offset = 0;
652 break;
653 case 15:
654 var->red.offset = 10;
655 var->green.offset = 5;
656 var->blue.offset = 0;
657 var->red.length = 5;
658 var->green.length = 5;
659 var->blue.length = 5;
660 var->transp.length = 1;
661 var->transp.offset = 15;
662 break;
663 case 16:
664 var->red.offset = 11;
665 var->green.offset = 5;
666 var->blue.offset = 0;
667 var->red.length = 5;
668 var->green.length = 6;
669 var->blue.length = 5;
670 var->transp.length = 0;
671 var->transp.offset = 0;
672 break;
673 case 24:
674 var->red.offset = 16;
675 var->green.offset = 8;
676 var->blue.offset = 0;
677 var->red.length = 8;
678 var->green.length = 8;
679 var->blue.length = 8;
680 var->transp.length = 0;
681 var->transp.offset = 0;
682 break;
683 case 32:
684 var->red.offset = 16;
685 var->green.offset = 8;
686 var->blue.offset = 0;
687 var->red.length = 8;
688 var->green.length = 8;
689 var->blue.length = 8;
690 var->transp.length = 8;
691 var->transp.offset = 24;
692 break;
693 default:
694 return -EINVAL;
695 }
696 return 0;
697}
698EXPORT_SYMBOL(drm_fb_helper_check_var);
699
700/* this will let fbcon do the mode init */
701int drm_fb_helper_set_par(struct fb_info *info)
702{
703 struct drm_fb_helper *fb_helper = info->par;
704 struct drm_device *dev = fb_helper->dev;
705 struct fb_var_screeninfo *var = &info->var;
706 struct drm_crtc *crtc;
707 int ret;
708 int i;
709
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100710 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000711 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000712 return -EINVAL;
713 }
714
Dave Airlie8be48d92010-03-30 05:34:14 +0000715 mutex_lock(&dev->mode_config.mutex);
716 for (i = 0; i < fb_helper->crtc_count; i++) {
717 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000718 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
719 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000720 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000721 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000722 }
723 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000724 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000725
726 if (fb_helper->delayed_hotplug) {
727 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000728 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000729 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000730 return 0;
731}
732EXPORT_SYMBOL(drm_fb_helper_set_par);
733
734int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
735 struct fb_info *info)
736{
737 struct drm_fb_helper *fb_helper = info->par;
738 struct drm_device *dev = fb_helper->dev;
739 struct drm_mode_set *modeset;
740 struct drm_crtc *crtc;
741 int ret = 0;
742 int i;
743
Dave Airlie8be48d92010-03-30 05:34:14 +0000744 mutex_lock(&dev->mode_config.mutex);
745 for (i = 0; i < fb_helper->crtc_count; i++) {
746 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000747
748 modeset = &fb_helper->crtc_info[i].mode_set;
749
750 modeset->x = var->xoffset;
751 modeset->y = var->yoffset;
752
753 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000754 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000755 if (!ret) {
756 info->var.xoffset = var->xoffset;
757 info->var.yoffset = var->yoffset;
758 }
759 }
760 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000761 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000762 return ret;
763}
764EXPORT_SYMBOL(drm_fb_helper_pan_display);
765
Dave Airlie8be48d92010-03-30 05:34:14 +0000766int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
767 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000768{
Dave Airlie785b93e2009-08-28 15:46:53 +1000769 int new_fb = 0;
770 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000771 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000772 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000773 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000774 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000775
776 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
777 sizes.surface_depth = 24;
778 sizes.surface_bpp = 32;
779 sizes.fb_width = (unsigned)-1;
780 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000781
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000782 /* if driver picks 8 or 16 by default use that
783 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000784 if (preferred_bpp != sizes.surface_bpp) {
785 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000786 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000787 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000788 for (i = 0; i < fb_helper->connector_count; i++) {
789 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100790 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000791
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000792 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000793
794 if (cmdline_mode->bpp_specified) {
795 switch (cmdline_mode->bpp) {
796 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000797 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000798 break;
799 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000800 sizes.surface_depth = 15;
801 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000802 break;
803 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000804 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000805 break;
806 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000807 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000808 break;
809 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000810 sizes.surface_depth = 24;
811 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000812 break;
813 }
814 break;
815 }
816 }
817
Dave Airlie8be48d92010-03-30 05:34:14 +0000818 crtc_count = 0;
819 for (i = 0; i < fb_helper->crtc_count; i++) {
820 struct drm_display_mode *desired_mode;
821 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000822
Dave Airlie8be48d92010-03-30 05:34:14 +0000823 if (desired_mode) {
824 if (gamma_size == 0)
825 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
826 if (desired_mode->hdisplay < sizes.fb_width)
827 sizes.fb_width = desired_mode->hdisplay;
828 if (desired_mode->vdisplay < sizes.fb_height)
829 sizes.fb_height = desired_mode->vdisplay;
830 if (desired_mode->hdisplay > sizes.surface_width)
831 sizes.surface_width = desired_mode->hdisplay;
832 if (desired_mode->vdisplay > sizes.surface_height)
833 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000834 crtc_count++;
835 }
836 }
837
Dave Airlie38651672010-03-30 05:34:13 +0000838 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000839 /* hmm everyone went away - assume VGA cable just fell out
840 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000841 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000842 sizes.fb_width = sizes.surface_width = 1024;
843 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000844 }
845
Dave Airlie38651672010-03-30 05:34:13 +0000846 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000847 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000848 if (new_fb < 0)
849 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000850
Dave Airlie38651672010-03-30 05:34:13 +0000851 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000852
Dave Airlie8be48d92010-03-30 05:34:14 +0000853 /* set the fb pointer */
854 for (i = 0; i < fb_helper->crtc_count; i++) {
855 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000856 }
857
Dave Airlie785b93e2009-08-28 15:46:53 +1000858 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100859 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100860 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000861 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100862 }
Dave Airlie38651672010-03-30 05:34:13 +0000863
864 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
865 info->fix.id);
866
Dave Airlie785b93e2009-08-28 15:46:53 +1000867 } else {
868 drm_fb_helper_set_par(info);
869 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000870
871 /* Switch back to kernel console on panic */
872 /* multi card linked list maybe */
873 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400874 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000875 atomic_notifier_chain_register(&panic_notifier_list,
876 &paniced);
877 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
878 }
Dave Airlie38651672010-03-30 05:34:13 +0000879 if (new_fb)
880 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
881
Dave Airlie785b93e2009-08-28 15:46:53 +1000882 return 0;
883}
884EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
885
Dave Airlie3632ef82011-01-15 09:27:00 +1000886void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
887 uint32_t depth)
888{
889 info->fix.type = FB_TYPE_PACKED_PIXELS;
890 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
891 FB_VISUAL_TRUECOLOR;
892 info->fix.mmio_start = 0;
893 info->fix.mmio_len = 0;
894 info->fix.type_aux = 0;
895 info->fix.xpanstep = 1; /* doing it in hw */
896 info->fix.ypanstep = 1; /* doing it in hw */
897 info->fix.ywrapstep = 0;
898 info->fix.accel = FB_ACCEL_NONE;
899 info->fix.type_aux = 0;
900
901 info->fix.line_length = pitch;
902 return;
903}
904EXPORT_SYMBOL(drm_fb_helper_fill_fix);
905
Dave Airlie38651672010-03-30 05:34:13 +0000906void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000907 uint32_t fb_width, uint32_t fb_height)
908{
Dave Airlie38651672010-03-30 05:34:13 +0000909 struct drm_framebuffer *fb = fb_helper->fb;
910 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000911 info->var.xres_virtual = fb->width;
912 info->var.yres_virtual = fb->height;
913 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000914 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000915 info->var.xoffset = 0;
916 info->var.yoffset = 0;
917 info->var.activate = FB_ACTIVATE_NOW;
918 info->var.height = -1;
919 info->var.width = -1;
920
921 switch (fb->depth) {
922 case 8:
923 info->var.red.offset = 0;
924 info->var.green.offset = 0;
925 info->var.blue.offset = 0;
926 info->var.red.length = 8; /* 8bit DAC */
927 info->var.green.length = 8;
928 info->var.blue.length = 8;
929 info->var.transp.offset = 0;
930 info->var.transp.length = 0;
931 break;
932 case 15:
933 info->var.red.offset = 10;
934 info->var.green.offset = 5;
935 info->var.blue.offset = 0;
936 info->var.red.length = 5;
937 info->var.green.length = 5;
938 info->var.blue.length = 5;
939 info->var.transp.offset = 15;
940 info->var.transp.length = 1;
941 break;
942 case 16:
943 info->var.red.offset = 11;
944 info->var.green.offset = 5;
945 info->var.blue.offset = 0;
946 info->var.red.length = 5;
947 info->var.green.length = 6;
948 info->var.blue.length = 5;
949 info->var.transp.offset = 0;
950 break;
951 case 24:
952 info->var.red.offset = 16;
953 info->var.green.offset = 8;
954 info->var.blue.offset = 0;
955 info->var.red.length = 8;
956 info->var.green.length = 8;
957 info->var.blue.length = 8;
958 info->var.transp.offset = 0;
959 info->var.transp.length = 0;
960 break;
961 case 32:
962 info->var.red.offset = 16;
963 info->var.green.offset = 8;
964 info->var.blue.offset = 0;
965 info->var.red.length = 8;
966 info->var.green.length = 8;
967 info->var.blue.length = 8;
968 info->var.transp.offset = 24;
969 info->var.transp.length = 8;
970 break;
971 default:
972 break;
973 }
974
975 info->var.xres = fb_width;
976 info->var.yres = fb_height;
977}
978EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000979
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000980static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
981 uint32_t maxX,
982 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000983{
984 struct drm_connector *connector;
985 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000986 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000987
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000988 for (i = 0; i < fb_helper->connector_count; i++) {
989 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000990 count += connector->funcs->fill_modes(connector, maxX, maxY);
991 }
992
993 return count;
994}
995
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000996static 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 +0000997{
998 struct drm_display_mode *mode;
999
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001000 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001001 if (drm_mode_width(mode) > width ||
1002 drm_mode_height(mode) > height)
1003 continue;
1004 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1005 return mode;
1006 }
1007 return NULL;
1008}
1009
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001010static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001011{
Chris Wilson1794d252011-04-17 07:43:32 +01001012 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001013 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001014 return cmdline_mode->specified;
1015}
1016
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001017static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1018 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001019{
Chris Wilson1794d252011-04-17 07:43:32 +01001020 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001021 struct drm_display_mode *mode = NULL;
1022
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001023 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001024 if (cmdline_mode->specified == false)
1025 return mode;
1026
1027 /* attempt to find a matching mode in the list of modes
1028 * we have gotten so far, if not add a CVT mode that conforms
1029 */
1030 if (cmdline_mode->rb || cmdline_mode->margins)
1031 goto create_mode;
1032
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001033 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001034 /* check width/height */
1035 if (mode->hdisplay != cmdline_mode->xres ||
1036 mode->vdisplay != cmdline_mode->yres)
1037 continue;
1038
1039 if (cmdline_mode->refresh_specified) {
1040 if (mode->vrefresh != cmdline_mode->refresh)
1041 continue;
1042 }
1043
1044 if (cmdline_mode->interlace) {
1045 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1046 continue;
1047 }
1048 return mode;
1049 }
1050
1051create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001052 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1053 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001054 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001055 return mode;
1056}
1057
1058static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1059{
1060 bool enable;
1061
1062 if (strict) {
1063 enable = connector->status == connector_status_connected;
1064 } else {
1065 enable = connector->status != connector_status_disconnected;
1066 }
1067 return enable;
1068}
1069
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001070static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1071 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001072{
1073 bool any_enabled = false;
1074 struct drm_connector *connector;
1075 int i = 0;
1076
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001077 for (i = 0; i < fb_helper->connector_count; i++) {
1078 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001079 enabled[i] = drm_connector_enabled(connector, true);
1080 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1081 enabled[i] ? "yes" : "no");
1082 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001083 }
1084
1085 if (any_enabled)
1086 return;
1087
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001088 for (i = 0; i < fb_helper->connector_count; i++) {
1089 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001090 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001091 }
1092}
1093
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001094static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1095 struct drm_display_mode **modes,
1096 bool *enabled, int width, int height)
1097{
1098 int count, i, j;
1099 bool can_clone = false;
1100 struct drm_fb_helper_connector *fb_helper_conn;
1101 struct drm_display_mode *dmt_mode, *mode;
1102
1103 /* only contemplate cloning in the single crtc case */
1104 if (fb_helper->crtc_count > 1)
1105 return false;
1106
1107 count = 0;
1108 for (i = 0; i < fb_helper->connector_count; i++) {
1109 if (enabled[i])
1110 count++;
1111 }
1112
1113 /* only contemplate cloning if more than one connector is enabled */
1114 if (count <= 1)
1115 return false;
1116
1117 /* check the command line or if nothing common pick 1024x768 */
1118 can_clone = true;
1119 for (i = 0; i < fb_helper->connector_count; i++) {
1120 if (!enabled[i])
1121 continue;
1122 fb_helper_conn = fb_helper->connector_info[i];
1123 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1124 if (!modes[i]) {
1125 can_clone = false;
1126 break;
1127 }
1128 for (j = 0; j < i; j++) {
1129 if (!enabled[j])
1130 continue;
1131 if (!drm_mode_equal(modes[j], modes[i]))
1132 can_clone = false;
1133 }
1134 }
1135
1136 if (can_clone) {
1137 DRM_DEBUG_KMS("can clone using command line\n");
1138 return true;
1139 }
1140
1141 /* try and find a 1024x768 mode on each connector */
1142 can_clone = true;
1143 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1144
1145 for (i = 0; i < fb_helper->connector_count; i++) {
1146
1147 if (!enabled[i])
1148 continue;
1149
1150 fb_helper_conn = fb_helper->connector_info[i];
1151 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1152 if (drm_mode_equal(mode, dmt_mode))
1153 modes[i] = mode;
1154 }
1155 if (!modes[i])
1156 can_clone = false;
1157 }
1158
1159 if (can_clone) {
1160 DRM_DEBUG_KMS("can clone using 1024x768\n");
1161 return true;
1162 }
1163 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1164 return false;
1165}
1166
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001168 struct drm_display_mode **modes,
1169 bool *enabled, int width, int height)
1170{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001171 struct drm_fb_helper_connector *fb_helper_conn;
1172 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001173
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001174 for (i = 0; i < fb_helper->connector_count; i++) {
1175 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001176
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001177 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001178 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001179
1180 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001181 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001182
1183 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001184 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001185 if (!modes[i]) {
1186 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001187 fb_helper_conn->connector->base.id);
1188 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001189 }
1190 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001191 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1192 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001193 break;
1194 }
1195 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1196 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001197 }
1198 return true;
1199}
1200
Dave Airlie8be48d92010-03-30 05:34:14 +00001201static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1202 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001203 struct drm_display_mode **modes,
1204 int n, int width, int height)
1205{
1206 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001207 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001208 struct drm_connector *connector;
1209 struct drm_connector_helper_funcs *connector_funcs;
1210 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001211 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001212 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001213 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001214 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001215
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001216 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001217 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001218
1219 fb_helper_conn = fb_helper->connector_info[n];
1220 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001221
1222 best_crtcs[n] = NULL;
1223 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001224 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001225 if (modes[n] == NULL)
1226 return best_score;
1227
Dave Airlie8be48d92010-03-30 05:34:14 +00001228 crtcs = kzalloc(dev->mode_config.num_connector *
1229 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001230 if (!crtcs)
1231 return best_score;
1232
1233 my_score = 1;
1234 if (connector->status == connector_status_connected)
1235 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001236 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001237 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001238 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001239 my_score++;
1240
1241 connector_funcs = connector->helper_private;
1242 encoder = connector_funcs->best_encoder(connector);
1243 if (!encoder)
1244 goto out;
1245
Dave Airlie38651672010-03-30 05:34:13 +00001246 /* select a crtc for this connector and then attempt to configure
1247 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001248 for (c = 0; c < fb_helper->crtc_count; c++) {
1249 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001250
1251 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001252 continue;
1253 }
1254
1255 for (o = 0; o < n; o++)
1256 if (best_crtcs[o] == crtc)
1257 break;
1258
1259 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001260 /* ignore cloning unless only a single crtc */
1261 if (fb_helper->crtc_count > 1)
1262 continue;
1263
1264 if (!drm_mode_equal(modes[o], modes[n]))
1265 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001266 }
1267
1268 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001269 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1270 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001271 width, height);
1272 if (score > best_score) {
1273 best_crtc = crtc;
1274 best_score = score;
1275 memcpy(best_crtcs, crtcs,
1276 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001277 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001278 }
Dave Airlie38651672010-03-30 05:34:13 +00001279 }
1280out:
1281 kfree(crtcs);
1282 return best_score;
1283}
1284
Dave Airlie8be48d92010-03-30 05:34:14 +00001285static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001286{
Dave Airlie8be48d92010-03-30 05:34:14 +00001287 struct drm_device *dev = fb_helper->dev;
1288 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001289 struct drm_display_mode **modes;
1290 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001291 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001292 bool *enabled;
1293 int width, height;
1294 int i, ret;
1295
1296 DRM_DEBUG_KMS("\n");
1297
1298 width = dev->mode_config.max_width;
1299 height = dev->mode_config.max_height;
1300
1301 /* clean out all the encoder/crtc combos */
1302 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1303 encoder->crtc = NULL;
1304 }
1305
1306 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001307 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001308 modes = kcalloc(dev->mode_config.num_connector,
1309 sizeof(struct drm_display_mode *), GFP_KERNEL);
1310 enabled = kcalloc(dev->mode_config.num_connector,
1311 sizeof(bool), GFP_KERNEL);
1312
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001313 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001314
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001315 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1316 if (!ret) {
1317 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1318 if (!ret)
1319 DRM_ERROR("Unable to find initial modes\n");
1320 }
Dave Airlie38651672010-03-30 05:34:13 +00001321
1322 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1323
Dave Airlie8be48d92010-03-30 05:34:14 +00001324 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1325
1326 /* need to set the modesets up here for use later */
1327 /* fill out the connector<->crtc mappings into the modesets */
1328 for (i = 0; i < fb_helper->crtc_count; i++) {
1329 modeset = &fb_helper->crtc_info[i].mode_set;
1330 modeset->num_connectors = 0;
1331 }
Dave Airlie38651672010-03-30 05:34:13 +00001332
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001333 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001334 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001335 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1336 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001337
Dave Airlie8be48d92010-03-30 05:34:14 +00001338 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001339 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001340 mode->name, fb_crtc->mode_set.crtc->base.id);
1341 fb_crtc->desired_mode = mode;
1342 if (modeset->mode)
1343 drm_mode_destroy(dev, modeset->mode);
1344 modeset->mode = drm_mode_duplicate(dev,
1345 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001346 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001347 }
Dave Airlie38651672010-03-30 05:34:13 +00001348 }
1349
1350 kfree(crtcs);
1351 kfree(modes);
1352 kfree(enabled);
1353}
1354
1355/**
1356 * drm_helper_initial_config - setup a sane initial connector configuration
1357 * @dev: DRM device
1358 *
1359 * LOCKING:
1360 * Called at init time, must take mode config lock.
1361 *
1362 * Scan the CRTCs and connectors and try to put together an initial setup.
1363 * At the moment, this is a cloned configuration across all heads with
1364 * a new framebuffer object as the backing store.
1365 *
1366 * RETURNS:
1367 * Zero if everything went ok, nonzero otherwise.
1368 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001369bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001370{
Dave Airlie8be48d92010-03-30 05:34:14 +00001371 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001372 int count = 0;
1373
1374 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001375 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001376
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001377 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001378
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001379 count = drm_fb_helper_probe_connector_modes(fb_helper,
1380 dev->mode_config.max_width,
1381 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001382 /*
1383 * we shouldn't end up with no modes here.
1384 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001385 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001386 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001387 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001388 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001389
Dave Airlie4abe3522010-03-30 05:34:18 +00001390 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001391}
Dave Airlie8be48d92010-03-30 05:34:14 +00001392EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001393
Chris Wilson73943712011-04-22 11:03:57 +01001394/**
1395 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1396 * probing all the outputs attached to the fb.
1397 * @fb_helper: the drm_fb_helper
1398 *
1399 * LOCKING:
1400 * Called at runtime, must take mode config lock.
1401 *
1402 * Scan the connectors attached to the fb_helper and try to put together a
1403 * setup after *notification of a change in output configuration.
1404 *
1405 * RETURNS:
1406 * 0 on success and a non-zero error code otherwise.
1407 */
1408int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001409{
Chris Wilson73943712011-04-22 11:03:57 +01001410 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001411 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001412 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001413 bool bound = false, crtcs_bound = false;
1414 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001415
1416 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001417 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001418
Chris Wilson73943712011-04-22 11:03:57 +01001419 mutex_lock(&dev->mode_config.mutex);
1420 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001421 if (crtc->fb)
1422 crtcs_bound = true;
1423 if (crtc->fb == fb_helper->fb)
1424 bound = true;
1425 }
1426
1427 if (!bound && crtcs_bound) {
1428 fb_helper->delayed_hotplug = true;
Chris Wilson73943712011-04-22 11:03:57 +01001429 mutex_unlock(&dev->mode_config.mutex);
1430 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001431 }
Dave Airlie38651672010-03-30 05:34:13 +00001432 DRM_DEBUG_KMS("\n");
1433
Dave Airlie4abe3522010-03-30 05:34:18 +00001434 max_width = fb_helper->fb->width;
1435 max_height = fb_helper->fb->height;
1436 bpp_sel = fb_helper->fb->bits_per_pixel;
1437
Dave Airlie5c4426a2010-03-30 05:34:17 +00001438 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001439 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001440 drm_setup_crtcs(fb_helper);
Chris Wilson73943712011-04-22 11:03:57 +01001441 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001442
Dave Airlie4abe3522010-03-30 05:34:18 +00001443 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001444}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001445EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001446
David Rientjes6a108a12011-01-20 14:44:16 -08001447/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001448 * but the module doesn't depend on any fb console symbols. At least
1449 * attempt to load fbcon to avoid leaving the system without a usable console.
1450 */
David Rientjes6a108a12011-01-20 14:44:16 -08001451#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001452static int __init drm_fb_helper_modinit(void)
1453{
1454 const char *name = "fbcon";
1455 struct module *fbcon;
1456
1457 mutex_lock(&module_mutex);
1458 fbcon = find_module(name);
1459 mutex_unlock(&module_mutex);
1460
1461 if (!fbcon)
1462 request_module_nowait(name);
1463 return 0;
1464}
1465
1466module_init(drm_fb_helper_modinit);
1467#endif