blob: 3144db9dc0f1f0bf3b0ea7ca8d42665fb47a37f6 [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
42static LIST_HEAD(kernel_fb_helper_list);
43
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010044/**
45 * DOC: fbdev helpers
46 *
47 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
Thierry Reding83c617c2014-04-29 11:44:35 +020048 * mode setting driver. They can be used mostly independently from the crtc
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010049 * helper functions used by many drivers to implement the kernel mode setting
50 * interfaces.
Daniel Vetter207fd322013-01-20 22:13:14 +010051 *
Thierry Reding10a23102014-06-27 17:19:24 +020052 * Initialization is done as a four-step process with drm_fb_helper_prepare(),
53 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
54 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
55 * default behaviour can override the third step with their own code.
56 * Teardown is done with drm_fb_helper_fini().
Daniel Vetter207fd322013-01-20 22:13:14 +010057 *
58 * At runtime drivers should restore the fbdev console by calling
59 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
60 * should also notify the fb helper code from updates to the output
61 * configuration by calling drm_fb_helper_hotplug_event(). For easier
62 * integration with the output polling code in drm_crtc_helper.c the modeset
Thierry Reding83c617c2014-04-29 11:44:35 +020063 * code provides a ->output_poll_changed callback.
Daniel Vetter207fd322013-01-20 22:13:14 +010064 *
65 * All other functions exported by the fb helper library can be used to
66 * implement the fbdev driver interface by the driver.
Thierry Reding10a23102014-06-27 17:19:24 +020067 *
68 * It is possible, though perhaps somewhat tricky, to implement race-free
69 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
70 * helper must be called first to initialize the minimum required to make
71 * hotplug detection work. Drivers also need to make sure to properly set up
72 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
73 * it is safe to enable interrupts and start processing hotplug events. At the
74 * same time, drivers should initialize all modeset objects such as CRTCs,
75 * encoders and connectors. To finish up the fbdev helper initialization, the
76 * drm_fb_helper_init() function is called. To probe for all attached displays
77 * and set up an initial configuration using the detected hardware, drivers
78 * should call drm_fb_helper_single_add_all_connectors() followed by
79 * drm_fb_helper_initial_config().
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010080 */
81
Daniel Vetter207fd322013-01-20 22:13:14 +010082/**
83 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
84 * emulation helper
85 * @fb_helper: fbdev initialized with drm_fb_helper_init
86 *
87 * This functions adds all the available connectors for use with the given
88 * fb_helper. This is a separate step to allow drivers to freely assign
89 * connectors to the fbdev, e.g. if some are reserved for special purposes or
90 * not adequate to be used for the fbcon.
91 *
92 * Since this is part of the initial setup before the fbdev is published, no
93 * locking is required.
94 */
Dave Airlie0b4c0f32010-03-30 05:34:15 +000095int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100096{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000097 struct drm_device *dev = fb_helper->dev;
98 struct drm_connector *connector;
99 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000100
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000101 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
102 struct drm_fb_helper_connector *fb_helper_connector;
103
104 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
105 if (!fb_helper_connector)
106 goto fail;
107
108 fb_helper_connector->connector = connector;
109 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
110 }
Dave Airlied50ba252009-09-23 14:44:08 +1000111 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000112fail:
113 for (i = 0; i < fb_helper->connector_count; i++) {
114 kfree(fb_helper->connector_info[i]);
115 fb_helper->connector_info[i] = NULL;
116 }
117 fb_helper->connector_count = 0;
118 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +1000119}
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000120EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +1000121
Dave Airlie65c2a892014-06-05 14:01:30 +1000122int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
123{
124 struct drm_fb_helper_connector **temp;
125 struct drm_fb_helper_connector *fb_helper_connector;
126
127 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
128 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
129 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector) * (fb_helper->connector_count + 1), GFP_KERNEL);
130 if (!temp)
131 return -ENOMEM;
132
133 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
134 fb_helper->connector_info = temp;
135 }
136
137
138 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
139 if (!fb_helper_connector)
140 return -ENOMEM;
141
142 fb_helper_connector->connector = connector;
143 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
144 return 0;
145}
146EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
147
148int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
149 struct drm_connector *connector)
150{
151 struct drm_fb_helper_connector *fb_helper_connector;
152 int i, j;
153
154 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
155
156 for (i = 0; i < fb_helper->connector_count; i++) {
157 if (fb_helper->connector_info[i]->connector == connector)
158 break;
159 }
160
161 if (i == fb_helper->connector_count)
162 return -EINVAL;
163 fb_helper_connector = fb_helper->connector_info[i];
164
165 for (j = i + 1; j < fb_helper->connector_count; j++) {
166 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
167 }
168 fb_helper->connector_count--;
169 kfree(fb_helper_connector);
170 return 0;
171}
172EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
173
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000174static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +1000175{
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000176 struct drm_fb_helper_connector *fb_helper_conn;
177 int i;
Dave Airlied50ba252009-09-23 14:44:08 +1000178
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000179 for (i = 0; i < fb_helper->connector_count; i++) {
Chris Wilson1794d252011-04-17 07:43:32 +0100180 struct drm_cmdline_mode *mode;
181 struct drm_connector *connector;
Dave Airlied50ba252009-09-23 14:44:08 +1000182 char *option = NULL;
183
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000184 fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100185 connector = fb_helper_conn->connector;
186 mode = &fb_helper_conn->cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000187
Dave Airlied50ba252009-09-23 14:44:08 +1000188 /* do something on return - turn off connector maybe */
Jani Nikula25933822014-06-03 14:56:20 +0300189 if (fb_get_options(connector->name, &option))
Dave Airlied50ba252009-09-23 14:44:08 +1000190 continue;
191
Chris Wilson1794d252011-04-17 07:43:32 +0100192 if (drm_mode_parse_command_line_for_connector(option,
193 connector,
194 mode)) {
195 if (mode->force) {
196 const char *s;
197 switch (mode->force) {
Sachin Kamat6c910832012-11-15 03:43:28 +0000198 case DRM_FORCE_OFF:
199 s = "OFF";
200 break;
201 case DRM_FORCE_ON_DIGITAL:
202 s = "ON - dig";
203 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100204 default:
Sachin Kamat6c910832012-11-15 03:43:28 +0000205 case DRM_FORCE_ON:
206 s = "ON";
207 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100208 }
209
210 DRM_INFO("forcing %s connector %s\n",
Jani Nikula25933822014-06-03 14:56:20 +0300211 connector->name, s);
Chris Wilson1794d252011-04-17 07:43:32 +0100212 connector->force = mode->force;
213 }
214
215 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
Jani Nikula25933822014-06-03 14:56:20 +0300216 connector->name,
Chris Wilson1794d252011-04-17 07:43:32 +0100217 mode->xres, mode->yres,
218 mode->refresh_specified ? mode->refresh : 60,
219 mode->rb ? " reduced blanking" : "",
220 mode->margins ? " with margins" : "",
221 mode->interlace ? " interlaced" : "");
222 }
223
Dave Airlied50ba252009-09-23 14:44:08 +1000224 }
225 return 0;
226}
227
Jason Wessel99231022010-10-13 14:09:43 -0500228static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
229{
230 uint16_t *r_base, *g_base, *b_base;
231 int i;
232
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300233 if (helper->funcs->gamma_get == NULL)
234 return;
235
Jason Wessel99231022010-10-13 14:09:43 -0500236 r_base = crtc->gamma_store;
237 g_base = r_base + crtc->gamma_size;
238 b_base = g_base + crtc->gamma_size;
239
240 for (i = 0; i < crtc->gamma_size; i++)
241 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
242}
243
244static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
245{
246 uint16_t *r_base, *g_base, *b_base;
247
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200248 if (crtc->funcs->gamma_set == NULL)
249 return;
250
Jason Wessel99231022010-10-13 14:09:43 -0500251 r_base = crtc->gamma_store;
252 g_base = r_base + crtc->gamma_size;
253 b_base = g_base + crtc->gamma_size;
254
255 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
256}
257
Daniel Vetter207fd322013-01-20 22:13:14 +0100258/**
259 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
260 * @info: fbdev registered by the helper
261 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500262int drm_fb_helper_debug_enter(struct fb_info *info)
263{
264 struct drm_fb_helper *helper = info->par;
265 struct drm_crtc_helper_funcs *funcs;
266 int i;
267
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500268 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
269 for (i = 0; i < helper->crtc_count; i++) {
270 struct drm_mode_set *mode_set =
271 &helper->crtc_info[i].mode_set;
272
273 if (!mode_set->crtc->enabled)
274 continue;
275
276 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500277 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500278 funcs->mode_set_base_atomic(mode_set->crtc,
279 mode_set->fb,
280 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500281 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500282 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500283 }
284 }
285
286 return 0;
287}
288EXPORT_SYMBOL(drm_fb_helper_debug_enter);
289
290/* Find the real fb for a given fb helper CRTC */
291static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
292{
293 struct drm_device *dev = crtc->dev;
294 struct drm_crtc *c;
295
296 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
297 if (crtc->base.id == c->base.id)
Matt Roperf4510a22014-04-01 15:22:40 -0700298 return c->primary->fb;
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500299 }
300
301 return NULL;
302}
303
Daniel Vetter207fd322013-01-20 22:13:14 +0100304/**
305 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
306 * @info: fbdev registered by the helper
307 */
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500308int drm_fb_helper_debug_leave(struct fb_info *info)
309{
310 struct drm_fb_helper *helper = info->par;
311 struct drm_crtc *crtc;
312 struct drm_crtc_helper_funcs *funcs;
313 struct drm_framebuffer *fb;
314 int i;
315
316 for (i = 0; i < helper->crtc_count; i++) {
317 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
318 crtc = mode_set->crtc;
319 funcs = crtc->helper_private;
320 fb = drm_mode_config_fb(crtc);
321
322 if (!crtc->enabled)
323 continue;
324
325 if (!fb) {
326 DRM_ERROR("no fb to restore??\n");
327 continue;
328 }
329
Jason Wessel99231022010-10-13 14:09:43 -0500330 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500331 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500332 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500333 }
334
335 return 0;
336}
337EXPORT_SYMBOL(drm_fb_helper_debug_leave);
338
Rob Clark5ea1f752014-05-30 12:29:48 -0400339static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100340{
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300341 struct drm_device *dev = fb_helper->dev;
342 struct drm_plane *plane;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100343 bool error = false;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300344 int i;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100345
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300346 drm_warn_on_modeset_not_all_locked(dev);
347
348 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
Matt Ropere27dde32014-04-01 15:22:30 -0700349 if (plane->type != DRM_PLANE_TYPE_PRIMARY)
350 drm_plane_force_disable(plane);
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100351
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100352 for (i = 0; i < fb_helper->crtc_count; i++) {
353 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Ville Syrjälä3858bc52013-06-03 16:10:42 +0300354 struct drm_crtc *crtc = mode_set->crtc;
355 int ret;
356
357 if (crtc->funcs->cursor_set) {
358 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
359 if (ret)
360 error = true;
361 }
362
Daniel Vetter2d13b672012-12-11 13:47:23 +0100363 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100364 if (ret)
365 error = true;
366 }
367 return error;
368}
Rob Clark5ea1f752014-05-30 12:29:48 -0400369/**
370 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
371 * @fb_helper: fbcon to restore
372 *
373 * This should be called from driver's drm ->lastclose callback
374 * when implementing an fbcon on top of kms using this helper. This ensures that
375 * the user isn't greeted with a black screen when e.g. X dies.
376 *
377 * Use this variant if you need to bypass locking (panic), or already
378 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked()
379 */
380static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
381{
382 return restore_fbdev_mode(fb_helper);
383}
384
385/**
386 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
387 * @fb_helper: fbcon to restore
388 *
389 * This should be called from driver's drm ->lastclose callback
390 * when implementing an fbcon on top of kms using this helper. This ensures that
391 * the user isn't greeted with a black screen when e.g. X dies.
392 */
393bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
394{
395 struct drm_device *dev = fb_helper->dev;
396 bool ret;
397 drm_modeset_lock_all(dev);
398 ret = restore_fbdev_mode(fb_helper);
399 drm_modeset_unlock_all(dev);
400 return ret;
401}
402EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100403
Daniel Vetterd21bf462013-01-20 18:09:52 +0100404/*
405 * restore fbcon display for all kms driver's using this helper, used for sysrq
406 * and panic handling.
407 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530408static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000409{
Dave Airlie785b93e2009-08-28 15:46:53 +1000410 bool ret, error = false;
411 struct drm_fb_helper *helper;
412
413 if (list_empty(&kernel_fb_helper_list))
414 return false;
415
416 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Thierry Redingb77f0762014-04-29 11:44:32 +0200417 struct drm_device *dev = helper->dev;
418
419 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100420 continue;
421
Rob Clark51fd3712013-11-19 12:10:12 -0500422 /* NOTE: we use lockless flag below to avoid grabbing other
423 * modeset locks. So just trylock the underlying mutex
424 * directly:
425 */
Thierry Redingb77f0762014-04-29 11:44:32 +0200426 if (!mutex_trylock(&dev->mode_config.mutex)) {
427 error = true;
428 continue;
429 }
430
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100431 ret = drm_fb_helper_restore_fbdev_mode(helper);
432 if (ret)
433 error = true;
Thierry Redingb77f0762014-04-29 11:44:32 +0200434
435 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000436 }
437 return error;
438}
439
Daniel Vetter43c8a842013-01-20 18:18:07 +0100440static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000441 void *panic_str)
442{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700443 /*
444 * It's a waste of time and effort to switch back to text console
445 * if the kernel should reboot before panic messages can be seen.
446 */
447 if (panic_timeout < 0)
448 return 0;
449
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000450 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000451 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000452}
Dave Airlie785b93e2009-08-28 15:46:53 +1000453
454static struct notifier_block paniced = {
455 .notifier_call = drm_fb_helper_panic,
456};
457
Daniel Vetter20c60c32012-12-17 12:13:23 +0100458static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
459{
460 struct drm_device *dev = fb_helper->dev;
461 struct drm_crtc *crtc;
462 int bound = 0, crtcs_bound = 0;
463
Paulo Zanoni520edd12013-11-27 18:24:08 -0200464 /* Sometimes user space wants everything disabled, so don't steal the
465 * display if there's a master. */
466 if (dev->primary->master)
467 return false;
468
Daniel Vetter20c60c32012-12-17 12:13:23 +0100469 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700470 if (crtc->primary->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100471 crtcs_bound++;
Matt Roperf4510a22014-04-01 15:22:40 -0700472 if (crtc->primary->fb == fb_helper->fb)
Daniel Vetter20c60c32012-12-17 12:13:23 +0100473 bound++;
474 }
475
476 if (bound < crtcs_bound)
477 return false;
Paulo Zanoni520edd12013-11-27 18:24:08 -0200478
Daniel Vetter20c60c32012-12-17 12:13:23 +0100479 return true;
480}
481
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200482#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000483static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
484{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100485 bool ret;
486 ret = drm_fb_helper_force_kernel_mode();
487 if (ret == true)
488 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000489}
490static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
491
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700492static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000493{
494 schedule_work(&drm_fb_helper_restore_work);
495}
496
497static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
498 .handler = drm_fb_helper_sysrq,
499 .help_msg = "force-fb(V)",
500 .action_msg = "Restore framebuffer console",
501};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000502#else
503static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200504#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000505
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100506static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000507{
508 struct drm_fb_helper *fb_helper = info->par;
509 struct drm_device *dev = fb_helper->dev;
510 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700511 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700512 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000513
514 /*
Daniel Vetter1b1d5392013-01-24 16:42:07 +0100515 * fbdev->blank can be called from irq context in case of a panic.
516 * Since we already have our own special panic handler which will
517 * restore the fbdev console mode completely, just bail out early.
518 */
519 if (oops_in_progress)
520 return;
521
522 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100523 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000524 */
Daniel Vetter84849902012-12-02 00:28:11 +0100525 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100526 if (!drm_fb_helper_is_bound(fb_helper)) {
527 drm_modeset_unlock_all(dev);
528 return;
529 }
530
Jesse Barnese87b2c42009-09-17 18:14:41 -0700531 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000532 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000533
Dave Airlie8be48d92010-03-30 05:34:14 +0000534 if (!crtc->enabled)
535 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000536
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100537 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700538 for (j = 0; j < fb_helper->connector_count; j++) {
539 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200540 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500541 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100542 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700543 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000544 }
Daniel Vetter84849902012-12-02 00:28:11 +0100545 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000546}
547
Daniel Vetter207fd322013-01-20 22:13:14 +0100548/**
549 * drm_fb_helper_blank - implementation for ->fb_blank
550 * @blank: desired blanking state
551 * @info: fbdev registered by the helper
552 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000553int drm_fb_helper_blank(int blank, struct fb_info *info)
554{
555 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000556 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000557 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100558 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000559 break;
James Simmons731b5a12009-10-29 20:39:07 +0000560 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000561 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100562 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000563 break;
James Simmons731b5a12009-10-29 20:39:07 +0000564 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000565 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100566 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 break;
James Simmons731b5a12009-10-29 20:39:07 +0000568 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000569 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100570 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000571 break;
James Simmons731b5a12009-10-29 20:39:07 +0000572 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000573 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100574 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000575 break;
576 }
577 return 0;
578}
579EXPORT_SYMBOL(drm_fb_helper_blank);
580
581static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
582{
583 int i;
584
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000585 for (i = 0; i < helper->connector_count; i++)
586 kfree(helper->connector_info[i]);
587 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100588 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000589 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100590 if (helper->crtc_info[i].mode_set.mode)
591 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
592 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000593 kfree(helper->crtc_info);
594}
595
Daniel Vetter207fd322013-01-20 22:13:14 +0100596/**
Thierry Reding10a23102014-06-27 17:19:24 +0200597 * drm_fb_helper_prepare - setup a drm_fb_helper structure
598 * @dev: DRM device
599 * @helper: driver-allocated fbdev helper structure to set up
600 * @funcs: pointer to structure of functions associate with this helper
601 *
602 * Sets up the bare minimum to make the framebuffer helper usable. This is
603 * useful to implement race-free initialization of the polling helpers.
604 */
605void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
606 const struct drm_fb_helper_funcs *funcs)
607{
608 INIT_LIST_HEAD(&helper->kernel_fb_list);
609 helper->funcs = funcs;
610 helper->dev = dev;
611}
612EXPORT_SYMBOL(drm_fb_helper_prepare);
613
614/**
Daniel Vetter207fd322013-01-20 22:13:14 +0100615 * drm_fb_helper_init - initialize a drm_fb_helper structure
616 * @dev: drm device
617 * @fb_helper: driver-allocated fbdev helper structure to initialize
618 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
619 * @max_conn_count: max connector count
620 *
621 * This allocates the structures for the fbdev helper with the given limits.
622 * Note that this won't yet touch the hardware (through the driver interfaces)
623 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
624 * to allow driver writes more control over the exact init sequence.
625 *
Thierry Reding10a23102014-06-27 17:19:24 +0200626 * Drivers must call drm_fb_helper_prepare() before calling this function.
Daniel Vetter207fd322013-01-20 22:13:14 +0100627 *
628 * RETURNS:
629 * Zero if everything went ok, nonzero otherwise.
630 */
Dave Airlie4abe3522010-03-30 05:34:18 +0000631int drm_fb_helper_init(struct drm_device *dev,
632 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000633 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000634{
Dave Airlie785b93e2009-08-28 15:46:53 +1000635 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000636 int i;
637
Xiubo Li04cfe972014-03-10 09:33:58 +0800638 if (!max_conn_count)
639 return -EINVAL;
640
Dave Airlie4abe3522010-03-30 05:34:18 +0000641 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
642 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000643 return -ENOMEM;
644
Dave Airlie4abe3522010-03-30 05:34:18 +0000645 fb_helper->crtc_count = crtc_count;
646 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
647 if (!fb_helper->connector_info) {
648 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000649 return -ENOMEM;
650 }
Dave Airlie65c2a892014-06-05 14:01:30 +1000651 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
Dave Airlie4abe3522010-03-30 05:34:18 +0000652 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000653
654 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000655 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000656 kcalloc(max_conn_count,
657 sizeof(struct drm_connector *),
658 GFP_KERNEL);
659
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200660 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000661 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000662 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 }
664
665 i = 0;
666 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000667 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000668 i++;
669 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100670
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 return 0;
672out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000673 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000674 return -ENOMEM;
675}
Dave Airlie4abe3522010-03-30 05:34:18 +0000676EXPORT_SYMBOL(drm_fb_helper_init);
677
678void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
679{
680 if (!list_empty(&fb_helper->kernel_fb_list)) {
681 list_del(&fb_helper->kernel_fb_list);
682 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000683 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000684 atomic_notifier_chain_unregister(&panic_notifier_list,
685 &paniced);
686 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
687 }
688 }
689
690 drm_fb_helper_crtc_free(fb_helper);
691
Dave Airlie4abe3522010-03-30 05:34:18 +0000692}
693EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000694
Dave Airliec850cb72009-10-23 18:49:03 +1000695static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000696 u16 blue, u16 regno, struct fb_info *info)
697{
698 struct drm_fb_helper *fb_helper = info->par;
699 struct drm_framebuffer *fb = fb_helper->fb;
700 int pindex;
701
Dave Airliec850cb72009-10-23 18:49:03 +1000702 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
703 u32 *palette;
704 u32 value;
705 /* place color in psuedopalette */
706 if (regno > 16)
707 return -EINVAL;
708 palette = (u32 *)info->pseudo_palette;
709 red >>= (16 - info->var.red.length);
710 green >>= (16 - info->var.green.length);
711 blue >>= (16 - info->var.blue.length);
712 value = (red << info->var.red.offset) |
713 (green << info->var.green.offset) |
714 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000715 if (info->var.transp.length > 0) {
716 u32 mask = (1 << info->var.transp.length) - 1;
717 mask <<= info->var.transp.offset;
718 value |= mask;
719 }
Dave Airliec850cb72009-10-23 18:49:03 +1000720 palette[regno] = value;
721 return 0;
722 }
723
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300724 /*
725 * The driver really shouldn't advertise pseudo/directcolor
726 * visuals if it can't deal with the palette.
727 */
728 if (WARN_ON(!fb_helper->funcs->gamma_set ||
729 !fb_helper->funcs->gamma_get))
730 return -EINVAL;
731
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000732 pindex = regno;
733
734 if (fb->bits_per_pixel == 16) {
735 pindex = regno << 3;
736
737 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000738 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000739 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000740 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000741
742 if (fb->depth == 16) {
743 u16 r, g, b;
744 int i;
745 if (regno < 32) {
746 for (i = 0; i < 8; i++)
747 fb_helper->funcs->gamma_set(crtc, red,
748 green, blue, pindex + i);
749 }
750
751 fb_helper->funcs->gamma_get(crtc, &r,
752 &g, &b,
753 pindex >> 1);
754
755 for (i = 0; i < 4; i++)
756 fb_helper->funcs->gamma_set(crtc, r,
757 green, b,
758 (pindex >> 1) + i);
759 }
760 }
761
762 if (fb->depth != 16)
763 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000764 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000765}
766
Daniel Vetter207fd322013-01-20 22:13:14 +0100767/**
768 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
769 * @cmap: cmap to set
770 * @info: fbdev registered by the helper
771 */
Dave Airlie068143d2009-10-05 09:58:02 +1000772int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
773{
774 struct drm_fb_helper *fb_helper = info->par;
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300775 struct drm_device *dev = fb_helper->dev;
Dave Airlie8be48d92010-03-30 05:34:14 +0000776 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000777 u16 *red, *green, *blue, *transp;
778 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100779 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000780 int start;
781
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300782 drm_modeset_lock_all(dev);
783 if (!drm_fb_helper_is_bound(fb_helper)) {
784 drm_modeset_unlock_all(dev);
785 return -EBUSY;
786 }
787
Dave Airlie8be48d92010-03-30 05:34:14 +0000788 for (i = 0; i < fb_helper->crtc_count; i++) {
789 crtc = fb_helper->crtc_info[i].mode_set.crtc;
790 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000791
792 red = cmap->red;
793 green = cmap->green;
794 blue = cmap->blue;
795 transp = cmap->transp;
796 start = cmap->start;
797
roel062ac622011-03-07 18:00:34 +0100798 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000799 u16 hred, hgreen, hblue, htransp = 0xffff;
800
801 hred = *red++;
802 hgreen = *green++;
803 hblue = *blue++;
804
805 if (transp)
806 htransp = *transp++;
807
Dave Airliec850cb72009-10-23 18:49:03 +1000808 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
809 if (rc)
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300810 goto out;
Dave Airlie068143d2009-10-05 09:58:02 +1000811 }
Ville Syrjälä04c0c562013-05-27 20:19:57 +0300812 if (crtc_funcs->load_lut)
813 crtc_funcs->load_lut(crtc);
Dave Airlie068143d2009-10-05 09:58:02 +1000814 }
Ville Syrjälä8391a3d2013-05-27 20:19:56 +0300815 out:
816 drm_modeset_unlock_all(dev);
Dave Airlie068143d2009-10-05 09:58:02 +1000817 return rc;
818}
819EXPORT_SYMBOL(drm_fb_helper_setcmap);
820
Daniel Vetter207fd322013-01-20 22:13:14 +0100821/**
822 * drm_fb_helper_check_var - implementation for ->fb_check_var
823 * @var: screeninfo to check
824 * @info: fbdev registered by the helper
825 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000826int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
827 struct fb_info *info)
828{
829 struct drm_fb_helper *fb_helper = info->par;
830 struct drm_framebuffer *fb = fb_helper->fb;
831 int depth;
832
Jason Wesself90ebd92010-08-05 09:22:32 -0500833 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000834 return -EINVAL;
835
836 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100837 if (var->bits_per_pixel > fb->bits_per_pixel ||
838 var->xres > fb->width || var->yres > fb->height ||
839 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000840 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100841 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
842 var->xres, var->yres, var->bits_per_pixel,
843 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000844 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000845 return -EINVAL;
846 }
847
848 switch (var->bits_per_pixel) {
849 case 16:
850 depth = (var->green.length == 6) ? 16 : 15;
851 break;
852 case 32:
853 depth = (var->transp.length > 0) ? 32 : 24;
854 break;
855 default:
856 depth = var->bits_per_pixel;
857 break;
858 }
859
860 switch (depth) {
861 case 8:
862 var->red.offset = 0;
863 var->green.offset = 0;
864 var->blue.offset = 0;
865 var->red.length = 8;
866 var->green.length = 8;
867 var->blue.length = 8;
868 var->transp.length = 0;
869 var->transp.offset = 0;
870 break;
871 case 15:
872 var->red.offset = 10;
873 var->green.offset = 5;
874 var->blue.offset = 0;
875 var->red.length = 5;
876 var->green.length = 5;
877 var->blue.length = 5;
878 var->transp.length = 1;
879 var->transp.offset = 15;
880 break;
881 case 16:
882 var->red.offset = 11;
883 var->green.offset = 5;
884 var->blue.offset = 0;
885 var->red.length = 5;
886 var->green.length = 6;
887 var->blue.length = 5;
888 var->transp.length = 0;
889 var->transp.offset = 0;
890 break;
891 case 24:
892 var->red.offset = 16;
893 var->green.offset = 8;
894 var->blue.offset = 0;
895 var->red.length = 8;
896 var->green.length = 8;
897 var->blue.length = 8;
898 var->transp.length = 0;
899 var->transp.offset = 0;
900 break;
901 case 32:
902 var->red.offset = 16;
903 var->green.offset = 8;
904 var->blue.offset = 0;
905 var->red.length = 8;
906 var->green.length = 8;
907 var->blue.length = 8;
908 var->transp.length = 8;
909 var->transp.offset = 24;
910 break;
911 default:
912 return -EINVAL;
913 }
914 return 0;
915}
916EXPORT_SYMBOL(drm_fb_helper_check_var);
917
Daniel Vetter207fd322013-01-20 22:13:14 +0100918/**
919 * drm_fb_helper_set_par - implementation for ->fb_set_par
920 * @info: fbdev registered by the helper
921 *
922 * This will let fbcon do the mode init and is called at initialization time by
923 * the fbdev core when registering the driver, and later on through the hotplug
924 * callback.
925 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000926int drm_fb_helper_set_par(struct fb_info *info)
927{
928 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie785b93e2009-08-28 15:46:53 +1000929 struct fb_var_screeninfo *var = &info->var;
Dave Airlie785b93e2009-08-28 15:46:53 +1000930
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100931 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000932 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000933 return -EINVAL;
934 }
935
Rob Clark5ea1f752014-05-30 12:29:48 -0400936 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000937
938 if (fb_helper->delayed_hotplug) {
939 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000940 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000941 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000942 return 0;
943}
944EXPORT_SYMBOL(drm_fb_helper_set_par);
945
Daniel Vetter207fd322013-01-20 22:13:14 +0100946/**
947 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
948 * @var: updated screen information
949 * @info: fbdev registered by the helper
950 */
Dave Airlie785b93e2009-08-28 15:46:53 +1000951int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
952 struct fb_info *info)
953{
954 struct drm_fb_helper *fb_helper = info->par;
955 struct drm_device *dev = fb_helper->dev;
956 struct drm_mode_set *modeset;
Dave Airlie785b93e2009-08-28 15:46:53 +1000957 int ret = 0;
958 int i;
959
Daniel Vetter84849902012-12-02 00:28:11 +0100960 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100961 if (!drm_fb_helper_is_bound(fb_helper)) {
962 drm_modeset_unlock_all(dev);
963 return -EBUSY;
964 }
965
Dave Airlie8be48d92010-03-30 05:34:14 +0000966 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000967 modeset = &fb_helper->crtc_info[i].mode_set;
968
969 modeset->x = var->xoffset;
970 modeset->y = var->yoffset;
971
972 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100973 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000974 if (!ret) {
975 info->var.xoffset = var->xoffset;
976 info->var.yoffset = var->yoffset;
977 }
978 }
979 }
Daniel Vetter84849902012-12-02 00:28:11 +0100980 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000981 return ret;
982}
983EXPORT_SYMBOL(drm_fb_helper_pan_display);
984
Daniel Vetter8acf6582013-01-21 23:38:37 +0100985/*
Daniel Vetter207fd322013-01-20 22:13:14 +0100986 * Allocates the backing storage and sets up the fbdev info structure through
987 * the ->fb_probe callback and then registers the fbdev and sets up the panic
988 * notifier.
Daniel Vetter8acf6582013-01-21 23:38:37 +0100989 */
Daniel Vetterde1ace52013-01-20 21:50:49 +0100990static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
991 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000992{
Daniel Vetter8acf6582013-01-21 23:38:37 +0100993 int ret = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000994 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000995 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000996 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000997 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000998 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000999
1000 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1001 sizes.surface_depth = 24;
1002 sizes.surface_bpp = 32;
1003 sizes.fb_width = (unsigned)-1;
1004 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +10001005
Dave Airlieb8c00ac2009-10-06 13:54:01 +10001006 /* if driver picks 8 or 16 by default use that
1007 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001008 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +00001009 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001010
Dave Airlie785b93e2009-08-28 15:46:53 +10001011 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001012 for (i = 0; i < fb_helper->connector_count; i++) {
1013 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +01001014 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +10001015
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001016 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +10001017
1018 if (cmdline_mode->bpp_specified) {
1019 switch (cmdline_mode->bpp) {
1020 case 8:
Dave Airlie38651672010-03-30 05:34:13 +00001021 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +10001022 break;
1023 case 15:
Dave Airlie38651672010-03-30 05:34:13 +00001024 sizes.surface_depth = 15;
1025 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001026 break;
1027 case 16:
Dave Airlie38651672010-03-30 05:34:13 +00001028 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +10001029 break;
1030 case 24:
Dave Airlie38651672010-03-30 05:34:13 +00001031 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +10001032 break;
1033 case 32:
Dave Airlie38651672010-03-30 05:34:13 +00001034 sizes.surface_depth = 24;
1035 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +10001036 break;
1037 }
1038 break;
1039 }
1040 }
1041
Dave Airlie8be48d92010-03-30 05:34:14 +00001042 crtc_count = 0;
1043 for (i = 0; i < fb_helper->crtc_count; i++) {
1044 struct drm_display_mode *desired_mode;
1045 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +10001046
Dave Airlie8be48d92010-03-30 05:34:14 +00001047 if (desired_mode) {
1048 if (gamma_size == 0)
1049 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1050 if (desired_mode->hdisplay < sizes.fb_width)
1051 sizes.fb_width = desired_mode->hdisplay;
1052 if (desired_mode->vdisplay < sizes.fb_height)
1053 sizes.fb_height = desired_mode->vdisplay;
1054 if (desired_mode->hdisplay > sizes.surface_width)
1055 sizes.surface_width = desired_mode->hdisplay;
1056 if (desired_mode->vdisplay > sizes.surface_height)
1057 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +10001058 crtc_count++;
1059 }
1060 }
1061
Dave Airlie38651672010-03-30 05:34:13 +00001062 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001063 /* hmm everyone went away - assume VGA cable just fell out
1064 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001065 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +00001066 sizes.fb_width = sizes.surface_width = 1024;
1067 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +10001068 }
1069
Dave Airlie38651672010-03-30 05:34:13 +00001070 /* push down into drivers */
Daniel Vetter8acf6582013-01-21 23:38:37 +01001071 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1072 if (ret < 0)
1073 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +10001074
Dave Airlie38651672010-03-30 05:34:13 +00001075 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +10001076
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001077 /*
1078 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1079 * events, but at init time drm_setup_crtcs needs to be called before
1080 * the fb is allocated (since we need to figure out the desired size of
1081 * the fb before we can allocate it ...). Hence we need to fix things up
1082 * here again.
1083 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001084 for (i = 0; i < fb_helper->crtc_count; i++)
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001085 if (fb_helper->crtc_info[i].mode_set.num_connectors)
1086 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1087
Dave Airlie785b93e2009-08-28 15:46:53 +10001088
Daniel Vetter8acf6582013-01-21 23:38:37 +01001089 info->var.pixclock = 0;
1090 if (register_framebuffer(info) < 0)
1091 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +00001092
Daniel Vetter8acf6582013-01-21 23:38:37 +01001093 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1094 info->node, info->fix.id);
Dave Airlie785b93e2009-08-28 15:46:53 +10001095
1096 /* Switch back to kernel console on panic */
1097 /* multi card linked list maybe */
1098 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001099 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +10001100 atomic_notifier_chain_register(&panic_notifier_list,
1101 &paniced);
1102 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1103 }
Daniel Vetter8acf6582013-01-21 23:38:37 +01001104
1105 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
Dave Airlie38651672010-03-30 05:34:13 +00001106
Dave Airlie785b93e2009-08-28 15:46:53 +10001107 return 0;
1108}
Dave Airlie785b93e2009-08-28 15:46:53 +10001109
Daniel Vetter207fd322013-01-20 22:13:14 +01001110/**
1111 * drm_fb_helper_fill_fix - initializes fixed fbdev information
1112 * @info: fbdev registered by the helper
1113 * @pitch: desired pitch
1114 * @depth: desired depth
1115 *
1116 * Helper to fill in the fixed fbdev information useful for a non-accelerated
1117 * fbdev emulations. Drivers which support acceleration methods which impose
1118 * additional constraints need to set up their own limits.
1119 *
1120 * Drivers should call this (or their equivalent setup code) from their
1121 * ->fb_probe callback.
1122 */
Dave Airlie3632ef82011-01-15 09:27:00 +10001123void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1124 uint32_t depth)
1125{
1126 info->fix.type = FB_TYPE_PACKED_PIXELS;
1127 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1128 FB_VISUAL_TRUECOLOR;
1129 info->fix.mmio_start = 0;
1130 info->fix.mmio_len = 0;
1131 info->fix.type_aux = 0;
1132 info->fix.xpanstep = 1; /* doing it in hw */
1133 info->fix.ypanstep = 1; /* doing it in hw */
1134 info->fix.ywrapstep = 0;
1135 info->fix.accel = FB_ACCEL_NONE;
Dave Airlie3632ef82011-01-15 09:27:00 +10001136
1137 info->fix.line_length = pitch;
1138 return;
1139}
1140EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1141
Daniel Vetter207fd322013-01-20 22:13:14 +01001142/**
1143 * drm_fb_helper_fill_var - initalizes variable fbdev information
1144 * @info: fbdev instance to set up
1145 * @fb_helper: fb helper instance to use as template
1146 * @fb_width: desired fb width
1147 * @fb_height: desired fb height
1148 *
1149 * Sets up the variable fbdev metainformation from the given fb helper instance
1150 * and the drm framebuffer allocated in fb_helper->fb.
1151 *
1152 * Drivers should call this (or their equivalent setup code) from their
1153 * ->fb_probe callback after having allocated the fbdev backing
1154 * storage framebuffer.
1155 */
Dave Airlie38651672010-03-30 05:34:13 +00001156void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +10001157 uint32_t fb_width, uint32_t fb_height)
1158{
Dave Airlie38651672010-03-30 05:34:13 +00001159 struct drm_framebuffer *fb = fb_helper->fb;
1160 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +10001161 info->var.xres_virtual = fb->width;
1162 info->var.yres_virtual = fb->height;
1163 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +00001164 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +10001165 info->var.xoffset = 0;
1166 info->var.yoffset = 0;
1167 info->var.activate = FB_ACTIVATE_NOW;
1168 info->var.height = -1;
1169 info->var.width = -1;
1170
1171 switch (fb->depth) {
1172 case 8:
1173 info->var.red.offset = 0;
1174 info->var.green.offset = 0;
1175 info->var.blue.offset = 0;
1176 info->var.red.length = 8; /* 8bit DAC */
1177 info->var.green.length = 8;
1178 info->var.blue.length = 8;
1179 info->var.transp.offset = 0;
1180 info->var.transp.length = 0;
1181 break;
1182 case 15:
1183 info->var.red.offset = 10;
1184 info->var.green.offset = 5;
1185 info->var.blue.offset = 0;
1186 info->var.red.length = 5;
1187 info->var.green.length = 5;
1188 info->var.blue.length = 5;
1189 info->var.transp.offset = 15;
1190 info->var.transp.length = 1;
1191 break;
1192 case 16:
1193 info->var.red.offset = 11;
1194 info->var.green.offset = 5;
1195 info->var.blue.offset = 0;
1196 info->var.red.length = 5;
1197 info->var.green.length = 6;
1198 info->var.blue.length = 5;
1199 info->var.transp.offset = 0;
1200 break;
1201 case 24:
1202 info->var.red.offset = 16;
1203 info->var.green.offset = 8;
1204 info->var.blue.offset = 0;
1205 info->var.red.length = 8;
1206 info->var.green.length = 8;
1207 info->var.blue.length = 8;
1208 info->var.transp.offset = 0;
1209 info->var.transp.length = 0;
1210 break;
1211 case 32:
1212 info->var.red.offset = 16;
1213 info->var.green.offset = 8;
1214 info->var.blue.offset = 0;
1215 info->var.red.length = 8;
1216 info->var.green.length = 8;
1217 info->var.blue.length = 8;
1218 info->var.transp.offset = 24;
1219 info->var.transp.length = 8;
1220 break;
1221 default:
1222 break;
1223 }
1224
1225 info->var.xres = fb_width;
1226 info->var.yres = fb_height;
1227}
1228EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +00001229
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001230static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1231 uint32_t maxX,
1232 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +00001233{
1234 struct drm_connector *connector;
1235 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001236 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001237
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001238 for (i = 0; i < fb_helper->connector_count; i++) {
1239 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001240 count += connector->funcs->fill_modes(connector, maxX, maxY);
1241 }
1242
1243 return count;
1244}
1245
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001246struct 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 +00001247{
1248 struct drm_display_mode *mode;
1249
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001250 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Daniel Vetter9d3de132014-01-23 16:27:56 +01001251 if (mode->hdisplay > width ||
1252 mode->vdisplay > height)
Dave Airlie38651672010-03-30 05:34:13 +00001253 continue;
1254 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1255 return mode;
1256 }
1257 return NULL;
1258}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001259EXPORT_SYMBOL(drm_has_preferred_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001260
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001261static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001262{
Chris Wilson1794d252011-04-17 07:43:32 +01001263 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001264 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001265 return cmdline_mode->specified;
1266}
1267
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001268struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001269 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001270{
Chris Wilson1794d252011-04-17 07:43:32 +01001271 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001272 struct drm_display_mode *mode = NULL;
Takashi Iwaic683f422014-03-19 14:53:13 +01001273 bool prefer_non_interlace;
Dave Airlie38651672010-03-30 05:34:13 +00001274
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001275 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001276 if (cmdline_mode->specified == false)
1277 return mode;
1278
1279 /* attempt to find a matching mode in the list of modes
1280 * we have gotten so far, if not add a CVT mode that conforms
1281 */
1282 if (cmdline_mode->rb || cmdline_mode->margins)
1283 goto create_mode;
1284
Takashi Iwaic683f422014-03-19 14:53:13 +01001285 prefer_non_interlace = !cmdline_mode->interlace;
1286 again:
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001287 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001288 /* check width/height */
1289 if (mode->hdisplay != cmdline_mode->xres ||
1290 mode->vdisplay != cmdline_mode->yres)
1291 continue;
1292
1293 if (cmdline_mode->refresh_specified) {
1294 if (mode->vrefresh != cmdline_mode->refresh)
1295 continue;
1296 }
1297
1298 if (cmdline_mode->interlace) {
1299 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1300 continue;
Takashi Iwaic683f422014-03-19 14:53:13 +01001301 } else if (prefer_non_interlace) {
1302 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1303 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001304 }
1305 return mode;
1306 }
1307
Takashi Iwaic683f422014-03-19 14:53:13 +01001308 if (prefer_non_interlace) {
1309 prefer_non_interlace = false;
1310 goto again;
1311 }
1312
Dave Airlie38651672010-03-30 05:34:13 +00001313create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001314 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1315 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001316 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001317 return mode;
1318}
Jesse Barnes2f1046f2014-02-12 12:26:24 -08001319EXPORT_SYMBOL(drm_pick_cmdline_mode);
Dave Airlie38651672010-03-30 05:34:13 +00001320
1321static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1322{
1323 bool enable;
1324
Sachin Kamat96081cd2012-11-15 03:43:30 +00001325 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001326 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001327 else
Dave Airlie38651672010-03-30 05:34:13 +00001328 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001329
Dave Airlie38651672010-03-30 05:34:13 +00001330 return enable;
1331}
1332
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001333static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1334 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001335{
1336 bool any_enabled = false;
1337 struct drm_connector *connector;
1338 int i = 0;
1339
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001340 for (i = 0; i < fb_helper->connector_count; i++) {
1341 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001342 enabled[i] = drm_connector_enabled(connector, true);
1343 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1344 enabled[i] ? "yes" : "no");
1345 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001346 }
1347
1348 if (any_enabled)
1349 return;
1350
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001351 for (i = 0; i < fb_helper->connector_count; i++) {
1352 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001353 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001354 }
1355}
1356
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001357static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1358 struct drm_display_mode **modes,
1359 bool *enabled, int width, int height)
1360{
1361 int count, i, j;
1362 bool can_clone = false;
1363 struct drm_fb_helper_connector *fb_helper_conn;
1364 struct drm_display_mode *dmt_mode, *mode;
1365
1366 /* only contemplate cloning in the single crtc case */
1367 if (fb_helper->crtc_count > 1)
1368 return false;
1369
1370 count = 0;
1371 for (i = 0; i < fb_helper->connector_count; i++) {
1372 if (enabled[i])
1373 count++;
1374 }
1375
1376 /* only contemplate cloning if more than one connector is enabled */
1377 if (count <= 1)
1378 return false;
1379
1380 /* check the command line or if nothing common pick 1024x768 */
1381 can_clone = true;
1382 for (i = 0; i < fb_helper->connector_count; i++) {
1383 if (!enabled[i])
1384 continue;
1385 fb_helper_conn = fb_helper->connector_info[i];
1386 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1387 if (!modes[i]) {
1388 can_clone = false;
1389 break;
1390 }
1391 for (j = 0; j < i; j++) {
1392 if (!enabled[j])
1393 continue;
1394 if (!drm_mode_equal(modes[j], modes[i]))
1395 can_clone = false;
1396 }
1397 }
1398
1399 if (can_clone) {
1400 DRM_DEBUG_KMS("can clone using command line\n");
1401 return true;
1402 }
1403
1404 /* try and find a 1024x768 mode on each connector */
1405 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001406 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001407
1408 for (i = 0; i < fb_helper->connector_count; i++) {
1409
1410 if (!enabled[i])
1411 continue;
1412
1413 fb_helper_conn = fb_helper->connector_info[i];
1414 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1415 if (drm_mode_equal(mode, dmt_mode))
1416 modes[i] = mode;
1417 }
1418 if (!modes[i])
1419 can_clone = false;
1420 }
1421
1422 if (can_clone) {
1423 DRM_DEBUG_KMS("can clone using 1024x768\n");
1424 return true;
1425 }
1426 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1427 return false;
1428}
1429
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001430static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001431 struct drm_display_mode **modes,
1432 bool *enabled, int width, int height)
1433{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001434 struct drm_fb_helper_connector *fb_helper_conn;
1435 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001436
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001437 for (i = 0; i < fb_helper->connector_count; i++) {
1438 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001439
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001440 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001441 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001442
1443 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001444 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001445
1446 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001447 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001448 if (!modes[i]) {
1449 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001450 fb_helper_conn->connector->base.id);
1451 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001452 }
1453 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001454 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1455 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001456 break;
1457 }
1458 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1459 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001460 }
1461 return true;
1462}
1463
Dave Airlie8be48d92010-03-30 05:34:14 +00001464static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1465 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001466 struct drm_display_mode **modes,
1467 int n, int width, int height)
1468{
1469 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001470 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001471 struct drm_connector *connector;
1472 struct drm_connector_helper_funcs *connector_funcs;
1473 struct drm_encoder *encoder;
Dave Airlie38651672010-03-30 05:34:13 +00001474 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001475 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001476 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001477
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001478 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001479 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001480
1481 fb_helper_conn = fb_helper->connector_info[n];
1482 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001483
1484 best_crtcs[n] = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001485 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001486 if (modes[n] == NULL)
1487 return best_score;
1488
Dave Airlie8be48d92010-03-30 05:34:14 +00001489 crtcs = kzalloc(dev->mode_config.num_connector *
1490 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001491 if (!crtcs)
1492 return best_score;
1493
1494 my_score = 1;
1495 if (connector->status == connector_status_connected)
1496 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001497 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001498 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001499 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001500 my_score++;
1501
1502 connector_funcs = connector->helper_private;
1503 encoder = connector_funcs->best_encoder(connector);
1504 if (!encoder)
1505 goto out;
1506
Dave Airlie38651672010-03-30 05:34:13 +00001507 /* select a crtc for this connector and then attempt to configure
1508 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001509 for (c = 0; c < fb_helper->crtc_count; c++) {
1510 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001511
Sachin Kamat96081cd2012-11-15 03:43:30 +00001512 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001513 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001514
1515 for (o = 0; o < n; o++)
1516 if (best_crtcs[o] == crtc)
1517 break;
1518
1519 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001520 /* ignore cloning unless only a single crtc */
1521 if (fb_helper->crtc_count > 1)
1522 continue;
1523
1524 if (!drm_mode_equal(modes[o], modes[n]))
1525 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001526 }
1527
1528 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001529 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1530 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001531 width, height);
1532 if (score > best_score) {
Dave Airlie38651672010-03-30 05:34:13 +00001533 best_score = score;
1534 memcpy(best_crtcs, crtcs,
1535 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001536 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001537 }
Dave Airlie38651672010-03-30 05:34:13 +00001538 }
1539out:
1540 kfree(crtcs);
1541 return best_score;
1542}
1543
Dave Airlie8be48d92010-03-30 05:34:14 +00001544static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001545{
Dave Airlie8be48d92010-03-30 05:34:14 +00001546 struct drm_device *dev = fb_helper->dev;
1547 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001548 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001549 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001550 bool *enabled;
1551 int width, height;
Jesse Barnes11e17a02013-02-19 13:31:39 -08001552 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001553
1554 DRM_DEBUG_KMS("\n");
1555
1556 width = dev->mode_config.max_width;
1557 height = dev->mode_config.max_height;
1558
Dave Airlie38651672010-03-30 05:34:13 +00001559 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001560 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001561 modes = kcalloc(dev->mode_config.num_connector,
1562 sizeof(struct drm_display_mode *), GFP_KERNEL);
1563 enabled = kcalloc(dev->mode_config.num_connector,
1564 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001565 if (!crtcs || !modes || !enabled) {
1566 DRM_ERROR("Memory allocation failed\n");
1567 goto out;
1568 }
1569
Dave Airlie38651672010-03-30 05:34:13 +00001570
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001571 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001572
Jesse Barnes11e17a02013-02-19 13:31:39 -08001573 if (!(fb_helper->funcs->initial_config &&
1574 fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1575 enabled, width, height))) {
1576 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1577 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1578
1579 if (!drm_target_cloned(fb_helper,
1580 modes, enabled, width, height) &&
1581 !drm_target_preferred(fb_helper,
1582 modes, enabled, width, height))
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001583 DRM_ERROR("Unable to find initial modes\n");
Jesse Barnes11e17a02013-02-19 13:31:39 -08001584
1585 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1586 width, height);
1587
1588 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001589 }
Dave Airlie38651672010-03-30 05:34:13 +00001590
Dave Airlie8be48d92010-03-30 05:34:14 +00001591 /* need to set the modesets up here for use later */
1592 /* fill out the connector<->crtc mappings into the modesets */
1593 for (i = 0; i < fb_helper->crtc_count; i++) {
1594 modeset = &fb_helper->crtc_info[i].mode_set;
1595 modeset->num_connectors = 0;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001596 modeset->fb = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001597 }
Dave Airlie38651672010-03-30 05:34:13 +00001598
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001599 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001600 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001601 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1602 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001603
Dave Airlie8be48d92010-03-30 05:34:14 +00001604 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001605 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001606 mode->name, fb_crtc->mode_set.crtc->base.id);
1607 fb_crtc->desired_mode = mode;
1608 if (modeset->mode)
1609 drm_mode_destroy(dev, modeset->mode);
1610 modeset->mode = drm_mode_duplicate(dev,
1611 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001612 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001613 modeset->fb = fb_helper->fb;
Dave Airlie38651672010-03-30 05:34:13 +00001614 }
Dave Airlie38651672010-03-30 05:34:13 +00001615 }
1616
Daniel Vetter7e53f3a2013-01-21 10:52:17 +01001617 /* Clear out any old modes if there are no more connected outputs. */
1618 for (i = 0; i < fb_helper->crtc_count; i++) {
1619 modeset = &fb_helper->crtc_info[i].mode_set;
1620 if (modeset->num_connectors == 0) {
1621 BUG_ON(modeset->fb);
1622 BUG_ON(modeset->num_connectors);
1623 if (modeset->mode)
1624 drm_mode_destroy(dev, modeset->mode);
1625 modeset->mode = NULL;
1626 }
1627 }
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001628out:
Dave Airlie38651672010-03-30 05:34:13 +00001629 kfree(crtcs);
1630 kfree(modes);
1631 kfree(enabled);
1632}
1633
1634/**
Daniel Vetter207fd322013-01-20 22:13:14 +01001635 * drm_fb_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001636 * @fb_helper: fb_helper device struct
1637 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001638 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001639 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001640 * At the moment, this is a cloned configuration across all heads with
1641 * a new framebuffer object as the backing store.
1642 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001643 * Note that this also registers the fbdev and so allows userspace to call into
1644 * the driver through the fbdev interfaces.
1645 *
1646 * This function will call down into the ->fb_probe callback to let
1647 * the driver allocate and initialize the fbdev info structure and the drm
1648 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1649 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1650 * values for the fbdev info structure.
1651 *
Dave Airlie38651672010-03-30 05:34:13 +00001652 * RETURNS:
1653 * Zero if everything went ok, nonzero otherwise.
1654 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001655bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001656{
Dave Airlie8be48d92010-03-30 05:34:14 +00001657 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001658 int count = 0;
1659
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001660 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001661
Daniel Vetter53f19042014-03-20 14:26:35 +01001662 mutex_lock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001663 count = drm_fb_helper_probe_connector_modes(fb_helper,
1664 dev->mode_config.max_width,
1665 dev->mode_config.max_height);
Daniel Vetter53f19042014-03-20 14:26:35 +01001666 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001667 /*
1668 * we shouldn't end up with no modes here.
1669 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001670 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001671 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001672
Dave Airlie8be48d92010-03-30 05:34:14 +00001673 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001674
Dave Airlie4abe3522010-03-30 05:34:18 +00001675 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001676}
Dave Airlie8be48d92010-03-30 05:34:14 +00001677EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001678
Chris Wilson73943712011-04-22 11:03:57 +01001679/**
1680 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001681 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001682 * @fb_helper: the drm_fb_helper
1683 *
Chris Wilson73943712011-04-22 11:03:57 +01001684 * Scan the connectors attached to the fb_helper and try to put together a
1685 * setup after *notification of a change in output configuration.
1686 *
Daniel Vetter207fd322013-01-20 22:13:14 +01001687 * Called at runtime, takes the mode config locks to be able to check/change the
1688 * modeset configuration. Must be run from process context (which usually means
1689 * either the output polling work or a work item launched from the driver's
1690 * hotplug interrupt).
1691 *
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001692 * Note that drivers may call this even before calling
1693 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
1694 * for a race-free fbcon setup and will make sure that the fbdev emulation will
1695 * not miss any hotplug events.
Daniel Vetter207fd322013-01-20 22:13:14 +01001696 *
Chris Wilson73943712011-04-22 11:03:57 +01001697 * RETURNS:
1698 * 0 on success and a non-zero error code otherwise.
1699 */
1700int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001701{
Chris Wilson73943712011-04-22 11:03:57 +01001702 struct drm_device *dev = fb_helper->dev;
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001703 u32 max_width, max_height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001704
Daniel Vetter89ced122013-04-11 14:26:55 +00001705 mutex_lock(&fb_helper->dev->mode_config.mutex);
Daniel Vetter50c3dc92014-06-27 17:19:22 +02001706 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001707 fb_helper->delayed_hotplug = true;
Daniel Vetter89ced122013-04-11 14:26:55 +00001708 mutex_unlock(&fb_helper->dev->mode_config.mutex);
Chris Wilson73943712011-04-22 11:03:57 +01001709 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001710 }
Dave Airlie38651672010-03-30 05:34:13 +00001711 DRM_DEBUG_KMS("\n");
1712
Dave Airlie4abe3522010-03-30 05:34:18 +00001713 max_width = fb_helper->fb->width;
1714 max_height = fb_helper->fb->height;
Dave Airlie4abe3522010-03-30 05:34:18 +00001715
Lespiau, Damien51bbd272013-09-28 16:24:05 +01001716 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
Daniel Vetter89ced122013-04-11 14:26:55 +00001717 mutex_unlock(&fb_helper->dev->mode_config.mutex);
1718
1719 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +00001720 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001721 drm_modeset_unlock_all(dev);
Daniel Vetter2180c3c2013-01-21 23:12:36 +01001722 drm_fb_helper_set_par(fb_helper->fbdev);
1723
1724 return 0;
Dave Airlie38651672010-03-30 05:34:13 +00001725}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001726EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001727
David Rientjes6a108a12011-01-20 14:44:16 -08001728/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001729 * but the module doesn't depend on any fb console symbols. At least
1730 * attempt to load fbcon to avoid leaving the system without a usable console.
1731 */
David Rientjes6a108a12011-01-20 14:44:16 -08001732#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001733static int __init drm_fb_helper_modinit(void)
1734{
1735 const char *name = "fbcon";
1736 struct module *fbcon;
1737
1738 mutex_lock(&module_mutex);
1739 fbcon = find_module(name);
1740 mutex_unlock(&module_mutex);
1741
1742 if (!fbcon)
1743 request_module_nowait(name);
1744 return 0;
1745}
1746
1747module_init(drm_fb_helper_modinit);
1748#endif