blob: 07cba5af59f0bb14660e8ae0aaa1116c21a9592c [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 */
Sachin Kamatd56b1b92012-11-15 03:43:29 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Andy Shevchenko3b40a442010-02-02 14:40:32 -080032#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include <linux/fb.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100041
Dave Airlie6fcefd52009-09-08 11:08:32 +100042MODULE_AUTHOR("David Airlie, Jesse Barnes");
43MODULE_DESCRIPTION("DRM KMS helper");
44MODULE_LICENSE("GPL and additional rights");
45
Dave Airlie785b93e2009-08-28 15:46:53 +100046static LIST_HEAD(kernel_fb_helper_list);
47
Dave Airlie0b4c0f32010-03-30 05:34:15 +000048/* simple single crtc case helper function */
49int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100050{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000051 struct drm_device *dev = fb_helper->dev;
52 struct drm_connector *connector;
53 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100054
Dave Airlie0b4c0f32010-03-30 05:34:15 +000055 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
56 struct drm_fb_helper_connector *fb_helper_connector;
57
58 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
59 if (!fb_helper_connector)
60 goto fail;
61
62 fb_helper_connector->connector = connector;
63 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
64 }
Dave Airlied50ba252009-09-23 14:44:08 +100065 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000066fail:
67 for (i = 0; i < fb_helper->connector_count; i++) {
68 kfree(fb_helper->connector_info[i]);
69 fb_helper->connector_info[i] = NULL;
70 }
71 fb_helper->connector_count = 0;
72 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +100073}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000074EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100075
Dave Airlie0b4c0f32010-03-30 05:34:15 +000076static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100077{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000078 struct drm_fb_helper_connector *fb_helper_conn;
79 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100080
Dave Airlie0b4c0f32010-03-30 05:34:15 +000081 for (i = 0; i < fb_helper->connector_count; i++) {
Chris Wilson1794d252011-04-17 07:43:32 +010082 struct drm_cmdline_mode *mode;
83 struct drm_connector *connector;
Dave Airlied50ba252009-09-23 14:44:08 +100084 char *option = NULL;
85
Dave Airlie0b4c0f32010-03-30 05:34:15 +000086 fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +010087 connector = fb_helper_conn->connector;
88 mode = &fb_helper_conn->cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000089
Dave Airlied50ba252009-09-23 14:44:08 +100090 /* do something on return - turn off connector maybe */
Chris Wilson1794d252011-04-17 07:43:32 +010091 if (fb_get_options(drm_get_connector_name(connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +100092 continue;
93
Chris Wilson1794d252011-04-17 07:43:32 +010094 if (drm_mode_parse_command_line_for_connector(option,
95 connector,
96 mode)) {
97 if (mode->force) {
98 const char *s;
99 switch (mode->force) {
Sachin Kamat6c910832012-11-15 03:43:28 +0000100 case DRM_FORCE_OFF:
101 s = "OFF";
102 break;
103 case DRM_FORCE_ON_DIGITAL:
104 s = "ON - dig";
105 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100106 default:
Sachin Kamat6c910832012-11-15 03:43:28 +0000107 case DRM_FORCE_ON:
108 s = "ON";
109 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100110 }
111
112 DRM_INFO("forcing %s connector %s\n",
113 drm_get_connector_name(connector), s);
114 connector->force = mode->force;
115 }
116
117 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
118 drm_get_connector_name(connector),
119 mode->xres, mode->yres,
120 mode->refresh_specified ? mode->refresh : 60,
121 mode->rb ? " reduced blanking" : "",
122 mode->margins ? " with margins" : "",
123 mode->interlace ? " interlaced" : "");
124 }
125
Dave Airlied50ba252009-09-23 14:44:08 +1000126 }
127 return 0;
128}
129
Jason Wessel99231022010-10-13 14:09:43 -0500130static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
131{
132 uint16_t *r_base, *g_base, *b_base;
133 int i;
134
135 r_base = crtc->gamma_store;
136 g_base = r_base + crtc->gamma_size;
137 b_base = g_base + crtc->gamma_size;
138
139 for (i = 0; i < crtc->gamma_size; i++)
140 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
141}
142
143static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
144{
145 uint16_t *r_base, *g_base, *b_base;
146
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200147 if (crtc->funcs->gamma_set == NULL)
148 return;
149
Jason Wessel99231022010-10-13 14:09:43 -0500150 r_base = crtc->gamma_store;
151 g_base = r_base + crtc->gamma_size;
152 b_base = g_base + crtc->gamma_size;
153
154 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
155}
156
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500157int drm_fb_helper_debug_enter(struct fb_info *info)
158{
159 struct drm_fb_helper *helper = info->par;
160 struct drm_crtc_helper_funcs *funcs;
161 int i;
162
163 if (list_empty(&kernel_fb_helper_list))
164 return false;
165
166 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
167 for (i = 0; i < helper->crtc_count; i++) {
168 struct drm_mode_set *mode_set =
169 &helper->crtc_info[i].mode_set;
170
171 if (!mode_set->crtc->enabled)
172 continue;
173
174 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500175 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500176 funcs->mode_set_base_atomic(mode_set->crtc,
177 mode_set->fb,
178 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500179 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500180 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500181 }
182 }
183
184 return 0;
185}
186EXPORT_SYMBOL(drm_fb_helper_debug_enter);
187
188/* Find the real fb for a given fb helper CRTC */
189static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
190{
191 struct drm_device *dev = crtc->dev;
192 struct drm_crtc *c;
193
194 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
195 if (crtc->base.id == c->base.id)
196 return c->fb;
197 }
198
199 return NULL;
200}
201
202int drm_fb_helper_debug_leave(struct fb_info *info)
203{
204 struct drm_fb_helper *helper = info->par;
205 struct drm_crtc *crtc;
206 struct drm_crtc_helper_funcs *funcs;
207 struct drm_framebuffer *fb;
208 int i;
209
210 for (i = 0; i < helper->crtc_count; i++) {
211 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
212 crtc = mode_set->crtc;
213 funcs = crtc->helper_private;
214 fb = drm_mode_config_fb(crtc);
215
216 if (!crtc->enabled)
217 continue;
218
219 if (!fb) {
220 DRM_ERROR("no fb to restore??\n");
221 continue;
222 }
223
Jason Wessel99231022010-10-13 14:09:43 -0500224 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500225 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500226 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500227 }
228
229 return 0;
230}
231EXPORT_SYMBOL(drm_fb_helper_debug_leave);
232
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100233bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
234{
235 bool error = false;
236 int i, ret;
237 for (i = 0; i < fb_helper->crtc_count; i++) {
238 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Daniel Vetterd3904752012-07-11 16:28:07 +0200239 ret = mode_set->crtc->funcs->set_config(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100240 if (ret)
241 error = true;
242 }
243 return error;
244}
245EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
246
Sachin Kamat78b9c352012-08-01 17:15:32 +0530247static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000248{
Dave Airlie785b93e2009-08-28 15:46:53 +1000249 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) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100256 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
257 continue;
258
259 ret = drm_fb_helper_restore_fbdev_mode(helper);
260 if (ret)
261 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000262 }
263 return error;
264}
265
266int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267 void *panic_str)
268{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700269 /*
270 * It's a waste of time and effort to switch back to text console
271 * if the kernel should reboot before panic messages can be seen.
272 */
273 if (panic_timeout < 0)
274 return 0;
275
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000276 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000277 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000278}
279EXPORT_SYMBOL(drm_fb_helper_panic);
280
281static struct notifier_block paniced = {
282 .notifier_call = drm_fb_helper_panic,
283};
284
285/**
286 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
287 *
288 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
289 */
290void drm_fb_helper_restore(void)
291{
292 bool ret;
293 ret = drm_fb_helper_force_kernel_mode();
294 if (ret == true)
295 DRM_ERROR("Failed to restore crtc configuration\n");
296}
297EXPORT_SYMBOL(drm_fb_helper_restore);
298
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200299#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000300static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
301{
302 drm_fb_helper_restore();
303}
304static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
305
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700306static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000307{
308 schedule_work(&drm_fb_helper_restore_work);
309}
310
311static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
312 .handler = drm_fb_helper_sysrq,
313 .help_msg = "force-fb(V)",
314 .action_msg = "Restore framebuffer console",
315};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000316#else
317static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200318#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000319
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100320static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000321{
322 struct drm_fb_helper *fb_helper = info->par;
323 struct drm_device *dev = fb_helper->dev;
324 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700325 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700326 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000327
328 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100329 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000330 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000331 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700332 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000333 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000334
Dave Airlie8be48d92010-03-30 05:34:14 +0000335 if (!crtc->enabled)
336 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000337
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100338 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700339 for (j = 0; j < fb_helper->connector_count; j++) {
340 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200341 connector->funcs->dpms(connector, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700342 drm_connector_property_set_value(connector,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100343 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700344 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000345 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000346 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000347}
348
349int drm_fb_helper_blank(int blank, struct fb_info *info)
350{
351 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000352 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000353 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100354 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000355 break;
James Simmons731b5a12009-10-29 20:39:07 +0000356 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000357 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100358 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000359 break;
James Simmons731b5a12009-10-29 20:39:07 +0000360 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000361 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100362 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000363 break;
James Simmons731b5a12009-10-29 20:39:07 +0000364 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000365 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100366 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000367 break;
James Simmons731b5a12009-10-29 20:39:07 +0000368 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000369 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100370 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000371 break;
372 }
373 return 0;
374}
375EXPORT_SYMBOL(drm_fb_helper_blank);
376
377static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
378{
379 int i;
380
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000381 for (i = 0; i < helper->connector_count; i++)
382 kfree(helper->connector_info[i]);
383 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100384 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000385 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100386 if (helper->crtc_info[i].mode_set.mode)
387 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
388 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000389 kfree(helper->crtc_info);
390}
391
Dave Airlie4abe3522010-03-30 05:34:18 +0000392int drm_fb_helper_init(struct drm_device *dev,
393 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000394 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000395{
Dave Airlie785b93e2009-08-28 15:46:53 +1000396 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000397 int i;
398
Dave Airlie4abe3522010-03-30 05:34:18 +0000399 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000400
401 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
402
403 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
404 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000405 return -ENOMEM;
406
Dave Airlie4abe3522010-03-30 05:34:18 +0000407 fb_helper->crtc_count = crtc_count;
408 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
409 if (!fb_helper->connector_info) {
410 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000411 return -ENOMEM;
412 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000413 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000414
415 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000416 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000417 kcalloc(max_conn_count,
418 sizeof(struct drm_connector *),
419 GFP_KERNEL);
420
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200421 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000422 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000423 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000424 }
425
426 i = 0;
427 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000428 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000429 i++;
430 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100431
Dave Airlie785b93e2009-08-28 15:46:53 +1000432 return 0;
433out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000434 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000435 return -ENOMEM;
436}
Dave Airlie4abe3522010-03-30 05:34:18 +0000437EXPORT_SYMBOL(drm_fb_helper_init);
438
439void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
440{
441 if (!list_empty(&fb_helper->kernel_fb_list)) {
442 list_del(&fb_helper->kernel_fb_list);
443 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000444 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000445 atomic_notifier_chain_unregister(&panic_notifier_list,
446 &paniced);
447 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
448 }
449 }
450
451 drm_fb_helper_crtc_free(fb_helper);
452
Dave Airlie4abe3522010-03-30 05:34:18 +0000453}
454EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000455
Dave Airliec850cb72009-10-23 18:49:03 +1000456static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000457 u16 blue, u16 regno, struct fb_info *info)
458{
459 struct drm_fb_helper *fb_helper = info->par;
460 struct drm_framebuffer *fb = fb_helper->fb;
461 int pindex;
462
Dave Airliec850cb72009-10-23 18:49:03 +1000463 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
464 u32 *palette;
465 u32 value;
466 /* place color in psuedopalette */
467 if (regno > 16)
468 return -EINVAL;
469 palette = (u32 *)info->pseudo_palette;
470 red >>= (16 - info->var.red.length);
471 green >>= (16 - info->var.green.length);
472 blue >>= (16 - info->var.blue.length);
473 value = (red << info->var.red.offset) |
474 (green << info->var.green.offset) |
475 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000476 if (info->var.transp.length > 0) {
477 u32 mask = (1 << info->var.transp.length) - 1;
478 mask <<= info->var.transp.offset;
479 value |= mask;
480 }
Dave Airliec850cb72009-10-23 18:49:03 +1000481 palette[regno] = value;
482 return 0;
483 }
484
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000485 pindex = regno;
486
487 if (fb->bits_per_pixel == 16) {
488 pindex = regno << 3;
489
490 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000491 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000492 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000493 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000494
495 if (fb->depth == 16) {
496 u16 r, g, b;
497 int i;
498 if (regno < 32) {
499 for (i = 0; i < 8; i++)
500 fb_helper->funcs->gamma_set(crtc, red,
501 green, blue, pindex + i);
502 }
503
504 fb_helper->funcs->gamma_get(crtc, &r,
505 &g, &b,
506 pindex >> 1);
507
508 for (i = 0; i < 4; i++)
509 fb_helper->funcs->gamma_set(crtc, r,
510 green, b,
511 (pindex >> 1) + i);
512 }
513 }
514
515 if (fb->depth != 16)
516 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000517 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000518}
519
Dave Airlie068143d2009-10-05 09:58:02 +1000520int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
521{
522 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000523 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000524 u16 *red, *green, *blue, *transp;
525 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100526 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000527 int start;
528
Dave Airlie8be48d92010-03-30 05:34:14 +0000529 for (i = 0; i < fb_helper->crtc_count; i++) {
530 crtc = fb_helper->crtc_info[i].mode_set.crtc;
531 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000532
533 red = cmap->red;
534 green = cmap->green;
535 blue = cmap->blue;
536 transp = cmap->transp;
537 start = cmap->start;
538
roel062ac622011-03-07 18:00:34 +0100539 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000540 u16 hred, hgreen, hblue, htransp = 0xffff;
541
542 hred = *red++;
543 hgreen = *green++;
544 hblue = *blue++;
545
546 if (transp)
547 htransp = *transp++;
548
Dave Airliec850cb72009-10-23 18:49:03 +1000549 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
550 if (rc)
551 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000552 }
553 crtc_funcs->load_lut(crtc);
554 }
555 return rc;
556}
557EXPORT_SYMBOL(drm_fb_helper_setcmap);
558
Dave Airlie785b93e2009-08-28 15:46:53 +1000559int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
560 struct fb_info *info)
561{
562 struct drm_fb_helper *fb_helper = info->par;
563 struct drm_framebuffer *fb = fb_helper->fb;
564 int depth;
565
Jason Wesself90ebd92010-08-05 09:22:32 -0500566 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 return -EINVAL;
568
569 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100570 if (var->bits_per_pixel > fb->bits_per_pixel ||
571 var->xres > fb->width || var->yres > fb->height ||
572 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000573 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100574 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
575 var->xres, var->yres, var->bits_per_pixel,
576 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000577 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000578 return -EINVAL;
579 }
580
581 switch (var->bits_per_pixel) {
582 case 16:
583 depth = (var->green.length == 6) ? 16 : 15;
584 break;
585 case 32:
586 depth = (var->transp.length > 0) ? 32 : 24;
587 break;
588 default:
589 depth = var->bits_per_pixel;
590 break;
591 }
592
593 switch (depth) {
594 case 8:
595 var->red.offset = 0;
596 var->green.offset = 0;
597 var->blue.offset = 0;
598 var->red.length = 8;
599 var->green.length = 8;
600 var->blue.length = 8;
601 var->transp.length = 0;
602 var->transp.offset = 0;
603 break;
604 case 15:
605 var->red.offset = 10;
606 var->green.offset = 5;
607 var->blue.offset = 0;
608 var->red.length = 5;
609 var->green.length = 5;
610 var->blue.length = 5;
611 var->transp.length = 1;
612 var->transp.offset = 15;
613 break;
614 case 16:
615 var->red.offset = 11;
616 var->green.offset = 5;
617 var->blue.offset = 0;
618 var->red.length = 5;
619 var->green.length = 6;
620 var->blue.length = 5;
621 var->transp.length = 0;
622 var->transp.offset = 0;
623 break;
624 case 24:
625 var->red.offset = 16;
626 var->green.offset = 8;
627 var->blue.offset = 0;
628 var->red.length = 8;
629 var->green.length = 8;
630 var->blue.length = 8;
631 var->transp.length = 0;
632 var->transp.offset = 0;
633 break;
634 case 32:
635 var->red.offset = 16;
636 var->green.offset = 8;
637 var->blue.offset = 0;
638 var->red.length = 8;
639 var->green.length = 8;
640 var->blue.length = 8;
641 var->transp.length = 8;
642 var->transp.offset = 24;
643 break;
644 default:
645 return -EINVAL;
646 }
647 return 0;
648}
649EXPORT_SYMBOL(drm_fb_helper_check_var);
650
651/* this will let fbcon do the mode init */
652int drm_fb_helper_set_par(struct fb_info *info)
653{
654 struct drm_fb_helper *fb_helper = info->par;
655 struct drm_device *dev = fb_helper->dev;
656 struct fb_var_screeninfo *var = &info->var;
657 struct drm_crtc *crtc;
658 int ret;
659 int i;
660
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100661 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000662 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 return -EINVAL;
664 }
665
Dave Airlie8be48d92010-03-30 05:34:14 +0000666 mutex_lock(&dev->mode_config.mutex);
667 for (i = 0; i < fb_helper->crtc_count; i++) {
668 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000669 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
670 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000672 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000673 }
674 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000675 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000676
677 if (fb_helper->delayed_hotplug) {
678 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000679 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000680 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000681 return 0;
682}
683EXPORT_SYMBOL(drm_fb_helper_set_par);
684
685int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
686 struct fb_info *info)
687{
688 struct drm_fb_helper *fb_helper = info->par;
689 struct drm_device *dev = fb_helper->dev;
690 struct drm_mode_set *modeset;
691 struct drm_crtc *crtc;
692 int ret = 0;
693 int i;
694
Dave Airlie8be48d92010-03-30 05:34:14 +0000695 mutex_lock(&dev->mode_config.mutex);
696 for (i = 0; i < fb_helper->crtc_count; i++) {
697 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000698
699 modeset = &fb_helper->crtc_info[i].mode_set;
700
701 modeset->x = var->xoffset;
702 modeset->y = var->yoffset;
703
704 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000705 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000706 if (!ret) {
707 info->var.xoffset = var->xoffset;
708 info->var.yoffset = var->yoffset;
709 }
710 }
711 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000712 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000713 return ret;
714}
715EXPORT_SYMBOL(drm_fb_helper_pan_display);
716
Dave Airlie8be48d92010-03-30 05:34:14 +0000717int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
718 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000719{
Dave Airlie785b93e2009-08-28 15:46:53 +1000720 int new_fb = 0;
721 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000722 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000723 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000724 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000725 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000726
727 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
728 sizes.surface_depth = 24;
729 sizes.surface_bpp = 32;
730 sizes.fb_width = (unsigned)-1;
731 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000732
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000733 /* if driver picks 8 or 16 by default use that
734 for both depth/bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000735 if (preferred_bpp != sizes.surface_bpp) {
736 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000737 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000738 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000739 for (i = 0; i < fb_helper->connector_count; i++) {
740 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100741 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000742
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000743 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000744
745 if (cmdline_mode->bpp_specified) {
746 switch (cmdline_mode->bpp) {
747 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000748 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000749 break;
750 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000751 sizes.surface_depth = 15;
752 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000753 break;
754 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000755 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000756 break;
757 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000758 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000759 break;
760 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000761 sizes.surface_depth = 24;
762 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000763 break;
764 }
765 break;
766 }
767 }
768
Dave Airlie8be48d92010-03-30 05:34:14 +0000769 crtc_count = 0;
770 for (i = 0; i < fb_helper->crtc_count; i++) {
771 struct drm_display_mode *desired_mode;
772 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000773
Dave Airlie8be48d92010-03-30 05:34:14 +0000774 if (desired_mode) {
775 if (gamma_size == 0)
776 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
777 if (desired_mode->hdisplay < sizes.fb_width)
778 sizes.fb_width = desired_mode->hdisplay;
779 if (desired_mode->vdisplay < sizes.fb_height)
780 sizes.fb_height = desired_mode->vdisplay;
781 if (desired_mode->hdisplay > sizes.surface_width)
782 sizes.surface_width = desired_mode->hdisplay;
783 if (desired_mode->vdisplay > sizes.surface_height)
784 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000785 crtc_count++;
786 }
787 }
788
Dave Airlie38651672010-03-30 05:34:13 +0000789 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000790 /* hmm everyone went away - assume VGA cable just fell out
791 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000792 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000793 sizes.fb_width = sizes.surface_width = 1024;
794 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000795 }
796
Dave Airlie38651672010-03-30 05:34:13 +0000797 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000798 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000799 if (new_fb < 0)
800 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000801
Dave Airlie38651672010-03-30 05:34:13 +0000802 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000803
Dave Airlie8be48d92010-03-30 05:34:14 +0000804 /* set the fb pointer */
805 for (i = 0; i < fb_helper->crtc_count; i++) {
806 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000807 }
808
Dave Airlie785b93e2009-08-28 15:46:53 +1000809 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100810 info->var.pixclock = 0;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100811 if (register_framebuffer(info) < 0) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000812 return -EINVAL;
Clemens Ladisch3bea21b2009-11-04 09:43:00 +0100813 }
Dave Airlie38651672010-03-30 05:34:13 +0000814
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000815 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
816 info->node, info->fix.id);
Dave Airlie38651672010-03-30 05:34:13 +0000817
Dave Airlie785b93e2009-08-28 15:46:53 +1000818 } else {
819 drm_fb_helper_set_par(info);
820 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000821
822 /* Switch back to kernel console on panic */
823 /* multi card linked list maybe */
824 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000825 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000826 atomic_notifier_chain_register(&panic_notifier_list,
827 &paniced);
828 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
829 }
Dave Airlie38651672010-03-30 05:34:13 +0000830 if (new_fb)
831 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
832
Dave Airlie785b93e2009-08-28 15:46:53 +1000833 return 0;
834}
835EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
836
Dave Airlie3632ef82011-01-15 09:27:00 +1000837void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
838 uint32_t depth)
839{
840 info->fix.type = FB_TYPE_PACKED_PIXELS;
841 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
842 FB_VISUAL_TRUECOLOR;
843 info->fix.mmio_start = 0;
844 info->fix.mmio_len = 0;
845 info->fix.type_aux = 0;
846 info->fix.xpanstep = 1; /* doing it in hw */
847 info->fix.ypanstep = 1; /* doing it in hw */
848 info->fix.ywrapstep = 0;
849 info->fix.accel = FB_ACCEL_NONE;
850 info->fix.type_aux = 0;
851
852 info->fix.line_length = pitch;
853 return;
854}
855EXPORT_SYMBOL(drm_fb_helper_fill_fix);
856
Dave Airlie38651672010-03-30 05:34:13 +0000857void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000858 uint32_t fb_width, uint32_t fb_height)
859{
Dave Airlie38651672010-03-30 05:34:13 +0000860 struct drm_framebuffer *fb = fb_helper->fb;
861 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000862 info->var.xres_virtual = fb->width;
863 info->var.yres_virtual = fb->height;
864 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000865 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000866 info->var.xoffset = 0;
867 info->var.yoffset = 0;
868 info->var.activate = FB_ACTIVATE_NOW;
869 info->var.height = -1;
870 info->var.width = -1;
871
872 switch (fb->depth) {
873 case 8:
874 info->var.red.offset = 0;
875 info->var.green.offset = 0;
876 info->var.blue.offset = 0;
877 info->var.red.length = 8; /* 8bit DAC */
878 info->var.green.length = 8;
879 info->var.blue.length = 8;
880 info->var.transp.offset = 0;
881 info->var.transp.length = 0;
882 break;
883 case 15:
884 info->var.red.offset = 10;
885 info->var.green.offset = 5;
886 info->var.blue.offset = 0;
887 info->var.red.length = 5;
888 info->var.green.length = 5;
889 info->var.blue.length = 5;
890 info->var.transp.offset = 15;
891 info->var.transp.length = 1;
892 break;
893 case 16:
894 info->var.red.offset = 11;
895 info->var.green.offset = 5;
896 info->var.blue.offset = 0;
897 info->var.red.length = 5;
898 info->var.green.length = 6;
899 info->var.blue.length = 5;
900 info->var.transp.offset = 0;
901 break;
902 case 24:
903 info->var.red.offset = 16;
904 info->var.green.offset = 8;
905 info->var.blue.offset = 0;
906 info->var.red.length = 8;
907 info->var.green.length = 8;
908 info->var.blue.length = 8;
909 info->var.transp.offset = 0;
910 info->var.transp.length = 0;
911 break;
912 case 32:
913 info->var.red.offset = 16;
914 info->var.green.offset = 8;
915 info->var.blue.offset = 0;
916 info->var.red.length = 8;
917 info->var.green.length = 8;
918 info->var.blue.length = 8;
919 info->var.transp.offset = 24;
920 info->var.transp.length = 8;
921 break;
922 default:
923 break;
924 }
925
926 info->var.xres = fb_width;
927 info->var.yres = fb_height;
928}
929EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000930
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000931static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
932 uint32_t maxX,
933 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000934{
935 struct drm_connector *connector;
936 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000937 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000938
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000939 for (i = 0; i < fb_helper->connector_count; i++) {
940 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000941 count += connector->funcs->fill_modes(connector, maxX, maxY);
942 }
943
944 return count;
945}
946
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000947static 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 +0000948{
949 struct drm_display_mode *mode;
950
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000951 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000952 if (drm_mode_width(mode) > width ||
953 drm_mode_height(mode) > height)
954 continue;
955 if (mode->type & DRM_MODE_TYPE_PREFERRED)
956 return mode;
957 }
958 return NULL;
959}
960
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000961static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000962{
Chris Wilson1794d252011-04-17 07:43:32 +0100963 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000964 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000965 return cmdline_mode->specified;
966}
967
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000968static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
969 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +0000970{
Chris Wilson1794d252011-04-17 07:43:32 +0100971 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000972 struct drm_display_mode *mode = NULL;
973
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000974 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000975 if (cmdline_mode->specified == false)
976 return mode;
977
978 /* attempt to find a matching mode in the list of modes
979 * we have gotten so far, if not add a CVT mode that conforms
980 */
981 if (cmdline_mode->rb || cmdline_mode->margins)
982 goto create_mode;
983
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000984 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000985 /* check width/height */
986 if (mode->hdisplay != cmdline_mode->xres ||
987 mode->vdisplay != cmdline_mode->yres)
988 continue;
989
990 if (cmdline_mode->refresh_specified) {
991 if (mode->vrefresh != cmdline_mode->refresh)
992 continue;
993 }
994
995 if (cmdline_mode->interlace) {
996 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
997 continue;
998 }
999 return mode;
1000 }
1001
1002create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001003 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1004 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001005 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001006 return mode;
1007}
1008
1009static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1010{
1011 bool enable;
1012
1013 if (strict) {
1014 enable = connector->status == connector_status_connected;
1015 } else {
1016 enable = connector->status != connector_status_disconnected;
1017 }
1018 return enable;
1019}
1020
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001021static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1022 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001023{
1024 bool any_enabled = false;
1025 struct drm_connector *connector;
1026 int i = 0;
1027
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001028 for (i = 0; i < fb_helper->connector_count; i++) {
1029 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001030 enabled[i] = drm_connector_enabled(connector, true);
1031 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1032 enabled[i] ? "yes" : "no");
1033 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001034 }
1035
1036 if (any_enabled)
1037 return;
1038
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001039 for (i = 0; i < fb_helper->connector_count; i++) {
1040 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001041 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001042 }
1043}
1044
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001045static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1046 struct drm_display_mode **modes,
1047 bool *enabled, int width, int height)
1048{
1049 int count, i, j;
1050 bool can_clone = false;
1051 struct drm_fb_helper_connector *fb_helper_conn;
1052 struct drm_display_mode *dmt_mode, *mode;
1053
1054 /* only contemplate cloning in the single crtc case */
1055 if (fb_helper->crtc_count > 1)
1056 return false;
1057
1058 count = 0;
1059 for (i = 0; i < fb_helper->connector_count; i++) {
1060 if (enabled[i])
1061 count++;
1062 }
1063
1064 /* only contemplate cloning if more than one connector is enabled */
1065 if (count <= 1)
1066 return false;
1067
1068 /* check the command line or if nothing common pick 1024x768 */
1069 can_clone = true;
1070 for (i = 0; i < fb_helper->connector_count; i++) {
1071 if (!enabled[i])
1072 continue;
1073 fb_helper_conn = fb_helper->connector_info[i];
1074 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1075 if (!modes[i]) {
1076 can_clone = false;
1077 break;
1078 }
1079 for (j = 0; j < i; j++) {
1080 if (!enabled[j])
1081 continue;
1082 if (!drm_mode_equal(modes[j], modes[i]))
1083 can_clone = false;
1084 }
1085 }
1086
1087 if (can_clone) {
1088 DRM_DEBUG_KMS("can clone using command line\n");
1089 return true;
1090 }
1091
1092 /* try and find a 1024x768 mode on each connector */
1093 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001094 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001095
1096 for (i = 0; i < fb_helper->connector_count; i++) {
1097
1098 if (!enabled[i])
1099 continue;
1100
1101 fb_helper_conn = fb_helper->connector_info[i];
1102 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1103 if (drm_mode_equal(mode, dmt_mode))
1104 modes[i] = mode;
1105 }
1106 if (!modes[i])
1107 can_clone = false;
1108 }
1109
1110 if (can_clone) {
1111 DRM_DEBUG_KMS("can clone using 1024x768\n");
1112 return true;
1113 }
1114 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1115 return false;
1116}
1117
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001118static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001119 struct drm_display_mode **modes,
1120 bool *enabled, int width, int height)
1121{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001122 struct drm_fb_helper_connector *fb_helper_conn;
1123 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001124
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001125 for (i = 0; i < fb_helper->connector_count; i++) {
1126 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001127
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001128 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001129 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001130
1131 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001132 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001133
1134 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001135 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001136 if (!modes[i]) {
1137 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001138 fb_helper_conn->connector->base.id);
1139 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001140 }
1141 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001142 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1143 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001144 break;
1145 }
1146 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1147 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001148 }
1149 return true;
1150}
1151
Dave Airlie8be48d92010-03-30 05:34:14 +00001152static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1153 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001154 struct drm_display_mode **modes,
1155 int n, int width, int height)
1156{
1157 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001158 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001159 struct drm_connector *connector;
1160 struct drm_connector_helper_funcs *connector_funcs;
1161 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001162 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001163 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001164 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001165 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001166
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001168 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001169
1170 fb_helper_conn = fb_helper->connector_info[n];
1171 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001172
1173 best_crtcs[n] = NULL;
1174 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001175 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001176 if (modes[n] == NULL)
1177 return best_score;
1178
Dave Airlie8be48d92010-03-30 05:34:14 +00001179 crtcs = kzalloc(dev->mode_config.num_connector *
1180 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001181 if (!crtcs)
1182 return best_score;
1183
1184 my_score = 1;
1185 if (connector->status == connector_status_connected)
1186 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001187 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001188 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001189 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001190 my_score++;
1191
1192 connector_funcs = connector->helper_private;
1193 encoder = connector_funcs->best_encoder(connector);
1194 if (!encoder)
1195 goto out;
1196
Dave Airlie38651672010-03-30 05:34:13 +00001197 /* select a crtc for this connector and then attempt to configure
1198 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001199 for (c = 0; c < fb_helper->crtc_count; c++) {
1200 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001201
1202 if ((encoder->possible_crtcs & (1 << c)) == 0) {
Dave Airlie38651672010-03-30 05:34:13 +00001203 continue;
1204 }
1205
1206 for (o = 0; o < n; o++)
1207 if (best_crtcs[o] == crtc)
1208 break;
1209
1210 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001211 /* ignore cloning unless only a single crtc */
1212 if (fb_helper->crtc_count > 1)
1213 continue;
1214
1215 if (!drm_mode_equal(modes[o], modes[n]))
1216 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001217 }
1218
1219 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001220 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1221 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001222 width, height);
1223 if (score > best_score) {
1224 best_crtc = crtc;
1225 best_score = score;
1226 memcpy(best_crtcs, crtcs,
1227 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001228 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001229 }
Dave Airlie38651672010-03-30 05:34:13 +00001230 }
1231out:
1232 kfree(crtcs);
1233 return best_score;
1234}
1235
Dave Airlie8be48d92010-03-30 05:34:14 +00001236static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001237{
Dave Airlie8be48d92010-03-30 05:34:14 +00001238 struct drm_device *dev = fb_helper->dev;
1239 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001240 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001241 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001242 bool *enabled;
1243 int width, height;
1244 int i, ret;
1245
1246 DRM_DEBUG_KMS("\n");
1247
1248 width = dev->mode_config.max_width;
1249 height = dev->mode_config.max_height;
1250
Dave Airlie38651672010-03-30 05:34:13 +00001251 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001252 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001253 modes = kcalloc(dev->mode_config.num_connector,
1254 sizeof(struct drm_display_mode *), GFP_KERNEL);
1255 enabled = kcalloc(dev->mode_config.num_connector,
1256 sizeof(bool), GFP_KERNEL);
1257
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001258 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001259
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001260 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1261 if (!ret) {
1262 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1263 if (!ret)
1264 DRM_ERROR("Unable to find initial modes\n");
1265 }
Dave Airlie38651672010-03-30 05:34:13 +00001266
1267 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1268
Dave Airlie8be48d92010-03-30 05:34:14 +00001269 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1270
1271 /* need to set the modesets up here for use later */
1272 /* fill out the connector<->crtc mappings into the modesets */
1273 for (i = 0; i < fb_helper->crtc_count; i++) {
1274 modeset = &fb_helper->crtc_info[i].mode_set;
1275 modeset->num_connectors = 0;
1276 }
Dave Airlie38651672010-03-30 05:34:13 +00001277
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001278 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001279 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001280 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1281 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001282
Dave Airlie8be48d92010-03-30 05:34:14 +00001283 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001284 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001285 mode->name, fb_crtc->mode_set.crtc->base.id);
1286 fb_crtc->desired_mode = mode;
1287 if (modeset->mode)
1288 drm_mode_destroy(dev, modeset->mode);
1289 modeset->mode = drm_mode_duplicate(dev,
1290 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001291 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001292 }
Dave Airlie38651672010-03-30 05:34:13 +00001293 }
1294
1295 kfree(crtcs);
1296 kfree(modes);
1297 kfree(enabled);
1298}
1299
1300/**
1301 * drm_helper_initial_config - setup a sane initial connector configuration
1302 * @dev: DRM device
1303 *
1304 * LOCKING:
1305 * Called at init time, must take mode config lock.
1306 *
1307 * Scan the CRTCs and connectors and try to put together an initial setup.
1308 * At the moment, this is a cloned configuration across all heads with
1309 * a new framebuffer object as the backing store.
1310 *
1311 * RETURNS:
1312 * Zero if everything went ok, nonzero otherwise.
1313 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001314bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001315{
Dave Airlie8be48d92010-03-30 05:34:14 +00001316 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001317 int count = 0;
1318
1319 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001320 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001321
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001322 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001323
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001324 count = drm_fb_helper_probe_connector_modes(fb_helper,
1325 dev->mode_config.max_width,
1326 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001327 /*
1328 * we shouldn't end up with no modes here.
1329 */
Dave Airlie5c4426a2010-03-30 05:34:17 +00001330 if (count == 0) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001331 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Dave Airlie5c4426a2010-03-30 05:34:17 +00001332 }
Dave Airlie8be48d92010-03-30 05:34:14 +00001333 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001334
Dave Airlie4abe3522010-03-30 05:34:18 +00001335 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001336}
Dave Airlie8be48d92010-03-30 05:34:14 +00001337EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001338
Chris Wilson73943712011-04-22 11:03:57 +01001339/**
1340 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1341 * probing all the outputs attached to the fb.
1342 * @fb_helper: the drm_fb_helper
1343 *
1344 * LOCKING:
1345 * Called at runtime, must take mode config lock.
1346 *
1347 * Scan the connectors attached to the fb_helper and try to put together a
1348 * setup after *notification of a change in output configuration.
1349 *
1350 * RETURNS:
1351 * 0 on success and a non-zero error code otherwise.
1352 */
1353int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001354{
Chris Wilson73943712011-04-22 11:03:57 +01001355 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001356 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001357 u32 max_width, max_height, bpp_sel;
Daniel Vetter343065a2012-06-15 11:01:22 +02001358 int bound = 0, crtcs_bound = 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001359 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001360
1361 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001362 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001363
Chris Wilson73943712011-04-22 11:03:57 +01001364 mutex_lock(&dev->mode_config.mutex);
1365 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001366 if (crtc->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001367 crtcs_bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001368 if (crtc->fb == fb_helper->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001369 bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001370 }
1371
Daniel Vetter343065a2012-06-15 11:01:22 +02001372 if (bound < crtcs_bound) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001373 fb_helper->delayed_hotplug = true;
Chris Wilson73943712011-04-22 11:03:57 +01001374 mutex_unlock(&dev->mode_config.mutex);
1375 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001376 }
Dave Airlie38651672010-03-30 05:34:13 +00001377 DRM_DEBUG_KMS("\n");
1378
Dave Airlie4abe3522010-03-30 05:34:18 +00001379 max_width = fb_helper->fb->width;
1380 max_height = fb_helper->fb->height;
1381 bpp_sel = fb_helper->fb->bits_per_pixel;
1382
Dave Airlie5c4426a2010-03-30 05:34:17 +00001383 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001384 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001385 drm_setup_crtcs(fb_helper);
Chris Wilson73943712011-04-22 11:03:57 +01001386 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001387
Dave Airlie4abe3522010-03-30 05:34:18 +00001388 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001389}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001390EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001391
David Rientjes6a108a12011-01-20 14:44:16 -08001392/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001393 * but the module doesn't depend on any fb console symbols. At least
1394 * attempt to load fbcon to avoid leaving the system without a usable console.
1395 */
David Rientjes6a108a12011-01-20 14:44:16 -08001396#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001397static int __init drm_fb_helper_modinit(void)
1398{
1399 const char *name = "fbcon";
1400 struct module *fbcon;
1401
1402 mutex_lock(&module_mutex);
1403 fbcon = find_module(name);
1404 mutex_unlock(&module_mutex);
1405
1406 if (!fbcon)
1407 request_module_nowait(name);
1408 return 0;
1409}
1410
1411module_init(drm_fb_helper_modinit);
1412#endif