blob: eaab092b995d9e43b8432b00c951b627ffe1c86b [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
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100242/**
243 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration
244 * @fb_helper: fbcon to restore
245 *
246 * This should be called from driver's drm->lastclose callback when implementing
247 * an fbcon on top of kms using this helper. This ensures that the user isn't
248 * greeted with a black screen when e.g. X dies.
249 */
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100250bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
251{
252 bool error = false;
253 int i, ret;
Daniel Vetter6aed8ec2013-01-20 17:32:21 +0100254
255 drm_warn_on_modeset_not_all_locked(fb_helper->dev);
256
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100257 for (i = 0; i < fb_helper->crtc_count; i++) {
258 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100259 ret = drm_mode_set_config_internal(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100260 if (ret)
261 error = true;
262 }
263 return error;
264}
265EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
266
Daniel Vetterd21bf462013-01-20 18:09:52 +0100267/*
268 * restore fbcon display for all kms driver's using this helper, used for sysrq
269 * and panic handling.
270 */
Sachin Kamat78b9c352012-08-01 17:15:32 +0530271static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000272{
Dave Airlie785b93e2009-08-28 15:46:53 +1000273 bool ret, error = false;
274 struct drm_fb_helper *helper;
275
276 if (list_empty(&kernel_fb_helper_list))
277 return false;
278
279 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100280 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
281 continue;
282
283 ret = drm_fb_helper_restore_fbdev_mode(helper);
284 if (ret)
285 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000286 }
287 return error;
288}
289
Daniel Vetter43c8a842013-01-20 18:18:07 +0100290static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
Dave Airlie785b93e2009-08-28 15:46:53 +1000291 void *panic_str)
292{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700293 /*
294 * It's a waste of time and effort to switch back to text console
295 * if the kernel should reboot before panic messages can be seen.
296 */
297 if (panic_timeout < 0)
298 return 0;
299
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000300 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000301 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000302}
Dave Airlie785b93e2009-08-28 15:46:53 +1000303
304static struct notifier_block paniced = {
305 .notifier_call = drm_fb_helper_panic,
306};
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{
Daniel Vetterd21bf462013-01-20 18:09:52 +0100329 bool ret;
330 ret = drm_fb_helper_force_kernel_mode();
331 if (ret == true)
332 DRM_ERROR("Failed to restore crtc configuration\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000333}
334static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
335
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700336static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000337{
338 schedule_work(&drm_fb_helper_restore_work);
339}
340
341static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
342 .handler = drm_fb_helper_sysrq,
343 .help_msg = "force-fb(V)",
344 .action_msg = "Restore framebuffer console",
345};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000346#else
347static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200348#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000349
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100350static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000351{
352 struct drm_fb_helper *fb_helper = info->par;
353 struct drm_device *dev = fb_helper->dev;
354 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700355 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700356 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000357
358 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100359 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000360 */
Daniel Vetter84849902012-12-02 00:28:11 +0100361 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100362 if (!drm_fb_helper_is_bound(fb_helper)) {
363 drm_modeset_unlock_all(dev);
364 return;
365 }
366
Jesse Barnese87b2c42009-09-17 18:14:41 -0700367 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000368 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000369
Dave Airlie8be48d92010-03-30 05:34:14 +0000370 if (!crtc->enabled)
371 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000372
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100373 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700374 for (j = 0; j < fb_helper->connector_count; j++) {
375 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200376 connector->funcs->dpms(connector, dpms_mode);
Rob Clark58495562012-10-11 20:50:56 -0500377 drm_object_property_set_value(&connector->base,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100378 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700379 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000380 }
Daniel Vetter84849902012-12-02 00:28:11 +0100381 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000382}
383
384int drm_fb_helper_blank(int blank, struct fb_info *info)
385{
386 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000387 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000388 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100389 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000390 break;
James Simmons731b5a12009-10-29 20:39:07 +0000391 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000392 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100393 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000394 break;
James Simmons731b5a12009-10-29 20:39:07 +0000395 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000396 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100397 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000398 break;
James Simmons731b5a12009-10-29 20:39:07 +0000399 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000400 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100401 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000402 break;
James Simmons731b5a12009-10-29 20:39:07 +0000403 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000404 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100405 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000406 break;
407 }
408 return 0;
409}
410EXPORT_SYMBOL(drm_fb_helper_blank);
411
412static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
413{
414 int i;
415
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000416 for (i = 0; i < helper->connector_count; i++)
417 kfree(helper->connector_info[i]);
418 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100419 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000420 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100421 if (helper->crtc_info[i].mode_set.mode)
422 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
423 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000424 kfree(helper->crtc_info);
425}
426
Dave Airlie4abe3522010-03-30 05:34:18 +0000427int drm_fb_helper_init(struct drm_device *dev,
428 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000429 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000430{
Dave Airlie785b93e2009-08-28 15:46:53 +1000431 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000432 int i;
433
Dave Airlie4abe3522010-03-30 05:34:18 +0000434 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000435
436 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
437
438 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
439 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000440 return -ENOMEM;
441
Dave Airlie4abe3522010-03-30 05:34:18 +0000442 fb_helper->crtc_count = crtc_count;
443 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
444 if (!fb_helper->connector_info) {
445 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000446 return -ENOMEM;
447 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000448 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000449
450 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000451 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000452 kcalloc(max_conn_count,
453 sizeof(struct drm_connector *),
454 GFP_KERNEL);
455
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200456 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000457 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000458 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000459 }
460
461 i = 0;
462 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000463 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000464 i++;
465 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100466
Dave Airlie785b93e2009-08-28 15:46:53 +1000467 return 0;
468out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000469 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000470 return -ENOMEM;
471}
Dave Airlie4abe3522010-03-30 05:34:18 +0000472EXPORT_SYMBOL(drm_fb_helper_init);
473
474void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
475{
476 if (!list_empty(&fb_helper->kernel_fb_list)) {
477 list_del(&fb_helper->kernel_fb_list);
478 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000479 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000480 atomic_notifier_chain_unregister(&panic_notifier_list,
481 &paniced);
482 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
483 }
484 }
485
486 drm_fb_helper_crtc_free(fb_helper);
487
Dave Airlie4abe3522010-03-30 05:34:18 +0000488}
489EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000490
Dave Airliec850cb72009-10-23 18:49:03 +1000491static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000492 u16 blue, u16 regno, struct fb_info *info)
493{
494 struct drm_fb_helper *fb_helper = info->par;
495 struct drm_framebuffer *fb = fb_helper->fb;
496 int pindex;
497
Dave Airliec850cb72009-10-23 18:49:03 +1000498 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
499 u32 *palette;
500 u32 value;
501 /* place color in psuedopalette */
502 if (regno > 16)
503 return -EINVAL;
504 palette = (u32 *)info->pseudo_palette;
505 red >>= (16 - info->var.red.length);
506 green >>= (16 - info->var.green.length);
507 blue >>= (16 - info->var.blue.length);
508 value = (red << info->var.red.offset) |
509 (green << info->var.green.offset) |
510 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000511 if (info->var.transp.length > 0) {
512 u32 mask = (1 << info->var.transp.length) - 1;
513 mask <<= info->var.transp.offset;
514 value |= mask;
515 }
Dave Airliec850cb72009-10-23 18:49:03 +1000516 palette[regno] = value;
517 return 0;
518 }
519
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000520 pindex = regno;
521
522 if (fb->bits_per_pixel == 16) {
523 pindex = regno << 3;
524
525 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000526 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000527 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000528 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000529
530 if (fb->depth == 16) {
531 u16 r, g, b;
532 int i;
533 if (regno < 32) {
534 for (i = 0; i < 8; i++)
535 fb_helper->funcs->gamma_set(crtc, red,
536 green, blue, pindex + i);
537 }
538
539 fb_helper->funcs->gamma_get(crtc, &r,
540 &g, &b,
541 pindex >> 1);
542
543 for (i = 0; i < 4; i++)
544 fb_helper->funcs->gamma_set(crtc, r,
545 green, b,
546 (pindex >> 1) + i);
547 }
548 }
549
550 if (fb->depth != 16)
551 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000552 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000553}
554
Dave Airlie068143d2009-10-05 09:58:02 +1000555int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
556{
557 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000558 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000559 u16 *red, *green, *blue, *transp;
560 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100561 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000562 int start;
563
Dave Airlie8be48d92010-03-30 05:34:14 +0000564 for (i = 0; i < fb_helper->crtc_count; i++) {
565 crtc = fb_helper->crtc_info[i].mode_set.crtc;
566 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000567
568 red = cmap->red;
569 green = cmap->green;
570 blue = cmap->blue;
571 transp = cmap->transp;
572 start = cmap->start;
573
roel062ac622011-03-07 18:00:34 +0100574 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000575 u16 hred, hgreen, hblue, htransp = 0xffff;
576
577 hred = *red++;
578 hgreen = *green++;
579 hblue = *blue++;
580
581 if (transp)
582 htransp = *transp++;
583
Dave Airliec850cb72009-10-23 18:49:03 +1000584 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
585 if (rc)
586 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000587 }
588 crtc_funcs->load_lut(crtc);
589 }
590 return rc;
591}
592EXPORT_SYMBOL(drm_fb_helper_setcmap);
593
Dave Airlie785b93e2009-08-28 15:46:53 +1000594int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
595 struct fb_info *info)
596{
597 struct drm_fb_helper *fb_helper = info->par;
598 struct drm_framebuffer *fb = fb_helper->fb;
599 int depth;
600
Jason Wesself90ebd92010-08-05 09:22:32 -0500601 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000602 return -EINVAL;
603
604 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100605 if (var->bits_per_pixel > fb->bits_per_pixel ||
606 var->xres > fb->width || var->yres > fb->height ||
607 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000608 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100609 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
610 var->xres, var->yres, var->bits_per_pixel,
611 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000612 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000613 return -EINVAL;
614 }
615
616 switch (var->bits_per_pixel) {
617 case 16:
618 depth = (var->green.length == 6) ? 16 : 15;
619 break;
620 case 32:
621 depth = (var->transp.length > 0) ? 32 : 24;
622 break;
623 default:
624 depth = var->bits_per_pixel;
625 break;
626 }
627
628 switch (depth) {
629 case 8:
630 var->red.offset = 0;
631 var->green.offset = 0;
632 var->blue.offset = 0;
633 var->red.length = 8;
634 var->green.length = 8;
635 var->blue.length = 8;
636 var->transp.length = 0;
637 var->transp.offset = 0;
638 break;
639 case 15:
640 var->red.offset = 10;
641 var->green.offset = 5;
642 var->blue.offset = 0;
643 var->red.length = 5;
644 var->green.length = 5;
645 var->blue.length = 5;
646 var->transp.length = 1;
647 var->transp.offset = 15;
648 break;
649 case 16:
650 var->red.offset = 11;
651 var->green.offset = 5;
652 var->blue.offset = 0;
653 var->red.length = 5;
654 var->green.length = 6;
655 var->blue.length = 5;
656 var->transp.length = 0;
657 var->transp.offset = 0;
658 break;
659 case 24:
660 var->red.offset = 16;
661 var->green.offset = 8;
662 var->blue.offset = 0;
663 var->red.length = 8;
664 var->green.length = 8;
665 var->blue.length = 8;
666 var->transp.length = 0;
667 var->transp.offset = 0;
668 break;
669 case 32:
670 var->red.offset = 16;
671 var->green.offset = 8;
672 var->blue.offset = 0;
673 var->red.length = 8;
674 var->green.length = 8;
675 var->blue.length = 8;
676 var->transp.length = 8;
677 var->transp.offset = 24;
678 break;
679 default:
680 return -EINVAL;
681 }
682 return 0;
683}
684EXPORT_SYMBOL(drm_fb_helper_check_var);
685
686/* this will let fbcon do the mode init */
687int drm_fb_helper_set_par(struct fb_info *info)
688{
689 struct drm_fb_helper *fb_helper = info->par;
690 struct drm_device *dev = fb_helper->dev;
691 struct fb_var_screeninfo *var = &info->var;
692 struct drm_crtc *crtc;
693 int ret;
694 int i;
695
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100696 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000697 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000698 return -EINVAL;
699 }
700
Daniel Vetter84849902012-12-02 00:28:11 +0100701 drm_modeset_lock_all(dev);
Dave Airlie8be48d92010-03-30 05:34:14 +0000702 for (i = 0; i < fb_helper->crtc_count; i++) {
703 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100704 ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000705 if (ret) {
Daniel Vetter84849902012-12-02 00:28:11 +0100706 drm_modeset_unlock_all(dev);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000707 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000708 }
709 }
Daniel Vetter84849902012-12-02 00:28:11 +0100710 drm_modeset_unlock_all(dev);
Dave Airlie4abe3522010-03-30 05:34:18 +0000711
712 if (fb_helper->delayed_hotplug) {
713 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000714 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000715 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000716 return 0;
717}
718EXPORT_SYMBOL(drm_fb_helper_set_par);
719
720int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
721 struct fb_info *info)
722{
723 struct drm_fb_helper *fb_helper = info->par;
724 struct drm_device *dev = fb_helper->dev;
725 struct drm_mode_set *modeset;
726 struct drm_crtc *crtc;
727 int ret = 0;
728 int i;
729
Daniel Vetter84849902012-12-02 00:28:11 +0100730 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +0100731 if (!drm_fb_helper_is_bound(fb_helper)) {
732 drm_modeset_unlock_all(dev);
733 return -EBUSY;
734 }
735
Dave Airlie8be48d92010-03-30 05:34:14 +0000736 for (i = 0; i < fb_helper->crtc_count; i++) {
737 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000738
739 modeset = &fb_helper->crtc_info[i].mode_set;
740
741 modeset->x = var->xoffset;
742 modeset->y = var->yoffset;
743
744 if (modeset->num_connectors) {
Daniel Vetter2d13b672012-12-11 13:47:23 +0100745 ret = drm_mode_set_config_internal(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000746 if (!ret) {
747 info->var.xoffset = var->xoffset;
748 info->var.yoffset = var->yoffset;
749 }
750 }
751 }
Daniel Vetter84849902012-12-02 00:28:11 +0100752 drm_modeset_unlock_all(dev);
Dave Airlie785b93e2009-08-28 15:46:53 +1000753 return ret;
754}
755EXPORT_SYMBOL(drm_fb_helper_pan_display);
756
Dave Airlie8be48d92010-03-30 05:34:14 +0000757int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
758 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000759{
Dave Airlie785b93e2009-08-28 15:46:53 +1000760 int new_fb = 0;
761 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000762 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000763 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000764 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000765 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000766
767 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
768 sizes.surface_depth = 24;
769 sizes.surface_bpp = 32;
770 sizes.fb_width = (unsigned)-1;
771 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000772
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000773 /* if driver picks 8 or 16 by default use that
774 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000775 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000776 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000777
Dave Airlie785b93e2009-08-28 15:46:53 +1000778 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000779 for (i = 0; i < fb_helper->connector_count; i++) {
780 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100781 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000782
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000783 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000784
785 if (cmdline_mode->bpp_specified) {
786 switch (cmdline_mode->bpp) {
787 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000788 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000789 break;
790 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000791 sizes.surface_depth = 15;
792 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000793 break;
794 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000795 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000796 break;
797 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000798 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000799 break;
800 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000801 sizes.surface_depth = 24;
802 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000803 break;
804 }
805 break;
806 }
807 }
808
Dave Airlie8be48d92010-03-30 05:34:14 +0000809 crtc_count = 0;
810 for (i = 0; i < fb_helper->crtc_count; i++) {
811 struct drm_display_mode *desired_mode;
812 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000813
Dave Airlie8be48d92010-03-30 05:34:14 +0000814 if (desired_mode) {
815 if (gamma_size == 0)
816 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
817 if (desired_mode->hdisplay < sizes.fb_width)
818 sizes.fb_width = desired_mode->hdisplay;
819 if (desired_mode->vdisplay < sizes.fb_height)
820 sizes.fb_height = desired_mode->vdisplay;
821 if (desired_mode->hdisplay > sizes.surface_width)
822 sizes.surface_width = desired_mode->hdisplay;
823 if (desired_mode->vdisplay > sizes.surface_height)
824 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000825 crtc_count++;
826 }
827 }
828
Dave Airlie38651672010-03-30 05:34:13 +0000829 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000830 /* hmm everyone went away - assume VGA cable just fell out
831 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000832 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000833 sizes.fb_width = sizes.surface_width = 1024;
834 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000835 }
836
Dave Airlie38651672010-03-30 05:34:13 +0000837 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000838 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000839 if (new_fb < 0)
840 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000841
Dave Airlie38651672010-03-30 05:34:13 +0000842 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000843
Dave Airlie8be48d92010-03-30 05:34:14 +0000844 /* set the fb pointer */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000845 for (i = 0; i < fb_helper->crtc_count; i++)
Dave Airlie8be48d92010-03-30 05:34:14 +0000846 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000847
Dave Airlie785b93e2009-08-28 15:46:53 +1000848 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100849 info->var.pixclock = 0;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000850 if (register_framebuffer(info) < 0)
Dave Airlie785b93e2009-08-28 15:46:53 +1000851 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +0000852
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000853 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
854 info->node, info->fix.id);
Dave Airlie38651672010-03-30 05:34:13 +0000855
Dave Airlie785b93e2009-08-28 15:46:53 +1000856 } else {
857 drm_fb_helper_set_par(info);
858 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000859
860 /* Switch back to kernel console on panic */
861 /* multi card linked list maybe */
862 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000863 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000864 atomic_notifier_chain_register(&panic_notifier_list,
865 &paniced);
866 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
867 }
Dave Airlie38651672010-03-30 05:34:13 +0000868 if (new_fb)
869 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
870
Dave Airlie785b93e2009-08-28 15:46:53 +1000871 return 0;
872}
873EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
874
Dave Airlie3632ef82011-01-15 09:27:00 +1000875void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
876 uint32_t depth)
877{
878 info->fix.type = FB_TYPE_PACKED_PIXELS;
879 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
880 FB_VISUAL_TRUECOLOR;
881 info->fix.mmio_start = 0;
882 info->fix.mmio_len = 0;
883 info->fix.type_aux = 0;
884 info->fix.xpanstep = 1; /* doing it in hw */
885 info->fix.ypanstep = 1; /* doing it in hw */
886 info->fix.ywrapstep = 0;
887 info->fix.accel = FB_ACCEL_NONE;
888 info->fix.type_aux = 0;
889
890 info->fix.line_length = pitch;
891 return;
892}
893EXPORT_SYMBOL(drm_fb_helper_fill_fix);
894
Dave Airlie38651672010-03-30 05:34:13 +0000895void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000896 uint32_t fb_width, uint32_t fb_height)
897{
Dave Airlie38651672010-03-30 05:34:13 +0000898 struct drm_framebuffer *fb = fb_helper->fb;
899 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000900 info->var.xres_virtual = fb->width;
901 info->var.yres_virtual = fb->height;
902 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000903 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000904 info->var.xoffset = 0;
905 info->var.yoffset = 0;
906 info->var.activate = FB_ACTIVATE_NOW;
907 info->var.height = -1;
908 info->var.width = -1;
909
910 switch (fb->depth) {
911 case 8:
912 info->var.red.offset = 0;
913 info->var.green.offset = 0;
914 info->var.blue.offset = 0;
915 info->var.red.length = 8; /* 8bit DAC */
916 info->var.green.length = 8;
917 info->var.blue.length = 8;
918 info->var.transp.offset = 0;
919 info->var.transp.length = 0;
920 break;
921 case 15:
922 info->var.red.offset = 10;
923 info->var.green.offset = 5;
924 info->var.blue.offset = 0;
925 info->var.red.length = 5;
926 info->var.green.length = 5;
927 info->var.blue.length = 5;
928 info->var.transp.offset = 15;
929 info->var.transp.length = 1;
930 break;
931 case 16:
932 info->var.red.offset = 11;
933 info->var.green.offset = 5;
934 info->var.blue.offset = 0;
935 info->var.red.length = 5;
936 info->var.green.length = 6;
937 info->var.blue.length = 5;
938 info->var.transp.offset = 0;
939 break;
940 case 24:
941 info->var.red.offset = 16;
942 info->var.green.offset = 8;
943 info->var.blue.offset = 0;
944 info->var.red.length = 8;
945 info->var.green.length = 8;
946 info->var.blue.length = 8;
947 info->var.transp.offset = 0;
948 info->var.transp.length = 0;
949 break;
950 case 32:
951 info->var.red.offset = 16;
952 info->var.green.offset = 8;
953 info->var.blue.offset = 0;
954 info->var.red.length = 8;
955 info->var.green.length = 8;
956 info->var.blue.length = 8;
957 info->var.transp.offset = 24;
958 info->var.transp.length = 8;
959 break;
960 default:
961 break;
962 }
963
964 info->var.xres = fb_width;
965 info->var.yres = fb_height;
966}
967EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000968
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000969static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
970 uint32_t maxX,
971 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000972{
973 struct drm_connector *connector;
974 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000975 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000976
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000977 for (i = 0; i < fb_helper->connector_count; i++) {
978 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000979 count += connector->funcs->fill_modes(connector, maxX, maxY);
980 }
981
982 return count;
983}
984
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000985static 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 +0000986{
987 struct drm_display_mode *mode;
988
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000989 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000990 if (drm_mode_width(mode) > width ||
991 drm_mode_height(mode) > height)
992 continue;
993 if (mode->type & DRM_MODE_TYPE_PREFERRED)
994 return mode;
995 }
996 return NULL;
997}
998
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000999static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +00001000{
Chris Wilson1794d252011-04-17 07:43:32 +01001001 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001002 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001003 return cmdline_mode->specified;
1004}
1005
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001006static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1007 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +00001008{
Chris Wilson1794d252011-04-17 07:43:32 +01001009 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001010 struct drm_display_mode *mode = NULL;
1011
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001012 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +00001013 if (cmdline_mode->specified == false)
1014 return mode;
1015
1016 /* attempt to find a matching mode in the list of modes
1017 * we have gotten so far, if not add a CVT mode that conforms
1018 */
1019 if (cmdline_mode->rb || cmdline_mode->margins)
1020 goto create_mode;
1021
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001022 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +00001023 /* check width/height */
1024 if (mode->hdisplay != cmdline_mode->xres ||
1025 mode->vdisplay != cmdline_mode->yres)
1026 continue;
1027
1028 if (cmdline_mode->refresh_specified) {
1029 if (mode->vrefresh != cmdline_mode->refresh)
1030 continue;
1031 }
1032
1033 if (cmdline_mode->interlace) {
1034 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1035 continue;
1036 }
1037 return mode;
1038 }
1039
1040create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001041 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1042 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001043 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001044 return mode;
1045}
1046
1047static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1048{
1049 bool enable;
1050
Sachin Kamat96081cd2012-11-15 03:43:30 +00001051 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001052 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001053 else
Dave Airlie38651672010-03-30 05:34:13 +00001054 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001055
Dave Airlie38651672010-03-30 05:34:13 +00001056 return enable;
1057}
1058
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001059static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1060 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001061{
1062 bool any_enabled = false;
1063 struct drm_connector *connector;
1064 int i = 0;
1065
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001066 for (i = 0; i < fb_helper->connector_count; i++) {
1067 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001068 enabled[i] = drm_connector_enabled(connector, true);
1069 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1070 enabled[i] ? "yes" : "no");
1071 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001072 }
1073
1074 if (any_enabled)
1075 return;
1076
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001077 for (i = 0; i < fb_helper->connector_count; i++) {
1078 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001079 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001080 }
1081}
1082
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001083static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1084 struct drm_display_mode **modes,
1085 bool *enabled, int width, int height)
1086{
1087 int count, i, j;
1088 bool can_clone = false;
1089 struct drm_fb_helper_connector *fb_helper_conn;
1090 struct drm_display_mode *dmt_mode, *mode;
1091
1092 /* only contemplate cloning in the single crtc case */
1093 if (fb_helper->crtc_count > 1)
1094 return false;
1095
1096 count = 0;
1097 for (i = 0; i < fb_helper->connector_count; i++) {
1098 if (enabled[i])
1099 count++;
1100 }
1101
1102 /* only contemplate cloning if more than one connector is enabled */
1103 if (count <= 1)
1104 return false;
1105
1106 /* check the command line or if nothing common pick 1024x768 */
1107 can_clone = true;
1108 for (i = 0; i < fb_helper->connector_count; i++) {
1109 if (!enabled[i])
1110 continue;
1111 fb_helper_conn = fb_helper->connector_info[i];
1112 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1113 if (!modes[i]) {
1114 can_clone = false;
1115 break;
1116 }
1117 for (j = 0; j < i; j++) {
1118 if (!enabled[j])
1119 continue;
1120 if (!drm_mode_equal(modes[j], modes[i]))
1121 can_clone = false;
1122 }
1123 }
1124
1125 if (can_clone) {
1126 DRM_DEBUG_KMS("can clone using command line\n");
1127 return true;
1128 }
1129
1130 /* try and find a 1024x768 mode on each connector */
1131 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001132 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001133
1134 for (i = 0; i < fb_helper->connector_count; i++) {
1135
1136 if (!enabled[i])
1137 continue;
1138
1139 fb_helper_conn = fb_helper->connector_info[i];
1140 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1141 if (drm_mode_equal(mode, dmt_mode))
1142 modes[i] = mode;
1143 }
1144 if (!modes[i])
1145 can_clone = false;
1146 }
1147
1148 if (can_clone) {
1149 DRM_DEBUG_KMS("can clone using 1024x768\n");
1150 return true;
1151 }
1152 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1153 return false;
1154}
1155
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001156static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001157 struct drm_display_mode **modes,
1158 bool *enabled, int width, int height)
1159{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001160 struct drm_fb_helper_connector *fb_helper_conn;
1161 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001162
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001163 for (i = 0; i < fb_helper->connector_count; i++) {
1164 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001165
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001166 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001167 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001168
1169 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001170 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001171
1172 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001173 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001174 if (!modes[i]) {
1175 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001176 fb_helper_conn->connector->base.id);
1177 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001178 }
1179 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001180 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1181 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001182 break;
1183 }
1184 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1185 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001186 }
1187 return true;
1188}
1189
Dave Airlie8be48d92010-03-30 05:34:14 +00001190static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1191 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001192 struct drm_display_mode **modes,
1193 int n, int width, int height)
1194{
1195 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001196 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001197 struct drm_connector *connector;
1198 struct drm_connector_helper_funcs *connector_funcs;
1199 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001200 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001201 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001202 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001203 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001204
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001205 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001206 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001207
1208 fb_helper_conn = fb_helper->connector_info[n];
1209 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001210
1211 best_crtcs[n] = NULL;
1212 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001213 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001214 if (modes[n] == NULL)
1215 return best_score;
1216
Dave Airlie8be48d92010-03-30 05:34:14 +00001217 crtcs = kzalloc(dev->mode_config.num_connector *
1218 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001219 if (!crtcs)
1220 return best_score;
1221
1222 my_score = 1;
1223 if (connector->status == connector_status_connected)
1224 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001225 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001226 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001227 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001228 my_score++;
1229
1230 connector_funcs = connector->helper_private;
1231 encoder = connector_funcs->best_encoder(connector);
1232 if (!encoder)
1233 goto out;
1234
Dave Airlie38651672010-03-30 05:34:13 +00001235 /* select a crtc for this connector and then attempt to configure
1236 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001237 for (c = 0; c < fb_helper->crtc_count; c++) {
1238 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001239
Sachin Kamat96081cd2012-11-15 03:43:30 +00001240 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001241 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001242
1243 for (o = 0; o < n; o++)
1244 if (best_crtcs[o] == crtc)
1245 break;
1246
1247 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001248 /* ignore cloning unless only a single crtc */
1249 if (fb_helper->crtc_count > 1)
1250 continue;
1251
1252 if (!drm_mode_equal(modes[o], modes[n]))
1253 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001254 }
1255
1256 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001257 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1258 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001259 width, height);
1260 if (score > best_score) {
1261 best_crtc = crtc;
1262 best_score = score;
1263 memcpy(best_crtcs, crtcs,
1264 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001265 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001266 }
Dave Airlie38651672010-03-30 05:34:13 +00001267 }
1268out:
1269 kfree(crtcs);
1270 return best_score;
1271}
1272
Dave Airlie8be48d92010-03-30 05:34:14 +00001273static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001274{
Dave Airlie8be48d92010-03-30 05:34:14 +00001275 struct drm_device *dev = fb_helper->dev;
1276 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001277 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001278 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001279 bool *enabled;
1280 int width, height;
1281 int i, ret;
1282
1283 DRM_DEBUG_KMS("\n");
1284
1285 width = dev->mode_config.max_width;
1286 height = dev->mode_config.max_height;
1287
Dave Airlie38651672010-03-30 05:34:13 +00001288 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001289 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001290 modes = kcalloc(dev->mode_config.num_connector,
1291 sizeof(struct drm_display_mode *), GFP_KERNEL);
1292 enabled = kcalloc(dev->mode_config.num_connector,
1293 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001294 if (!crtcs || !modes || !enabled) {
1295 DRM_ERROR("Memory allocation failed\n");
1296 goto out;
1297 }
1298
Dave Airlie38651672010-03-30 05:34:13 +00001299
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001300 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001301
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001302 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1303 if (!ret) {
1304 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1305 if (!ret)
1306 DRM_ERROR("Unable to find initial modes\n");
1307 }
Dave Airlie38651672010-03-30 05:34:13 +00001308
1309 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1310
Dave Airlie8be48d92010-03-30 05:34:14 +00001311 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1312
1313 /* need to set the modesets up here for use later */
1314 /* fill out the connector<->crtc mappings into the modesets */
1315 for (i = 0; i < fb_helper->crtc_count; i++) {
1316 modeset = &fb_helper->crtc_info[i].mode_set;
1317 modeset->num_connectors = 0;
1318 }
Dave Airlie38651672010-03-30 05:34:13 +00001319
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001320 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001321 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001322 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1323 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001324
Dave Airlie8be48d92010-03-30 05:34:14 +00001325 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001326 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001327 mode->name, fb_crtc->mode_set.crtc->base.id);
1328 fb_crtc->desired_mode = mode;
1329 if (modeset->mode)
1330 drm_mode_destroy(dev, modeset->mode);
1331 modeset->mode = drm_mode_duplicate(dev,
1332 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001333 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001334 }
Dave Airlie38651672010-03-30 05:34:13 +00001335 }
1336
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001337out:
Dave Airlie38651672010-03-30 05:34:13 +00001338 kfree(crtcs);
1339 kfree(modes);
1340 kfree(enabled);
1341}
1342
1343/**
1344 * drm_helper_initial_config - setup a sane initial connector configuration
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001345 * @fb_helper: fb_helper device struct
1346 * @bpp_sel: bpp value to use for the framebuffer configuration
Dave Airlie38651672010-03-30 05:34:13 +00001347 *
1348 * LOCKING:
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001349 * Called at init time by the driver to set up the @fb_helper initial
1350 * configuration, must take the mode config lock.
Dave Airlie38651672010-03-30 05:34:13 +00001351 *
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001352 * Scans the CRTCs and connectors and tries to put together an initial setup.
Dave Airlie38651672010-03-30 05:34:13 +00001353 * At the moment, this is a cloned configuration across all heads with
1354 * a new framebuffer object as the backing store.
1355 *
1356 * RETURNS:
1357 * Zero if everything went ok, nonzero otherwise.
1358 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001359bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001360{
Dave Airlie8be48d92010-03-30 05:34:14 +00001361 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001362 int count = 0;
1363
1364 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001365 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001366
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001367 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001368
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001369 count = drm_fb_helper_probe_connector_modes(fb_helper,
1370 dev->mode_config.max_width,
1371 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001372 /*
1373 * we shouldn't end up with no modes here.
1374 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001375 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001376 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001377
Dave Airlie8be48d92010-03-30 05:34:14 +00001378 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001379
Dave Airlie4abe3522010-03-30 05:34:18 +00001380 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001381}
Dave Airlie8be48d92010-03-30 05:34:14 +00001382EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001383
Chris Wilson73943712011-04-22 11:03:57 +01001384/**
1385 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
Daniel Vetterd0ddc0332012-11-01 14:45:17 +01001386 * probing all the outputs attached to the fb
Chris Wilson73943712011-04-22 11:03:57 +01001387 * @fb_helper: the drm_fb_helper
1388 *
1389 * LOCKING:
1390 * Called at runtime, must take mode config lock.
1391 *
1392 * Scan the connectors attached to the fb_helper and try to put together a
1393 * setup after *notification of a change in output configuration.
1394 *
1395 * RETURNS:
1396 * 0 on success and a non-zero error code otherwise.
1397 */
1398int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001399{
Chris Wilson73943712011-04-22 11:03:57 +01001400 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001401 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001402 u32 max_width, max_height, bpp_sel;
1403
1404 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001405 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001406
Daniel Vetter84849902012-12-02 00:28:11 +01001407 drm_modeset_lock_all(dev);
Daniel Vetter20c60c32012-12-17 12:13:23 +01001408 if (!drm_fb_helper_is_bound(fb_helper)) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001409 fb_helper->delayed_hotplug = true;
Daniel Vetter84849902012-12-02 00:28:11 +01001410 drm_modeset_unlock_all(dev);
Chris Wilson73943712011-04-22 11:03:57 +01001411 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001412 }
Dave Airlie38651672010-03-30 05:34:13 +00001413 DRM_DEBUG_KMS("\n");
1414
Dave Airlie4abe3522010-03-30 05:34:18 +00001415 max_width = fb_helper->fb->width;
1416 max_height = fb_helper->fb->height;
1417 bpp_sel = fb_helper->fb->bits_per_pixel;
1418
Dave Airlie5c4426a2010-03-30 05:34:17 +00001419 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001420 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001421 drm_setup_crtcs(fb_helper);
Daniel Vetter84849902012-12-02 00:28:11 +01001422 drm_modeset_unlock_all(dev);
Dave Airlie38651672010-03-30 05:34:13 +00001423
Dave Airlie4abe3522010-03-30 05:34:18 +00001424 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001425}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001426EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001427
David Rientjes6a108a12011-01-20 14:44:16 -08001428/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001429 * but the module doesn't depend on any fb console symbols. At least
1430 * attempt to load fbcon to avoid leaving the system without a usable console.
1431 */
David Rientjes6a108a12011-01-20 14:44:16 -08001432#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001433static int __init drm_fb_helper_modinit(void)
1434{
1435 const char *name = "fbcon";
1436 struct module *fbcon;
1437
1438 mutex_lock(&module_mutex);
1439 fbcon = find_module(name);
1440 mutex_unlock(&module_mutex);
1441
1442 if (!fbcon)
1443 request_module_nowait(name);
1444 return 0;
1445}
1446
1447module_init(drm_fb_helper_modinit);
1448#endif