blob: 0c6e25e979dd7eb4b0c7005656196824e6c0c181 [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
Daniel Vetterd0ddc0332012-11-01 14:45:17 +010048/**
49 * DOC: fbdev helpers
50 *
51 * The fb helper functions are useful to provide an fbdev on top of a drm kernel
52 * mode setting driver. They can be used mostly independantely from the crtc
53 * helper functions used by many drivers to implement the kernel mode setting
54 * interfaces.
55 */
56
Dave Airlie0b4c0f32010-03-30 05:34:15 +000057/* simple single crtc case helper function */
58int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100059{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000060 struct drm_device *dev = fb_helper->dev;
61 struct drm_connector *connector;
62 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100063
Dave Airlie0b4c0f32010-03-30 05:34:15 +000064 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
65 struct drm_fb_helper_connector *fb_helper_connector;
66
67 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
68 if (!fb_helper_connector)
69 goto fail;
70
71 fb_helper_connector->connector = connector;
72 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
73 }
Dave Airlied50ba252009-09-23 14:44:08 +100074 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000075fail:
76 for (i = 0; i < fb_helper->connector_count; i++) {
77 kfree(fb_helper->connector_info[i]);
78 fb_helper->connector_info[i] = NULL;
79 }
80 fb_helper->connector_count = 0;
81 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +100082}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000083EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100084
Dave Airlie0b4c0f32010-03-30 05:34:15 +000085static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100086{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000087 struct drm_fb_helper_connector *fb_helper_conn;
88 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100089
Dave Airlie0b4c0f32010-03-30 05:34:15 +000090 for (i = 0; i < fb_helper->connector_count; i++) {
Chris Wilson1794d252011-04-17 07:43:32 +010091 struct drm_cmdline_mode *mode;
92 struct drm_connector *connector;
Dave Airlied50ba252009-09-23 14:44:08 +100093 char *option = NULL;
94
Dave Airlie0b4c0f32010-03-30 05:34:15 +000095 fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +010096 connector = fb_helper_conn->connector;
97 mode = &fb_helper_conn->cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000098
Dave Airlied50ba252009-09-23 14:44:08 +100099 /* do something on return - turn off connector maybe */
Chris Wilson1794d252011-04-17 07:43:32 +0100100 if (fb_get_options(drm_get_connector_name(connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +1000101 continue;
102
Chris Wilson1794d252011-04-17 07:43:32 +0100103 if (drm_mode_parse_command_line_for_connector(option,
104 connector,
105 mode)) {
106 if (mode->force) {
107 const char *s;
108 switch (mode->force) {
Sachin Kamat6c910832012-11-15 03:43:28 +0000109 case DRM_FORCE_OFF:
110 s = "OFF";
111 break;
112 case DRM_FORCE_ON_DIGITAL:
113 s = "ON - dig";
114 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100115 default:
Sachin Kamat6c910832012-11-15 03:43:28 +0000116 case DRM_FORCE_ON:
117 s = "ON";
118 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100119 }
120
121 DRM_INFO("forcing %s connector %s\n",
122 drm_get_connector_name(connector), s);
123 connector->force = mode->force;
124 }
125
126 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
127 drm_get_connector_name(connector),
128 mode->xres, mode->yres,
129 mode->refresh_specified ? mode->refresh : 60,
130 mode->rb ? " reduced blanking" : "",
131 mode->margins ? " with margins" : "",
132 mode->interlace ? " interlaced" : "");
133 }
134
Dave Airlied50ba252009-09-23 14:44:08 +1000135 }
136 return 0;
137}
138
Jason Wessel99231022010-10-13 14:09:43 -0500139static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
140{
141 uint16_t *r_base, *g_base, *b_base;
142 int i;
143
144 r_base = crtc->gamma_store;
145 g_base = r_base + crtc->gamma_size;
146 b_base = g_base + crtc->gamma_size;
147
148 for (i = 0; i < crtc->gamma_size; i++)
149 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
150}
151
152static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
153{
154 uint16_t *r_base, *g_base, *b_base;
155
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200156 if (crtc->funcs->gamma_set == NULL)
157 return;
158
Jason Wessel99231022010-10-13 14:09:43 -0500159 r_base = crtc->gamma_store;
160 g_base = r_base + crtc->gamma_size;
161 b_base = g_base + crtc->gamma_size;
162
163 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
164}
165
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500166int drm_fb_helper_debug_enter(struct fb_info *info)
167{
168 struct drm_fb_helper *helper = info->par;
169 struct drm_crtc_helper_funcs *funcs;
170 int i;
171
172 if (list_empty(&kernel_fb_helper_list))
173 return false;
174
175 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
176 for (i = 0; i < helper->crtc_count; i++) {
177 struct drm_mode_set *mode_set =
178 &helper->crtc_info[i].mode_set;
179
180 if (!mode_set->crtc->enabled)
181 continue;
182
183 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500184 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500185 funcs->mode_set_base_atomic(mode_set->crtc,
186 mode_set->fb,
187 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500188 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500189 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500190 }
191 }
192
193 return 0;
194}
195EXPORT_SYMBOL(drm_fb_helper_debug_enter);
196
197/* Find the real fb for a given fb helper CRTC */
198static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
199{
200 struct drm_device *dev = crtc->dev;
201 struct drm_crtc *c;
202
203 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
204 if (crtc->base.id == c->base.id)
205 return c->fb;
206 }
207
208 return NULL;
209}
210
211int drm_fb_helper_debug_leave(struct fb_info *info)
212{
213 struct drm_fb_helper *helper = info->par;
214 struct drm_crtc *crtc;
215 struct drm_crtc_helper_funcs *funcs;
216 struct drm_framebuffer *fb;
217 int i;
218
219 for (i = 0; i < helper->crtc_count; i++) {
220 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
221 crtc = mode_set->crtc;
222 funcs = crtc->helper_private;
223 fb = drm_mode_config_fb(crtc);
224
225 if (!crtc->enabled)
226 continue;
227
228 if (!fb) {
229 DRM_ERROR("no fb to restore??\n");
230 continue;
231 }
232
Jason Wessel99231022010-10-13 14:09:43 -0500233 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500234 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500235 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500236 }
237
238 return 0;
239}
240EXPORT_SYMBOL(drm_fb_helper_debug_leave);
241
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100242bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
243{
244 bool error = false;
245 int i, ret;
246 for (i = 0; i < fb_helper->crtc_count; i++) {
247 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100248 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100249 if (ret)
250 error = true;
251 }
252 return error;
253}
254EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
255
Sachin Kamat78b9c352012-08-01 17:15:32 +0530256static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000257{
Dave Airlie785b93e2009-08-28 15:46:53 +1000258 bool ret, error = false;
259 struct drm_fb_helper *helper;
260
261 if (list_empty(&kernel_fb_helper_list))
262 return false;
263
264 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100265 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
266 continue;
267
268 ret = drm_fb_helper_restore_fbdev_mode(helper);
269 if (ret)
270 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000271 }
272 return error;
273}
274
275int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
276 void *panic_str)
277{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700278 /*
279 * It's a waste of time and effort to switch back to text console
280 * if the kernel should reboot before panic messages can be seen.
281 */
282 if (panic_timeout < 0)
283 return 0;
284
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000285 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000286 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000287}
288EXPORT_SYMBOL(drm_fb_helper_panic);
289
290static struct notifier_block paniced = {
291 .notifier_call = drm_fb_helper_panic,
292};
293
294/**
295 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
296 *
297 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
298 */
299void drm_fb_helper_restore(void)
300{
301 bool ret;
302 ret = drm_fb_helper_force_kernel_mode();
303 if (ret == true)
304 DRM_ERROR("Failed to restore crtc configuration\n");
305}
306EXPORT_SYMBOL(drm_fb_helper_restore);
307
Daniel Vetter20c60c32012-12-17 12:13:23 +0100308static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
309{
310 struct drm_device *dev = fb_helper->dev;
311 struct drm_crtc *crtc;
312 int bound = 0, crtcs_bound = 0;
313
314 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
315 if (crtc->fb)
316 crtcs_bound++;
317 if (crtc->fb == fb_helper->fb)
318 bound++;
319 }
320
321 if (bound < crtcs_bound)
322 return false;
323 return true;
324}
325
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200326#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000327static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
328{
329 drm_fb_helper_restore();
330}
331static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
332
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700333static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000334{
335 schedule_work(&drm_fb_helper_restore_work);
336}
337
338static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
339 .handler = drm_fb_helper_sysrq,
340 .help_msg = "force-fb(V)",
341 .action_msg = "Restore framebuffer console",
342};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000343#else
344static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200345#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000346
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100347static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000348{
349 struct drm_fb_helper *fb_helper = info->par;
350 struct drm_device *dev = fb_helper->dev;
351 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700352 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700353 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000354
355 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100356 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000357 */
Daniel Vetter84849902012-12-02 00:28:11 +0100358 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100359 if (!drm_fb_helper_is_bound(fb_helper)) {
360 drm_modeset_unlock_all(dev);
361 return;
362 }
363
Jesse Barnese87b2c42009-09-17 18:14:41 -0700364 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000365 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000366
Dave Airlie8be48d92010-03-30 05:34:14 +0000367 if (!crtc->enabled)
368 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000369
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100370 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700371 for (j = 0; j < fb_helper->connector_count; j++) {
372 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200373 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500374 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100375 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700376 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000377 }
Daniel Vetter84849902012-12-02 00:28:11 +0100378 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000379}
380
381int drm_fb_helper_blank(int blank, struct fb_info *info)
382{
383 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000384 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000385 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100386 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000387 break;
James Simmons731b5a12009-10-29 20:39:07 +0000388 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000389 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100390 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000391 break;
James Simmons731b5a12009-10-29 20:39:07 +0000392 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000393 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100394 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000395 break;
James Simmons731b5a12009-10-29 20:39:07 +0000396 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000397 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100398 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000399 break;
James Simmons731b5a12009-10-29 20:39:07 +0000400 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000401 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100402 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000403 break;
404 }
405 return 0;
406}
407EXPORT_SYMBOL(drm_fb_helper_blank);
408
409static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
410{
411 int i;
412
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000413 for (i = 0; i < helper->connector_count; i++)
414 kfree(helper->connector_info[i]);
415 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100416 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000417 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100418 if (helper->crtc_info[i].mode_set.mode)
419 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
420 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000421 kfree(helper->crtc_info);
422}
423
Dave Airlie4abe3522010-03-30 05:34:18 +0000424int drm_fb_helper_init(struct drm_device *dev,
425 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000426 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000427{
Dave Airlie785b93e2009-08-28 15:46:53 +1000428 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000429 int i;
430
Dave Airlie4abe3522010-03-30 05:34:18 +0000431 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000432
433 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
434
435 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
436 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000437 return -ENOMEM;
438
Dave Airlie4abe3522010-03-30 05:34:18 +0000439 fb_helper->crtc_count = crtc_count;
440 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
441 if (!fb_helper->connector_info) {
442 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000443 return -ENOMEM;
444 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000445 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000446
447 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000448 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000449 kcalloc(max_conn_count,
450 sizeof(struct drm_connector *),
451 GFP_KERNEL);
452
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200453 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000454 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000455 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000456 }
457
458 i = 0;
459 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000460 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000461 i++;
462 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100463
Dave Airlie785b93e2009-08-28 15:46:53 +1000464 return 0;
465out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000466 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000467 return -ENOMEM;
468}
Dave Airlie4abe3522010-03-30 05:34:18 +0000469EXPORT_SYMBOL(drm_fb_helper_init);
470
471void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
472{
473 if (!list_empty(&fb_helper->kernel_fb_list)) {
474 list_del(&fb_helper->kernel_fb_list);
475 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000476 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000477 atomic_notifier_chain_unregister(&panic_notifier_list,
478 &paniced);
479 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
480 }
481 }
482
483 drm_fb_helper_crtc_free(fb_helper);
484
Dave Airlie4abe3522010-03-30 05:34:18 +0000485}
486EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000487
Dave Airliec850cb72009-10-23 18:49:03 +1000488static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000489 u16 blue, u16 regno, struct fb_info *info)
490{
491 struct drm_fb_helper *fb_helper = info->par;
492 struct drm_framebuffer *fb = fb_helper->fb;
493 int pindex;
494
Dave Airliec850cb72009-10-23 18:49:03 +1000495 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
496 u32 *palette;
497 u32 value;
498 /* place color in psuedopalette */
499 if (regno > 16)
500 return -EINVAL;
501 palette = (u32 *)info->pseudo_palette;
502 red >>= (16 - info->var.red.length);
503 green >>= (16 - info->var.green.length);
504 blue >>= (16 - info->var.blue.length);
505 value = (red << info->var.red.offset) |
506 (green << info->var.green.offset) |
507 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000508 if (info->var.transp.length > 0) {
509 u32 mask = (1 << info->var.transp.length) - 1;
510 mask <<= info->var.transp.offset;
511 value |= mask;
512 }
Dave Airliec850cb72009-10-23 18:49:03 +1000513 palette[regno] = value;
514 return 0;
515 }
516
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000517 pindex = regno;
518
519 if (fb->bits_per_pixel == 16) {
520 pindex = regno << 3;
521
522 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000523 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000524 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000525 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000526
527 if (fb->depth == 16) {
528 u16 r, g, b;
529 int i;
530 if (regno < 32) {
531 for (i = 0; i < 8; i++)
532 fb_helper->funcs->gamma_set(crtc, red,
533 green, blue, pindex + i);
534 }
535
536 fb_helper->funcs->gamma_get(crtc, &r,
537 &g, &b,
538 pindex >> 1);
539
540 for (i = 0; i < 4; i++)
541 fb_helper->funcs->gamma_set(crtc, r,
542 green, b,
543 (pindex >> 1) + i);
544 }
545 }
546
547 if (fb->depth != 16)
548 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000549 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000550}
551
Dave Airlie068143d2009-10-05 09:58:02 +1000552int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
553{
554 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000555 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000556 u16 *red, *green, *blue, *transp;
557 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100558 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000559 int start;
560
Dave Airlie8be48d92010-03-30 05:34:14 +0000561 for (i = 0; i < fb_helper->crtc_count; i++) {
562 crtc = fb_helper->crtc_info[i].mode_set.crtc;
563 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000564
565 red = cmap->red;
566 green = cmap->green;
567 blue = cmap->blue;
568 transp = cmap->transp;
569 start = cmap->start;
570
roel062ac622011-03-07 18:00:34 +0100571 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000572 u16 hred, hgreen, hblue, htransp = 0xffff;
573
574 hred = *red++;
575 hgreen = *green++;
576 hblue = *blue++;
577
578 if (transp)
579 htransp = *transp++;
580
Dave Airliec850cb72009-10-23 18:49:03 +1000581 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
582 if (rc)
583 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000584 }
585 crtc_funcs->load_lut(crtc);
586 }
587 return rc;
588}
589EXPORT_SYMBOL(drm_fb_helper_setcmap);
590
Dave Airlie785b93e2009-08-28 15:46:53 +1000591int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
592 struct fb_info *info)
593{
594 struct drm_fb_helper *fb_helper = info->par;
595 struct drm_framebuffer *fb = fb_helper->fb;
596 int depth;
597
Jason Wesself90ebd92010-08-05 09:22:32 -0500598 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000599 return -EINVAL;
600
601 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100602 if (var->bits_per_pixel > fb->bits_per_pixel ||
603 var->xres > fb->width || var->yres > fb->height ||
604 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000605 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100606 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
607 var->xres, var->yres, var->bits_per_pixel,
608 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000609 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000610 return -EINVAL;
611 }
612
613 switch (var->bits_per_pixel) {
614 case 16:
615 depth = (var->green.length == 6) ? 16 : 15;
616 break;
617 case 32:
618 depth = (var->transp.length > 0) ? 32 : 24;
619 break;
620 default:
621 depth = var->bits_per_pixel;
622 break;
623 }
624
625 switch (depth) {
626 case 8:
627 var->red.offset = 0;
628 var->green.offset = 0;
629 var->blue.offset = 0;
630 var->red.length = 8;
631 var->green.length = 8;
632 var->blue.length = 8;
633 var->transp.length = 0;
634 var->transp.offset = 0;
635 break;
636 case 15:
637 var->red.offset = 10;
638 var->green.offset = 5;
639 var->blue.offset = 0;
640 var->red.length = 5;
641 var->green.length = 5;
642 var->blue.length = 5;
643 var->transp.length = 1;
644 var->transp.offset = 15;
645 break;
646 case 16:
647 var->red.offset = 11;
648 var->green.offset = 5;
649 var->blue.offset = 0;
650 var->red.length = 5;
651 var->green.length = 6;
652 var->blue.length = 5;
653 var->transp.length = 0;
654 var->transp.offset = 0;
655 break;
656 case 24:
657 var->red.offset = 16;
658 var->green.offset = 8;
659 var->blue.offset = 0;
660 var->red.length = 8;
661 var->green.length = 8;
662 var->blue.length = 8;
663 var->transp.length = 0;
664 var->transp.offset = 0;
665 break;
666 case 32:
667 var->red.offset = 16;
668 var->green.offset = 8;
669 var->blue.offset = 0;
670 var->red.length = 8;
671 var->green.length = 8;
672 var->blue.length = 8;
673 var->transp.length = 8;
674 var->transp.offset = 24;
675 break;
676 default:
677 return -EINVAL;
678 }
679 return 0;
680}
681EXPORT_SYMBOL(drm_fb_helper_check_var);
682
683/* this will let fbcon do the mode init */
684int drm_fb_helper_set_par(struct fb_info *info)
685{
686 struct drm_fb_helper *fb_helper = info->par;
687 struct drm_device *dev = fb_helper->dev;
688 struct fb_var_screeninfo *var = &info->var;
689 struct drm_crtc *crtc;
690 int ret;
691 int i;
692
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100693 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000694 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000695 return -EINVAL;
696 }
697
Daniel Vetter84849902012-12-02 00:28:11 +0100698 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +0000699 for (i = 0; i < fb_helper->crtc_count; i++) {
700 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100701 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000702 if (ret) {
Daniel Vetter84849902012-12-02 00:28:11 +0100703 drm_modeset_unlock_all(dev);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000704 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000705 }
706 }
Daniel Vetter84849902012-12-02 00:28:11 +0100707 drm_modeset_unlock_all(dev);
Dave Airlie4abe3522010-03-30 05:34:18 +0000708
709 if (fb_helper->delayed_hotplug) {
710 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000711 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000712 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000713 return 0;
714}
715EXPORT_SYMBOL(drm_fb_helper_set_par);
716
717int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
718 struct fb_info *info)
719{
720 struct drm_fb_helper *fb_helper = info->par;
721 struct drm_device *dev = fb_helper->dev;
722 struct drm_mode_set *modeset;
723 struct drm_crtc *crtc;
724 int ret = 0;
725 int i;
726
Daniel Vetter84849902012-12-02 00:28:11 +0100727 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100728 if (!drm_fb_helper_is_bound(fb_helper)) {
729 drm_modeset_unlock_all(dev);
730 return -EBUSY;
731 }
732
Dave Airlie8be48d92010-03-30 05:34:14 +0000733 for (i = 0; i < fb_helper->crtc_count; i++) {
734 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000735
736 modeset = &fb_helper->crtc_info[i].mode_set;
737
738 modeset->x = var->xoffset;
739 modeset->y = var->yoffset;
740
741 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100742 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000743 if (!ret) {
744 info->var.xoffset = var->xoffset;
745 info->var.yoffset = var->yoffset;
746 }
747 }
748 }
Daniel Vetter84849902012-12-02 00:28:11 +0100749 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000750 return ret;
751}
752EXPORT_SYMBOL(drm_fb_helper_pan_display);
753
Dave Airlie8be48d92010-03-30 05:34:14 +0000754int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
755 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000756{
Dave Airlie785b93e2009-08-28 15:46:53 +1000757 int new_fb = 0;
758 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000759 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000760 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000761 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000762 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000763
764 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
765 sizes.surface_depth = 24;
766 sizes.surface_bpp = 32;
767 sizes.fb_width = (unsigned)-1;
768 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000769
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000770 /* if driver picks 8 or 16 by default use that
771 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000772 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000773 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000774
Dave Airlie785b93e2009-08-28 15:46:53 +1000775 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000776 for (i = 0; i < fb_helper->connector_count; i++) {
777 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100778 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000779
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000780 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000781
782 if (cmdline_mode->bpp_specified) {
783 switch (cmdline_mode->bpp) {
784 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000785 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000786 break;
787 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000788 sizes.surface_depth = 15;
789 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000790 break;
791 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000792 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000793 break;
794 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000795 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000796 break;
797 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000798 sizes.surface_depth = 24;
799 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000800 break;
801 }
802 break;
803 }
804 }
805
Dave Airlie8be48d92010-03-30 05:34:14 +0000806 crtc_count = 0;
807 for (i = 0; i < fb_helper->crtc_count; i++) {
808 struct drm_display_mode *desired_mode;
809 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000810
Dave Airlie8be48d92010-03-30 05:34:14 +0000811 if (desired_mode) {
812 if (gamma_size == 0)
813 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
814 if (desired_mode->hdisplay < sizes.fb_width)
815 sizes.fb_width = desired_mode->hdisplay;
816 if (desired_mode->vdisplay < sizes.fb_height)
817 sizes.fb_height = desired_mode->vdisplay;
818 if (desired_mode->hdisplay > sizes.surface_width)
819 sizes.surface_width = desired_mode->hdisplay;
820 if (desired_mode->vdisplay > sizes.surface_height)
821 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000822 crtc_count++;
823 }
824 }
825
Dave Airlie38651672010-03-30 05:34:13 +0000826 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000827 /* hmm everyone went away - assume VGA cable just fell out
828 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000829 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000830 sizes.fb_width = sizes.surface_width = 1024;
831 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000832 }
833
Dave Airlie38651672010-03-30 05:34:13 +0000834 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000835 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000836 if (new_fb < 0)
837 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000838
Dave Airlie38651672010-03-30 05:34:13 +0000839 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000840
Dave Airlie8be48d92010-03-30 05:34:14 +0000841 /* set the fb pointer */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000842 for (i = 0; i < fb_helper->crtc_count; i++)
Dave Airlie8be48d92010-03-30 05:34:14 +0000843 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000844
Dave Airlie785b93e2009-08-28 15:46:53 +1000845 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100846 info->var.pixclock = 0;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000847 if (register_framebuffer(info) < 0)
Dave Airlie785b93e2009-08-28 15:46:53 +1000848 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +0000849
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000850 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
851 info->node, info->fix.id);
Dave Airlie38651672010-03-30 05:34:13 +0000852
Dave Airlie785b93e2009-08-28 15:46:53 +1000853 } else {
854 drm_fb_helper_set_par(info);
855 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000856
857 /* Switch back to kernel console on panic */
858 /* multi card linked list maybe */
859 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000860 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000861 atomic_notifier_chain_register(&panic_notifier_list,
862 &paniced);
863 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
864 }
Dave Airlie38651672010-03-30 05:34:13 +0000865 if (new_fb)
866 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
867
Dave Airlie785b93e2009-08-28 15:46:53 +1000868 return 0;
869}
870EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
871
Dave Airlie3632ef82011-01-15 09:27:00 +1000872void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
873 uint32_t depth)
874{
875 info->fix.type = FB_TYPE_PACKED_PIXELS;
876 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
877 FB_VISUAL_TRUECOLOR;
878 info->fix.mmio_start = 0;
879 info->fix.mmio_len = 0;
880 info->fix.type_aux = 0;
881 info->fix.xpanstep = 1; /* doing it in hw */
882 info->fix.ypanstep = 1; /* doing it in hw */
883 info->fix.ywrapstep = 0;
884 info->fix.accel = FB_ACCEL_NONE;
885 info->fix.type_aux = 0;
886
887 info->fix.line_length = pitch;
888 return;
889}
890EXPORT_SYMBOL(drm_fb_helper_fill_fix);
891
Dave Airlie38651672010-03-30 05:34:13 +0000892void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000893 uint32_t fb_width, uint32_t fb_height)
894{
Dave Airlie38651672010-03-30 05:34:13 +0000895 struct drm_framebuffer *fb = fb_helper->fb;
896 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000897 info->var.xres_virtual = fb->width;
898 info->var.yres_virtual = fb->height;
899 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000900 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000901 info->var.xoffset = 0;
902 info->var.yoffset = 0;
903 info->var.activate = FB_ACTIVATE_NOW;
904 info->var.height = -1;
905 info->var.width = -1;
906
907 switch (fb->depth) {
908 case 8:
909 info->var.red.offset = 0;
910 info->var.green.offset = 0;
911 info->var.blue.offset = 0;
912 info->var.red.length = 8; /* 8bit DAC */
913 info->var.green.length = 8;
914 info->var.blue.length = 8;
915 info->var.transp.offset = 0;
916 info->var.transp.length = 0;
917 break;
918 case 15:
919 info->var.red.offset = 10;
920 info->var.green.offset = 5;
921 info->var.blue.offset = 0;
922 info->var.red.length = 5;
923 info->var.green.length = 5;
924 info->var.blue.length = 5;
925 info->var.transp.offset = 15;
926 info->var.transp.length = 1;
927 break;
928 case 16:
929 info->var.red.offset = 11;
930 info->var.green.offset = 5;
931 info->var.blue.offset = 0;
932 info->var.red.length = 5;
933 info->var.green.length = 6;
934 info->var.blue.length = 5;
935 info->var.transp.offset = 0;
936 break;
937 case 24:
938 info->var.red.offset = 16;
939 info->var.green.offset = 8;
940 info->var.blue.offset = 0;
941 info->var.red.length = 8;
942 info->var.green.length = 8;
943 info->var.blue.length = 8;
944 info->var.transp.offset = 0;
945 info->var.transp.length = 0;
946 break;
947 case 32:
948 info->var.red.offset = 16;
949 info->var.green.offset = 8;
950 info->var.blue.offset = 0;
951 info->var.red.length = 8;
952 info->var.green.length = 8;
953 info->var.blue.length = 8;
954 info->var.transp.offset = 24;
955 info->var.transp.length = 8;
956 break;
957 default:
958 break;
959 }
960
961 info->var.xres = fb_width;
962 info->var.yres = fb_height;
963}
964EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000965
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000966static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
967 uint32_t maxX,
968 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000969{
970 struct drm_connector *connector;
971 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000972 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000973
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000974 for (i = 0; i < fb_helper->connector_count; i++) {
975 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000976 count += connector->funcs->fill_modes(connector, maxX, maxY);
977 }
978
979 return count;
980}
981
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000982static 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 +0000983{
984 struct drm_display_mode *mode;
985
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000986 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000987 if (drm_mode_width(mode) > width ||
988 drm_mode_height(mode) > height)
989 continue;
990 if (mode->type & DRM_MODE_TYPE_PREFERRED)
991 return mode;
992 }
993 return NULL;
994}
995
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000996static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000997{
Chris Wilson1794d252011-04-17 07:43:32 +0100998 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000999 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001000 return cmdline_mode->specified;
1001}
1002
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001003static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1004 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001005{
Chris Wilson1794d252011-04-17 07:43:32 +01001006 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001007 struct drm_display_mode *mode = NULL;
1008
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001009 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001010 if (cmdline_mode->specified == false)
1011 return mode;
1012
1013 /* attempt to find a matching mode in the list of modes
1014 * we have gotten so far, if not add a CVT mode that conforms
1015 */
1016 if (cmdline_mode->rb || cmdline_mode->margins)
1017 goto create_mode;
1018
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001019 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001020 /* check width/height */
1021 if (mode->hdisplay != cmdline_mode->xres ||
1022 mode->vdisplay != cmdline_mode->yres)
1023 continue;
1024
1025 if (cmdline_mode->refresh_specified) {
1026 if (mode->vrefresh != cmdline_mode->refresh)
1027 continue;
1028 }
1029
1030 if (cmdline_mode->interlace) {
1031 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1032 continue;
1033 }
1034 return mode;
1035 }
1036
1037create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001038 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1039 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001040 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001041 return mode;
1042}
1043
1044static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1045{
1046 bool enable;
1047
Sachin Kamat96081cd2012-11-15 03:43:30 +00001048 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001049 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001050 else
Dave Airlie38651672010-03-30 05:34:13 +00001051 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001052
Dave Airlie38651672010-03-30 05:34:13 +00001053 return enable;
1054}
1055
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001056static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1057 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001058{
1059 bool any_enabled = false;
1060 struct drm_connector *connector;
1061 int i = 0;
1062
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001063 for (i = 0; i < fb_helper->connector_count; i++) {
1064 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001065 enabled[i] = drm_connector_enabled(connector, true);
1066 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1067 enabled[i] ? "yes" : "no");
1068 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001069 }
1070
1071 if (any_enabled)
1072 return;
1073
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001074 for (i = 0; i < fb_helper->connector_count; i++) {
1075 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001076 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001077 }
1078}
1079
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001080static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1081 struct drm_display_mode **modes,
1082 bool *enabled, int width, int height)
1083{
1084 int count, i, j;
1085 bool can_clone = false;
1086 struct drm_fb_helper_connector *fb_helper_conn;
1087 struct drm_display_mode *dmt_mode, *mode;
1088
1089 /* only contemplate cloning in the single crtc case */
1090 if (fb_helper->crtc_count > 1)
1091 return false;
1092
1093 count = 0;
1094 for (i = 0; i < fb_helper->connector_count; i++) {
1095 if (enabled[i])
1096 count++;
1097 }
1098
1099 /* only contemplate cloning if more than one connector is enabled */
1100 if (count <= 1)
1101 return false;
1102
1103 /* check the command line or if nothing common pick 1024x768 */
1104 can_clone = true;
1105 for (i = 0; i < fb_helper->connector_count; i++) {
1106 if (!enabled[i])
1107 continue;
1108 fb_helper_conn = fb_helper->connector_info[i];
1109 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1110 if (!modes[i]) {
1111 can_clone = false;
1112 break;
1113 }
1114 for (j = 0; j < i; j++) {
1115 if (!enabled[j])
1116 continue;
1117 if (!drm_mode_equal(modes[j], modes[i]))
1118 can_clone = false;
1119 }
1120 }
1121
1122 if (can_clone) {
1123 DRM_DEBUG_KMS("can clone using command line\n");
1124 return true;
1125 }
1126
1127 /* try and find a 1024x768 mode on each connector */
1128 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001129 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001130
1131 for (i = 0; i < fb_helper->connector_count; i++) {
1132
1133 if (!enabled[i])
1134 continue;
1135
1136 fb_helper_conn = fb_helper->connector_info[i];
1137 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1138 if (drm_mode_equal(mode, dmt_mode))
1139 modes[i] = mode;
1140 }
1141 if (!modes[i])
1142 can_clone = false;
1143 }
1144
1145 if (can_clone) {
1146 DRM_DEBUG_KMS("can clone using 1024x768\n");
1147 return true;
1148 }
1149 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1150 return false;
1151}
1152
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001153static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001154 struct drm_display_mode **modes,
1155 bool *enabled, int width, int height)
1156{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001157 struct drm_fb_helper_connector *fb_helper_conn;
1158 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001159
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001160 for (i = 0; i < fb_helper->connector_count; i++) {
1161 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001162
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001163 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001164 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001165
1166 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001168
1169 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001170 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001171 if (!modes[i]) {
1172 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001173 fb_helper_conn->connector->base.id);
1174 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001175 }
1176 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001177 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1178 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001179 break;
1180 }
1181 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1182 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001183 }
1184 return true;
1185}
1186
Dave Airlie8be48d92010-03-30 05:34:14 +00001187static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1188 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001189 struct drm_display_mode **modes,
1190 int n, int width, int height)
1191{
1192 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001193 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001194 struct drm_connector *connector;
1195 struct drm_connector_helper_funcs *connector_funcs;
1196 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001197 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001198 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001199 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001200 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001201
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001202 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001203 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001204
1205 fb_helper_conn = fb_helper->connector_info[n];
1206 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001207
1208 best_crtcs[n] = NULL;
1209 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001210 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001211 if (modes[n] == NULL)
1212 return best_score;
1213
Dave Airlie8be48d92010-03-30 05:34:14 +00001214 crtcs = kzalloc(dev->mode_config.num_connector *
1215 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001216 if (!crtcs)
1217 return best_score;
1218
1219 my_score = 1;
1220 if (connector->status == connector_status_connected)
1221 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001222 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001223 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001224 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001225 my_score++;
1226
1227 connector_funcs = connector->helper_private;
1228 encoder = connector_funcs->best_encoder(connector);
1229 if (!encoder)
1230 goto out;
1231
Dave Airlie38651672010-03-30 05:34:13 +00001232 /* select a crtc for this connector and then attempt to configure
1233 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001234 for (c = 0; c < fb_helper->crtc_count; c++) {
1235 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001236
Sachin Kamat96081cd2012-11-15 03:43:30 +00001237 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001238 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001239
1240 for (o = 0; o < n; o++)
1241 if (best_crtcs[o] == crtc)
1242 break;
1243
1244 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001245 /* ignore cloning unless only a single crtc */
1246 if (fb_helper->crtc_count > 1)
1247 continue;
1248
1249 if (!drm_mode_equal(modes[o], modes[n]))
1250 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001251 }
1252
1253 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001254 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1255 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001256 width, height);
1257 if (score > best_score) {
1258 best_crtc = crtc;
1259 best_score = score;
1260 memcpy(best_crtcs, crtcs,
1261 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001262 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001263 }
Dave Airlie38651672010-03-30 05:34:13 +00001264 }
1265out:
1266 kfree(crtcs);
1267 return best_score;
1268}
1269
Dave Airlie8be48d92010-03-30 05:34:14 +00001270static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001271{
Dave Airlie8be48d92010-03-30 05:34:14 +00001272 struct drm_device *dev = fb_helper->dev;
1273 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001274 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001275 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001276 bool *enabled;
1277 int width, height;
1278 int i, ret;
1279
1280 DRM_DEBUG_KMS("\n");
1281
1282 width = dev->mode_config.max_width;
1283 height = dev->mode_config.max_height;
1284
Dave Airlie38651672010-03-30 05:34:13 +00001285 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001286 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001287 modes = kcalloc(dev->mode_config.num_connector,
1288 sizeof(struct drm_display_mode *), GFP_KERNEL);
1289 enabled = kcalloc(dev->mode_config.num_connector,
1290 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001291 if (!crtcs || !modes || !enabled) {
1292 DRM_ERROR("Memory allocation failed\n");
1293 goto out;
1294 }
1295
Dave Airlie38651672010-03-30 05:34:13 +00001296
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001297 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001298
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001299 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1300 if (!ret) {
1301 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1302 if (!ret)
1303 DRM_ERROR("Unable to find initial modes\n");
1304 }
Dave Airlie38651672010-03-30 05:34:13 +00001305
1306 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1307
Dave Airlie8be48d92010-03-30 05:34:14 +00001308 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1309
1310 /* need to set the modesets up here for use later */
1311 /* fill out the connector<->crtc mappings into the modesets */
1312 for (i = 0; i < fb_helper->crtc_count; i++) {
1313 modeset = &fb_helper->crtc_info[i].mode_set;
1314 modeset->num_connectors = 0;
1315 }
Dave Airlie38651672010-03-30 05:34:13 +00001316
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001317 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001318 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001319 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1320 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001321
Dave Airlie8be48d92010-03-30 05:34:14 +00001322 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001323 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001324 mode->name, fb_crtc->mode_set.crtc->base.id);
1325 fb_crtc->desired_mode = mode;
1326 if (modeset->mode)
1327 drm_mode_destroy(dev, modeset->mode);
1328 modeset->mode = drm_mode_duplicate(dev,
1329 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001330 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001331 }
Dave Airlie38651672010-03-30 05:34:13 +00001332 }
1333
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001334out:
Dave Airlie38651672010-03-30 05:34:13 +00001335 kfree(crtcs);
1336 kfree(modes);
1337 kfree(enabled);
1338}
1339
1340/**
1341 * drm_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001342 * @fb_helper: fb_helper device struct
1343 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001344 *
1345 * LOCKING:
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001346 * Called at init time by the driver to set up the @fb_helper initial
1347 * configuration, must take the mode config lock.
Dave Airlie38651672010-03-30 05:34:13 +00001348 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001349 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001350 * At the moment, this is a cloned configuration across all heads with
1351 * a new framebuffer object as the backing store.
1352 *
1353 * RETURNS:
1354 * Zero if everything went ok, nonzero otherwise.
1355 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001356bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001357{
Dave Airlie8be48d92010-03-30 05:34:14 +00001358 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001359 int count = 0;
1360
1361 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001362 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001363
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001364 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001365
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001366 count = drm_fb_helper_probe_connector_modes(fb_helper,
1367 dev->mode_config.max_width,
1368 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001369 /*
1370 * we shouldn't end up with no modes here.
1371 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001372 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001373 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001374
Dave Airlie8be48d92010-03-30 05:34:14 +00001375 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001376
Dave Airlie4abe3522010-03-30 05:34:18 +00001377 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001378}
Dave Airlie8be48d92010-03-30 05:34:14 +00001379EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001380
Chris Wilson73943712011-04-22 11:03:57 +01001381/**
1382 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001383 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001384 * @fb_helper: the drm_fb_helper
1385 *
1386 * LOCKING:
1387 * Called at runtime, must take mode config lock.
1388 *
1389 * Scan the connectors attached to the fb_helper and try to put together a
1390 * setup after *notification of a change in output configuration.
1391 *
1392 * RETURNS:
1393 * 0 on success and a non-zero error code otherwise.
1394 */
1395int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001396{
Chris Wilson73943712011-04-22 11:03:57 +01001397 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001398 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001399 u32 max_width, max_height, bpp_sel;
1400
1401 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001402 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001403
Daniel Vetter84849902012-12-02 00:28:11 +01001404 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001405 if (!drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001406 fb_helper->delayed_hotplug = true;
Daniel Vetter84849902012-12-02 00:28:11 +01001407 drm_modeset_unlock_all(dev);
Chris Wilson73943712011-04-22 11:03:57 +01001408 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001409 }
Dave Airlie38651672010-03-30 05:34:13 +00001410 DRM_DEBUG_KMS("\n");
1411
Dave Airlie4abe3522010-03-30 05:34:18 +00001412 max_width = fb_helper->fb->width;
1413 max_height = fb_helper->fb->height;
1414 bpp_sel = fb_helper->fb->bits_per_pixel;
1415
Dave Airlie5c4426a2010-03-30 05:34:17 +00001416 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001417 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001418 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001419 drm_modeset_unlock_all(dev);
Dave Airlie38651672010-03-30 05:34:13 +00001420
Dave Airlie4abe3522010-03-30 05:34:18 +00001421 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001422}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001423EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001424
David Rientjes6a108a12011-01-20 14:44:16 -08001425/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001426 * but the module doesn't depend on any fb console symbols. At least
1427 * attempt to load fbcon to avoid leaving the system without a usable console.
1428 */
David Rientjes6a108a12011-01-20 14:44:16 -08001429#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001430static int __init drm_fb_helper_modinit(void)
1431{
1432 const char *name = "fbcon";
1433 struct module *fbcon;
1434
1435 mutex_lock(&module_mutex);
1436 fbcon = find_module(name);
1437 mutex_unlock(&module_mutex);
1438
1439 if (!fbcon)
1440 request_module_nowait(name);
1441 return 0;
1442}
1443
1444module_init(drm_fb_helper_modinit);
1445#endif