blob: 2c44af691e98e1953582ba1177cf03961ba21961 [file] [log] [blame]
Dave Airlie785b93e2009-08-28 15:46:53 +10001/*
2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5 *
6 * DRM framebuffer helper functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
29 */
Sachin Kamatd56b1b92012-11-15 03:43:29 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Andy Shevchenko3b40a442010-02-02 14:40:32 -080032#include <linux/kernel.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100033#include <linux/sysrq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100035#include <linux/fb.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040036#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/drm_crtc.h>
39#include <drm/drm_fb_helper.h>
40#include <drm/drm_crtc_helper.h>
Dave Airlie785b93e2009-08-28 15:46:53 +100041
Dave Airlie6fcefd52009-09-08 11:08:32 +100042MODULE_AUTHOR("David Airlie, Jesse Barnes");
43MODULE_DESCRIPTION("DRM KMS helper");
44MODULE_LICENSE("GPL and additional rights");
45
Dave Airlie785b93e2009-08-28 15:46:53 +100046static LIST_HEAD(kernel_fb_helper_list);
47
Dave Airlie0b4c0f32010-03-30 05:34:15 +000048/* simple single crtc case helper function */
49int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100050{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000051 struct drm_device *dev = fb_helper->dev;
52 struct drm_connector *connector;
53 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100054
Dave Airlie0b4c0f32010-03-30 05:34:15 +000055 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
56 struct drm_fb_helper_connector *fb_helper_connector;
57
58 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
59 if (!fb_helper_connector)
60 goto fail;
61
62 fb_helper_connector->connector = connector;
63 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
64 }
Dave Airlied50ba252009-09-23 14:44:08 +100065 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000066fail:
67 for (i = 0; i < fb_helper->connector_count; i++) {
68 kfree(fb_helper->connector_info[i]);
69 fb_helper->connector_info[i] = NULL;
70 }
71 fb_helper->connector_count = 0;
72 return -ENOMEM;
Dave Airlied50ba252009-09-23 14:44:08 +100073}
Dave Airlie0b4c0f32010-03-30 05:34:15 +000074EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
Dave Airlied50ba252009-09-23 14:44:08 +100075
Dave Airlie0b4c0f32010-03-30 05:34:15 +000076static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
Dave Airlied50ba252009-09-23 14:44:08 +100077{
Dave Airlie0b4c0f32010-03-30 05:34:15 +000078 struct drm_fb_helper_connector *fb_helper_conn;
79 int i;
Dave Airlied50ba252009-09-23 14:44:08 +100080
Dave Airlie0b4c0f32010-03-30 05:34:15 +000081 for (i = 0; i < fb_helper->connector_count; i++) {
Chris Wilson1794d252011-04-17 07:43:32 +010082 struct drm_cmdline_mode *mode;
83 struct drm_connector *connector;
Dave Airlied50ba252009-09-23 14:44:08 +100084 char *option = NULL;
85
Dave Airlie0b4c0f32010-03-30 05:34:15 +000086 fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +010087 connector = fb_helper_conn->connector;
88 mode = &fb_helper_conn->cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +000089
Dave Airlied50ba252009-09-23 14:44:08 +100090 /* do something on return - turn off connector maybe */
Chris Wilson1794d252011-04-17 07:43:32 +010091 if (fb_get_options(drm_get_connector_name(connector), &option))
Dave Airlied50ba252009-09-23 14:44:08 +100092 continue;
93
Chris Wilson1794d252011-04-17 07:43:32 +010094 if (drm_mode_parse_command_line_for_connector(option,
95 connector,
96 mode)) {
97 if (mode->force) {
98 const char *s;
99 switch (mode->force) {
Sachin Kamat6c910832012-11-15 03:43:28 +0000100 case DRM_FORCE_OFF:
101 s = "OFF";
102 break;
103 case DRM_FORCE_ON_DIGITAL:
104 s = "ON - dig";
105 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100106 default:
Sachin Kamat6c910832012-11-15 03:43:28 +0000107 case DRM_FORCE_ON:
108 s = "ON";
109 break;
Chris Wilson1794d252011-04-17 07:43:32 +0100110 }
111
112 DRM_INFO("forcing %s connector %s\n",
113 drm_get_connector_name(connector), s);
114 connector->force = mode->force;
115 }
116
117 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
118 drm_get_connector_name(connector),
119 mode->xres, mode->yres,
120 mode->refresh_specified ? mode->refresh : 60,
121 mode->rb ? " reduced blanking" : "",
122 mode->margins ? " with margins" : "",
123 mode->interlace ? " interlaced" : "");
124 }
125
Dave Airlied50ba252009-09-23 14:44:08 +1000126 }
127 return 0;
128}
129
Jason Wessel99231022010-10-13 14:09:43 -0500130static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
131{
132 uint16_t *r_base, *g_base, *b_base;
133 int i;
134
135 r_base = crtc->gamma_store;
136 g_base = r_base + crtc->gamma_size;
137 b_base = g_base + crtc->gamma_size;
138
139 for (i = 0; i < crtc->gamma_size; i++)
140 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
141}
142
143static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
144{
145 uint16_t *r_base, *g_base, *b_base;
146
Laurent Pinchartebe0f242012-05-17 13:27:24 +0200147 if (crtc->funcs->gamma_set == NULL)
148 return;
149
Jason Wessel99231022010-10-13 14:09:43 -0500150 r_base = crtc->gamma_store;
151 g_base = r_base + crtc->gamma_size;
152 b_base = g_base + crtc->gamma_size;
153
154 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
155}
156
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500157int drm_fb_helper_debug_enter(struct fb_info *info)
158{
159 struct drm_fb_helper *helper = info->par;
160 struct drm_crtc_helper_funcs *funcs;
161 int i;
162
163 if (list_empty(&kernel_fb_helper_list))
164 return false;
165
166 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
167 for (i = 0; i < helper->crtc_count; i++) {
168 struct drm_mode_set *mode_set =
169 &helper->crtc_info[i].mode_set;
170
171 if (!mode_set->crtc->enabled)
172 continue;
173
174 funcs = mode_set->crtc->helper_private;
Jason Wessel99231022010-10-13 14:09:43 -0500175 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500176 funcs->mode_set_base_atomic(mode_set->crtc,
177 mode_set->fb,
178 mode_set->x,
Jason Wessel413d45d2010-09-26 06:47:25 -0500179 mode_set->y,
Jason Wessel21c74a82010-10-13 14:09:44 -0500180 ENTER_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500181 }
182 }
183
184 return 0;
185}
186EXPORT_SYMBOL(drm_fb_helper_debug_enter);
187
188/* Find the real fb for a given fb helper CRTC */
189static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
190{
191 struct drm_device *dev = crtc->dev;
192 struct drm_crtc *c;
193
194 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
195 if (crtc->base.id == c->base.id)
196 return c->fb;
197 }
198
199 return NULL;
200}
201
202int drm_fb_helper_debug_leave(struct fb_info *info)
203{
204 struct drm_fb_helper *helper = info->par;
205 struct drm_crtc *crtc;
206 struct drm_crtc_helper_funcs *funcs;
207 struct drm_framebuffer *fb;
208 int i;
209
210 for (i = 0; i < helper->crtc_count; i++) {
211 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
212 crtc = mode_set->crtc;
213 funcs = crtc->helper_private;
214 fb = drm_mode_config_fb(crtc);
215
216 if (!crtc->enabled)
217 continue;
218
219 if (!fb) {
220 DRM_ERROR("no fb to restore??\n");
221 continue;
222 }
223
Jason Wessel99231022010-10-13 14:09:43 -0500224 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500225 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
Jason Wessel21c74a82010-10-13 14:09:44 -0500226 crtc->y, LEAVE_ATOMIC_MODE_SET);
Jesse Barnes1a7aba72010-08-05 09:22:31 -0500227 }
228
229 return 0;
230}
231EXPORT_SYMBOL(drm_fb_helper_debug_leave);
232
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100233bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
234{
235 bool error = false;
236 int i, ret;
237 for (i = 0; i < fb_helper->crtc_count; i++) {
238 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
Daniel Vetterd3904752012-07-11 16:28:07 +0200239 ret = mode_set->crtc->funcs->set_config(mode_set);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100240 if (ret)
241 error = true;
242 }
243 return error;
244}
245EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
246
Sachin Kamat78b9c352012-08-01 17:15:32 +0530247static bool drm_fb_helper_force_kernel_mode(void)
Dave Airlie785b93e2009-08-28 15:46:53 +1000248{
Dave Airlie785b93e2009-08-28 15:46:53 +1000249 bool ret, error = false;
250 struct drm_fb_helper *helper;
251
252 if (list_empty(&kernel_fb_helper_list))
253 return false;
254
255 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100256 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
257 continue;
258
259 ret = drm_fb_helper_restore_fbdev_mode(helper);
260 if (ret)
261 error = true;
Dave Airlie785b93e2009-08-28 15:46:53 +1000262 }
263 return error;
264}
265
266int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
267 void *panic_str)
268{
Hugh Dickinsd68752c2011-10-17 17:06:40 -0700269 /*
270 * It's a waste of time and effort to switch back to text console
271 * if the kernel should reboot before panic messages can be seen.
272 */
273 if (panic_timeout < 0)
274 return 0;
275
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000276 pr_err("panic occurred, switching back to text console\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000277 return drm_fb_helper_force_kernel_mode();
Dave Airlie785b93e2009-08-28 15:46:53 +1000278}
279EXPORT_SYMBOL(drm_fb_helper_panic);
280
281static struct notifier_block paniced = {
282 .notifier_call = drm_fb_helper_panic,
283};
284
285/**
286 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
287 *
288 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
289 */
290void drm_fb_helper_restore(void)
291{
292 bool ret;
293 ret = drm_fb_helper_force_kernel_mode();
294 if (ret == true)
295 DRM_ERROR("Failed to restore crtc configuration\n");
296}
297EXPORT_SYMBOL(drm_fb_helper_restore);
298
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200299#ifdef CONFIG_MAGIC_SYSRQ
Dave Airlie785b93e2009-08-28 15:46:53 +1000300static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
301{
302 drm_fb_helper_restore();
303}
304static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
305
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700306static void drm_fb_helper_sysrq(int dummy1)
Dave Airlie785b93e2009-08-28 15:46:53 +1000307{
308 schedule_work(&drm_fb_helper_restore_work);
309}
310
311static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
312 .handler = drm_fb_helper_sysrq,
313 .help_msg = "force-fb(V)",
314 .action_msg = "Restore framebuffer console",
315};
Randy Dunlapb8c40d62010-03-25 18:29:05 +0000316#else
317static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
Mikael Petterssonbea1d352009-09-28 18:26:25 +0200318#endif
Dave Airlie785b93e2009-08-28 15:46:53 +1000319
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100320static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
Dave Airlie785b93e2009-08-28 15:46:53 +1000321{
322 struct drm_fb_helper *fb_helper = info->par;
323 struct drm_device *dev = fb_helper->dev;
324 struct drm_crtc *crtc;
Jesse Barnes023eb572010-07-02 10:48:08 -0700325 struct drm_connector *connector;
Jesse Barnes023eb572010-07-02 10:48:08 -0700326 int i, j;
Dave Airlie785b93e2009-08-28 15:46:53 +1000327
328 /*
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100329 * For each CRTC in this fb, turn the connectors on/off.
Dave Airlie785b93e2009-08-28 15:46:53 +1000330 */
Dave Airlie8be48d92010-03-30 05:34:14 +0000331 mutex_lock(&dev->mode_config.mutex);
Jesse Barnese87b2c42009-09-17 18:14:41 -0700332 for (i = 0; i < fb_helper->crtc_count; i++) {
Dave Airlie8be48d92010-03-30 05:34:14 +0000333 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000334
Dave Airlie8be48d92010-03-30 05:34:14 +0000335 if (!crtc->enabled)
336 continue;
Dave Airlie785b93e2009-08-28 15:46:53 +1000337
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100338 /* Walk the connectors & encoders on this fb turning them on/off */
Jesse Barnes023eb572010-07-02 10:48:08 -0700339 for (j = 0; j < fb_helper->connector_count; j++) {
340 connector = fb_helper->connector_info[j]->connector;
Daniel Vettere04190e2012-09-07 10:14:52 +0200341 connector->funcs->dpms(connector, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700342 drm_connector_property_set_value(connector,
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100343 dev->mode_config.dpms_property, dpms_mode);
Jesse Barnes023eb572010-07-02 10:48:08 -0700344 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000345 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000346 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000347}
348
349int drm_fb_helper_blank(int blank, struct fb_info *info)
350{
351 switch (blank) {
James Simmons731b5a12009-10-29 20:39:07 +0000352 /* Display: On; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000353 case FB_BLANK_UNBLANK:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100354 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
Dave Airlie785b93e2009-08-28 15:46:53 +1000355 break;
James Simmons731b5a12009-10-29 20:39:07 +0000356 /* Display: Off; HSync: On, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000357 case FB_BLANK_NORMAL:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100358 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000359 break;
James Simmons731b5a12009-10-29 20:39:07 +0000360 /* Display: Off; HSync: Off, VSync: On */
Dave Airlie785b93e2009-08-28 15:46:53 +1000361 case FB_BLANK_HSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100362 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
Dave Airlie785b93e2009-08-28 15:46:53 +1000363 break;
James Simmons731b5a12009-10-29 20:39:07 +0000364 /* Display: Off; HSync: On, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000365 case FB_BLANK_VSYNC_SUSPEND:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100366 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
Dave Airlie785b93e2009-08-28 15:46:53 +1000367 break;
James Simmons731b5a12009-10-29 20:39:07 +0000368 /* Display: Off; HSync: Off, VSync: Off */
Dave Airlie785b93e2009-08-28 15:46:53 +1000369 case FB_BLANK_POWERDOWN:
Sascha Hauer3a8148c2012-02-01 11:38:24 +0100370 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
Dave Airlie785b93e2009-08-28 15:46:53 +1000371 break;
372 }
373 return 0;
374}
375EXPORT_SYMBOL(drm_fb_helper_blank);
376
377static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
378{
379 int i;
380
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000381 for (i = 0; i < helper->connector_count; i++)
382 kfree(helper->connector_info[i]);
383 kfree(helper->connector_info);
Sascha Hauera1b77362012-02-01 11:38:22 +0100384 for (i = 0; i < helper->crtc_count; i++) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000385 kfree(helper->crtc_info[i].mode_set.connectors);
Sascha Hauera1b77362012-02-01 11:38:22 +0100386 if (helper->crtc_info[i].mode_set.mode)
387 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
388 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000389 kfree(helper->crtc_info);
390}
391
Dave Airlie4abe3522010-03-30 05:34:18 +0000392int drm_fb_helper_init(struct drm_device *dev,
393 struct drm_fb_helper *fb_helper,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000394 int crtc_count, int max_conn_count)
Dave Airlie785b93e2009-08-28 15:46:53 +1000395{
Dave Airlie785b93e2009-08-28 15:46:53 +1000396 struct drm_crtc *crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000397 int i;
398
Dave Airlie4abe3522010-03-30 05:34:18 +0000399 fb_helper->dev = dev;
Dave Airlie4abe3522010-03-30 05:34:18 +0000400
401 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
402
403 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
404 if (!fb_helper->crtc_info)
Dave Airlie785b93e2009-08-28 15:46:53 +1000405 return -ENOMEM;
406
Dave Airlie4abe3522010-03-30 05:34:18 +0000407 fb_helper->crtc_count = crtc_count;
408 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
409 if (!fb_helper->connector_info) {
410 kfree(fb_helper->crtc_info);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000411 return -ENOMEM;
412 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000413 fb_helper->connector_count = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000414
415 for (i = 0; i < crtc_count; i++) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000416 fb_helper->crtc_info[i].mode_set.connectors =
Dave Airlie785b93e2009-08-28 15:46:53 +1000417 kcalloc(max_conn_count,
418 sizeof(struct drm_connector *),
419 GFP_KERNEL);
420
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200421 if (!fb_helper->crtc_info[i].mode_set.connectors)
Dave Airlie785b93e2009-08-28 15:46:53 +1000422 goto out_free;
Dave Airlie4abe3522010-03-30 05:34:18 +0000423 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000424 }
425
426 i = 0;
427 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie4abe3522010-03-30 05:34:18 +0000428 fb_helper->crtc_info[i].mode_set.crtc = crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000429 i++;
430 }
Sascha Hauere9ad3182012-02-01 11:38:25 +0100431
Dave Airlie785b93e2009-08-28 15:46:53 +1000432 return 0;
433out_free:
Dave Airlie4abe3522010-03-30 05:34:18 +0000434 drm_fb_helper_crtc_free(fb_helper);
Dave Airlie785b93e2009-08-28 15:46:53 +1000435 return -ENOMEM;
436}
Dave Airlie4abe3522010-03-30 05:34:18 +0000437EXPORT_SYMBOL(drm_fb_helper_init);
438
439void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
440{
441 if (!list_empty(&fb_helper->kernel_fb_list)) {
442 list_del(&fb_helper->kernel_fb_list);
443 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000444 pr_info("drm: unregistered panic notifier\n");
Dave Airlie4abe3522010-03-30 05:34:18 +0000445 atomic_notifier_chain_unregister(&panic_notifier_list,
446 &paniced);
447 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
448 }
449 }
450
451 drm_fb_helper_crtc_free(fb_helper);
452
Dave Airlie4abe3522010-03-30 05:34:18 +0000453}
454EXPORT_SYMBOL(drm_fb_helper_fini);
Dave Airlie785b93e2009-08-28 15:46:53 +1000455
Dave Airliec850cb72009-10-23 18:49:03 +1000456static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000457 u16 blue, u16 regno, struct fb_info *info)
458{
459 struct drm_fb_helper *fb_helper = info->par;
460 struct drm_framebuffer *fb = fb_helper->fb;
461 int pindex;
462
Dave Airliec850cb72009-10-23 18:49:03 +1000463 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
464 u32 *palette;
465 u32 value;
466 /* place color in psuedopalette */
467 if (regno > 16)
468 return -EINVAL;
469 palette = (u32 *)info->pseudo_palette;
470 red >>= (16 - info->var.red.length);
471 green >>= (16 - info->var.green.length);
472 blue >>= (16 - info->var.blue.length);
473 value = (red << info->var.red.offset) |
474 (green << info->var.green.offset) |
475 (blue << info->var.blue.offset);
Rob Clark9da12b6a2011-02-16 02:45:51 +0000476 if (info->var.transp.length > 0) {
477 u32 mask = (1 << info->var.transp.length) - 1;
478 mask <<= info->var.transp.offset;
479 value |= mask;
480 }
Dave Airliec850cb72009-10-23 18:49:03 +1000481 palette[regno] = value;
482 return 0;
483 }
484
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000485 pindex = regno;
486
487 if (fb->bits_per_pixel == 16) {
488 pindex = regno << 3;
489
490 if (fb->depth == 16 && regno > 63)
Dave Airliec850cb72009-10-23 18:49:03 +1000491 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000492 if (fb->depth == 15 && regno > 31)
Dave Airliec850cb72009-10-23 18:49:03 +1000493 return -EINVAL;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000494
495 if (fb->depth == 16) {
496 u16 r, g, b;
497 int i;
498 if (regno < 32) {
499 for (i = 0; i < 8; i++)
500 fb_helper->funcs->gamma_set(crtc, red,
501 green, blue, pindex + i);
502 }
503
504 fb_helper->funcs->gamma_get(crtc, &r,
505 &g, &b,
506 pindex >> 1);
507
508 for (i = 0; i < 4; i++)
509 fb_helper->funcs->gamma_set(crtc, r,
510 green, b,
511 (pindex >> 1) + i);
512 }
513 }
514
515 if (fb->depth != 16)
516 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
Dave Airliec850cb72009-10-23 18:49:03 +1000517 return 0;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000518}
519
Dave Airlie068143d2009-10-05 09:58:02 +1000520int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
521{
522 struct drm_fb_helper *fb_helper = info->par;
Dave Airlie8be48d92010-03-30 05:34:14 +0000523 struct drm_crtc_helper_funcs *crtc_funcs;
Dave Airlie068143d2009-10-05 09:58:02 +1000524 u16 *red, *green, *blue, *transp;
525 struct drm_crtc *crtc;
roel062ac622011-03-07 18:00:34 +0100526 int i, j, rc = 0;
Dave Airlie068143d2009-10-05 09:58:02 +1000527 int start;
528
Dave Airlie8be48d92010-03-30 05:34:14 +0000529 for (i = 0; i < fb_helper->crtc_count; i++) {
530 crtc = fb_helper->crtc_info[i].mode_set.crtc;
531 crtc_funcs = crtc->helper_private;
Dave Airlie068143d2009-10-05 09:58:02 +1000532
533 red = cmap->red;
534 green = cmap->green;
535 blue = cmap->blue;
536 transp = cmap->transp;
537 start = cmap->start;
538
roel062ac622011-03-07 18:00:34 +0100539 for (j = 0; j < cmap->len; j++) {
Dave Airlie068143d2009-10-05 09:58:02 +1000540 u16 hred, hgreen, hblue, htransp = 0xffff;
541
542 hred = *red++;
543 hgreen = *green++;
544 hblue = *blue++;
545
546 if (transp)
547 htransp = *transp++;
548
Dave Airliec850cb72009-10-23 18:49:03 +1000549 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
550 if (rc)
551 return rc;
Dave Airlie068143d2009-10-05 09:58:02 +1000552 }
553 crtc_funcs->load_lut(crtc);
554 }
555 return rc;
556}
557EXPORT_SYMBOL(drm_fb_helper_setcmap);
558
Dave Airlie785b93e2009-08-28 15:46:53 +1000559int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
560 struct fb_info *info)
561{
562 struct drm_fb_helper *fb_helper = info->par;
563 struct drm_framebuffer *fb = fb_helper->fb;
564 int depth;
565
Jason Wesself90ebd92010-08-05 09:22:32 -0500566 if (var->pixclock != 0 || in_dbg_master())
Dave Airlie785b93e2009-08-28 15:46:53 +1000567 return -EINVAL;
568
569 /* Need to resize the fb object !!! */
Chris Wilson62fb3762012-03-26 21:15:53 +0100570 if (var->bits_per_pixel > fb->bits_per_pixel ||
571 var->xres > fb->width || var->yres > fb->height ||
572 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
Dave Airlie509c7d82010-01-08 09:27:08 +1000573 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
Chris Wilson62fb3762012-03-26 21:15:53 +0100574 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
575 var->xres, var->yres, var->bits_per_pixel,
576 var->xres_virtual, var->yres_virtual,
Dave Airlie509c7d82010-01-08 09:27:08 +1000577 fb->width, fb->height, fb->bits_per_pixel);
Dave Airlie785b93e2009-08-28 15:46:53 +1000578 return -EINVAL;
579 }
580
581 switch (var->bits_per_pixel) {
582 case 16:
583 depth = (var->green.length == 6) ? 16 : 15;
584 break;
585 case 32:
586 depth = (var->transp.length > 0) ? 32 : 24;
587 break;
588 default:
589 depth = var->bits_per_pixel;
590 break;
591 }
592
593 switch (depth) {
594 case 8:
595 var->red.offset = 0;
596 var->green.offset = 0;
597 var->blue.offset = 0;
598 var->red.length = 8;
599 var->green.length = 8;
600 var->blue.length = 8;
601 var->transp.length = 0;
602 var->transp.offset = 0;
603 break;
604 case 15:
605 var->red.offset = 10;
606 var->green.offset = 5;
607 var->blue.offset = 0;
608 var->red.length = 5;
609 var->green.length = 5;
610 var->blue.length = 5;
611 var->transp.length = 1;
612 var->transp.offset = 15;
613 break;
614 case 16:
615 var->red.offset = 11;
616 var->green.offset = 5;
617 var->blue.offset = 0;
618 var->red.length = 5;
619 var->green.length = 6;
620 var->blue.length = 5;
621 var->transp.length = 0;
622 var->transp.offset = 0;
623 break;
624 case 24:
625 var->red.offset = 16;
626 var->green.offset = 8;
627 var->blue.offset = 0;
628 var->red.length = 8;
629 var->green.length = 8;
630 var->blue.length = 8;
631 var->transp.length = 0;
632 var->transp.offset = 0;
633 break;
634 case 32:
635 var->red.offset = 16;
636 var->green.offset = 8;
637 var->blue.offset = 0;
638 var->red.length = 8;
639 var->green.length = 8;
640 var->blue.length = 8;
641 var->transp.length = 8;
642 var->transp.offset = 24;
643 break;
644 default:
645 return -EINVAL;
646 }
647 return 0;
648}
649EXPORT_SYMBOL(drm_fb_helper_check_var);
650
651/* this will let fbcon do the mode init */
652int drm_fb_helper_set_par(struct fb_info *info)
653{
654 struct drm_fb_helper *fb_helper = info->par;
655 struct drm_device *dev = fb_helper->dev;
656 struct fb_var_screeninfo *var = &info->var;
657 struct drm_crtc *crtc;
658 int ret;
659 int i;
660
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100661 if (var->pixclock != 0) {
Pavel Roskin172e91f2010-02-11 14:31:32 +1000662 DRM_ERROR("PIXEL CLOCK SET\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000663 return -EINVAL;
664 }
665
Dave Airlie8be48d92010-03-30 05:34:14 +0000666 mutex_lock(&dev->mode_config.mutex);
667 for (i = 0; i < fb_helper->crtc_count; i++) {
668 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000669 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
670 if (ret) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000671 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000672 return ret;
Dave Airlie785b93e2009-08-28 15:46:53 +1000673 }
674 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000675 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie4abe3522010-03-30 05:34:18 +0000676
677 if (fb_helper->delayed_hotplug) {
678 fb_helper->delayed_hotplug = false;
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000679 drm_fb_helper_hotplug_event(fb_helper);
Dave Airlie4abe3522010-03-30 05:34:18 +0000680 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000681 return 0;
682}
683EXPORT_SYMBOL(drm_fb_helper_set_par);
684
685int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
686 struct fb_info *info)
687{
688 struct drm_fb_helper *fb_helper = info->par;
689 struct drm_device *dev = fb_helper->dev;
690 struct drm_mode_set *modeset;
691 struct drm_crtc *crtc;
692 int ret = 0;
693 int i;
694
Dave Airlie8be48d92010-03-30 05:34:14 +0000695 mutex_lock(&dev->mode_config.mutex);
696 for (i = 0; i < fb_helper->crtc_count; i++) {
697 crtc = fb_helper->crtc_info[i].mode_set.crtc;
Dave Airlie785b93e2009-08-28 15:46:53 +1000698
699 modeset = &fb_helper->crtc_info[i].mode_set;
700
701 modeset->x = var->xoffset;
702 modeset->y = var->yoffset;
703
704 if (modeset->num_connectors) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000705 ret = crtc->funcs->set_config(modeset);
Dave Airlie785b93e2009-08-28 15:46:53 +1000706 if (!ret) {
707 info->var.xoffset = var->xoffset;
708 info->var.yoffset = var->yoffset;
709 }
710 }
711 }
Dave Airlie8be48d92010-03-30 05:34:14 +0000712 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie785b93e2009-08-28 15:46:53 +1000713 return ret;
714}
715EXPORT_SYMBOL(drm_fb_helper_pan_display);
716
Dave Airlie8be48d92010-03-30 05:34:14 +0000717int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
718 int preferred_bpp)
Dave Airlie785b93e2009-08-28 15:46:53 +1000719{
Dave Airlie785b93e2009-08-28 15:46:53 +1000720 int new_fb = 0;
721 int crtc_count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +0000722 int i;
Dave Airlie785b93e2009-08-28 15:46:53 +1000723 struct fb_info *info;
Dave Airlie38651672010-03-30 05:34:13 +0000724 struct drm_fb_helper_surface_size sizes;
Dave Airlie8be48d92010-03-30 05:34:14 +0000725 int gamma_size = 0;
Dave Airlie38651672010-03-30 05:34:13 +0000726
727 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
728 sizes.surface_depth = 24;
729 sizes.surface_bpp = 32;
730 sizes.fb_width = (unsigned)-1;
731 sizes.fb_height = (unsigned)-1;
Dave Airlie785b93e2009-08-28 15:46:53 +1000732
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000733 /* if driver picks 8 or 16 by default use that
734 for both depth/bpp */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000735 if (preferred_bpp != sizes.surface_bpp)
Dave Airlie38651672010-03-30 05:34:13 +0000736 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000737
Dave Airlie785b93e2009-08-28 15:46:53 +1000738 /* first up get a count of crtcs now in use and new min/maxes width/heights */
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000739 for (i = 0; i < fb_helper->connector_count; i++) {
740 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
Chris Wilson1794d252011-04-17 07:43:32 +0100741 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie8ef86782009-09-26 06:39:00 +1000742
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000743 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlied50ba252009-09-23 14:44:08 +1000744
745 if (cmdline_mode->bpp_specified) {
746 switch (cmdline_mode->bpp) {
747 case 8:
Dave Airlie38651672010-03-30 05:34:13 +0000748 sizes.surface_depth = sizes.surface_bpp = 8;
Dave Airlied50ba252009-09-23 14:44:08 +1000749 break;
750 case 15:
Dave Airlie38651672010-03-30 05:34:13 +0000751 sizes.surface_depth = 15;
752 sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000753 break;
754 case 16:
Dave Airlie38651672010-03-30 05:34:13 +0000755 sizes.surface_depth = sizes.surface_bpp = 16;
Dave Airlied50ba252009-09-23 14:44:08 +1000756 break;
757 case 24:
Dave Airlie38651672010-03-30 05:34:13 +0000758 sizes.surface_depth = sizes.surface_bpp = 24;
Dave Airlied50ba252009-09-23 14:44:08 +1000759 break;
760 case 32:
Dave Airlie38651672010-03-30 05:34:13 +0000761 sizes.surface_depth = 24;
762 sizes.surface_bpp = 32;
Dave Airlied50ba252009-09-23 14:44:08 +1000763 break;
764 }
765 break;
766 }
767 }
768
Dave Airlie8be48d92010-03-30 05:34:14 +0000769 crtc_count = 0;
770 for (i = 0; i < fb_helper->crtc_count; i++) {
771 struct drm_display_mode *desired_mode;
772 desired_mode = fb_helper->crtc_info[i].desired_mode;
Dave Airlie785b93e2009-08-28 15:46:53 +1000773
Dave Airlie8be48d92010-03-30 05:34:14 +0000774 if (desired_mode) {
775 if (gamma_size == 0)
776 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
777 if (desired_mode->hdisplay < sizes.fb_width)
778 sizes.fb_width = desired_mode->hdisplay;
779 if (desired_mode->vdisplay < sizes.fb_height)
780 sizes.fb_height = desired_mode->vdisplay;
781 if (desired_mode->hdisplay > sizes.surface_width)
782 sizes.surface_width = desired_mode->hdisplay;
783 if (desired_mode->vdisplay > sizes.surface_height)
784 sizes.surface_height = desired_mode->vdisplay;
Dave Airlie785b93e2009-08-28 15:46:53 +1000785 crtc_count++;
786 }
787 }
788
Dave Airlie38651672010-03-30 05:34:13 +0000789 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000790 /* hmm everyone went away - assume VGA cable just fell out
791 and will come back later. */
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000792 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
Dave Airlie19b4b442010-03-30 05:34:16 +0000793 sizes.fb_width = sizes.surface_width = 1024;
794 sizes.fb_height = sizes.surface_height = 768;
Dave Airlie785b93e2009-08-28 15:46:53 +1000795 }
796
Dave Airlie38651672010-03-30 05:34:13 +0000797 /* push down into drivers */
Dave Airlie4abe3522010-03-30 05:34:18 +0000798 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
Dave Airlie38651672010-03-30 05:34:13 +0000799 if (new_fb < 0)
800 return new_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000801
Dave Airlie38651672010-03-30 05:34:13 +0000802 info = fb_helper->fbdev;
Dave Airlie785b93e2009-08-28 15:46:53 +1000803
Dave Airlie8be48d92010-03-30 05:34:14 +0000804 /* set the fb pointer */
Sachin Kamat96081cd2012-11-15 03:43:30 +0000805 for (i = 0; i < fb_helper->crtc_count; i++)
Dave Airlie8be48d92010-03-30 05:34:14 +0000806 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000807
Dave Airlie785b93e2009-08-28 15:46:53 +1000808 if (new_fb) {
Clemens Ladisch5349ef32009-11-04 09:42:52 +0100809 info->var.pixclock = 0;
Sachin Kamat96081cd2012-11-15 03:43:30 +0000810 if (register_framebuffer(info) < 0)
Dave Airlie785b93e2009-08-28 15:46:53 +1000811 return -EINVAL;
Dave Airlie38651672010-03-30 05:34:13 +0000812
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000813 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
814 info->node, info->fix.id);
Dave Airlie38651672010-03-30 05:34:13 +0000815
Dave Airlie785b93e2009-08-28 15:46:53 +1000816 } else {
817 drm_fb_helper_set_par(info);
818 }
Dave Airlie785b93e2009-08-28 15:46:53 +1000819
820 /* Switch back to kernel console on panic */
821 /* multi card linked list maybe */
822 if (list_empty(&kernel_fb_helper_list)) {
Sachin Kamatd56b1b92012-11-15 03:43:29 +0000823 dev_info(fb_helper->dev->dev, "registered panic notifier\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000824 atomic_notifier_chain_register(&panic_notifier_list,
825 &paniced);
826 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
827 }
Dave Airlie38651672010-03-30 05:34:13 +0000828 if (new_fb)
829 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
830
Dave Airlie785b93e2009-08-28 15:46:53 +1000831 return 0;
832}
833EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
834
Dave Airlie3632ef82011-01-15 09:27:00 +1000835void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
836 uint32_t depth)
837{
838 info->fix.type = FB_TYPE_PACKED_PIXELS;
839 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
840 FB_VISUAL_TRUECOLOR;
841 info->fix.mmio_start = 0;
842 info->fix.mmio_len = 0;
843 info->fix.type_aux = 0;
844 info->fix.xpanstep = 1; /* doing it in hw */
845 info->fix.ypanstep = 1; /* doing it in hw */
846 info->fix.ywrapstep = 0;
847 info->fix.accel = FB_ACCEL_NONE;
848 info->fix.type_aux = 0;
849
850 info->fix.line_length = pitch;
851 return;
852}
853EXPORT_SYMBOL(drm_fb_helper_fill_fix);
854
Dave Airlie38651672010-03-30 05:34:13 +0000855void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
Dave Airlie785b93e2009-08-28 15:46:53 +1000856 uint32_t fb_width, uint32_t fb_height)
857{
Dave Airlie38651672010-03-30 05:34:13 +0000858 struct drm_framebuffer *fb = fb_helper->fb;
859 info->pseudo_palette = fb_helper->pseudo_palette;
Dave Airlie785b93e2009-08-28 15:46:53 +1000860 info->var.xres_virtual = fb->width;
861 info->var.yres_virtual = fb->height;
862 info->var.bits_per_pixel = fb->bits_per_pixel;
James Simmons57084d02010-12-20 19:10:39 +0000863 info->var.accel_flags = FB_ACCELF_TEXT;
Dave Airlie785b93e2009-08-28 15:46:53 +1000864 info->var.xoffset = 0;
865 info->var.yoffset = 0;
866 info->var.activate = FB_ACTIVATE_NOW;
867 info->var.height = -1;
868 info->var.width = -1;
869
870 switch (fb->depth) {
871 case 8:
872 info->var.red.offset = 0;
873 info->var.green.offset = 0;
874 info->var.blue.offset = 0;
875 info->var.red.length = 8; /* 8bit DAC */
876 info->var.green.length = 8;
877 info->var.blue.length = 8;
878 info->var.transp.offset = 0;
879 info->var.transp.length = 0;
880 break;
881 case 15:
882 info->var.red.offset = 10;
883 info->var.green.offset = 5;
884 info->var.blue.offset = 0;
885 info->var.red.length = 5;
886 info->var.green.length = 5;
887 info->var.blue.length = 5;
888 info->var.transp.offset = 15;
889 info->var.transp.length = 1;
890 break;
891 case 16:
892 info->var.red.offset = 11;
893 info->var.green.offset = 5;
894 info->var.blue.offset = 0;
895 info->var.red.length = 5;
896 info->var.green.length = 6;
897 info->var.blue.length = 5;
898 info->var.transp.offset = 0;
899 break;
900 case 24:
901 info->var.red.offset = 16;
902 info->var.green.offset = 8;
903 info->var.blue.offset = 0;
904 info->var.red.length = 8;
905 info->var.green.length = 8;
906 info->var.blue.length = 8;
907 info->var.transp.offset = 0;
908 info->var.transp.length = 0;
909 break;
910 case 32:
911 info->var.red.offset = 16;
912 info->var.green.offset = 8;
913 info->var.blue.offset = 0;
914 info->var.red.length = 8;
915 info->var.green.length = 8;
916 info->var.blue.length = 8;
917 info->var.transp.offset = 24;
918 info->var.transp.length = 8;
919 break;
920 default:
921 break;
922 }
923
924 info->var.xres = fb_width;
925 info->var.yres = fb_height;
926}
927EXPORT_SYMBOL(drm_fb_helper_fill_var);
Dave Airlie38651672010-03-30 05:34:13 +0000928
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000929static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
930 uint32_t maxX,
931 uint32_t maxY)
Dave Airlie38651672010-03-30 05:34:13 +0000932{
933 struct drm_connector *connector;
934 int count = 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000935 int i;
Dave Airlie38651672010-03-30 05:34:13 +0000936
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000937 for (i = 0; i < fb_helper->connector_count; i++) {
938 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +0000939 count += connector->funcs->fill_modes(connector, maxX, maxY);
940 }
941
942 return count;
943}
944
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000945static 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 +0000946{
947 struct drm_display_mode *mode;
948
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000949 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000950 if (drm_mode_width(mode) > width ||
951 drm_mode_height(mode) > height)
952 continue;
953 if (mode->type & DRM_MODE_TYPE_PREFERRED)
954 return mode;
955 }
956 return NULL;
957}
958
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000959static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
Dave Airlie38651672010-03-30 05:34:13 +0000960{
Chris Wilson1794d252011-04-17 07:43:32 +0100961 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000962 cmdline_mode = &fb_connector->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000963 return cmdline_mode->specified;
964}
965
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000966static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
967 int width, int height)
Dave Airlie38651672010-03-30 05:34:13 +0000968{
Chris Wilson1794d252011-04-17 07:43:32 +0100969 struct drm_cmdline_mode *cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000970 struct drm_display_mode *mode = NULL;
971
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000972 cmdline_mode = &fb_helper_conn->cmdline_mode;
Dave Airlie38651672010-03-30 05:34:13 +0000973 if (cmdline_mode->specified == false)
974 return mode;
975
976 /* attempt to find a matching mode in the list of modes
977 * we have gotten so far, if not add a CVT mode that conforms
978 */
979 if (cmdline_mode->rb || cmdline_mode->margins)
980 goto create_mode;
981
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000982 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
Dave Airlie38651672010-03-30 05:34:13 +0000983 /* check width/height */
984 if (mode->hdisplay != cmdline_mode->xres ||
985 mode->vdisplay != cmdline_mode->yres)
986 continue;
987
988 if (cmdline_mode->refresh_specified) {
989 if (mode->vrefresh != cmdline_mode->refresh)
990 continue;
991 }
992
993 if (cmdline_mode->interlace) {
994 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
995 continue;
996 }
997 return mode;
998 }
999
1000create_mode:
Chris Wilson1794d252011-04-17 07:43:32 +01001001 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1002 cmdline_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001003 list_add(&mode->head, &fb_helper_conn->connector->modes);
Dave Airlie38651672010-03-30 05:34:13 +00001004 return mode;
1005}
1006
1007static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1008{
1009 bool enable;
1010
Sachin Kamat96081cd2012-11-15 03:43:30 +00001011 if (strict)
Dave Airlie38651672010-03-30 05:34:13 +00001012 enable = connector->status == connector_status_connected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001013 else
Dave Airlie38651672010-03-30 05:34:13 +00001014 enable = connector->status != connector_status_disconnected;
Sachin Kamat96081cd2012-11-15 03:43:30 +00001015
Dave Airlie38651672010-03-30 05:34:13 +00001016 return enable;
1017}
1018
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001019static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1020 bool *enabled)
Dave Airlie38651672010-03-30 05:34:13 +00001021{
1022 bool any_enabled = false;
1023 struct drm_connector *connector;
1024 int i = 0;
1025
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001026 for (i = 0; i < fb_helper->connector_count; i++) {
1027 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001028 enabled[i] = drm_connector_enabled(connector, true);
1029 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1030 enabled[i] ? "yes" : "no");
1031 any_enabled |= enabled[i];
Dave Airlie38651672010-03-30 05:34:13 +00001032 }
1033
1034 if (any_enabled)
1035 return;
1036
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001037 for (i = 0; i < fb_helper->connector_count; i++) {
1038 connector = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001039 enabled[i] = drm_connector_enabled(connector, false);
Dave Airlie38651672010-03-30 05:34:13 +00001040 }
1041}
1042
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001043static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1044 struct drm_display_mode **modes,
1045 bool *enabled, int width, int height)
1046{
1047 int count, i, j;
1048 bool can_clone = false;
1049 struct drm_fb_helper_connector *fb_helper_conn;
1050 struct drm_display_mode *dmt_mode, *mode;
1051
1052 /* only contemplate cloning in the single crtc case */
1053 if (fb_helper->crtc_count > 1)
1054 return false;
1055
1056 count = 0;
1057 for (i = 0; i < fb_helper->connector_count; i++) {
1058 if (enabled[i])
1059 count++;
1060 }
1061
1062 /* only contemplate cloning if more than one connector is enabled */
1063 if (count <= 1)
1064 return false;
1065
1066 /* check the command line or if nothing common pick 1024x768 */
1067 can_clone = true;
1068 for (i = 0; i < fb_helper->connector_count; i++) {
1069 if (!enabled[i])
1070 continue;
1071 fb_helper_conn = fb_helper->connector_info[i];
1072 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1073 if (!modes[i]) {
1074 can_clone = false;
1075 break;
1076 }
1077 for (j = 0; j < i; j++) {
1078 if (!enabled[j])
1079 continue;
1080 if (!drm_mode_equal(modes[j], modes[i]))
1081 can_clone = false;
1082 }
1083 }
1084
1085 if (can_clone) {
1086 DRM_DEBUG_KMS("can clone using command line\n");
1087 return true;
1088 }
1089
1090 /* try and find a 1024x768 mode on each connector */
1091 can_clone = true;
Adam Jacksonf6e252b2012-04-13 16:33:31 -04001092 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001093
1094 for (i = 0; i < fb_helper->connector_count; i++) {
1095
1096 if (!enabled[i])
1097 continue;
1098
1099 fb_helper_conn = fb_helper->connector_info[i];
1100 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1101 if (drm_mode_equal(mode, dmt_mode))
1102 modes[i] = mode;
1103 }
1104 if (!modes[i])
1105 can_clone = false;
1106 }
1107
1108 if (can_clone) {
1109 DRM_DEBUG_KMS("can clone using 1024x768\n");
1110 return true;
1111 }
1112 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1113 return false;
1114}
1115
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001116static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
Dave Airlie38651672010-03-30 05:34:13 +00001117 struct drm_display_mode **modes,
1118 bool *enabled, int width, int height)
1119{
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001120 struct drm_fb_helper_connector *fb_helper_conn;
1121 int i;
Dave Airlie38651672010-03-30 05:34:13 +00001122
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001123 for (i = 0; i < fb_helper->connector_count; i++) {
1124 fb_helper_conn = fb_helper->connector_info[i];
Dave Airlie38651672010-03-30 05:34:13 +00001125
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001126 if (enabled[i] == false)
Dave Airlie38651672010-03-30 05:34:13 +00001127 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001128
1129 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001130 fb_helper_conn->connector->base.id);
Dave Airlie38651672010-03-30 05:34:13 +00001131
1132 /* got for command line mode first */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001133 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001134 if (!modes[i]) {
1135 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001136 fb_helper_conn->connector->base.id);
1137 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001138 }
1139 /* No preferred modes, pick one off the list */
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001140 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1141 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
Dave Airlie38651672010-03-30 05:34:13 +00001142 break;
1143 }
1144 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1145 "none");
Dave Airlie38651672010-03-30 05:34:13 +00001146 }
1147 return true;
1148}
1149
Dave Airlie8be48d92010-03-30 05:34:14 +00001150static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1151 struct drm_fb_helper_crtc **best_crtcs,
Dave Airlie38651672010-03-30 05:34:13 +00001152 struct drm_display_mode **modes,
1153 int n, int width, int height)
1154{
1155 int c, o;
Dave Airlie8be48d92010-03-30 05:34:14 +00001156 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001157 struct drm_connector *connector;
1158 struct drm_connector_helper_funcs *connector_funcs;
1159 struct drm_encoder *encoder;
Dave Airlie8be48d92010-03-30 05:34:14 +00001160 struct drm_fb_helper_crtc *best_crtc;
Dave Airlie38651672010-03-30 05:34:13 +00001161 int my_score, best_score, score;
Dave Airlie8be48d92010-03-30 05:34:14 +00001162 struct drm_fb_helper_crtc **crtcs, *crtc;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001163 struct drm_fb_helper_connector *fb_helper_conn;
Dave Airlie38651672010-03-30 05:34:13 +00001164
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001165 if (n == fb_helper->connector_count)
Dave Airlie38651672010-03-30 05:34:13 +00001166 return 0;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001167
1168 fb_helper_conn = fb_helper->connector_info[n];
1169 connector = fb_helper_conn->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001170
1171 best_crtcs[n] = NULL;
1172 best_crtc = NULL;
Dave Airlie8be48d92010-03-30 05:34:14 +00001173 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
Dave Airlie38651672010-03-30 05:34:13 +00001174 if (modes[n] == NULL)
1175 return best_score;
1176
Dave Airlie8be48d92010-03-30 05:34:14 +00001177 crtcs = kzalloc(dev->mode_config.num_connector *
1178 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001179 if (!crtcs)
1180 return best_score;
1181
1182 my_score = 1;
1183 if (connector->status == connector_status_connected)
1184 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001185 if (drm_has_cmdline_mode(fb_helper_conn))
Dave Airlie38651672010-03-30 05:34:13 +00001186 my_score++;
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001187 if (drm_has_preferred_mode(fb_helper_conn, width, height))
Dave Airlie38651672010-03-30 05:34:13 +00001188 my_score++;
1189
1190 connector_funcs = connector->helper_private;
1191 encoder = connector_funcs->best_encoder(connector);
1192 if (!encoder)
1193 goto out;
1194
Dave Airlie38651672010-03-30 05:34:13 +00001195 /* select a crtc for this connector and then attempt to configure
1196 remaining connectors */
Dave Airlie8be48d92010-03-30 05:34:14 +00001197 for (c = 0; c < fb_helper->crtc_count; c++) {
1198 crtc = &fb_helper->crtc_info[c];
Dave Airlie38651672010-03-30 05:34:13 +00001199
Sachin Kamat96081cd2012-11-15 03:43:30 +00001200 if ((encoder->possible_crtcs & (1 << c)) == 0)
Dave Airlie38651672010-03-30 05:34:13 +00001201 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001202
1203 for (o = 0; o < n; o++)
1204 if (best_crtcs[o] == crtc)
1205 break;
1206
1207 if (o < n) {
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001208 /* ignore cloning unless only a single crtc */
1209 if (fb_helper->crtc_count > 1)
1210 continue;
1211
1212 if (!drm_mode_equal(modes[o], modes[n]))
1213 continue;
Dave Airlie38651672010-03-30 05:34:13 +00001214 }
1215
1216 crtcs[n] = crtc;
Dave Airlie8be48d92010-03-30 05:34:14 +00001217 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1218 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
Dave Airlie38651672010-03-30 05:34:13 +00001219 width, height);
1220 if (score > best_score) {
1221 best_crtc = crtc;
1222 best_score = score;
1223 memcpy(best_crtcs, crtcs,
1224 dev->mode_config.num_connector *
Dave Airlie8be48d92010-03-30 05:34:14 +00001225 sizeof(struct drm_fb_helper_crtc *));
Dave Airlie38651672010-03-30 05:34:13 +00001226 }
Dave Airlie38651672010-03-30 05:34:13 +00001227 }
1228out:
1229 kfree(crtcs);
1230 return best_score;
1231}
1232
Dave Airlie8be48d92010-03-30 05:34:14 +00001233static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001234{
Dave Airlie8be48d92010-03-30 05:34:14 +00001235 struct drm_device *dev = fb_helper->dev;
1236 struct drm_fb_helper_crtc **crtcs;
Dave Airlie38651672010-03-30 05:34:13 +00001237 struct drm_display_mode **modes;
Dave Airlie8be48d92010-03-30 05:34:14 +00001238 struct drm_mode_set *modeset;
Dave Airlie38651672010-03-30 05:34:13 +00001239 bool *enabled;
1240 int width, height;
1241 int i, ret;
1242
1243 DRM_DEBUG_KMS("\n");
1244
1245 width = dev->mode_config.max_width;
1246 height = dev->mode_config.max_height;
1247
Dave Airlie38651672010-03-30 05:34:13 +00001248 crtcs = kcalloc(dev->mode_config.num_connector,
Dave Airlie8be48d92010-03-30 05:34:14 +00001249 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
Dave Airlie38651672010-03-30 05:34:13 +00001250 modes = kcalloc(dev->mode_config.num_connector,
1251 sizeof(struct drm_display_mode *), GFP_KERNEL);
1252 enabled = kcalloc(dev->mode_config.num_connector,
1253 sizeof(bool), GFP_KERNEL);
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001254 if (!crtcs || !modes || !enabled) {
1255 DRM_ERROR("Memory allocation failed\n");
1256 goto out;
1257 }
1258
Dave Airlie38651672010-03-30 05:34:13 +00001259
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001260 drm_enable_connectors(fb_helper, enabled);
Dave Airlie38651672010-03-30 05:34:13 +00001261
Dave Airlie1d42bbc2010-05-07 05:02:30 +00001262 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1263 if (!ret) {
1264 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1265 if (!ret)
1266 DRM_ERROR("Unable to find initial modes\n");
1267 }
Dave Airlie38651672010-03-30 05:34:13 +00001268
1269 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1270
Dave Airlie8be48d92010-03-30 05:34:14 +00001271 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1272
1273 /* need to set the modesets up here for use later */
1274 /* fill out the connector<->crtc mappings into the modesets */
1275 for (i = 0; i < fb_helper->crtc_count; i++) {
1276 modeset = &fb_helper->crtc_info[i].mode_set;
1277 modeset->num_connectors = 0;
1278 }
Dave Airlie38651672010-03-30 05:34:13 +00001279
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001280 for (i = 0; i < fb_helper->connector_count; i++) {
Dave Airlie38651672010-03-30 05:34:13 +00001281 struct drm_display_mode *mode = modes[i];
Dave Airlie8be48d92010-03-30 05:34:14 +00001282 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1283 modeset = &fb_crtc->mode_set;
Dave Airlie38651672010-03-30 05:34:13 +00001284
Dave Airlie8be48d92010-03-30 05:34:14 +00001285 if (mode && fb_crtc) {
Dave Airlie38651672010-03-30 05:34:13 +00001286 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
Dave Airlie8be48d92010-03-30 05:34:14 +00001287 mode->name, fb_crtc->mode_set.crtc->base.id);
1288 fb_crtc->desired_mode = mode;
1289 if (modeset->mode)
1290 drm_mode_destroy(dev, modeset->mode);
1291 modeset->mode = drm_mode_duplicate(dev,
1292 fb_crtc->desired_mode);
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001293 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
Dave Airlie38651672010-03-30 05:34:13 +00001294 }
Dave Airlie38651672010-03-30 05:34:13 +00001295 }
1296
Sachin Kamat8c5eaca2012-11-19 09:44:58 +00001297out:
Dave Airlie38651672010-03-30 05:34:13 +00001298 kfree(crtcs);
1299 kfree(modes);
1300 kfree(enabled);
1301}
1302
1303/**
1304 * drm_helper_initial_config - setup a sane initial connector configuration
1305 * @dev: DRM device
1306 *
1307 * LOCKING:
1308 * Called at init time, must take mode config lock.
1309 *
1310 * Scan the CRTCs and connectors and try to put together an initial setup.
1311 * At the moment, this is a cloned configuration across all heads with
1312 * a new framebuffer object as the backing store.
1313 *
1314 * RETURNS:
1315 * Zero if everything went ok, nonzero otherwise.
1316 */
Dave Airlie4abe3522010-03-30 05:34:18 +00001317bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
Dave Airlie38651672010-03-30 05:34:13 +00001318{
Dave Airlie8be48d92010-03-30 05:34:14 +00001319 struct drm_device *dev = fb_helper->dev;
Dave Airlie38651672010-03-30 05:34:13 +00001320 int count = 0;
1321
1322 /* disable all the possible outputs/crtcs before entering KMS mode */
Dave Airlie8be48d92010-03-30 05:34:14 +00001323 drm_helper_disable_unused_functions(fb_helper->dev);
Dave Airlie38651672010-03-30 05:34:13 +00001324
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001325 drm_fb_helper_parse_command_line(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001326
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001327 count = drm_fb_helper_probe_connector_modes(fb_helper,
1328 dev->mode_config.max_width,
1329 dev->mode_config.max_height);
Dave Airlie38651672010-03-30 05:34:13 +00001330 /*
1331 * we shouldn't end up with no modes here.
1332 */
Sachin Kamat96081cd2012-11-15 03:43:30 +00001333 if (count == 0)
Sachin Kamatd56b1b92012-11-15 03:43:29 +00001334 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
Sachin Kamat96081cd2012-11-15 03:43:30 +00001335
Dave Airlie8be48d92010-03-30 05:34:14 +00001336 drm_setup_crtcs(fb_helper);
Dave Airlie38651672010-03-30 05:34:13 +00001337
Dave Airlie4abe3522010-03-30 05:34:18 +00001338 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001339}
Dave Airlie8be48d92010-03-30 05:34:14 +00001340EXPORT_SYMBOL(drm_fb_helper_initial_config);
Dave Airlie38651672010-03-30 05:34:13 +00001341
Chris Wilson73943712011-04-22 11:03:57 +01001342/**
1343 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1344 * probing all the outputs attached to the fb.
1345 * @fb_helper: the drm_fb_helper
1346 *
1347 * LOCKING:
1348 * Called at runtime, must take mode config lock.
1349 *
1350 * Scan the connectors attached to the fb_helper and try to put together a
1351 * setup after *notification of a change in output configuration.
1352 *
1353 * RETURNS:
1354 * 0 on success and a non-zero error code otherwise.
1355 */
1356int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
Dave Airlie38651672010-03-30 05:34:13 +00001357{
Chris Wilson73943712011-04-22 11:03:57 +01001358 struct drm_device *dev = fb_helper->dev;
Dave Airlie5c4426a2010-03-30 05:34:17 +00001359 int count = 0;
Dave Airlie4abe3522010-03-30 05:34:18 +00001360 u32 max_width, max_height, bpp_sel;
Daniel Vetter343065a2012-06-15 11:01:22 +02001361 int bound = 0, crtcs_bound = 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001362 struct drm_crtc *crtc;
Dave Airlie4abe3522010-03-30 05:34:18 +00001363
1364 if (!fb_helper->fb)
Chris Wilson73943712011-04-22 11:03:57 +01001365 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001366
Chris Wilson73943712011-04-22 11:03:57 +01001367 mutex_lock(&dev->mode_config.mutex);
1368 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001369 if (crtc->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001370 crtcs_bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001371 if (crtc->fb == fb_helper->fb)
Daniel Vetter343065a2012-06-15 11:01:22 +02001372 bound++;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001373 }
1374
Daniel Vetter343065a2012-06-15 11:01:22 +02001375 if (bound < crtcs_bound) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001376 fb_helper->delayed_hotplug = true;
Chris Wilson73943712011-04-22 11:03:57 +01001377 mutex_unlock(&dev->mode_config.mutex);
1378 return 0;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001379 }
Dave Airlie38651672010-03-30 05:34:13 +00001380 DRM_DEBUG_KMS("\n");
1381
Dave Airlie4abe3522010-03-30 05:34:18 +00001382 max_width = fb_helper->fb->width;
1383 max_height = fb_helper->fb->height;
1384 bpp_sel = fb_helper->fb->bits_per_pixel;
1385
Dave Airlie5c4426a2010-03-30 05:34:17 +00001386 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
Dave Airlie0b4c0f32010-03-30 05:34:15 +00001387 max_height);
Dave Airlie8be48d92010-03-30 05:34:14 +00001388 drm_setup_crtcs(fb_helper);
Chris Wilson73943712011-04-22 11:03:57 +01001389 mutex_unlock(&dev->mode_config.mutex);
Dave Airlie38651672010-03-30 05:34:13 +00001390
Dave Airlie4abe3522010-03-30 05:34:18 +00001391 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
Dave Airlie38651672010-03-30 05:34:13 +00001392}
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001393EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
Dave Airlie5c4426a2010-03-30 05:34:17 +00001394
David Rientjes6a108a12011-01-20 14:44:16 -08001395/* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001396 * but the module doesn't depend on any fb console symbols. At least
1397 * attempt to load fbcon to avoid leaving the system without a usable console.
1398 */
David Rientjes6a108a12011-01-20 14:44:16 -08001399#if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
David Fries3ce05162010-12-12 12:39:22 -06001400static int __init drm_fb_helper_modinit(void)
1401{
1402 const char *name = "fbcon";
1403 struct module *fbcon;
1404
1405 mutex_lock(&module_mutex);
1406 fbcon = find_module(name);
1407 mutex_unlock(&module_mutex);
1408
1409 if (!fbcon)
1410 request_module_nowait(name);
1411 return 0;
1412}
1413
1414module_init(drm_fb_helper_modinit);
1415#endif