blob: 9b60e93e0a683d8de90422c281dc15d24702b1e3 [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>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_fb_helper.h>
38#include <drm/drm_crtc_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100039
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) {
Sachin Kamat6c910832012-11-15 03:43:28 +000098 case DRM_FORCE_OFF:
99 s = "OFF";
100 break;
101 case DRM_FORCE_ON_DIGITAL:
102 s = "ON - dig";
103 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100104 default:
Sachin Kamat6c910832012-11-15 03:43:28 +0000105 case DRM_FORCE_ON:
106 s = "ON";
107 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100108 }
109
110 DRM_INFO("forcing %s connector %s\n",
111 drm_get_connector_name(connector), s);
112 connector->force = mode->force;
113 }
114
115 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
116 drm_get_connector_name(connector),
117 mode->xres, mode->yres,
118 mode->refresh_specified ? mode->refresh : 60,
119 mode->rb ? " reduced blanking" : "",
120 mode->margins ? " with margins" : "",
121 mode->interlace ? " interlaced" : "");
122 }
123
Dave Airlied50ba252009-09-23 14:44:08 +1000124 }
125 return 0;
126}
127
Jason Wessel99231022010-10-13 14:09:43 -0500128static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
129{
130 uint16_t *r_base, *g_base, *b_base;
131 int i;
132
133 r_base = crtc->gamma_store;
134 g_base = r_base + crtc->gamma_size;
135 b_base = g_base + crtc->gamma_size;
136
137 for (i = 0; i < crtc->gamma_size; i++)
138 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
139}
140
141static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
142{
143 uint16_t *r_base, *g_base, *b_base;
144
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200145 if (crtc->funcs->gamma_set == NULL)
146 return;
147
Jason Wessel99231022010-10-13 14:09:43 -0500148 r_base = crtc->gamma_store;
149 g_base = r_base + crtc->gamma_size;
150 b_base = g_base + crtc->gamma_size;
151
152 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
153}
154
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500155int drm_fb_helper_debug_enter(struct fb_info *info)
156{
157 struct drm_fb_helper *helper = info->par;
158 struct drm_crtc_helper_funcs *funcs;
159 int i;
160
161 if (list_empty(&kernel_fb_helper_list))
162 return false;
163
164 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
165 for (i = 0; i < helper->crtc_count; i++) {
166 struct drm_mode_set *mode_set =
167 &helper->crtc_info[i].mode_set;
168
169 if (!mode_set->crtc->enabled)
170 continue;
171
172 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500173 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500174 funcs->mode_set_base_atomic(mode_set->crtc,
175 mode_set->fb,
176 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500177 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500178 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500179 }
180 }
181
182 return 0;
183}
184EXPORT_SYMBOL(drm_fb_helper_debug_enter);
185
186/* Find the real fb for a given fb helper CRTC */
187static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
188{
189 struct drm_device *dev = crtc->dev;
190 struct drm_crtc *c;
191
192 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
193 if (crtc->base.id == c->base.id)
194 return c->fb;
195 }
196
197 return NULL;
198}
199
200int drm_fb_helper_debug_leave(struct fb_info *info)
201{
202 struct drm_fb_helper *helper = info->par;
203 struct drm_crtc *crtc;
204 struct drm_crtc_helper_funcs *funcs;
205 struct drm_framebuffer *fb;
206 int i;
207
208 for (i = 0; i < helper->crtc_count; i++) {
209 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
210 crtc = mode_set->crtc;
211 funcs = crtc->helper_private;
212 fb = drm_mode_config_fb(crtc);
213
214 if (!crtc->enabled)
215 continue;
216
217 if (!fb) {
218 DRM_ERROR("no fb to restore??\n");
219 continue;
220 }
221
Jason Wessel99231022010-10-13 14:09:43 -0500222 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500223 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500224 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500225 }
226
227 return 0;
228}
229EXPORT_SYMBOL(drm_fb_helper_debug_leave);
230
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100231bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
232{
233 bool error = false;
234 int i, ret;
235 for (i = 0; i < fb_helper->crtc_count; i++) {
236 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Daniel Vetterd3904752012-07-11 16:28:07 +0200237 ret = mode_set->crtc->funcs->set_config(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100238 if (ret)
239 error = true;
240 }
241 return error;
242}
243EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
244
Sachin Kamat78b9c352012-08-01 17:15:32 +0530245static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000246{
Dave Airlie785b93e2009-08-28 15:46:53 +1000247 bool ret, error = false;
248 struct drm_fb_helper *helper;
249
250 if (list_empty(&kernel_fb_helper_list))
251 return false;
252
253 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100254 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
255 continue;
256
257 ret = drm_fb_helper_restore_fbdev_mode(helper);
258 if (ret)
259 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000260 }
261 return error;
262}
263
264int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
265 void *panic_str)
266{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700267 /*
268 * It's a waste of time and effort to switch back to text console
269 * if the kernel should reboot before panic messages can be seen.
270 */
271 if (panic_timeout < 0)
272 return 0;
273
Dave Airliefc2362a2010-06-07 12:14:54 +1000274 printk(KERN_ERR "panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000275 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000276}
277EXPORT_SYMBOL(drm_fb_helper_panic);
278
279static struct notifier_block paniced = {
280 .notifier_call = drm_fb_helper_panic,
281};
282
283/**
284 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
285 *
286 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
287 */
288void drm_fb_helper_restore(void)
289{
290 bool ret;
291 ret = drm_fb_helper_force_kernel_mode();
292 if (ret == true)
293 DRM_ERROR("Failed to restore crtc configuration\n");
294}
295EXPORT_SYMBOL(drm_fb_helper_restore);
296
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200297#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000298static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
299{
300 drm_fb_helper_restore();
301}
302static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
303
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700304static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000305{
306 schedule_work(&drm_fb_helper_restore_work);
307}
308
309static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
310 .handler = drm_fb_helper_sysrq,
311 .help_msg = "force-fb(V)",
312 .action_msg = "Restore framebuffer console",
313};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000314#else
315static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200316#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000317
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100318static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000319{
320 struct drm_fb_helper *fb_helper = info->par;
321 struct drm_device *dev = fb_helper->dev;
322 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700323 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700324 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000325
326 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100327 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000328 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000329 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700330 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000331 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000332
Dave Airlie8be48d92010-03-30 05:34:14 +0000333 if (!crtc->enabled)
334 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000335
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100336 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700337 for (j = 0; j < fb_helper->connector_count; j++) {
338 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200339 connector->funcs->dpms(connector, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700340 drm_connector_property_set_value(connector,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100341 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700342 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000343 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000344 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000345}
346
347int drm_fb_helper_blank(int blank, struct fb_info *info)
348{
349 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000350 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000351 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100352 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000353 break;
James Simmons731b5a12009-10-29 20:39:07 +0000354 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000355 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100356 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000357 break;
James Simmons731b5a12009-10-29 20:39:07 +0000358 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000359 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100360 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000361 break;
James Simmons731b5a12009-10-29 20:39:07 +0000362 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000363 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100364 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000365 break;
James Simmons731b5a12009-10-29 20:39:07 +0000366 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000367 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100368 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000369 break;
370 }
371 return 0;
372}
373EXPORT_SYMBOL(drm_fb_helper_blank);
374
375static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
376{
377 int i;
378
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000379 for (i = 0; i < helper->connector_count; i++)
380 kfree(helper->connector_info[i]);
381 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100382 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000383 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100384 if (helper->crtc_info[i].mode_set.mode)
385 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
386 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000387 kfree(helper->crtc_info);
388}
389
Dave Airlie4abe3522010-03-30 05:34:18 +0000390int drm_fb_helper_init(struct drm_device *dev,
391 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000392 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000393{
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000395 int i;
396
Dave Airlie4abe3522010-03-30 05:34:18 +0000397 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000398
399 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
400
401 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
402 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000403 return -ENOMEM;
404
Dave Airlie4abe3522010-03-30 05:34:18 +0000405 fb_helper->crtc_count = crtc_count;
406 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
407 if (!fb_helper->connector_info) {
408 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000409 return -ENOMEM;
410 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000411 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000412
413 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000414 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000415 kcalloc(max_conn_count,
416 sizeof(struct drm_connector *),
417 GFP_KERNEL);
418
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200419 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000420 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000421 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000422 }
423
424 i = 0;
425 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000426 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000427 i++;
428 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100429
Dave Airlie785b93e2009-08-28 15:46:53 +1000430 return 0;
431out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000432 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000433 return -ENOMEM;
434}
Dave Airlie4abe3522010-03-30 05:34:18 +0000435EXPORT_SYMBOL(drm_fb_helper_init);
436
437void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
438{
439 if (!list_empty(&fb_helper->kernel_fb_list)) {
440 list_del(&fb_helper->kernel_fb_list);
441 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400442 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000443 atomic_notifier_chain_unregister(&panic_notifier_list,
444 &paniced);
445 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
446 }
447 }
448
449 drm_fb_helper_crtc_free(fb_helper);
450
Dave Airlie4abe3522010-03-30 05:34:18 +0000451}
452EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000453
Dave Airliec850cb72009-10-23 18:49:03 +1000454static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000455 u16 blue, u16 regno, struct fb_info *info)
456{
457 struct drm_fb_helper *fb_helper = info->par;
458 struct drm_framebuffer *fb = fb_helper->fb;
459 int pindex;
460
Dave Airliec850cb72009-10-23 18:49:03 +1000461 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
462 u32 *palette;
463 u32 value;
464 /* place color in psuedopalette */
465 if (regno > 16)
466 return -EINVAL;
467 palette = (u32 *)info->pseudo_palette;
468 red >>= (16 - info->var.red.length);
469 green >>= (16 - info->var.green.length);
470 blue >>= (16 - info->var.blue.length);
471 value = (red << info->var.red.offset) |
472 (green << info->var.green.offset) |
473 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000474 if (info->var.transp.length > 0) {
475 u32 mask = (1 << info->var.transp.length) - 1;
476 mask <<= info->var.transp.offset;
477 value |= mask;
478 }
Dave Airliec850cb72009-10-23 18:49:03 +1000479 palette[regno] = value;
480 return 0;
481 }
482
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000483 pindex = regno;
484
485 if (fb->bits_per_pixel == 16) {
486 pindex = regno << 3;
487
488 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000489 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000490 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000491 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000492
493 if (fb->depth == 16) {
494 u16 r, g, b;
495 int i;
496 if (regno < 32) {
497 for (i = 0; i < 8; i++)
498 fb_helper->funcs->gamma_set(crtc, red,
499 green, blue, pindex + i);
500 }
501
502 fb_helper->funcs->gamma_get(crtc, &r,
503 &g, &b,
504 pindex >> 1);
505
506 for (i = 0; i < 4; i++)
507 fb_helper->funcs->gamma_set(crtc, r,
508 green, b,
509 (pindex >> 1) + i);
510 }
511 }
512
513 if (fb->depth != 16)
514 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000515 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000516}
517
Dave Airlie068143d2009-10-05 09:58:02 +1000518int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
519{
520 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000521 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000522 u16 *red, *green, *blue, *transp;
523 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100524 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000525 int start;
526
Dave Airlie8be48d92010-03-30 05:34:14 +0000527 for (i = 0; i < fb_helper->crtc_count; i++) {
528 crtc = fb_helper->crtc_info[i].mode_set.crtc;
529 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000530
531 red = cmap->red;
532 green = cmap->green;
533 blue = cmap->blue;
534 transp = cmap->transp;
535 start = cmap->start;
536
roel062ac622011-03-07 18:00:34 +0100537 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000538 u16 hred, hgreen, hblue, htransp = 0xffff;
539
540 hred = *red++;
541 hgreen = *green++;
542 hblue = *blue++;
543
544 if (transp)
545 htransp = *transp++;
546
Dave Airliec850cb72009-10-23 18:49:03 +1000547 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
548 if (rc)
549 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000550 }
551 crtc_funcs->load_lut(crtc);
552 }
553 return rc;
554}
555EXPORT_SYMBOL(drm_fb_helper_setcmap);
556
Dave Airlie785b93e2009-08-28 15:46:53 +1000557int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
558 struct fb_info *info)
559{
560 struct drm_fb_helper *fb_helper = info->par;
561 struct drm_framebuffer *fb = fb_helper->fb;
562 int depth;
563
Jason Wesself90ebd92010-08-05 09:22:32 -0500564 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000565 return -EINVAL;
566
567 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100568 if (var->bits_per_pixel > fb->bits_per_pixel ||
569 var->xres > fb->width || var->yres > fb->height ||
570 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000571 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100572 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
573 var->xres, var->yres, var->bits_per_pixel,
574 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000575 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000576 return -EINVAL;
577 }
578
579 switch (var->bits_per_pixel) {
580 case 16:
581 depth = (var->green.length == 6) ? 16 : 15;
582 break;
583 case 32:
584 depth = (var->transp.length > 0) ? 32 : 24;
585 break;
586 default:
587 depth = var->bits_per_pixel;
588 break;
589 }
590
591 switch (depth) {
592 case 8:
593 var->red.offset = 0;
594 var->green.offset = 0;
595 var->blue.offset = 0;
596 var->red.length = 8;
597 var->green.length = 8;
598 var->blue.length = 8;
599 var->transp.length = 0;
600 var->transp.offset = 0;
601 break;
602 case 15:
603 var->red.offset = 10;
604 var->green.offset = 5;
605 var->blue.offset = 0;
606 var->red.length = 5;
607 var->green.length = 5;
608 var->blue.length = 5;
609 var->transp.length = 1;
610 var->transp.offset = 15;
611 break;
612 case 16:
613 var->red.offset = 11;
614 var->green.offset = 5;
615 var->blue.offset = 0;
616 var->red.length = 5;
617 var->green.length = 6;
618 var->blue.length = 5;
619 var->transp.length = 0;
620 var->transp.offset = 0;
621 break;
622 case 24:
623 var->red.offset = 16;
624 var->green.offset = 8;
625 var->blue.offset = 0;
626 var->red.length = 8;
627 var->green.length = 8;
628 var->blue.length = 8;
629 var->transp.length = 0;
630 var->transp.offset = 0;
631 break;
632 case 32:
633 var->red.offset = 16;
634 var->green.offset = 8;
635 var->blue.offset = 0;
636 var->red.length = 8;
637 var->green.length = 8;
638 var->blue.length = 8;
639 var->transp.length = 8;
640 var->transp.offset = 24;
641 break;
642 default:
643 return -EINVAL;
644 }
645 return 0;
646}
647EXPORT_SYMBOL(drm_fb_helper_check_var);
648
649/* this will let fbcon do the mode init */
650int drm_fb_helper_set_par(struct fb_info *info)
651{
652 struct drm_fb_helper *fb_helper = info->par;
653 struct drm_device *dev = fb_helper->dev;
654 struct fb_var_screeninfo *var = &info->var;
655 struct drm_crtc *crtc;
656 int ret;
657 int i;
658
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100659 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000660 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000661 return -EINVAL;
662 }
663
Dave Airlie8be48d92010-03-30 05:34:14 +0000664 mutex_lock(&dev->mode_config.mutex);
665 for (i = 0; i < fb_helper->crtc_count; i++) {
666 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000667 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
668 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000669 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000670 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 }
672 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000673 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000674
675 if (fb_helper->delayed_hotplug) {
676 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000677 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000678 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000679 return 0;
680}
681EXPORT_SYMBOL(drm_fb_helper_set_par);
682
683int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
684 struct fb_info *info)
685{
686 struct drm_fb_helper *fb_helper = info->par;
687 struct drm_device *dev = fb_helper->dev;
688 struct drm_mode_set *modeset;
689 struct drm_crtc *crtc;
690 int ret = 0;
691 int i;
692
Dave Airlie8be48d92010-03-30 05:34:14 +0000693 mutex_lock(&dev->mode_config.mutex);
694 for (i = 0; i < fb_helper->crtc_count; i++) {
695 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000696
697 modeset = &fb_helper->crtc_info[i].mode_set;
698
699 modeset->x = var->xoffset;
700 modeset->y = var->yoffset;
701
702 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000703 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000704 if (!ret) {
705 info->var.xoffset = var->xoffset;
706 info->var.yoffset = var->yoffset;
707 }
708 }
709 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000710 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000711 return ret;
712}
713EXPORT_SYMBOL(drm_fb_helper_pan_display);
714
Dave Airlie8be48d92010-03-30 05:34:14 +0000715int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
716 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000717{
Dave Airlie785b93e2009-08-28 15:46:53 +1000718 int new_fb = 0;
719 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000720 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000721 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000722 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000723 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000724
725 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
726 sizes.surface_depth = 24;
727 sizes.surface_bpp = 32;
728 sizes.fb_width = (unsigned)-1;
729 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000730
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000731 /* if driver picks 8 or 16 by default use that
732 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000733 if (preferred_bpp != sizes.surface_bpp) {
734 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000735 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000736 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000737 for (i = 0; i < fb_helper->connector_count; i++) {
738 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100739 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000740
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000741 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000742
743 if (cmdline_mode->bpp_specified) {
744 switch (cmdline_mode->bpp) {
745 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000746 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000747 break;
748 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000749 sizes.surface_depth = 15;
750 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000751 break;
752 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000753 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000754 break;
755 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000756 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000757 break;
758 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000759 sizes.surface_depth = 24;
760 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000761 break;
762 }
763 break;
764 }
765 }
766
Dave Airlie8be48d92010-03-30 05:34:14 +0000767 crtc_count = 0;
768 for (i = 0; i < fb_helper->crtc_count; i++) {
769 struct drm_display_mode *desired_mode;
770 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000771
Dave Airlie8be48d92010-03-30 05:34:14 +0000772 if (desired_mode) {
773 if (gamma_size == 0)
774 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
775 if (desired_mode->hdisplay < sizes.fb_width)
776 sizes.fb_width = desired_mode->hdisplay;
777 if (desired_mode->vdisplay < sizes.fb_height)
778 sizes.fb_height = desired_mode->vdisplay;
779 if (desired_mode->hdisplay > sizes.surface_width)
780 sizes.surface_width = desired_mode->hdisplay;
781 if (desired_mode->vdisplay > sizes.surface_height)
782 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000783 crtc_count++;
784 }
785 }
786
Dave Airlie38651672010-03-30 05:34:13 +0000787 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000788 /* hmm everyone went away - assume VGA cable just fell out
789 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000790 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000791 sizes.fb_width = sizes.surface_width = 1024;
792 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000793 }
794
Dave Airlie38651672010-03-30 05:34:13 +0000795 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000796 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000797 if (new_fb < 0)
798 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000799
Dave Airlie38651672010-03-30 05:34:13 +0000800 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000801
Dave Airlie8be48d92010-03-30 05:34:14 +0000802 /* set the fb pointer */
803 for (i = 0; i < fb_helper->crtc_count; i++) {
804 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000805 }
806
Dave Airlie785b93e2009-08-28 15:46:53 +1000807 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100808 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100809 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000810 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100811 }
Dave Airlie38651672010-03-30 05:34:13 +0000812
813 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
814 info->fix.id);
815
Dave Airlie785b93e2009-08-28 15:46:53 +1000816 } else {
817 drm_fb_helper_set_par(info);
818 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000819
820 /* Switch back to kernel console on panic */
821 /* multi card linked list maybe */
822 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400823 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 atomic_notifier_chain_register(&panic_notifier_list,
825 &paniced);
826 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
827 }
Dave Airlie38651672010-03-30 05:34:13 +0000828 if (new_fb)
829 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
830
Dave Airlie785b93e2009-08-28 15:46:53 +1000831 return 0;
832}
833EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
834
Dave Airlie3632ef82011-01-15 09:27:00 +1000835void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
836 uint32_t depth)
837{
838 info->fix.type = FB_TYPE_PACKED_PIXELS;
839 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
840 FB_VISUAL_TRUECOLOR;
841 info->fix.mmio_start = 0;
842 info->fix.mmio_len = 0;
843 info->fix.type_aux = 0;
844 info->fix.xpanstep = 1; /* doing it in hw */
845 info->fix.ypanstep = 1; /* doing it in hw */
846 info->fix.ywrapstep = 0;
847 info->fix.accel = FB_ACCEL_NONE;
848 info->fix.type_aux = 0;
849
850 info->fix.line_length = pitch;
851 return;
852}
853EXPORT_SYMBOL(drm_fb_helper_fill_fix);
854
Dave Airlie38651672010-03-30 05:34:13 +0000855void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000856 uint32_t fb_width, uint32_t fb_height)
857{
Dave Airlie38651672010-03-30 05:34:13 +0000858 struct drm_framebuffer *fb = fb_helper->fb;
859 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000860 info->var.xres_virtual = fb->width;
861 info->var.yres_virtual = fb->height;
862 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000863 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000864 info->var.xoffset = 0;
865 info->var.yoffset = 0;
866 info->var.activate = FB_ACTIVATE_NOW;
867 info->var.height = -1;
868 info->var.width = -1;
869
870 switch (fb->depth) {
871 case 8:
872 info->var.red.offset = 0;
873 info->var.green.offset = 0;
874 info->var.blue.offset = 0;
875 info->var.red.length = 8; /* 8bit DAC */
876 info->var.green.length = 8;
877 info->var.blue.length = 8;
878 info->var.transp.offset = 0;
879 info->var.transp.length = 0;
880 break;
881 case 15:
882 info->var.red.offset = 10;
883 info->var.green.offset = 5;
884 info->var.blue.offset = 0;
885 info->var.red.length = 5;
886 info->var.green.length = 5;
887 info->var.blue.length = 5;
888 info->var.transp.offset = 15;
889 info->var.transp.length = 1;
890 break;
891 case 16:
892 info->var.red.offset = 11;
893 info->var.green.offset = 5;
894 info->var.blue.offset = 0;
895 info->var.red.length = 5;
896 info->var.green.length = 6;
897 info->var.blue.length = 5;
898 info->var.transp.offset = 0;
899 break;
900 case 24:
901 info->var.red.offset = 16;
902 info->var.green.offset = 8;
903 info->var.blue.offset = 0;
904 info->var.red.length = 8;
905 info->var.green.length = 8;
906 info->var.blue.length = 8;
907 info->var.transp.offset = 0;
908 info->var.transp.length = 0;
909 break;
910 case 32:
911 info->var.red.offset = 16;
912 info->var.green.offset = 8;
913 info->var.blue.offset = 0;
914 info->var.red.length = 8;
915 info->var.green.length = 8;
916 info->var.blue.length = 8;
917 info->var.transp.offset = 24;
918 info->var.transp.length = 8;
919 break;
920 default:
921 break;
922 }
923
924 info->var.xres = fb_width;
925 info->var.yres = fb_height;
926}
927EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000928
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000929static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
930 uint32_t maxX,
931 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000932{
933 struct drm_connector *connector;
934 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000935 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000936
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000937 for (i = 0; i < fb_helper->connector_count; i++) {
938 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000939 count += connector->funcs->fill_modes(connector, maxX, maxY);
940 }
941
942 return count;
943}
944
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000945static 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 +0000946{
947 struct drm_display_mode *mode;
948
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000949 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000950 if (drm_mode_width(mode) > width ||
951 drm_mode_height(mode) > height)
952 continue;
953 if (mode->type & DRM_MODE_TYPE_PREFERRED)
954 return mode;
955 }
956 return NULL;
957}
958
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000959static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000960{
Chris Wilson1794d252011-04-17 07:43:32 +0100961 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000962 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000963 return cmdline_mode->specified;
964}
965
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000966static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
967 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +0000968{
Chris Wilson1794d252011-04-17 07:43:32 +0100969 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000970 struct drm_display_mode *mode = NULL;
971
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000972 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000973 if (cmdline_mode->specified == false)
974 return mode;
975
976 /* attempt to find a matching mode in the list of modes
977 * we have gotten so far, if not add a CVT mode that conforms
978 */
979 if (cmdline_mode->rb || cmdline_mode->margins)
980 goto create_mode;
981
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000982 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000983 /* check width/height */
984 if (mode->hdisplay != cmdline_mode->xres ||
985 mode->vdisplay != cmdline_mode->yres)
986 continue;
987
988 if (cmdline_mode->refresh_specified) {
989 if (mode->vrefresh != cmdline_mode->refresh)
990 continue;
991 }
992
993 if (cmdline_mode->interlace) {
994 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
995 continue;
996 }
997 return mode;
998 }
999
1000create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001001 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1002 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001003 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001004 return mode;
1005}
1006
1007static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1008{
1009 bool enable;
1010
1011 if (strict) {
1012 enable = connector->status == connector_status_connected;
1013 } else {
1014 enable = connector->status != connector_status_disconnected;
1015 }
1016 return enable;
1017}
1018
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001019static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1020 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001021{
1022 bool any_enabled = false;
1023 struct drm_connector *connector;
1024 int i = 0;
1025
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001026 for (i = 0; i < fb_helper->connector_count; i++) {
1027 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001028 enabled[i] = drm_connector_enabled(connector, true);
1029 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1030 enabled[i] ? "yes" : "no");
1031 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001032 }
1033
1034 if (any_enabled)
1035 return;
1036
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001037 for (i = 0; i < fb_helper->connector_count; i++) {
1038 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001039 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001040 }
1041}
1042
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001043static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1044 struct drm_display_mode **modes,
1045 bool *enabled, int width, int height)
1046{
1047 int count, i, j;
1048 bool can_clone = false;
1049 struct drm_fb_helper_connector *fb_helper_conn;
1050 struct drm_display_mode *dmt_mode, *mode;
1051
1052 /* only contemplate cloning in the single crtc case */
1053 if (fb_helper->crtc_count > 1)
1054 return false;
1055
1056 count = 0;
1057 for (i = 0; i < fb_helper->connector_count; i++) {
1058 if (enabled[i])
1059 count++;
1060 }
1061
1062 /* only contemplate cloning if more than one connector is enabled */
1063 if (count <= 1)
1064 return false;
1065
1066 /* check the command line or if nothing common pick 1024x768 */
1067 can_clone = true;
1068 for (i = 0; i < fb_helper->connector_count; i++) {
1069 if (!enabled[i])
1070 continue;
1071 fb_helper_conn = fb_helper->connector_info[i];
1072 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1073 if (!modes[i]) {
1074 can_clone = false;
1075 break;
1076 }
1077 for (j = 0; j < i; j++) {
1078 if (!enabled[j])
1079 continue;
1080 if (!drm_mode_equal(modes[j], modes[i]))
1081 can_clone = false;
1082 }
1083 }
1084
1085 if (can_clone) {
1086 DRM_DEBUG_KMS("can clone using command line\n");
1087 return true;
1088 }
1089
1090 /* try and find a 1024x768 mode on each connector */
1091 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001092 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001093
1094 for (i = 0; i < fb_helper->connector_count; i++) {
1095
1096 if (!enabled[i])
1097 continue;
1098
1099 fb_helper_conn = fb_helper->connector_info[i];
1100 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1101 if (drm_mode_equal(mode, dmt_mode))
1102 modes[i] = mode;
1103 }
1104 if (!modes[i])
1105 can_clone = false;
1106 }
1107
1108 if (can_clone) {
1109 DRM_DEBUG_KMS("can clone using 1024x768\n");
1110 return true;
1111 }
1112 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1113 return false;
1114}
1115
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001116static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001117 struct drm_display_mode **modes,
1118 bool *enabled, int width, int height)
1119{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001120 struct drm_fb_helper_connector *fb_helper_conn;
1121 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001122
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001123 for (i = 0; i < fb_helper->connector_count; i++) {
1124 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001125
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001126 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001127 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001128
1129 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001130 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001131
1132 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001133 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001134 if (!modes[i]) {
1135 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001136 fb_helper_conn->connector->base.id);
1137 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001138 }
1139 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001140 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1141 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001142 break;
1143 }
1144 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1145 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001146 }
1147 return true;
1148}
1149
Dave Airlie8be48d92010-03-30 05:34:14 +00001150static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1151 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001152 struct drm_display_mode **modes,
1153 int n, int width, int height)
1154{
1155 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001156 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001157 struct drm_connector *connector;
1158 struct drm_connector_helper_funcs *connector_funcs;
1159 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001160 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001161 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001162 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001163 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001164
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001165 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001166 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167
1168 fb_helper_conn = fb_helper->connector_info[n];
1169 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001170
1171 best_crtcs[n] = NULL;
1172 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001173 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001174 if (modes[n] == NULL)
1175 return best_score;
1176
Dave Airlie8be48d92010-03-30 05:34:14 +00001177 crtcs = kzalloc(dev->mode_config.num_connector *
1178 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001179 if (!crtcs)
1180 return best_score;
1181
1182 my_score = 1;
1183 if (connector->status == connector_status_connected)
1184 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001185 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001186 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001187 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001188 my_score++;
1189
1190 connector_funcs = connector->helper_private;
1191 encoder = connector_funcs->best_encoder(connector);
1192 if (!encoder)
1193 goto out;
1194
Dave Airlie38651672010-03-30 05:34:13 +00001195 /* select a crtc for this connector and then attempt to configure
1196 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001197 for (c = 0; c < fb_helper->crtc_count; c++) {
1198 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001199
1200 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001201 continue;
1202 }
1203
1204 for (o = 0; o < n; o++)
1205 if (best_crtcs[o] == crtc)
1206 break;
1207
1208 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001209 /* ignore cloning unless only a single crtc */
1210 if (fb_helper->crtc_count > 1)
1211 continue;
1212
1213 if (!drm_mode_equal(modes[o], modes[n]))
1214 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001215 }
1216
1217 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001218 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1219 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001220 width, height);
1221 if (score > best_score) {
1222 best_crtc = crtc;
1223 best_score = score;
1224 memcpy(best_crtcs, crtcs,
1225 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001226 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001227 }
Dave Airlie38651672010-03-30 05:34:13 +00001228 }
1229out:
1230 kfree(crtcs);
1231 return best_score;
1232}
1233
Dave Airlie8be48d92010-03-30 05:34:14 +00001234static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001235{
Dave Airlie8be48d92010-03-30 05:34:14 +00001236 struct drm_device *dev = fb_helper->dev;
1237 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001238 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001239 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001240 bool *enabled;
1241 int width, height;
1242 int i, ret;
1243
1244 DRM_DEBUG_KMS("\n");
1245
1246 width = dev->mode_config.max_width;
1247 height = dev->mode_config.max_height;
1248
Dave Airlie38651672010-03-30 05:34:13 +00001249 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001250 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001251 modes = kcalloc(dev->mode_config.num_connector,
1252 sizeof(struct drm_display_mode *), GFP_KERNEL);
1253 enabled = kcalloc(dev->mode_config.num_connector,
1254 sizeof(bool), GFP_KERNEL);
1255
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001256 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001257
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001258 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1259 if (!ret) {
1260 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1261 if (!ret)
1262 DRM_ERROR("Unable to find initial modes\n");
1263 }
Dave Airlie38651672010-03-30 05:34:13 +00001264
1265 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1266
Dave Airlie8be48d92010-03-30 05:34:14 +00001267 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1268
1269 /* need to set the modesets up here for use later */
1270 /* fill out the connector<->crtc mappings into the modesets */
1271 for (i = 0; i < fb_helper->crtc_count; i++) {
1272 modeset = &fb_helper->crtc_info[i].mode_set;
1273 modeset->num_connectors = 0;
1274 }
Dave Airlie38651672010-03-30 05:34:13 +00001275
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001276 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001277 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001278 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1279 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001280
Dave Airlie8be48d92010-03-30 05:34:14 +00001281 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001282 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001283 mode->name, fb_crtc->mode_set.crtc->base.id);
1284 fb_crtc->desired_mode = mode;
1285 if (modeset->mode)
1286 drm_mode_destroy(dev, modeset->mode);
1287 modeset->mode = drm_mode_duplicate(dev,
1288 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001289 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001290 }
Dave Airlie38651672010-03-30 05:34:13 +00001291 }
1292
1293 kfree(crtcs);
1294 kfree(modes);
1295 kfree(enabled);
1296}
1297
1298/**
1299 * drm_helper_initial_config - setup a sane initial connector configuration
1300 * @dev: DRM device
1301 *
1302 * LOCKING:
1303 * Called at init time, must take mode config lock.
1304 *
1305 * Scan the CRTCs and connectors and try to put together an initial setup.
1306 * At the moment, this is a cloned configuration across all heads with
1307 * a new framebuffer object as the backing store.
1308 *
1309 * RETURNS:
1310 * Zero if everything went ok, nonzero otherwise.
1311 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001312bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001313{
Dave Airlie8be48d92010-03-30 05:34:14 +00001314 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001315 int count = 0;
1316
1317 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001318 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001319
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001320 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001321
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001322 count = drm_fb_helper_probe_connector_modes(fb_helper,
1323 dev->mode_config.max_width,
1324 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001325 /*
1326 * we shouldn't end up with no modes here.
1327 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001328 if (count == 0) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001329 printk(KERN_INFO "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001330 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001331 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001332
Dave Airlie4abe3522010-03-30 05:34:18 +00001333 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001334}
Dave Airlie8be48d92010-03-30 05:34:14 +00001335EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001336
Chris Wilson73943712011-04-22 11:03:57 +01001337/**
1338 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1339 * probing all the outputs attached to the fb.
1340 * @fb_helper: the drm_fb_helper
1341 *
1342 * LOCKING:
1343 * Called at runtime, must take mode config lock.
1344 *
1345 * Scan the connectors attached to the fb_helper and try to put together a
1346 * setup after *notification of a change in output configuration.
1347 *
1348 * RETURNS:
1349 * 0 on success and a non-zero error code otherwise.
1350 */
1351int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001352{
Chris Wilson73943712011-04-22 11:03:57 +01001353 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001354 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001355 u32 max_width, max_height, bpp_sel;
Daniel Vetter343065a2012-06-15 11:01:22 +02001356 int bound = 0, crtcs_bound = 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001357 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001358
1359 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001360 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001361
Chris Wilson73943712011-04-22 11:03:57 +01001362 mutex_lock(&dev->mode_config.mutex);
1363 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001364 if (crtc->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001365 crtcs_bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001366 if (crtc->fb == fb_helper->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001367 bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001368 }
1369
Daniel Vetter343065a2012-06-15 11:01:22 +02001370 if (bound < crtcs_bound) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001371 fb_helper->delayed_hotplug = true;
Chris Wilson73943712011-04-22 11:03:57 +01001372 mutex_unlock(&dev->mode_config.mutex);
1373 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001374 }
Dave Airlie38651672010-03-30 05:34:13 +00001375 DRM_DEBUG_KMS("\n");
1376
Dave Airlie4abe3522010-03-30 05:34:18 +00001377 max_width = fb_helper->fb->width;
1378 max_height = fb_helper->fb->height;
1379 bpp_sel = fb_helper->fb->bits_per_pixel;
1380
Dave Airlie5c4426a2010-03-30 05:34:17 +00001381 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001382 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001383 drm_setup_crtcs(fb_helper);
Chris Wilson73943712011-04-22 11:03:57 +01001384 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001385
Dave Airlie4abe3522010-03-30 05:34:18 +00001386 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001387}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001388EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001389
David Rientjes6a108a12011-01-20 14:44:16 -08001390/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001391 * but the module doesn't depend on any fb console symbols. At least
1392 * attempt to load fbcon to avoid leaving the system without a usable console.
1393 */
David Rientjes6a108a12011-01-20 14:44:16 -08001394#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001395static int __init drm_fb_helper_modinit(void)
1396{
1397 const char *name = "fbcon";
1398 struct module *fbcon;
1399
1400 mutex_lock(&module_mutex);
1401 fbcon = find_module(name);
1402 mutex_unlock(&module_mutex);
1403
1404 if (!fbcon)
1405 request_module_nowait(name);
1406 return 0;
1407}
1408
1409module_init(drm_fb_helper_modinit);
1410#endif