blob: 4fc38a7fc0bac761e80b24fed800404d413f7a2f [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
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100309static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000310{
311 struct drm_fb_helper *fb_helper = info->par;
312 struct drm_device *dev = fb_helper->dev;
313 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700314 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700315 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000316
317 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100318 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000319 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000320 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700321 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000322 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000323
Dave Airlie8be48d92010-03-30 05:34:14 +0000324 if (!crtc->enabled)
325 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000326
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100327 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700328 for (j = 0; j < fb_helper->connector_count; j++) {
329 connector = fb_helper->connector_info[j]->connector;
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100330 drm_helper_connector_dpms(connector, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700331 drm_connector_property_set_value(connector,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100332 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700333 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000334 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000335 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000336}
337
338int drm_fb_helper_blank(int blank, struct fb_info *info)
339{
340 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000341 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000342 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100343 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000344 break;
James Simmons731b5a12009-10-29 20:39:07 +0000345 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000346 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100347 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000348 break;
James Simmons731b5a12009-10-29 20:39:07 +0000349 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000350 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100351 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000352 break;
James Simmons731b5a12009-10-29 20:39:07 +0000353 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000354 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100355 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000356 break;
James Simmons731b5a12009-10-29 20:39:07 +0000357 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000358 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100359 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000360 break;
361 }
362 return 0;
363}
364EXPORT_SYMBOL(drm_fb_helper_blank);
365
366static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
367{
368 int i;
369
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000370 for (i = 0; i < helper->connector_count; i++)
371 kfree(helper->connector_info[i]);
372 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100373 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000374 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100375 if (helper->crtc_info[i].mode_set.mode)
376 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
377 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000378 kfree(helper->crtc_info);
379}
380
Dave Airlie4abe3522010-03-30 05:34:18 +0000381int drm_fb_helper_init(struct drm_device *dev,
382 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000383 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000384{
Dave Airlie785b93e2009-08-28 15:46:53 +1000385 struct drm_crtc *crtc;
386 int ret = 0;
387 int i;
388
Dave Airlie4abe3522010-03-30 05:34:18 +0000389 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000390
391 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
392
393 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
394 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000395 return -ENOMEM;
396
Dave Airlie4abe3522010-03-30 05:34:18 +0000397 fb_helper->crtc_count = crtc_count;
398 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
399 if (!fb_helper->connector_info) {
400 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000401 return -ENOMEM;
402 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000403 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000404
405 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000406 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000407 kcalloc(max_conn_count,
408 sizeof(struct drm_connector *),
409 GFP_KERNEL);
410
Dave Airlie4abe3522010-03-30 05:34:18 +0000411 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000412 ret = -ENOMEM;
413 goto out_free;
414 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000415 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000416 }
417
418 i = 0;
419 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000420 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
421 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000422 i++;
423 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000424 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000425 return 0;
426out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000427 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000428 return -ENOMEM;
429}
Dave Airlie4abe3522010-03-30 05:34:18 +0000430EXPORT_SYMBOL(drm_fb_helper_init);
431
432void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
433{
434 if (!list_empty(&fb_helper->kernel_fb_list)) {
435 list_del(&fb_helper->kernel_fb_list);
436 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400437 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000438 atomic_notifier_chain_unregister(&panic_notifier_list,
439 &paniced);
440 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
441 }
442 }
443
444 drm_fb_helper_crtc_free(fb_helper);
445
Dave Airlie4abe3522010-03-30 05:34:18 +0000446}
447EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000448
Dave Airliec850cb72009-10-23 18:49:03 +1000449static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000450 u16 blue, u16 regno, struct fb_info *info)
451{
452 struct drm_fb_helper *fb_helper = info->par;
453 struct drm_framebuffer *fb = fb_helper->fb;
454 int pindex;
455
Dave Airliec850cb72009-10-23 18:49:03 +1000456 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
457 u32 *palette;
458 u32 value;
459 /* place color in psuedopalette */
460 if (regno > 16)
461 return -EINVAL;
462 palette = (u32 *)info->pseudo_palette;
463 red >>= (16 - info->var.red.length);
464 green >>= (16 - info->var.green.length);
465 blue >>= (16 - info->var.blue.length);
466 value = (red << info->var.red.offset) |
467 (green << info->var.green.offset) |
468 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000469 if (info->var.transp.length > 0) {
470 u32 mask = (1 << info->var.transp.length) - 1;
471 mask <<= info->var.transp.offset;
472 value |= mask;
473 }
Dave Airliec850cb72009-10-23 18:49:03 +1000474 palette[regno] = value;
475 return 0;
476 }
477
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000478 pindex = regno;
479
480 if (fb->bits_per_pixel == 16) {
481 pindex = regno << 3;
482
483 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000484 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000485 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000486 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000487
488 if (fb->depth == 16) {
489 u16 r, g, b;
490 int i;
491 if (regno < 32) {
492 for (i = 0; i < 8; i++)
493 fb_helper->funcs->gamma_set(crtc, red,
494 green, blue, pindex + i);
495 }
496
497 fb_helper->funcs->gamma_get(crtc, &r,
498 &g, &b,
499 pindex >> 1);
500
501 for (i = 0; i < 4; i++)
502 fb_helper->funcs->gamma_set(crtc, r,
503 green, b,
504 (pindex >> 1) + i);
505 }
506 }
507
508 if (fb->depth != 16)
509 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000510 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000511}
512
Dave Airlie068143d2009-10-05 09:58:02 +1000513int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
514{
515 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000516 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000517 u16 *red, *green, *blue, *transp;
518 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100519 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000520 int start;
521
Dave Airlie8be48d92010-03-30 05:34:14 +0000522 for (i = 0; i < fb_helper->crtc_count; i++) {
523 crtc = fb_helper->crtc_info[i].mode_set.crtc;
524 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000525
526 red = cmap->red;
527 green = cmap->green;
528 blue = cmap->blue;
529 transp = cmap->transp;
530 start = cmap->start;
531
roel062ac622011-03-07 18:00:34 +0100532 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000533 u16 hred, hgreen, hblue, htransp = 0xffff;
534
535 hred = *red++;
536 hgreen = *green++;
537 hblue = *blue++;
538
539 if (transp)
540 htransp = *transp++;
541
Dave Airliec850cb72009-10-23 18:49:03 +1000542 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
543 if (rc)
544 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000545 }
546 crtc_funcs->load_lut(crtc);
547 }
548 return rc;
549}
550EXPORT_SYMBOL(drm_fb_helper_setcmap);
551
Dave Airlie785b93e2009-08-28 15:46:53 +1000552int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
553 struct fb_info *info)
554{
555 struct drm_fb_helper *fb_helper = info->par;
556 struct drm_framebuffer *fb = fb_helper->fb;
557 int depth;
558
Jason Wesself90ebd92010-08-05 09:22:32 -0500559 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000560 return -EINVAL;
561
562 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000563 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
564 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
565 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
566 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 return -EINVAL;
568 }
569
570 switch (var->bits_per_pixel) {
571 case 16:
572 depth = (var->green.length == 6) ? 16 : 15;
573 break;
574 case 32:
575 depth = (var->transp.length > 0) ? 32 : 24;
576 break;
577 default:
578 depth = var->bits_per_pixel;
579 break;
580 }
581
582 switch (depth) {
583 case 8:
584 var->red.offset = 0;
585 var->green.offset = 0;
586 var->blue.offset = 0;
587 var->red.length = 8;
588 var->green.length = 8;
589 var->blue.length = 8;
590 var->transp.length = 0;
591 var->transp.offset = 0;
592 break;
593 case 15:
594 var->red.offset = 10;
595 var->green.offset = 5;
596 var->blue.offset = 0;
597 var->red.length = 5;
598 var->green.length = 5;
599 var->blue.length = 5;
600 var->transp.length = 1;
601 var->transp.offset = 15;
602 break;
603 case 16:
604 var->red.offset = 11;
605 var->green.offset = 5;
606 var->blue.offset = 0;
607 var->red.length = 5;
608 var->green.length = 6;
609 var->blue.length = 5;
610 var->transp.length = 0;
611 var->transp.offset = 0;
612 break;
613 case 24:
614 var->red.offset = 16;
615 var->green.offset = 8;
616 var->blue.offset = 0;
617 var->red.length = 8;
618 var->green.length = 8;
619 var->blue.length = 8;
620 var->transp.length = 0;
621 var->transp.offset = 0;
622 break;
623 case 32:
624 var->red.offset = 16;
625 var->green.offset = 8;
626 var->blue.offset = 0;
627 var->red.length = 8;
628 var->green.length = 8;
629 var->blue.length = 8;
630 var->transp.length = 8;
631 var->transp.offset = 24;
632 break;
633 default:
634 return -EINVAL;
635 }
636 return 0;
637}
638EXPORT_SYMBOL(drm_fb_helper_check_var);
639
640/* this will let fbcon do the mode init */
641int drm_fb_helper_set_par(struct fb_info *info)
642{
643 struct drm_fb_helper *fb_helper = info->par;
644 struct drm_device *dev = fb_helper->dev;
645 struct fb_var_screeninfo *var = &info->var;
646 struct drm_crtc *crtc;
647 int ret;
648 int i;
649
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100650 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000651 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000652 return -EINVAL;
653 }
654
Dave Airlie8be48d92010-03-30 05:34:14 +0000655 mutex_lock(&dev->mode_config.mutex);
656 for (i = 0; i < fb_helper->crtc_count; i++) {
657 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000658 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
659 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000660 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000661 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000662 }
663 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000664 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000665
666 if (fb_helper->delayed_hotplug) {
667 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000668 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000669 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000670 return 0;
671}
672EXPORT_SYMBOL(drm_fb_helper_set_par);
673
674int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
675 struct fb_info *info)
676{
677 struct drm_fb_helper *fb_helper = info->par;
678 struct drm_device *dev = fb_helper->dev;
679 struct drm_mode_set *modeset;
680 struct drm_crtc *crtc;
681 int ret = 0;
682 int i;
683
Dave Airlie8be48d92010-03-30 05:34:14 +0000684 mutex_lock(&dev->mode_config.mutex);
685 for (i = 0; i < fb_helper->crtc_count; i++) {
686 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000687
688 modeset = &fb_helper->crtc_info[i].mode_set;
689
690 modeset->x = var->xoffset;
691 modeset->y = var->yoffset;
692
693 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000694 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000695 if (!ret) {
696 info->var.xoffset = var->xoffset;
697 info->var.yoffset = var->yoffset;
698 }
699 }
700 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000701 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000702 return ret;
703}
704EXPORT_SYMBOL(drm_fb_helper_pan_display);
705
Dave Airlie8be48d92010-03-30 05:34:14 +0000706int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
707 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000708{
Dave Airlie785b93e2009-08-28 15:46:53 +1000709 int new_fb = 0;
710 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000711 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000712 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000713 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000714 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000715
716 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
717 sizes.surface_depth = 24;
718 sizes.surface_bpp = 32;
719 sizes.fb_width = (unsigned)-1;
720 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000721
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000722 /* if driver picks 8 or 16 by default use that
723 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000724 if (preferred_bpp != sizes.surface_bpp) {
725 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000726 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000727 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000728 for (i = 0; i < fb_helper->connector_count; i++) {
729 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100730 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000731
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000732 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000733
734 if (cmdline_mode->bpp_specified) {
735 switch (cmdline_mode->bpp) {
736 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000737 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000738 break;
739 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000740 sizes.surface_depth = 15;
741 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000742 break;
743 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000744 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000745 break;
746 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000747 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000748 break;
749 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000750 sizes.surface_depth = 24;
751 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000752 break;
753 }
754 break;
755 }
756 }
757
Dave Airlie8be48d92010-03-30 05:34:14 +0000758 crtc_count = 0;
759 for (i = 0; i < fb_helper->crtc_count; i++) {
760 struct drm_display_mode *desired_mode;
761 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000762
Dave Airlie8be48d92010-03-30 05:34:14 +0000763 if (desired_mode) {
764 if (gamma_size == 0)
765 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
766 if (desired_mode->hdisplay < sizes.fb_width)
767 sizes.fb_width = desired_mode->hdisplay;
768 if (desired_mode->vdisplay < sizes.fb_height)
769 sizes.fb_height = desired_mode->vdisplay;
770 if (desired_mode->hdisplay > sizes.surface_width)
771 sizes.surface_width = desired_mode->hdisplay;
772 if (desired_mode->vdisplay > sizes.surface_height)
773 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000774 crtc_count++;
775 }
776 }
777
Dave Airlie38651672010-03-30 05:34:13 +0000778 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000779 /* hmm everyone went away - assume VGA cable just fell out
780 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000781 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000782 sizes.fb_width = sizes.surface_width = 1024;
783 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000784 }
785
Dave Airlie38651672010-03-30 05:34:13 +0000786 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000787 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000788 if (new_fb < 0)
789 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000790
Dave Airlie38651672010-03-30 05:34:13 +0000791 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000792
Dave Airlie8be48d92010-03-30 05:34:14 +0000793 /* set the fb pointer */
794 for (i = 0; i < fb_helper->crtc_count; i++) {
795 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000796 }
797
Dave Airlie785b93e2009-08-28 15:46:53 +1000798 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100799 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100800 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000801 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100802 }
Dave Airlie38651672010-03-30 05:34:13 +0000803
804 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
805 info->fix.id);
806
Dave Airlie785b93e2009-08-28 15:46:53 +1000807 } else {
808 drm_fb_helper_set_par(info);
809 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000810
811 /* Switch back to kernel console on panic */
812 /* multi card linked list maybe */
813 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400814 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000815 atomic_notifier_chain_register(&panic_notifier_list,
816 &paniced);
817 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
818 }
Dave Airlie38651672010-03-30 05:34:13 +0000819 if (new_fb)
820 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
821
Dave Airlie785b93e2009-08-28 15:46:53 +1000822 return 0;
823}
824EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
825
Dave Airlie3632ef82011-01-15 09:27:00 +1000826void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
827 uint32_t depth)
828{
829 info->fix.type = FB_TYPE_PACKED_PIXELS;
830 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
831 FB_VISUAL_TRUECOLOR;
832 info->fix.mmio_start = 0;
833 info->fix.mmio_len = 0;
834 info->fix.type_aux = 0;
835 info->fix.xpanstep = 1; /* doing it in hw */
836 info->fix.ypanstep = 1; /* doing it in hw */
837 info->fix.ywrapstep = 0;
838 info->fix.accel = FB_ACCEL_NONE;
839 info->fix.type_aux = 0;
840
841 info->fix.line_length = pitch;
842 return;
843}
844EXPORT_SYMBOL(drm_fb_helper_fill_fix);
845
Dave Airlie38651672010-03-30 05:34:13 +0000846void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000847 uint32_t fb_width, uint32_t fb_height)
848{
Dave Airlie38651672010-03-30 05:34:13 +0000849 struct drm_framebuffer *fb = fb_helper->fb;
850 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000851 info->var.xres_virtual = fb->width;
852 info->var.yres_virtual = fb->height;
853 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000854 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000855 info->var.xoffset = 0;
856 info->var.yoffset = 0;
857 info->var.activate = FB_ACTIVATE_NOW;
858 info->var.height = -1;
859 info->var.width = -1;
860
861 switch (fb->depth) {
862 case 8:
863 info->var.red.offset = 0;
864 info->var.green.offset = 0;
865 info->var.blue.offset = 0;
866 info->var.red.length = 8; /* 8bit DAC */
867 info->var.green.length = 8;
868 info->var.blue.length = 8;
869 info->var.transp.offset = 0;
870 info->var.transp.length = 0;
871 break;
872 case 15:
873 info->var.red.offset = 10;
874 info->var.green.offset = 5;
875 info->var.blue.offset = 0;
876 info->var.red.length = 5;
877 info->var.green.length = 5;
878 info->var.blue.length = 5;
879 info->var.transp.offset = 15;
880 info->var.transp.length = 1;
881 break;
882 case 16:
883 info->var.red.offset = 11;
884 info->var.green.offset = 5;
885 info->var.blue.offset = 0;
886 info->var.red.length = 5;
887 info->var.green.length = 6;
888 info->var.blue.length = 5;
889 info->var.transp.offset = 0;
890 break;
891 case 24:
892 info->var.red.offset = 16;
893 info->var.green.offset = 8;
894 info->var.blue.offset = 0;
895 info->var.red.length = 8;
896 info->var.green.length = 8;
897 info->var.blue.length = 8;
898 info->var.transp.offset = 0;
899 info->var.transp.length = 0;
900 break;
901 case 32:
902 info->var.red.offset = 16;
903 info->var.green.offset = 8;
904 info->var.blue.offset = 0;
905 info->var.red.length = 8;
906 info->var.green.length = 8;
907 info->var.blue.length = 8;
908 info->var.transp.offset = 24;
909 info->var.transp.length = 8;
910 break;
911 default:
912 break;
913 }
914
915 info->var.xres = fb_width;
916 info->var.yres = fb_height;
917}
918EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000919
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000920static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
921 uint32_t maxX,
922 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000923{
924 struct drm_connector *connector;
925 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000926 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000927
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000928 for (i = 0; i < fb_helper->connector_count; i++) {
929 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000930 count += connector->funcs->fill_modes(connector, maxX, maxY);
931 }
932
933 return count;
934}
935
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000936static 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 +0000937{
938 struct drm_display_mode *mode;
939
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000940 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000941 if (drm_mode_width(mode) > width ||
942 drm_mode_height(mode) > height)
943 continue;
944 if (mode->type & DRM_MODE_TYPE_PREFERRED)
945 return mode;
946 }
947 return NULL;
948}
949
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000950static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000951{
Chris Wilson1794d252011-04-17 07:43:32 +0100952 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000953 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000954 return cmdline_mode->specified;
955}
956
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000957static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
958 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +0000959{
Chris Wilson1794d252011-04-17 07:43:32 +0100960 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000961 struct drm_display_mode *mode = NULL;
962
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000963 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000964 if (cmdline_mode->specified == false)
965 return mode;
966
967 /* attempt to find a matching mode in the list of modes
968 * we have gotten so far, if not add a CVT mode that conforms
969 */
970 if (cmdline_mode->rb || cmdline_mode->margins)
971 goto create_mode;
972
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000973 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000974 /* check width/height */
975 if (mode->hdisplay != cmdline_mode->xres ||
976 mode->vdisplay != cmdline_mode->yres)
977 continue;
978
979 if (cmdline_mode->refresh_specified) {
980 if (mode->vrefresh != cmdline_mode->refresh)
981 continue;
982 }
983
984 if (cmdline_mode->interlace) {
985 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
986 continue;
987 }
988 return mode;
989 }
990
991create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +0100992 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
993 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000994 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +0000995 return mode;
996}
997
998static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
999{
1000 bool enable;
1001
1002 if (strict) {
1003 enable = connector->status == connector_status_connected;
1004 } else {
1005 enable = connector->status != connector_status_disconnected;
1006 }
1007 return enable;
1008}
1009
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001010static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1011 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001012{
1013 bool any_enabled = false;
1014 struct drm_connector *connector;
1015 int i = 0;
1016
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001017 for (i = 0; i < fb_helper->connector_count; i++) {
1018 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001019 enabled[i] = drm_connector_enabled(connector, true);
1020 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1021 enabled[i] ? "yes" : "no");
1022 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001023 }
1024
1025 if (any_enabled)
1026 return;
1027
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001028 for (i = 0; i < fb_helper->connector_count; i++) {
1029 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001030 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001031 }
1032}
1033
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001034static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1035 struct drm_display_mode **modes,
1036 bool *enabled, int width, int height)
1037{
1038 int count, i, j;
1039 bool can_clone = false;
1040 struct drm_fb_helper_connector *fb_helper_conn;
1041 struct drm_display_mode *dmt_mode, *mode;
1042
1043 /* only contemplate cloning in the single crtc case */
1044 if (fb_helper->crtc_count > 1)
1045 return false;
1046
1047 count = 0;
1048 for (i = 0; i < fb_helper->connector_count; i++) {
1049 if (enabled[i])
1050 count++;
1051 }
1052
1053 /* only contemplate cloning if more than one connector is enabled */
1054 if (count <= 1)
1055 return false;
1056
1057 /* check the command line or if nothing common pick 1024x768 */
1058 can_clone = true;
1059 for (i = 0; i < fb_helper->connector_count; i++) {
1060 if (!enabled[i])
1061 continue;
1062 fb_helper_conn = fb_helper->connector_info[i];
1063 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1064 if (!modes[i]) {
1065 can_clone = false;
1066 break;
1067 }
1068 for (j = 0; j < i; j++) {
1069 if (!enabled[j])
1070 continue;
1071 if (!drm_mode_equal(modes[j], modes[i]))
1072 can_clone = false;
1073 }
1074 }
1075
1076 if (can_clone) {
1077 DRM_DEBUG_KMS("can clone using command line\n");
1078 return true;
1079 }
1080
1081 /* try and find a 1024x768 mode on each connector */
1082 can_clone = true;
1083 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1084
1085 for (i = 0; i < fb_helper->connector_count; i++) {
1086
1087 if (!enabled[i])
1088 continue;
1089
1090 fb_helper_conn = fb_helper->connector_info[i];
1091 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1092 if (drm_mode_equal(mode, dmt_mode))
1093 modes[i] = mode;
1094 }
1095 if (!modes[i])
1096 can_clone = false;
1097 }
1098
1099 if (can_clone) {
1100 DRM_DEBUG_KMS("can clone using 1024x768\n");
1101 return true;
1102 }
1103 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1104 return false;
1105}
1106
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001107static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001108 struct drm_display_mode **modes,
1109 bool *enabled, int width, int height)
1110{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001111 struct drm_fb_helper_connector *fb_helper_conn;
1112 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001113
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001114 for (i = 0; i < fb_helper->connector_count; i++) {
1115 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001116
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001117 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001118 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001119
1120 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001121 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001122
1123 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001124 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001125 if (!modes[i]) {
1126 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001127 fb_helper_conn->connector->base.id);
1128 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001129 }
1130 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001131 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1132 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001133 break;
1134 }
1135 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1136 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001137 }
1138 return true;
1139}
1140
Dave Airlie8be48d92010-03-30 05:34:14 +00001141static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1142 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001143 struct drm_display_mode **modes,
1144 int n, int width, int height)
1145{
1146 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001147 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001148 struct drm_connector *connector;
1149 struct drm_connector_helper_funcs *connector_funcs;
1150 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001151 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001152 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001153 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001154 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001155
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001156 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001157 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001158
1159 fb_helper_conn = fb_helper->connector_info[n];
1160 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001161
1162 best_crtcs[n] = NULL;
1163 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001164 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001165 if (modes[n] == NULL)
1166 return best_score;
1167
Dave Airlie8be48d92010-03-30 05:34:14 +00001168 crtcs = kzalloc(dev->mode_config.num_connector *
1169 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001170 if (!crtcs)
1171 return best_score;
1172
1173 my_score = 1;
1174 if (connector->status == connector_status_connected)
1175 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001176 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001177 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001178 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001179 my_score++;
1180
1181 connector_funcs = connector->helper_private;
1182 encoder = connector_funcs->best_encoder(connector);
1183 if (!encoder)
1184 goto out;
1185
Dave Airlie38651672010-03-30 05:34:13 +00001186 /* select a crtc for this connector and then attempt to configure
1187 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001188 for (c = 0; c < fb_helper->crtc_count; c++) {
1189 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001190
1191 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001192 continue;
1193 }
1194
1195 for (o = 0; o < n; o++)
1196 if (best_crtcs[o] == crtc)
1197 break;
1198
1199 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001200 /* ignore cloning unless only a single crtc */
1201 if (fb_helper->crtc_count > 1)
1202 continue;
1203
1204 if (!drm_mode_equal(modes[o], modes[n]))
1205 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001206 }
1207
1208 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001209 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1210 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001211 width, height);
1212 if (score > best_score) {
1213 best_crtc = crtc;
1214 best_score = score;
1215 memcpy(best_crtcs, crtcs,
1216 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001217 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001218 }
Dave Airlie38651672010-03-30 05:34:13 +00001219 }
1220out:
1221 kfree(crtcs);
1222 return best_score;
1223}
1224
Dave Airlie8be48d92010-03-30 05:34:14 +00001225static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001226{
Dave Airlie8be48d92010-03-30 05:34:14 +00001227 struct drm_device *dev = fb_helper->dev;
1228 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001229 struct drm_display_mode **modes;
1230 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001231 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001232 bool *enabled;
1233 int width, height;
1234 int i, ret;
1235
1236 DRM_DEBUG_KMS("\n");
1237
1238 width = dev->mode_config.max_width;
1239 height = dev->mode_config.max_height;
1240
1241 /* clean out all the encoder/crtc combos */
1242 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1243 encoder->crtc = NULL;
1244 }
1245
1246 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001247 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001248 modes = kcalloc(dev->mode_config.num_connector,
1249 sizeof(struct drm_display_mode *), GFP_KERNEL);
1250 enabled = kcalloc(dev->mode_config.num_connector,
1251 sizeof(bool), GFP_KERNEL);
1252
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001253 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001254
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001255 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1256 if (!ret) {
1257 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1258 if (!ret)
1259 DRM_ERROR("Unable to find initial modes\n");
1260 }
Dave Airlie38651672010-03-30 05:34:13 +00001261
1262 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1263
Dave Airlie8be48d92010-03-30 05:34:14 +00001264 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1265
1266 /* need to set the modesets up here for use later */
1267 /* fill out the connector<->crtc mappings into the modesets */
1268 for (i = 0; i < fb_helper->crtc_count; i++) {
1269 modeset = &fb_helper->crtc_info[i].mode_set;
1270 modeset->num_connectors = 0;
1271 }
Dave Airlie38651672010-03-30 05:34:13 +00001272
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001273 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001274 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001275 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1276 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001277
Dave Airlie8be48d92010-03-30 05:34:14 +00001278 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001279 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001280 mode->name, fb_crtc->mode_set.crtc->base.id);
1281 fb_crtc->desired_mode = mode;
1282 if (modeset->mode)
1283 drm_mode_destroy(dev, modeset->mode);
1284 modeset->mode = drm_mode_duplicate(dev,
1285 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001286 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001287 }
Dave Airlie38651672010-03-30 05:34:13 +00001288 }
1289
1290 kfree(crtcs);
1291 kfree(modes);
1292 kfree(enabled);
1293}
1294
1295/**
1296 * drm_helper_initial_config - setup a sane initial connector configuration
1297 * @dev: DRM device
1298 *
1299 * LOCKING:
1300 * Called at init time, must take mode config lock.
1301 *
1302 * Scan the CRTCs and connectors and try to put together an initial setup.
1303 * At the moment, this is a cloned configuration across all heads with
1304 * a new framebuffer object as the backing store.
1305 *
1306 * RETURNS:
1307 * Zero if everything went ok, nonzero otherwise.
1308 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001309bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001310{
Dave Airlie8be48d92010-03-30 05:34:14 +00001311 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001312 int count = 0;
1313
1314 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001315 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001316
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001317 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001318
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001319 count = drm_fb_helper_probe_connector_modes(fb_helper,
1320 dev->mode_config.max_width,
1321 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001322 /*
1323 * we shouldn't end up with no modes here.
1324 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001325 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001326 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001327 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001328 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001329
Dave Airlie4abe3522010-03-30 05:34:18 +00001330 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001331}
Dave Airlie8be48d92010-03-30 05:34:14 +00001332EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001333
Chris Wilson73943712011-04-22 11:03:57 +01001334/**
1335 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1336 * probing all the outputs attached to the fb.
1337 * @fb_helper: the drm_fb_helper
1338 *
1339 * LOCKING:
1340 * Called at runtime, must take mode config lock.
1341 *
1342 * Scan the connectors attached to the fb_helper and try to put together a
1343 * setup after *notification of a change in output configuration.
1344 *
1345 * RETURNS:
1346 * 0 on success and a non-zero error code otherwise.
1347 */
1348int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001349{
Chris Wilson73943712011-04-22 11:03:57 +01001350 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001351 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001352 u32 max_width, max_height, bpp_sel;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001353 bool bound = false, crtcs_bound = false;
1354 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001355
1356 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001357 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001358
Chris Wilson73943712011-04-22 11:03:57 +01001359 mutex_lock(&dev->mode_config.mutex);
1360 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001361 if (crtc->fb)
1362 crtcs_bound = true;
1363 if (crtc->fb == fb_helper->fb)
1364 bound = true;
1365 }
1366
1367 if (!bound && crtcs_bound) {
1368 fb_helper->delayed_hotplug = true;
Chris Wilson73943712011-04-22 11:03:57 +01001369 mutex_unlock(&dev->mode_config.mutex);
1370 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001371 }
Dave Airlie38651672010-03-30 05:34:13 +00001372 DRM_DEBUG_KMS("\n");
1373
Dave Airlie4abe3522010-03-30 05:34:18 +00001374 max_width = fb_helper->fb->width;
1375 max_height = fb_helper->fb->height;
1376 bpp_sel = fb_helper->fb->bits_per_pixel;
1377
Dave Airlie5c4426a2010-03-30 05:34:17 +00001378 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001379 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001380 drm_setup_crtcs(fb_helper);
Chris Wilson73943712011-04-22 11:03:57 +01001381 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001382
Dave Airlie4abe3522010-03-30 05:34:18 +00001383 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001384}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001385EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001386
David Rientjes6a108a12011-01-20 14:44:16 -08001387/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001388 * but the module doesn't depend on any fb console symbols. At least
1389 * attempt to load fbcon to avoid leaving the system without a usable console.
1390 */
David Rientjes6a108a12011-01-20 14:44:16 -08001391#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001392static int __init drm_fb_helper_modinit(void)
1393{
1394 const char *name = "fbcon";
1395 struct module *fbcon;
1396
1397 mutex_lock(&module_mutex);
1398 fbcon = find_module(name);
1399 mutex_unlock(&module_mutex);
1400
1401 if (!fbcon)
1402 request_module_nowait(name);
1403 return 0;
1404}
1405
1406module_init(drm_fb_helper_modinit);
1407#endif