blob: 5db4f47a0085dbac591af29fcd07398aafd67651 [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 *
6 * DRM framebuffer helper functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
29 */
Andy Shevchenko3b40a442010-02-02 14:40:32 -080030#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100031#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#include <linux/fb.h>
34#include "drmP.h"
35#include "drm_crtc.h"
36#include "drm_fb_helper.h"
37#include "drm_crtc_helper.h"
38
Dave Airlie6fcefd52009-09-08 11:08:32 +100039MODULE_AUTHOR("David Airlie, Jesse Barnes");
40MODULE_DESCRIPTION("DRM KMS helper");
41MODULE_LICENSE("GPL and additional rights");
42
Dave Airlie785b93e2009-08-28 15:46:53 +100043static LIST_HEAD(kernel_fb_helper_list);
44
Dave Airlie4abe3522010-03-30 05:34:18 +000045static struct slow_work_ops output_status_change_ops;
Dave Airlied50ba252009-09-23 14:44:08 +100046
Dave Airlie0b4c0f32010-03-30 05:34:15 +000047/* simple single crtc case helper function */
48int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100049{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000050 struct drm_device *dev = fb_helper->dev;
51 struct drm_connector *connector;
52 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100053
Dave Airlie0b4c0f32010-03-30 05:34:15 +000054 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
55 struct drm_fb_helper_connector *fb_helper_connector;
56
57 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
58 if (!fb_helper_connector)
59 goto fail;
60
61 fb_helper_connector->connector = connector;
62 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
63 }
Dave Airlied50ba252009-09-23 14:44:08 +100064 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000065fail:
66 for (i = 0; i < fb_helper->connector_count; i++) {
67 kfree(fb_helper->connector_info[i]);
68 fb_helper->connector_info[i] = NULL;
69 }
70 fb_helper->connector_count = 0;
71 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +100072}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000073EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100074
Dave Airlied50ba252009-09-23 14:44:08 +100075/**
76 * drm_fb_helper_connector_parse_command_line - parse command line for connector
77 * @connector - connector to parse line for
78 * @mode_option - per connector mode option
79 *
80 * This parses the connector specific then generic command lines for
81 * modes and options to configure the connector.
82 *
83 * This uses the same parameters as the fb modedb.c, except for extra
84 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
85 *
86 * enable/enable Digital/disable bit at the end
87 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000088static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlied50ba252009-09-23 14:44:08 +100089 const char *mode_option)
90{
91 const char *name;
92 unsigned int namelen;
93 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
94 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
95 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
96 int i;
97 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
Dave Airlie8ef86782009-09-26 06:39:00 +100098 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000099 struct drm_connector *connector = fb_helper_conn->connector;
Dave Airlied50ba252009-09-23 14:44:08 +1000100
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000101 if (!fb_helper_conn)
Dave Airlie8ef86782009-09-26 06:39:00 +1000102 return false;
103
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000104 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000105 if (!mode_option)
106 mode_option = fb_mode_option;
107
108 if (!mode_option) {
109 cmdline_mode->specified = false;
110 return false;
111 }
112
113 name = mode_option;
114 namelen = strlen(name);
115 for (i = namelen-1; i >= 0; i--) {
116 switch (name[i]) {
117 case '@':
118 namelen = i;
119 if (!refresh_specified && !bpp_specified &&
120 !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800121 refresh = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000122 refresh_specified = 1;
123 if (cvt || rb)
124 cvt = 0;
125 } else
126 goto done;
127 break;
128 case '-':
129 namelen = i;
130 if (!bpp_specified && !yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800131 bpp = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000132 bpp_specified = 1;
133 if (cvt || rb)
134 cvt = 0;
135 } else
136 goto done;
137 break;
138 case 'x':
139 if (!yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800140 yres = simple_strtol(&name[i+1], NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000141 yres_specified = 1;
142 } else
143 goto done;
144 case '0' ... '9':
145 break;
146 case 'M':
147 if (!yres_specified)
148 cvt = 1;
149 break;
150 case 'R':
151 if (!cvt)
152 rb = 1;
153 break;
154 case 'm':
155 if (!cvt)
156 margins = 1;
157 break;
158 case 'i':
159 if (!cvt)
160 interlace = 1;
161 break;
162 case 'e':
163 force = DRM_FORCE_ON;
164 break;
165 case 'D':
Roel Kluine89a8c92009-12-31 13:06:29 +0100166 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
Dave Airlied50ba252009-09-23 14:44:08 +1000167 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
168 force = DRM_FORCE_ON;
169 else
170 force = DRM_FORCE_ON_DIGITAL;
171 break;
172 case 'd':
173 force = DRM_FORCE_OFF;
174 break;
175 default:
176 goto done;
177 }
178 }
179 if (i < 0 && yres_specified) {
Andy Shevchenko3b40a442010-02-02 14:40:32 -0800180 xres = simple_strtol(name, NULL, 10);
Dave Airlied50ba252009-09-23 14:44:08 +1000181 res_specified = 1;
182 }
183done:
184
185 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
186 drm_get_connector_name(connector), xres, yres,
187 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
188 "", (margins) ? " with margins" : "", (interlace) ?
189 " interlaced" : "");
190
191 if (force) {
192 const char *s;
193 switch (force) {
194 case DRM_FORCE_OFF: s = "OFF"; break;
195 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
196 default:
197 case DRM_FORCE_ON: s = "ON"; break;
198 }
199
200 DRM_INFO("forcing %s connector %s\n",
201 drm_get_connector_name(connector), s);
202 connector->force = force;
203 }
204
205 if (res_specified) {
206 cmdline_mode->specified = true;
207 cmdline_mode->xres = xres;
208 cmdline_mode->yres = yres;
209 }
210
211 if (refresh_specified) {
212 cmdline_mode->refresh_specified = true;
213 cmdline_mode->refresh = refresh;
214 }
215
216 if (bpp_specified) {
217 cmdline_mode->bpp_specified = true;
218 cmdline_mode->bpp = bpp;
219 }
220 cmdline_mode->rb = rb ? true : false;
221 cmdline_mode->cvt = cvt ? true : false;
222 cmdline_mode->interlace = interlace ? true : false;
223
224 return true;
225}
226
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000227static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000228{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000229 struct drm_fb_helper_connector *fb_helper_conn;
230 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000231
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000232 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlied50ba252009-09-23 14:44:08 +1000233 char *option = NULL;
234
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000235 fb_helper_conn = fb_helper->connector_info[i];
236
Dave Airlied50ba252009-09-23 14:44:08 +1000237 /* do something on return - turn off connector maybe */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000238 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +1000239 continue;
240
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000241 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
Dave Airlied50ba252009-09-23 14:44:08 +1000242 }
243 return 0;
244}
245
Dave Airlie785b93e2009-08-28 15:46:53 +1000246bool drm_fb_helper_force_kernel_mode(void)
247{
248 int i = 0;
249 bool ret, error = false;
250 struct drm_fb_helper *helper;
251
252 if (list_empty(&kernel_fb_helper_list))
253 return false;
254
255 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
256 for (i = 0; i < helper->crtc_count; i++) {
257 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
258 ret = drm_crtc_helper_set_config(mode_set);
259 if (ret)
260 error = true;
261 }
262 }
263 return error;
264}
265
266int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267 void *panic_str)
268{
269 DRM_ERROR("panic occurred, switching back to text console\n");
270 return drm_fb_helper_force_kernel_mode();
271 return 0;
272}
273EXPORT_SYMBOL(drm_fb_helper_panic);
274
275static struct notifier_block paniced = {
276 .notifier_call = drm_fb_helper_panic,
277};
278
279/**
280 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
281 *
282 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
283 */
284void drm_fb_helper_restore(void)
285{
286 bool ret;
287 ret = drm_fb_helper_force_kernel_mode();
288 if (ret == true)
289 DRM_ERROR("Failed to restore crtc configuration\n");
290}
291EXPORT_SYMBOL(drm_fb_helper_restore);
292
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200293#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000294static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
295{
296 drm_fb_helper_restore();
297}
298static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
299
300static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
301{
302 schedule_work(&drm_fb_helper_restore_work);
303}
304
305static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
306 .handler = drm_fb_helper_sysrq,
307 .help_msg = "force-fb(V)",
308 .action_msg = "Restore framebuffer console",
309};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000310#else
311static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200312#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000313
314static void drm_fb_helper_on(struct fb_info *info)
315{
316 struct drm_fb_helper *fb_helper = info->par;
317 struct drm_device *dev = fb_helper->dev;
318 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000319 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000320 struct drm_encoder *encoder;
321 int i;
322
323 /*
324 * For each CRTC in this fb, turn the crtc on then,
325 * find all associated encoders and turn them on.
326 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000327 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700328 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000329 crtc = fb_helper->crtc_info[i].mode_set.crtc;
330 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000331
Dave Airlie8be48d92010-03-30 05:34:14 +0000332 if (!crtc->enabled)
333 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000334
Dave Airlie8be48d92010-03-30 05:34:14 +0000335 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000336
Dave Airlie785b93e2009-08-28 15:46:53 +1000337
Dave Airlie8be48d92010-03-30 05:34:14 +0000338 /* Found a CRTC on this fb, now find encoders */
339 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
340 if (encoder->crtc == crtc) {
341 struct drm_encoder_helper_funcs *encoder_funcs;
342
343 encoder_funcs = encoder->helper_private;
344 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000345 }
346 }
347 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000348 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000349}
350
351static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
352{
353 struct drm_fb_helper *fb_helper = info->par;
354 struct drm_device *dev = fb_helper->dev;
355 struct drm_crtc *crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +0000356 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000357 struct drm_encoder *encoder;
358 int i;
359
360 /*
361 * For each CRTC in this fb, find all associated encoders
362 * and turn them off, then turn off the CRTC.
363 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000364 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700365 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000366 crtc = fb_helper->crtc_info[i].mode_set.crtc;
367 crtc_funcs = crtc->helper_private;
Dave Airlie785b93e2009-08-28 15:46:53 +1000368
Dave Airlie8be48d92010-03-30 05:34:14 +0000369 if (!crtc->enabled)
370 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000371
Dave Airlie8be48d92010-03-30 05:34:14 +0000372 /* Found a CRTC on this fb, now find encoders */
373 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
374 if (encoder->crtc == crtc) {
375 struct drm_encoder_helper_funcs *encoder_funcs;
Dave Airlie785b93e2009-08-28 15:46:53 +1000376
Dave Airlie8be48d92010-03-30 05:34:14 +0000377 encoder_funcs = encoder->helper_private;
378 encoder_funcs->dpms(encoder, dpms_mode);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700379 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000380 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000381 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000382 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000383 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000384}
385
386int drm_fb_helper_blank(int blank, struct fb_info *info)
387{
388 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000389 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000390 case FB_BLANK_UNBLANK:
391 drm_fb_helper_on(info);
392 break;
James Simmons731b5a12009-10-29 20:39:07 +0000393 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 case FB_BLANK_NORMAL:
Zhenyu Wang5fd4df42010-01-18 16:47:04 +0800395 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000396 break;
James Simmons731b5a12009-10-29 20:39:07 +0000397 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000398 case FB_BLANK_HSYNC_SUSPEND:
399 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
400 break;
James Simmons731b5a12009-10-29 20:39:07 +0000401 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000402 case FB_BLANK_VSYNC_SUSPEND:
403 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
404 break;
James Simmons731b5a12009-10-29 20:39:07 +0000405 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000406 case FB_BLANK_POWERDOWN:
407 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
408 break;
409 }
410 return 0;
411}
412EXPORT_SYMBOL(drm_fb_helper_blank);
413
414static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
415{
416 int i;
417
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000418 for (i = 0; i < helper->connector_count; i++)
419 kfree(helper->connector_info[i]);
420 kfree(helper->connector_info);
Dave Airlie785b93e2009-08-28 15:46:53 +1000421 for (i = 0; i < helper->crtc_count; i++)
422 kfree(helper->crtc_info[i].mode_set.connectors);
423 kfree(helper->crtc_info);
424}
425
Dave Airlie4abe3522010-03-30 05:34:18 +0000426int drm_fb_helper_init(struct drm_device *dev,
427 struct drm_fb_helper *fb_helper,
428 int crtc_count, int max_conn_count,
429 bool polled)
Dave Airlie785b93e2009-08-28 15:46:53 +1000430{
Dave Airlie785b93e2009-08-28 15:46:53 +1000431 struct drm_crtc *crtc;
432 int ret = 0;
433 int i;
434
Dave Airlie4abe3522010-03-30 05:34:18 +0000435 fb_helper->dev = dev;
436 fb_helper->poll_enabled = polled;
Dave Airlie785b93e2009-08-28 15:46:53 +1000437
Dave Airlie4abe3522010-03-30 05:34:18 +0000438 slow_work_register_user(THIS_MODULE);
439 delayed_slow_work_init(&fb_helper->output_status_change_slow_work,
440 &output_status_change_ops);
441
442 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
443
444 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
445 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000446 return -ENOMEM;
447
Dave Airlie4abe3522010-03-30 05:34:18 +0000448 fb_helper->crtc_count = crtc_count;
449 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
450 if (!fb_helper->connector_info) {
451 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000452 return -ENOMEM;
453 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000454 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000455
456 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000457 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000458 kcalloc(max_conn_count,
459 sizeof(struct drm_connector *),
460 GFP_KERNEL);
461
Dave Airlie4abe3522010-03-30 05:34:18 +0000462 if (!fb_helper->crtc_info[i].mode_set.connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000463 ret = -ENOMEM;
464 goto out_free;
465 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000466 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000467 }
468
469 i = 0;
470 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000471 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
472 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000473 i++;
474 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000475 fb_helper->conn_limit = max_conn_count;
Dave Airlie785b93e2009-08-28 15:46:53 +1000476 return 0;
477out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000478 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000479 return -ENOMEM;
480}
Dave Airlie4abe3522010-03-30 05:34:18 +0000481EXPORT_SYMBOL(drm_fb_helper_init);
482
483void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
484{
485 if (!list_empty(&fb_helper->kernel_fb_list)) {
486 list_del(&fb_helper->kernel_fb_list);
487 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400488 printk(KERN_INFO "drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000489 atomic_notifier_chain_unregister(&panic_notifier_list,
490 &paniced);
491 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
492 }
493 }
494
495 drm_fb_helper_crtc_free(fb_helper);
496
497 delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
498 slow_work_unregister_user(THIS_MODULE);
499}
500EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000501
Dave Airliec850cb72009-10-23 18:49:03 +1000502static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000503 u16 blue, u16 regno, struct fb_info *info)
504{
505 struct drm_fb_helper *fb_helper = info->par;
506 struct drm_framebuffer *fb = fb_helper->fb;
507 int pindex;
508
Dave Airliec850cb72009-10-23 18:49:03 +1000509 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
510 u32 *palette;
511 u32 value;
512 /* place color in psuedopalette */
513 if (regno > 16)
514 return -EINVAL;
515 palette = (u32 *)info->pseudo_palette;
516 red >>= (16 - info->var.red.length);
517 green >>= (16 - info->var.green.length);
518 blue >>= (16 - info->var.blue.length);
519 value = (red << info->var.red.offset) |
520 (green << info->var.green.offset) |
521 (blue << info->var.blue.offset);
522 palette[regno] = value;
523 return 0;
524 }
525
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000526 pindex = regno;
527
528 if (fb->bits_per_pixel == 16) {
529 pindex = regno << 3;
530
531 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000532 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000533 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000534 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000535
536 if (fb->depth == 16) {
537 u16 r, g, b;
538 int i;
539 if (regno < 32) {
540 for (i = 0; i < 8; i++)
541 fb_helper->funcs->gamma_set(crtc, red,
542 green, blue, pindex + i);
543 }
544
545 fb_helper->funcs->gamma_get(crtc, &r,
546 &g, &b,
547 pindex >> 1);
548
549 for (i = 0; i < 4; i++)
550 fb_helper->funcs->gamma_set(crtc, r,
551 green, b,
552 (pindex >> 1) + i);
553 }
554 }
555
556 if (fb->depth != 16)
557 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000558 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000559}
560
Dave Airlie068143d2009-10-05 09:58:02 +1000561int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
562{
563 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000564 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000565 u16 *red, *green, *blue, *transp;
566 struct drm_crtc *crtc;
567 int i, rc = 0;
568 int start;
569
Dave Airlie8be48d92010-03-30 05:34:14 +0000570 for (i = 0; i < fb_helper->crtc_count; i++) {
571 crtc = fb_helper->crtc_info[i].mode_set.crtc;
572 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000573
574 red = cmap->red;
575 green = cmap->green;
576 blue = cmap->blue;
577 transp = cmap->transp;
578 start = cmap->start;
579
580 for (i = 0; i < cmap->len; i++) {
581 u16 hred, hgreen, hblue, htransp = 0xffff;
582
583 hred = *red++;
584 hgreen = *green++;
585 hblue = *blue++;
586
587 if (transp)
588 htransp = *transp++;
589
Dave Airliec850cb72009-10-23 18:49:03 +1000590 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
591 if (rc)
592 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000593 }
594 crtc_funcs->load_lut(crtc);
595 }
596 return rc;
597}
598EXPORT_SYMBOL(drm_fb_helper_setcmap);
599
Dave Airlie785b93e2009-08-28 15:46:53 +1000600int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
601 struct fb_info *info)
602{
603 struct drm_fb_helper *fb_helper = info->par;
604 struct drm_framebuffer *fb = fb_helper->fb;
605 int depth;
606
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100607 if (var->pixclock != 0)
Dave Airlie785b93e2009-08-28 15:46:53 +1000608 return -EINVAL;
609
610 /* Need to resize the fb object !!! */
Dave Airlie509c7d82010-01-08 09:27:08 +1000611 if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
612 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
613 "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
614 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000615 return -EINVAL;
616 }
617
618 switch (var->bits_per_pixel) {
619 case 16:
620 depth = (var->green.length == 6) ? 16 : 15;
621 break;
622 case 32:
623 depth = (var->transp.length > 0) ? 32 : 24;
624 break;
625 default:
626 depth = var->bits_per_pixel;
627 break;
628 }
629
630 switch (depth) {
631 case 8:
632 var->red.offset = 0;
633 var->green.offset = 0;
634 var->blue.offset = 0;
635 var->red.length = 8;
636 var->green.length = 8;
637 var->blue.length = 8;
638 var->transp.length = 0;
639 var->transp.offset = 0;
640 break;
641 case 15:
642 var->red.offset = 10;
643 var->green.offset = 5;
644 var->blue.offset = 0;
645 var->red.length = 5;
646 var->green.length = 5;
647 var->blue.length = 5;
648 var->transp.length = 1;
649 var->transp.offset = 15;
650 break;
651 case 16:
652 var->red.offset = 11;
653 var->green.offset = 5;
654 var->blue.offset = 0;
655 var->red.length = 5;
656 var->green.length = 6;
657 var->blue.length = 5;
658 var->transp.length = 0;
659 var->transp.offset = 0;
660 break;
661 case 24:
662 var->red.offset = 16;
663 var->green.offset = 8;
664 var->blue.offset = 0;
665 var->red.length = 8;
666 var->green.length = 8;
667 var->blue.length = 8;
668 var->transp.length = 0;
669 var->transp.offset = 0;
670 break;
671 case 32:
672 var->red.offset = 16;
673 var->green.offset = 8;
674 var->blue.offset = 0;
675 var->red.length = 8;
676 var->green.length = 8;
677 var->blue.length = 8;
678 var->transp.length = 8;
679 var->transp.offset = 24;
680 break;
681 default:
682 return -EINVAL;
683 }
684 return 0;
685}
686EXPORT_SYMBOL(drm_fb_helper_check_var);
687
688/* this will let fbcon do the mode init */
689int drm_fb_helper_set_par(struct fb_info *info)
690{
691 struct drm_fb_helper *fb_helper = info->par;
692 struct drm_device *dev = fb_helper->dev;
693 struct fb_var_screeninfo *var = &info->var;
694 struct drm_crtc *crtc;
695 int ret;
696 int i;
697
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100698 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000699 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000700 return -EINVAL;
701 }
702
Dave Airlie8be48d92010-03-30 05:34:14 +0000703 mutex_lock(&dev->mode_config.mutex);
704 for (i = 0; i < fb_helper->crtc_count; i++) {
705 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000706 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
707 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000708 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000709 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000710 }
711 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000712 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000713
714 if (fb_helper->delayed_hotplug) {
715 fb_helper->delayed_hotplug = false;
716 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
717 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000718 return 0;
719}
720EXPORT_SYMBOL(drm_fb_helper_set_par);
721
722int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
723 struct fb_info *info)
724{
725 struct drm_fb_helper *fb_helper = info->par;
726 struct drm_device *dev = fb_helper->dev;
727 struct drm_mode_set *modeset;
728 struct drm_crtc *crtc;
729 int ret = 0;
730 int i;
731
Dave Airlie8be48d92010-03-30 05:34:14 +0000732 mutex_lock(&dev->mode_config.mutex);
733 for (i = 0; i < fb_helper->crtc_count; i++) {
734 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000735
736 modeset = &fb_helper->crtc_info[i].mode_set;
737
738 modeset->x = var->xoffset;
739 modeset->y = var->yoffset;
740
741 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000742 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000743 if (!ret) {
744 info->var.xoffset = var->xoffset;
745 info->var.yoffset = var->yoffset;
746 }
747 }
748 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000749 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000750 return ret;
751}
752EXPORT_SYMBOL(drm_fb_helper_pan_display);
753
Dave Airlie8be48d92010-03-30 05:34:14 +0000754int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
755 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000756{
Dave Airlie785b93e2009-08-28 15:46:53 +1000757 int new_fb = 0;
758 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000759 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000760 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000761 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000762 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000763
764 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
765 sizes.surface_depth = 24;
766 sizes.surface_bpp = 32;
767 sizes.fb_width = (unsigned)-1;
768 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000769
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000772 if (preferred_bpp != sizes.surface_bpp) {
773 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000774 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000776 for (i = 0; i < fb_helper->connector_count; i++) {
777 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie8ef86782009-09-26 06:39:00 +1000778 struct drm_fb_helper_cmdline_mode *cmdline_mode;
779
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000780 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000781
782 if (cmdline_mode->bpp_specified) {
783 switch (cmdline_mode->bpp) {
784 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000785 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000786 break;
787 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000788 sizes.surface_depth = 15;
789 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000790 break;
791 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000792 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000793 break;
794 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000795 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000796 break;
797 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000798 sizes.surface_depth = 24;
799 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000800 break;
801 }
802 break;
803 }
804 }
805
Dave Airlie8be48d92010-03-30 05:34:14 +0000806 crtc_count = 0;
807 for (i = 0; i < fb_helper->crtc_count; i++) {
808 struct drm_display_mode *desired_mode;
809 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000810
Dave Airlie8be48d92010-03-30 05:34:14 +0000811 if (desired_mode) {
812 if (gamma_size == 0)
813 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
814 if (desired_mode->hdisplay < sizes.fb_width)
815 sizes.fb_width = desired_mode->hdisplay;
816 if (desired_mode->vdisplay < sizes.fb_height)
817 sizes.fb_height = desired_mode->vdisplay;
818 if (desired_mode->hdisplay > sizes.surface_width)
819 sizes.surface_width = desired_mode->hdisplay;
820 if (desired_mode->vdisplay > sizes.surface_height)
821 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000822 crtc_count++;
823 }
824 }
825
Dave Airlie38651672010-03-30 05:34:13 +0000826 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000827 /* hmm everyone went away - assume VGA cable just fell out
828 and will come back later. */
Dave Airlie19b4b442010-03-30 05:34:16 +0000829 DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n");
830 sizes.fb_width = sizes.surface_width = 1024;
831 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 }
833
Dave Airlie38651672010-03-30 05:34:13 +0000834 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000835 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000836 if (new_fb < 0)
837 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000838
Dave Airlie38651672010-03-30 05:34:13 +0000839 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000840
Dave Airlie8be48d92010-03-30 05:34:14 +0000841 /* set the fb pointer */
842 for (i = 0; i < fb_helper->crtc_count; i++) {
843 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000844 }
845
Dave Airlie785b93e2009-08-28 15:46:53 +1000846 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100847 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100848 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000849 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100850 }
Dave Airlie38651672010-03-30 05:34:13 +0000851
852 printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
853 info->fix.id);
854
Dave Airlie785b93e2009-08-28 15:46:53 +1000855 } else {
856 drm_fb_helper_set_par(info);
857 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000858
859 /* Switch back to kernel console on panic */
860 /* multi card linked list maybe */
861 if (list_empty(&kernel_fb_helper_list)) {
Kirill Smelkov3da1f332010-05-14 19:45:16 +0400862 printk(KERN_INFO "drm: registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000863 atomic_notifier_chain_register(&panic_notifier_list,
864 &paniced);
865 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
866 }
Dave Airlie38651672010-03-30 05:34:13 +0000867 if (new_fb)
868 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
869
Dave Airlie785b93e2009-08-28 15:46:53 +1000870 return 0;
871}
872EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
873
Dave Airlie068143d2009-10-05 09:58:02 +1000874void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
875 uint32_t depth)
Dave Airlie785b93e2009-08-28 15:46:53 +1000876{
877 info->fix.type = FB_TYPE_PACKED_PIXELS;
Dave Airlie068143d2009-10-05 09:58:02 +1000878 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
Dave Airliec850cb72009-10-23 18:49:03 +1000879 FB_VISUAL_TRUECOLOR;
Dave Airlie785b93e2009-08-28 15:46:53 +1000880 info->fix.type_aux = 0;
881 info->fix.xpanstep = 1; /* doing it in hw */
882 info->fix.ypanstep = 1; /* doing it in hw */
883 info->fix.ywrapstep = 0;
Dave Airlie3420e742009-08-31 10:33:29 +1000884 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie785b93e2009-08-28 15:46:53 +1000885 info->fix.type_aux = 0;
886
887 info->fix.line_length = pitch;
888 return;
889}
890EXPORT_SYMBOL(drm_fb_helper_fill_fix);
891
Dave Airlie38651672010-03-30 05:34:13 +0000892void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000893 uint32_t fb_width, uint32_t fb_height)
894{
Dave Airlie38651672010-03-30 05:34:13 +0000895 struct drm_framebuffer *fb = fb_helper->fb;
896 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000897 info->var.xres_virtual = fb->width;
898 info->var.yres_virtual = fb->height;
899 info->var.bits_per_pixel = fb->bits_per_pixel;
900 info->var.xoffset = 0;
901 info->var.yoffset = 0;
902 info->var.activate = FB_ACTIVATE_NOW;
903 info->var.height = -1;
904 info->var.width = -1;
905
906 switch (fb->depth) {
907 case 8:
908 info->var.red.offset = 0;
909 info->var.green.offset = 0;
910 info->var.blue.offset = 0;
911 info->var.red.length = 8; /* 8bit DAC */
912 info->var.green.length = 8;
913 info->var.blue.length = 8;
914 info->var.transp.offset = 0;
915 info->var.transp.length = 0;
916 break;
917 case 15:
918 info->var.red.offset = 10;
919 info->var.green.offset = 5;
920 info->var.blue.offset = 0;
921 info->var.red.length = 5;
922 info->var.green.length = 5;
923 info->var.blue.length = 5;
924 info->var.transp.offset = 15;
925 info->var.transp.length = 1;
926 break;
927 case 16:
928 info->var.red.offset = 11;
929 info->var.green.offset = 5;
930 info->var.blue.offset = 0;
931 info->var.red.length = 5;
932 info->var.green.length = 6;
933 info->var.blue.length = 5;
934 info->var.transp.offset = 0;
935 break;
936 case 24:
937 info->var.red.offset = 16;
938 info->var.green.offset = 8;
939 info->var.blue.offset = 0;
940 info->var.red.length = 8;
941 info->var.green.length = 8;
942 info->var.blue.length = 8;
943 info->var.transp.offset = 0;
944 info->var.transp.length = 0;
945 break;
946 case 32:
947 info->var.red.offset = 16;
948 info->var.green.offset = 8;
949 info->var.blue.offset = 0;
950 info->var.red.length = 8;
951 info->var.green.length = 8;
952 info->var.blue.length = 8;
953 info->var.transp.offset = 24;
954 info->var.transp.length = 8;
955 break;
956 default:
957 break;
958 }
959
960 info->var.xres = fb_width;
961 info->var.yres = fb_height;
962}
963EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000964
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000965static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
966 uint32_t maxX,
967 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000968{
969 struct drm_connector *connector;
970 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000971 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000972
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000973 for (i = 0; i < fb_helper->connector_count; i++) {
974 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000975 count += connector->funcs->fill_modes(connector, maxX, maxY);
976 }
977
978 return count;
979}
980
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000981static 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 +0000982{
983 struct drm_display_mode *mode;
984
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000985 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000986 if (drm_mode_width(mode) > width ||
987 drm_mode_height(mode) > height)
988 continue;
989 if (mode->type & DRM_MODE_TYPE_PREFERRED)
990 return mode;
991 }
992 return NULL;
993}
994
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000995static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000996{
Dave Airlie38651672010-03-30 05:34:13 +0000997 struct drm_fb_helper_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000998 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000999 return cmdline_mode->specified;
1000}
1001
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001002static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1003 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001004{
Dave Airlie38651672010-03-30 05:34:13 +00001005 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1006 struct drm_display_mode *mode = NULL;
1007
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001008 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001009 if (cmdline_mode->specified == false)
1010 return mode;
1011
1012 /* attempt to find a matching mode in the list of modes
1013 * we have gotten so far, if not add a CVT mode that conforms
1014 */
1015 if (cmdline_mode->rb || cmdline_mode->margins)
1016 goto create_mode;
1017
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001018 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001019 /* check width/height */
1020 if (mode->hdisplay != cmdline_mode->xres ||
1021 mode->vdisplay != cmdline_mode->yres)
1022 continue;
1023
1024 if (cmdline_mode->refresh_specified) {
1025 if (mode->vrefresh != cmdline_mode->refresh)
1026 continue;
1027 }
1028
1029 if (cmdline_mode->interlace) {
1030 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1031 continue;
1032 }
1033 return mode;
1034 }
1035
1036create_mode:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001037 mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
Dave Airlie38651672010-03-30 05:34:13 +00001038 cmdline_mode->yres,
1039 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1040 cmdline_mode->rb, cmdline_mode->interlace,
1041 cmdline_mode->margins);
1042 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001043 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001044 return mode;
1045}
1046
1047static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1048{
1049 bool enable;
1050
1051 if (strict) {
1052 enable = connector->status == connector_status_connected;
1053 } else {
1054 enable = connector->status != connector_status_disconnected;
1055 }
1056 return enable;
1057}
1058
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001059static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1060 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001061{
1062 bool any_enabled = false;
1063 struct drm_connector *connector;
1064 int i = 0;
1065
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001066 for (i = 0; i < fb_helper->connector_count; i++) {
1067 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001068 enabled[i] = drm_connector_enabled(connector, true);
1069 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1070 enabled[i] ? "yes" : "no");
1071 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001072 }
1073
1074 if (any_enabled)
1075 return;
1076
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001077 for (i = 0; i < fb_helper->connector_count; i++) {
1078 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001079 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001080 }
1081}
1082
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001083static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001084 struct drm_display_mode **modes,
1085 bool *enabled, int width, int height)
1086{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001087 struct drm_fb_helper_connector *fb_helper_conn;
1088 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001089
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001090 for (i = 0; i < fb_helper->connector_count; i++) {
1091 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001092
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001093 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001094 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001095
1096 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001097 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001098
1099 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001100 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001101 if (!modes[i]) {
1102 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001103 fb_helper_conn->connector->base.id);
1104 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001105 }
1106 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001107 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1108 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001109 break;
1110 }
1111 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1112 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001113 }
1114 return true;
1115}
1116
Dave Airlie8be48d92010-03-30 05:34:14 +00001117static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1118 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001119 struct drm_display_mode **modes,
1120 int n, int width, int height)
1121{
1122 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001123 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001124 struct drm_connector *connector;
1125 struct drm_connector_helper_funcs *connector_funcs;
1126 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001127 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001128 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001129 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001130 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001131
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001132 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001133 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001134
1135 fb_helper_conn = fb_helper->connector_info[n];
1136 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001137
1138 best_crtcs[n] = NULL;
1139 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001140 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001141 if (modes[n] == NULL)
1142 return best_score;
1143
Dave Airlie8be48d92010-03-30 05:34:14 +00001144 crtcs = kzalloc(dev->mode_config.num_connector *
1145 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001146 if (!crtcs)
1147 return best_score;
1148
1149 my_score = 1;
1150 if (connector->status == connector_status_connected)
1151 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001152 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001153 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001154 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001155 my_score++;
1156
1157 connector_funcs = connector->helper_private;
1158 encoder = connector_funcs->best_encoder(connector);
1159 if (!encoder)
1160 goto out;
1161
Dave Airlie38651672010-03-30 05:34:13 +00001162 /* select a crtc for this connector and then attempt to configure
1163 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001164 for (c = 0; c < fb_helper->crtc_count; c++) {
1165 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001166
1167 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001168 continue;
1169 }
1170
1171 for (o = 0; o < n; o++)
1172 if (best_crtcs[o] == crtc)
1173 break;
1174
1175 if (o < n) {
1176 /* ignore cloning for now */
Dave Airlie38651672010-03-30 05:34:13 +00001177 continue;
1178 }
1179
1180 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001181 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1182 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001183 width, height);
1184 if (score > best_score) {
1185 best_crtc = crtc;
1186 best_score = score;
1187 memcpy(best_crtcs, crtcs,
1188 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001189 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001190 }
Dave Airlie38651672010-03-30 05:34:13 +00001191 }
1192out:
1193 kfree(crtcs);
1194 return best_score;
1195}
1196
Dave Airlie8be48d92010-03-30 05:34:14 +00001197static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001198{
Dave Airlie8be48d92010-03-30 05:34:14 +00001199 struct drm_device *dev = fb_helper->dev;
1200 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001201 struct drm_display_mode **modes;
1202 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001203 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001204 bool *enabled;
1205 int width, height;
1206 int i, ret;
1207
1208 DRM_DEBUG_KMS("\n");
1209
1210 width = dev->mode_config.max_width;
1211 height = dev->mode_config.max_height;
1212
1213 /* clean out all the encoder/crtc combos */
1214 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1215 encoder->crtc = NULL;
1216 }
1217
1218 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001219 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001220 modes = kcalloc(dev->mode_config.num_connector,
1221 sizeof(struct drm_display_mode *), GFP_KERNEL);
1222 enabled = kcalloc(dev->mode_config.num_connector,
1223 sizeof(bool), GFP_KERNEL);
1224
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001225 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001226
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001227 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001228 if (!ret)
1229 DRM_ERROR("Unable to find initial modes\n");
1230
1231 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1232
Dave Airlie8be48d92010-03-30 05:34:14 +00001233 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1234
1235 /* need to set the modesets up here for use later */
1236 /* fill out the connector<->crtc mappings into the modesets */
1237 for (i = 0; i < fb_helper->crtc_count; i++) {
1238 modeset = &fb_helper->crtc_info[i].mode_set;
1239 modeset->num_connectors = 0;
1240 }
Dave Airlie38651672010-03-30 05:34:13 +00001241
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001242 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001243 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001244 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1245 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001246
Dave Airlie8be48d92010-03-30 05:34:14 +00001247 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001248 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001249 mode->name, fb_crtc->mode_set.crtc->base.id);
1250 fb_crtc->desired_mode = mode;
1251 if (modeset->mode)
1252 drm_mode_destroy(dev, modeset->mode);
1253 modeset->mode = drm_mode_duplicate(dev,
1254 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001255 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001256 }
Dave Airlie38651672010-03-30 05:34:13 +00001257 }
1258
1259 kfree(crtcs);
1260 kfree(modes);
1261 kfree(enabled);
1262}
1263
1264/**
1265 * drm_helper_initial_config - setup a sane initial connector configuration
1266 * @dev: DRM device
1267 *
1268 * LOCKING:
1269 * Called at init time, must take mode config lock.
1270 *
1271 * Scan the CRTCs and connectors and try to put together an initial setup.
1272 * At the moment, this is a cloned configuration across all heads with
1273 * a new framebuffer object as the backing store.
1274 *
1275 * RETURNS:
1276 * Zero if everything went ok, nonzero otherwise.
1277 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001278bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001279{
Dave Airlie8be48d92010-03-30 05:34:14 +00001280 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001281 int count = 0;
1282
1283 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001284 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001285
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001286 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001287
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001288 count = drm_fb_helper_probe_connector_modes(fb_helper,
1289 dev->mode_config.max_width,
1290 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001291 /*
1292 * we shouldn't end up with no modes here.
1293 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001294 if (count == 0) {
1295 if (fb_helper->poll_enabled) {
Dave Airlie4abe3522010-03-30 05:34:18 +00001296 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work,
Dave Airlie5c4426a2010-03-30 05:34:17 +00001297 5*HZ);
1298 printk(KERN_INFO "No connectors reported connected with modes - started polling\n");
1299 } else
1300 printk(KERN_INFO "No connectors reported connected with modes\n");
1301 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001302 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001303
Dave Airlie4abe3522010-03-30 05:34:18 +00001304 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001305}
Dave Airlie8be48d92010-03-30 05:34:14 +00001306EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001307
Dave Airlie4abe3522010-03-30 05:34:18 +00001308/* we got a hotplug irq - need to update fbcon */
1309void drm_helper_fb_hpd_irq_event(struct drm_fb_helper *fb_helper)
1310{
1311 /* if we don't have the fbdev registered yet do nothing */
1312 if (!fb_helper->fbdev)
1313 return;
1314
1315 /* schedule a slow work asap */
1316 delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 0);
1317}
1318EXPORT_SYMBOL(drm_helper_fb_hpd_irq_event);
1319
1320bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, bool polled)
Dave Airlie38651672010-03-30 05:34:13 +00001321{
Dave Airlie5c4426a2010-03-30 05:34:17 +00001322 int count = 0;
1323 int ret;
Dave Airlie4abe3522010-03-30 05:34:18 +00001324 u32 max_width, max_height, bpp_sel;
1325
1326 if (!fb_helper->fb)
1327 return false;
Dave Airlie38651672010-03-30 05:34:13 +00001328 DRM_DEBUG_KMS("\n");
1329
Dave Airlie4abe3522010-03-30 05:34:18 +00001330 max_width = fb_helper->fb->width;
1331 max_height = fb_helper->fb->height;
1332 bpp_sel = fb_helper->fb->bits_per_pixel;
1333
Dave Airlie5c4426a2010-03-30 05:34:17 +00001334 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001335 max_height);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001336 if (fb_helper->poll_enabled && !polled) {
1337 if (count) {
Dave Airlie4abe3522010-03-30 05:34:18 +00001338 delayed_slow_work_cancel(&fb_helper->output_status_change_slow_work);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001339 } else {
Dave Airlie4abe3522010-03-30 05:34:18 +00001340 ret = delayed_slow_work_enqueue(&fb_helper->output_status_change_slow_work, 5*HZ);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001341 }
1342 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001343 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001344
Dave Airlie4abe3522010-03-30 05:34:18 +00001345 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001346}
1347EXPORT_SYMBOL(drm_helper_fb_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001348
Dave Airlie4abe3522010-03-30 05:34:18 +00001349/*
1350 * delayed work queue execution function
1351 * - check if fbdev is actually in use on the gpu
1352 * - if not set delayed flag and repoll if necessary
1353 * - check for connector status change
1354 * - repoll if 0 modes found
1355 *- call driver output status changed notifier
1356 */
1357static void output_status_change_execute(struct slow_work *work)
Dave Airlie5c4426a2010-03-30 05:34:17 +00001358{
1359 struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work);
Dave Airlie4abe3522010-03-30 05:34:18 +00001360 struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_status_change_slow_work);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001361 struct drm_connector *connector;
1362 enum drm_connector_status old_status, status;
Dave Airlie4abe3522010-03-30 05:34:18 +00001363 bool repoll, changed = false;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001364 int ret;
Dave Airlie4abe3522010-03-30 05:34:18 +00001365 int i;
1366 bool bound = false, crtcs_bound = false;
1367 struct drm_crtc *crtc;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001368
Dave Airlie4abe3522010-03-30 05:34:18 +00001369 repoll = fb_helper->poll_enabled;
1370
1371 /* first of all check the fbcon framebuffer is actually bound to any crtc */
1372 /* take into account that no crtc at all maybe bound */
1373 list_for_each_entry(crtc, &fb_helper->dev->mode_config.crtc_list, head) {
1374 if (crtc->fb)
1375 crtcs_bound = true;
1376 if (crtc->fb == fb_helper->fb)
1377 bound = true;
1378 }
1379
1380 if (bound == false && crtcs_bound) {
1381 fb_helper->delayed_hotplug = true;
1382 goto requeue;
1383 }
1384
1385 for (i = 0; i < fb_helper->connector_count; i++) {
1386 connector = fb_helper->connector_info[i]->connector;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001387 old_status = connector->status;
1388 status = connector->funcs->detect(connector);
1389 if (old_status != status) {
1390 changed = true;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001391 }
Dave Airlie4abe3522010-03-30 05:34:18 +00001392 if (status == connector_status_connected && repoll) {
Dave Airlie5c4426a2010-03-30 05:34:17 +00001393 DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector));
1394 repoll = false;
1395 }
1396 }
1397
Dave Airlie4abe3522010-03-30 05:34:18 +00001398 if (changed) {
1399 if (fb_helper->funcs->fb_output_status_changed)
1400 fb_helper->funcs->fb_output_status_changed(fb_helper);
1401 }
1402
1403requeue:
Dave Airlie5c4426a2010-03-30 05:34:17 +00001404 if (repoll) {
1405 ret = delayed_slow_work_enqueue(delayed_work, 5*HZ);
1406 if (ret)
1407 DRM_ERROR("delayed enqueue failed %d\n", ret);
1408 }
Dave Airlie5c4426a2010-03-30 05:34:17 +00001409}
1410
Dave Airlie4abe3522010-03-30 05:34:18 +00001411static struct slow_work_ops output_status_change_ops = {
1412 .execute = output_status_change_execute,
Dave Airlie5c4426a2010-03-30 05:34:17 +00001413};
1414