Subversion Repositories Kolibri OS

Rev

Rev 4104 | Rev 4560 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1179 serge 1
/*
2
 * Copyright (c) 2006-2009 Red Hat Inc.
3
 * Copyright (c) 2006-2008 Intel Corporation
4
 * Copyright (c) 2007 Dave Airlie 
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 
28
 *      Jesse Barnes 
29
 */
3192 Serge 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
 
1430 serge 32
#include 
1963 serge 33
#include 
34
#include 
1179 serge 35
#include 
3031 serge 36
#include 
37
#include 
38
#include 
39
#include 
40
#include 
1179 serge 41
 
1430 serge 42
MODULE_AUTHOR("David Airlie, Jesse Barnes");
43
MODULE_DESCRIPTION("DRM KMS helper");
44
MODULE_LICENSE("GPL and additional rights");
1179 serge 45
 
46
static LIST_HEAD(kernel_fb_helper_list);
47
 
3192 Serge 48
/**
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.
3480 Serge 55
 *
56
 * Initialization is done as a three-step process with drm_fb_helper_init(),
57
 * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
58
 * Drivers with fancier requirements than the default beheviour can override the
59
 * second step with their own code.  Teardown is done with drm_fb_helper_fini().
60
 *
61
 * At runtime drivers should restore the fbdev console by calling
62
 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
63
 * should also notify the fb helper code from updates to the output
64
 * configuration by calling drm_fb_helper_hotplug_event(). For easier
65
 * integration with the output polling code in drm_crtc_helper.c the modeset
66
 * code proves a ->output_poll_changed callback.
67
 *
68
 * All other functions exported by the fb helper library can be used to
69
 * implement the fbdev driver interface by the driver.
3192 Serge 70
 */
71
 
3480 Serge 72
/**
73
 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
74
 * 					       emulation helper
75
 * @fb_helper: fbdev initialized with drm_fb_helper_init
76
 *
77
 * This functions adds all the available connectors for use with the given
78
 * fb_helper. This is a separate step to allow drivers to freely assign
79
 * connectors to the fbdev, e.g. if some are reserved for special purposes or
80
 * not adequate to be used for the fbcon.
81
 *
82
 * Since this is part of the initial setup before the fbdev is published, no
83
 * locking is required.
84
 */
1963 serge 85
int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
1221 serge 86
{
1963 serge 87
	struct drm_device *dev = fb_helper->dev;
88
	struct drm_connector *connector;
89
	int i;
1221 serge 90
 
1963 serge 91
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
92
		struct drm_fb_helper_connector *fb_helper_connector;
93
 
94
		fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
95
		if (!fb_helper_connector)
96
			goto fail;
97
 
98
		fb_helper_connector->connector = connector;
99
		fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
100
	}
1221 serge 101
	return 0;
1963 serge 102
fail:
103
	for (i = 0; i < fb_helper->connector_count; i++) {
104
		kfree(fb_helper->connector_info[i]);
105
		fb_helper->connector_info[i] = NULL;
106
	}
107
	fb_helper->connector_count = 0;
108
    return -ENOMEM;
1221 serge 109
}
1963 serge 110
EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
1221 serge 111
 
1963 serge 112
static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
1179 serge 113
{
1963 serge 114
	uint16_t *r_base, *g_base, *b_base;
115
	int i;
1179 serge 116
 
4104 Serge 117
	if (helper->funcs->gamma_get == NULL)
118
		return;
119
 
1963 serge 120
	r_base = crtc->gamma_store;
121
	g_base = r_base + crtc->gamma_size;
122
	b_base = g_base + crtc->gamma_size;
1179 serge 123
 
1963 serge 124
	for (i = 0; i < crtc->gamma_size; i++)
125
		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
1179 serge 126
}
127
 
1963 serge 128
static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
1179 serge 129
{
1963 serge 130
	uint16_t *r_base, *g_base, *b_base;
131
 
3031 serge 132
	if (crtc->funcs->gamma_set == NULL)
133
		return;
134
 
1963 serge 135
	r_base = crtc->gamma_store;
136
	g_base = r_base + crtc->gamma_size;
137
	b_base = g_base + crtc->gamma_size;
138
 
139
	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
1179 serge 140
}
141
 
142
 
3480 Serge 143
static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
144
{
145
	struct drm_device *dev = fb_helper->dev;
146
	struct drm_crtc *crtc;
147
	int bound = 0, crtcs_bound = 0;
1179 serge 148
 
3480 Serge 149
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
150
		if (crtc->fb)
151
			crtcs_bound++;
152
		if (crtc->fb == fb_helper->fb)
153
			bound++;
154
	}
155
 
156
	if (bound < crtcs_bound)
157
		return false;
158
	return true;
159
}
160
 
3031 serge 161
static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
1179 serge 162
{
163
	struct drm_fb_helper *fb_helper = info->par;
164
	struct drm_device *dev = fb_helper->dev;
165
	struct drm_crtc *crtc;
1963 serge 166
	struct drm_connector *connector;
167
	int i, j;
1179 serge 168
 
169
	/*
3480 Serge 170
	 * fbdev->blank can be called from irq context in case of a panic.
171
	 * Since we already have our own special panic handler which will
172
	 * restore the fbdev console mode completely, just bail out early.
173
	 */
174
 
175
	/*
3031 serge 176
	 * For each CRTC in this fb, turn the connectors on/off.
1179 serge 177
	 */
3480 Serge 178
	drm_modeset_lock_all(dev);
179
	if (!drm_fb_helper_is_bound(fb_helper)) {
180
		drm_modeset_unlock_all(dev);
181
		return;
182
	}
183
 
1179 serge 184
	for (i = 0; i < fb_helper->crtc_count; i++) {
1963 serge 185
		crtc = fb_helper->crtc_info[i].mode_set.crtc;
1179 serge 186
 
1963 serge 187
		if (!crtc->enabled)
1179 serge 188
				continue;
189
 
3031 serge 190
		/* Walk the connectors & encoders on this fb turning them on/off */
1963 serge 191
		for (j = 0; j < fb_helper->connector_count; j++) {
192
			connector = fb_helper->connector_info[j]->connector;
3031 serge 193
			connector->funcs->dpms(connector, dpms_mode);
3192 Serge 194
			drm_object_property_set_value(&connector->base,
3031 serge 195
				dev->mode_config.dpms_property, dpms_mode);
1963 serge 196
		}
197
	}
3480 Serge 198
	drm_modeset_unlock_all(dev);
1179 serge 199
}
200
 
3480 Serge 201
/**
202
 * drm_fb_helper_blank - implementation for ->fb_blank
203
 * @blank: desired blanking state
204
 * @info: fbdev registered by the helper
205
 */
1179 serge 206
int drm_fb_helper_blank(int blank, struct fb_info *info)
207
{
208
	switch (blank) {
1321 serge 209
	/* Display: On; HSync: On, VSync: On */
1179 serge 210
	case FB_BLANK_UNBLANK:
3031 serge 211
		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
1179 serge 212
		break;
1321 serge 213
	/* Display: Off; HSync: On, VSync: On */
1179 serge 214
	case FB_BLANK_NORMAL:
3031 serge 215
		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
1179 serge 216
		break;
1321 serge 217
	/* Display: Off; HSync: Off, VSync: On */
1179 serge 218
	case FB_BLANK_HSYNC_SUSPEND:
3031 serge 219
		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
1179 serge 220
		break;
1321 serge 221
	/* Display: Off; HSync: On, VSync: Off */
1179 serge 222
	case FB_BLANK_VSYNC_SUSPEND:
3031 serge 223
		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
1179 serge 224
		break;
1321 serge 225
	/* Display: Off; HSync: Off, VSync: Off */
1179 serge 226
	case FB_BLANK_POWERDOWN:
3031 serge 227
		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
1179 serge 228
		break;
229
	}
230
	return 0;
231
}
232
EXPORT_SYMBOL(drm_fb_helper_blank);
233
 
234
static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
235
{
236
	int i;
237
 
1963 serge 238
	for (i = 0; i < helper->connector_count; i++)
239
		kfree(helper->connector_info[i]);
240
	kfree(helper->connector_info);
3031 serge 241
	for (i = 0; i < helper->crtc_count; i++) {
1179 serge 242
		kfree(helper->crtc_info[i].mode_set.connectors);
3031 serge 243
		if (helper->crtc_info[i].mode_set.mode)
244
			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
245
	}
1179 serge 246
	kfree(helper->crtc_info);
247
}
248
 
3480 Serge 249
/**
250
 * drm_fb_helper_init - initialize a drm_fb_helper structure
251
 * @dev: drm device
252
 * @fb_helper: driver-allocated fbdev helper structure to initialize
253
 * @crtc_count: maximum number of crtcs to support in this fbdev emulation
254
 * @max_conn_count: max connector count
255
 *
256
 * This allocates the structures for the fbdev helper with the given limits.
257
 * Note that this won't yet touch the hardware (through the driver interfaces)
258
 * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
259
 * to allow driver writes more control over the exact init sequence.
260
 *
261
 * Drivers must set fb_helper->funcs before calling
262
 * drm_fb_helper_initial_config().
263
 *
264
 * RETURNS:
265
 * Zero if everything went ok, nonzero otherwise.
266
 */
1963 serge 267
int drm_fb_helper_init(struct drm_device *dev,
268
		       struct drm_fb_helper *fb_helper,
269
		       int crtc_count, int max_conn_count)
1179 serge 270
{
271
	struct drm_crtc *crtc;
272
	int i;
273
 
1963 serge 274
	fb_helper->dev = dev;
275
 
276
	INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
277
 
278
	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
279
	if (!fb_helper->crtc_info)
1179 serge 280
		return -ENOMEM;
281
 
1963 serge 282
	fb_helper->crtc_count = crtc_count;
283
	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
284
	if (!fb_helper->connector_info) {
285
		kfree(fb_helper->crtc_info);
286
		return -ENOMEM;
287
	}
288
	fb_helper->connector_count = 0;
1179 serge 289
 
290
	for (i = 0; i < crtc_count; i++) {
1963 serge 291
		fb_helper->crtc_info[i].mode_set.connectors =
1179 serge 292
			kcalloc(max_conn_count,
293
				sizeof(struct drm_connector *),
294
				GFP_KERNEL);
295
 
3031 serge 296
		if (!fb_helper->crtc_info[i].mode_set.connectors)
1179 serge 297
			goto out_free;
1963 serge 298
		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
1179 serge 299
	}
300
 
301
	i = 0;
302
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1963 serge 303
		fb_helper->crtc_info[i].mode_set.crtc = crtc;
1179 serge 304
		i++;
305
	}
3031 serge 306
 
1179 serge 307
	return 0;
308
out_free:
1963 serge 309
	drm_fb_helper_crtc_free(fb_helper);
1179 serge 310
	return -ENOMEM;
311
}
1963 serge 312
EXPORT_SYMBOL(drm_fb_helper_init);
1179 serge 313
 
1246 serge 314
static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
1221 serge 315
		     u16 blue, u16 regno, struct fb_info *info)
316
{
317
	struct drm_fb_helper *fb_helper = info->par;
318
	struct drm_framebuffer *fb = fb_helper->fb;
319
	int pindex;
320
 
1246 serge 321
	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
322
		u32 *palette;
323
		u32 value;
324
		/* place color in psuedopalette */
325
		if (regno > 16)
326
			return -EINVAL;
327
		palette = (u32 *)info->pseudo_palette;
328
		red >>= (16 - info->var.red.length);
329
		green >>= (16 - info->var.green.length);
330
		blue >>= (16 - info->var.blue.length);
331
		value = (red << info->var.red.offset) |
332
			(green << info->var.green.offset) |
333
			(blue << info->var.blue.offset);
1963 serge 334
		if (info->var.transp.length > 0) {
335
			u32 mask = (1 << info->var.transp.length) - 1;
336
			mask <<= info->var.transp.offset;
337
			value |= mask;
338
		}
1246 serge 339
		palette[regno] = value;
340
		return 0;
341
	}
342
 
4104 Serge 343
	/*
344
	 * The driver really shouldn't advertise pseudo/directcolor
345
	 * visuals if it can't deal with the palette.
346
	 */
347
	if (WARN_ON(!fb_helper->funcs->gamma_set ||
348
		    !fb_helper->funcs->gamma_get))
349
		return -EINVAL;
350
 
1221 serge 351
	pindex = regno;
352
 
353
	if (fb->bits_per_pixel == 16) {
354
		pindex = regno << 3;
355
 
356
		if (fb->depth == 16 && regno > 63)
1246 serge 357
			return -EINVAL;
1221 serge 358
		if (fb->depth == 15 && regno > 31)
1246 serge 359
			return -EINVAL;
1221 serge 360
 
361
		if (fb->depth == 16) {
362
			u16 r, g, b;
363
			int i;
364
			if (regno < 32) {
365
				for (i = 0; i < 8; i++)
366
					fb_helper->funcs->gamma_set(crtc, red,
367
						green, blue, pindex + i);
368
			}
369
 
370
			fb_helper->funcs->gamma_get(crtc, &r,
371
						    &g, &b,
372
						    pindex >> 1);
373
 
374
			for (i = 0; i < 4; i++)
375
				fb_helper->funcs->gamma_set(crtc, r,
376
							    green, b,
377
							    (pindex >> 1) + i);
378
		}
379
	}
380
 
381
	if (fb->depth != 16)
382
		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
1246 serge 383
	return 0;
1221 serge 384
}
385
 
3480 Serge 386
/**
387
 * drm_fb_helper_setcmap - implementation for ->fb_setcmap
388
 * @cmap: cmap to set
389
 * @info: fbdev registered by the helper
390
 */
1221 serge 391
int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
392
{
393
	struct drm_fb_helper *fb_helper = info->par;
4104 Serge 394
	struct drm_device *dev = fb_helper->dev;
1963 serge 395
	struct drm_crtc_helper_funcs *crtc_funcs;
1221 serge 396
	u16 *red, *green, *blue, *transp;
397
	struct drm_crtc *crtc;
1963 serge 398
	int i, j, rc = 0;
1221 serge 399
	int start;
400
 
4104 Serge 401
	drm_modeset_lock_all(dev);
402
	if (!drm_fb_helper_is_bound(fb_helper)) {
403
		drm_modeset_unlock_all(dev);
404
		return -EBUSY;
405
	}
406
 
1221 serge 407
		for (i = 0; i < fb_helper->crtc_count; i++) {
1963 serge 408
		crtc = fb_helper->crtc_info[i].mode_set.crtc;
409
		crtc_funcs = crtc->helper_private;
1221 serge 410
 
411
		red = cmap->red;
412
		green = cmap->green;
413
		blue = cmap->blue;
414
		transp = cmap->transp;
415
		start = cmap->start;
416
 
1963 serge 417
		for (j = 0; j < cmap->len; j++) {
1221 serge 418
			u16 hred, hgreen, hblue, htransp = 0xffff;
419
 
420
			hred = *red++;
421
			hgreen = *green++;
422
			hblue = *blue++;
423
 
424
			if (transp)
425
				htransp = *transp++;
426
 
1246 serge 427
			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
428
			if (rc)
4104 Serge 429
				goto out;
1221 serge 430
		}
4104 Serge 431
		if (crtc_funcs->load_lut)
1221 serge 432
		crtc_funcs->load_lut(crtc);
433
	}
4104 Serge 434
 out:
435
	drm_modeset_unlock_all(dev);
1221 serge 436
	return rc;
437
}
438
EXPORT_SYMBOL(drm_fb_helper_setcmap);
439
 
3480 Serge 440
/**
441
 * drm_fb_helper_check_var - implementation for ->fb_check_var
442
 * @var: screeninfo to check
443
 * @info: fbdev registered by the helper
444
 */
1179 serge 445
int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
446
			    struct fb_info *info)
447
{
448
	struct drm_fb_helper *fb_helper = info->par;
449
	struct drm_framebuffer *fb = fb_helper->fb;
450
	int depth;
451
 
1963 serge 452
	if (var->pixclock != 0 || in_dbg_master())
1179 serge 453
		return -EINVAL;
454
 
455
	/* Need to resize the fb object !!! */
3031 serge 456
	if (var->bits_per_pixel > fb->bits_per_pixel ||
457
	    var->xres > fb->width || var->yres > fb->height ||
458
	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1404 serge 459
		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
3031 serge 460
			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
461
			  var->xres, var->yres, var->bits_per_pixel,
462
			  var->xres_virtual, var->yres_virtual,
1404 serge 463
			  fb->width, fb->height, fb->bits_per_pixel);
1179 serge 464
		return -EINVAL;
465
	}
466
 
467
	switch (var->bits_per_pixel) {
468
	case 16:
469
		depth = (var->green.length == 6) ? 16 : 15;
470
		break;
471
	case 32:
472
		depth = (var->transp.length > 0) ? 32 : 24;
473
		break;
474
	default:
475
		depth = var->bits_per_pixel;
476
		break;
477
	}
478
 
479
	switch (depth) {
480
	case 8:
481
		var->red.offset = 0;
482
		var->green.offset = 0;
483
		var->blue.offset = 0;
484
		var->red.length = 8;
485
		var->green.length = 8;
486
		var->blue.length = 8;
487
		var->transp.length = 0;
488
		var->transp.offset = 0;
489
		break;
490
	case 15:
491
		var->red.offset = 10;
492
		var->green.offset = 5;
493
		var->blue.offset = 0;
494
		var->red.length = 5;
495
		var->green.length = 5;
496
		var->blue.length = 5;
497
		var->transp.length = 1;
498
		var->transp.offset = 15;
499
		break;
500
	case 16:
501
		var->red.offset = 11;
502
		var->green.offset = 5;
503
		var->blue.offset = 0;
504
		var->red.length = 5;
505
		var->green.length = 6;
506
		var->blue.length = 5;
507
		var->transp.length = 0;
508
		var->transp.offset = 0;
509
		break;
510
	case 24:
511
		var->red.offset = 16;
512
		var->green.offset = 8;
513
		var->blue.offset = 0;
514
		var->red.length = 8;
515
		var->green.length = 8;
516
		var->blue.length = 8;
517
		var->transp.length = 0;
518
		var->transp.offset = 0;
519
		break;
520
	case 32:
521
		var->red.offset = 16;
522
		var->green.offset = 8;
523
		var->blue.offset = 0;
524
		var->red.length = 8;
525
		var->green.length = 8;
526
		var->blue.length = 8;
527
		var->transp.length = 8;
528
		var->transp.offset = 24;
529
		break;
530
	default:
531
		return -EINVAL;
532
	}
533
	return 0;
534
}
535
EXPORT_SYMBOL(drm_fb_helper_check_var);
536
 
3480 Serge 537
/**
538
 * drm_fb_helper_set_par - implementation for ->fb_set_par
539
 * @info: fbdev registered by the helper
540
 *
541
 * This will let fbcon do the mode init and is called at initialization time by
542
 * the fbdev core when registering the driver, and later on through the hotplug
543
 * callback.
544
 */
1179 serge 545
int drm_fb_helper_set_par(struct fb_info *info)
546
{
547
	struct drm_fb_helper *fb_helper = info->par;
548
	struct drm_device *dev = fb_helper->dev;
549
	struct fb_var_screeninfo *var = &info->var;
550
	int ret;
551
	int i;
552
 
1313 serge 553
	if (var->pixclock != 0) {
1430 serge 554
		DRM_ERROR("PIXEL CLOCK SET\n");
1179 serge 555
		return -EINVAL;
556
	}
557
 
3480 Serge 558
	drm_modeset_lock_all(dev);
1179 serge 559
		for (i = 0; i < fb_helper->crtc_count; i++) {
3480 Serge 560
		ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set);
1963 serge 561
		if (ret) {
3480 Serge 562
			drm_modeset_unlock_all(dev);
1179 serge 563
				return ret;
564
		}
565
	}
3480 Serge 566
	drm_modeset_unlock_all(dev);
1963 serge 567
 
568
	if (fb_helper->delayed_hotplug) {
569
		fb_helper->delayed_hotplug = false;
3480 Serge 570
		drm_fb_helper_hotplug_event(fb_helper);
1963 serge 571
	}
1179 serge 572
	return 0;
573
}
574
EXPORT_SYMBOL(drm_fb_helper_set_par);
575
 
3480 Serge 576
/**
577
 * drm_fb_helper_pan_display - implementation for ->fb_pan_display
578
 * @var: updated screen information
579
 * @info: fbdev registered by the helper
580
 */
1179 serge 581
int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
582
			      struct fb_info *info)
583
{
584
	struct drm_fb_helper *fb_helper = info->par;
585
	struct drm_device *dev = fb_helper->dev;
586
	struct drm_mode_set *modeset;
587
	struct drm_crtc *crtc;
588
	int ret = 0;
589
	int i;
590
 
3480 Serge 591
	drm_modeset_lock_all(dev);
592
	if (!drm_fb_helper_is_bound(fb_helper)) {
593
		drm_modeset_unlock_all(dev);
594
		return -EBUSY;
595
	}
596
 
1179 serge 597
		for (i = 0; i < fb_helper->crtc_count; i++) {
1963 serge 598
		crtc = fb_helper->crtc_info[i].mode_set.crtc;
1179 serge 599
 
600
		modeset = &fb_helper->crtc_info[i].mode_set;
601
 
602
		modeset->x = var->xoffset;
603
		modeset->y = var->yoffset;
604
 
605
		if (modeset->num_connectors) {
3480 Serge 606
			ret = drm_mode_set_config_internal(modeset);
1179 serge 607
			if (!ret) {
608
				info->var.xoffset = var->xoffset;
609
				info->var.yoffset = var->yoffset;
610
			}
611
		}
612
	}
3480 Serge 613
	drm_modeset_unlock_all(dev);
1179 serge 614
	return ret;
615
}
616
EXPORT_SYMBOL(drm_fb_helper_pan_display);
617
 
3480 Serge 618
/*
619
 * Allocates the backing storage and sets up the fbdev info structure through
620
 * the ->fb_probe callback and then registers the fbdev and sets up the panic
621
 * notifier.
622
 */
623
static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1963 serge 624
				  int preferred_bpp)
1179 serge 625
{
3480 Serge 626
	int ret = 0;
1179 serge 627
	int crtc_count = 0;
1963 serge 628
	int i;
1179 serge 629
	struct fb_info *info;
1963 serge 630
	struct drm_fb_helper_surface_size sizes;
631
	int gamma_size = 0;
1179 serge 632
 
1963 serge 633
	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
634
	sizes.surface_depth = 24;
635
	sizes.surface_bpp = 32;
636
	sizes.fb_width = (unsigned)-1;
637
	sizes.fb_height = (unsigned)-1;
1221 serge 638
 
3031 serge 639
	/* if driver picks 8 or 16 by default use that
640
	   for both depth/bpp */
3192 Serge 641
	if (preferred_bpp != sizes.surface_bpp)
3031 serge 642
		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
3192 Serge 643
 
3031 serge 644
	/* first up get a count of crtcs now in use and new min/maxes width/heights */
645
	for (i = 0; i < fb_helper->connector_count; i++) {
646
		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
647
		struct drm_cmdline_mode *cmdline_mode;
648
 
649
		cmdline_mode = &fb_helper_conn->cmdline_mode;
650
 
651
		if (cmdline_mode->bpp_specified) {
652
			switch (cmdline_mode->bpp) {
653
			case 8:
654
				sizes.surface_depth = sizes.surface_bpp = 8;
655
				break;
656
			case 15:
657
				sizes.surface_depth = 15;
658
				sizes.surface_bpp = 16;
659
				break;
660
			case 16:
661
				sizes.surface_depth = sizes.surface_bpp = 16;
662
				break;
663
			case 24:
664
				sizes.surface_depth = sizes.surface_bpp = 24;
665
				break;
666
			case 32:
1963 serge 667
    sizes.surface_depth = 24;
668
    sizes.surface_bpp = 32;
3031 serge 669
				break;
670
			}
671
			break;
672
		}
673
	}
1221 serge 674
 
1963 serge 675
	crtc_count = 0;
676
	for (i = 0; i < fb_helper->crtc_count; i++) {
677
		struct drm_display_mode *desired_mode;
678
		desired_mode = fb_helper->crtc_info[i].desired_mode;
1221 serge 679
 
1963 serge 680
		if (desired_mode) {
681
			if (gamma_size == 0)
682
				gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
683
			if (desired_mode->hdisplay < sizes.fb_width)
684
				sizes.fb_width = desired_mode->hdisplay;
685
			if (desired_mode->vdisplay < sizes.fb_height)
686
				sizes.fb_height = desired_mode->vdisplay;
687
			if (desired_mode->hdisplay > sizes.surface_width)
688
				sizes.surface_width = desired_mode->hdisplay;
689
			if (desired_mode->vdisplay > sizes.surface_height)
690
				sizes.surface_height = desired_mode->vdisplay;
1179 serge 691
			crtc_count++;
4539 Serge 692
       }
1179 serge 693
	}
694
 
1963 serge 695
	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1179 serge 696
		/* hmm everyone went away - assume VGA cable just fell out
697
		   and will come back later. */
1963 serge 698
		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
699
		sizes.fb_width = sizes.surface_width = 1024;
700
		sizes.fb_height = sizes.surface_height = 768;
1179 serge 701
	}
702
 
1963 serge 703
	/* push down into drivers */
3480 Serge 704
	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
705
	if (ret < 0)
706
		return ret;
1179 serge 707
 
1963 serge 708
	info = fb_helper->fbdev;
1179 serge 709
 
3480 Serge 710
	/*
711
	 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
712
	 * events, but at init time drm_setup_crtcs needs to be called before
713
	 * the fb is allocated (since we need to figure out the desired size of
714
	 * the fb before we can allocate it ...). Hence we need to fix things up
715
	 * here again.
716
	 */
3192 Serge 717
	for (i = 0; i < fb_helper->crtc_count; i++)
3480 Serge 718
		if (fb_helper->crtc_info[i].mode_set.num_connectors)
1963 serge 719
		fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1179 serge 720
 
3480 Serge 721
 
1313 serge 722
		info->var.pixclock = 0;
1963 serge 723
 
724
        list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
725
 
1179 serge 726
	return 0;
727
}
728
 
3480 Serge 729
/**
730
 * drm_fb_helper_fill_fix - initializes fixed fbdev information
731
 * @info: fbdev registered by the helper
732
 * @pitch: desired pitch
733
 * @depth: desired depth
734
 *
735
 * Helper to fill in the fixed fbdev information useful for a non-accelerated
736
 * fbdev emulations. Drivers which support acceleration methods which impose
737
 * additional constraints need to set up their own limits.
738
 *
739
 * Drivers should call this (or their equivalent setup code) from their
740
 * ->fb_probe callback.
741
 */
1221 serge 742
void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
743
			    uint32_t depth)
1179 serge 744
{
745
	info->fix.type = FB_TYPE_PACKED_PIXELS;
1221 serge 746
	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1246 serge 747
		FB_VISUAL_TRUECOLOR;
1963 serge 748
	info->fix.mmio_start = 0;
749
	info->fix.mmio_len = 0;
1179 serge 750
	info->fix.type_aux = 0;
751
	info->fix.xpanstep = 1; /* doing it in hw */
752
	info->fix.ypanstep = 1; /* doing it in hw */
753
	info->fix.ywrapstep = 0;
754
	info->fix.accel = FB_ACCEL_NONE;
755
	info->fix.type_aux = 0;
756
 
757
	info->fix.line_length = pitch;
758
	return;
759
}
760
EXPORT_SYMBOL(drm_fb_helper_fill_fix);
761
 
3480 Serge 762
/**
763
 * drm_fb_helper_fill_var - initalizes variable fbdev information
764
 * @info: fbdev instance to set up
765
 * @fb_helper: fb helper instance to use as template
766
 * @fb_width: desired fb width
767
 * @fb_height: desired fb height
768
 *
769
 * Sets up the variable fbdev metainformation from the given fb helper instance
770
 * and the drm framebuffer allocated in fb_helper->fb.
771
 *
772
 * Drivers should call this (or their equivalent setup code) from their
773
 * ->fb_probe callback after having allocated the fbdev backing
774
 * storage framebuffer.
775
 */
1963 serge 776
void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1179 serge 777
			    uint32_t fb_width, uint32_t fb_height)
778
{
1963 serge 779
	struct drm_framebuffer *fb = fb_helper->fb;
780
	info->pseudo_palette = fb_helper->pseudo_palette;
1179 serge 781
	info->var.xres_virtual = fb->width;
782
	info->var.yres_virtual = fb->height;
783
	info->var.bits_per_pixel = fb->bits_per_pixel;
1963 serge 784
	info->var.accel_flags = FB_ACCELF_TEXT;
1179 serge 785
	info->var.xoffset = 0;
786
	info->var.yoffset = 0;
787
	info->var.activate = FB_ACTIVATE_NOW;
788
	info->var.height = -1;
789
	info->var.width = -1;
790
 
791
	switch (fb->depth) {
792
	case 8:
793
		info->var.red.offset = 0;
794
		info->var.green.offset = 0;
795
		info->var.blue.offset = 0;
796
		info->var.red.length = 8; /* 8bit DAC */
797
		info->var.green.length = 8;
798
		info->var.blue.length = 8;
799
		info->var.transp.offset = 0;
800
		info->var.transp.length = 0;
801
		break;
802
	case 15:
803
		info->var.red.offset = 10;
804
		info->var.green.offset = 5;
805
		info->var.blue.offset = 0;
806
		info->var.red.length = 5;
807
		info->var.green.length = 5;
808
		info->var.blue.length = 5;
809
		info->var.transp.offset = 15;
810
		info->var.transp.length = 1;
811
		break;
812
	case 16:
813
		info->var.red.offset = 11;
814
		info->var.green.offset = 5;
815
		info->var.blue.offset = 0;
816
		info->var.red.length = 5;
817
		info->var.green.length = 6;
818
		info->var.blue.length = 5;
819
		info->var.transp.offset = 0;
820
		break;
821
	case 24:
822
		info->var.red.offset = 16;
823
		info->var.green.offset = 8;
824
		info->var.blue.offset = 0;
825
		info->var.red.length = 8;
826
		info->var.green.length = 8;
827
		info->var.blue.length = 8;
828
		info->var.transp.offset = 0;
829
		info->var.transp.length = 0;
830
		break;
831
	case 32:
832
		info->var.red.offset = 16;
833
		info->var.green.offset = 8;
834
		info->var.blue.offset = 0;
835
		info->var.red.length = 8;
836
		info->var.green.length = 8;
837
		info->var.blue.length = 8;
838
		info->var.transp.offset = 24;
839
		info->var.transp.length = 8;
840
		break;
841
	default:
842
		break;
843
	}
844
 
845
	info->var.xres = fb_width;
846
	info->var.yres = fb_height;
847
}
848
EXPORT_SYMBOL(drm_fb_helper_fill_var);
1963 serge 849
 
850
static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
851
					       uint32_t maxX,
852
					       uint32_t maxY)
853
{
854
	struct drm_connector *connector;
855
	int count = 0;
856
	int i;
857
 
858
	for (i = 0; i < fb_helper->connector_count; i++) {
859
		connector = fb_helper->connector_info[i]->connector;
860
		count += connector->funcs->fill_modes(connector, maxX, maxY);
861
	}
862
 
863
	return count;
864
}
865
 
866
static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
867
{
868
	struct drm_display_mode *mode;
869
 
870
	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
871
		if (drm_mode_width(mode) > width ||
872
		    drm_mode_height(mode) > height)
873
			continue;
874
		if (mode->type & DRM_MODE_TYPE_PREFERRED)
875
			return mode;
876
	}
877
	return NULL;
878
}
879
 
880
static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
881
{
882
	struct drm_cmdline_mode *cmdline_mode;
883
	cmdline_mode = &fb_connector->cmdline_mode;
884
	return cmdline_mode->specified;
885
}
886
 
3746 Serge 887
static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
888
						      int width, int height)
889
{
890
	struct drm_cmdline_mode *cmdline_mode;
891
	struct drm_display_mode *mode = NULL;
1963 serge 892
 
3746 Serge 893
	return NULL;
894
 
895
	cmdline_mode = &fb_helper_conn->cmdline_mode;
896
	if (cmdline_mode->specified == false)
897
		return mode;
898
 
899
	/* attempt to find a matching mode in the list of modes
900
	 *  we have gotten so far, if not add a CVT mode that conforms
901
	 */
902
	if (cmdline_mode->rb || cmdline_mode->margins)
903
		goto create_mode;
904
 
905
	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
906
		/* check width/height */
907
		if (mode->hdisplay != cmdline_mode->xres ||
908
		    mode->vdisplay != cmdline_mode->yres)
909
			continue;
910
 
911
		if (cmdline_mode->refresh_specified) {
912
			if (mode->vrefresh != cmdline_mode->refresh)
913
				continue;
914
		}
915
 
916
		if (cmdline_mode->interlace) {
917
			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
918
				continue;
919
		}
920
		return mode;
921
	}
922
 
923
create_mode:
924
	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
925
						 cmdline_mode);
926
	list_add(&mode->head, &fb_helper_conn->connector->modes);
927
	return mode;
928
}
929
 
1963 serge 930
static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
931
{
932
	bool enable;
933
 
3192 Serge 934
	if (strict)
1963 serge 935
		enable = connector->status == connector_status_connected;
3192 Serge 936
	else
1963 serge 937
		enable = connector->status != connector_status_disconnected;
3192 Serge 938
 
1963 serge 939
	return enable;
940
}
941
 
942
static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
943
				  bool *enabled)
944
{
945
	bool any_enabled = false;
946
	struct drm_connector *connector;
947
	int i = 0;
948
 
949
	for (i = 0; i < fb_helper->connector_count; i++) {
950
		connector = fb_helper->connector_info[i]->connector;
951
		enabled[i] = drm_connector_enabled(connector, true);
952
		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
953
			  enabled[i] ? "yes" : "no");
954
		any_enabled |= enabled[i];
955
	}
956
 
957
	if (any_enabled)
958
		return;
959
 
960
	for (i = 0; i < fb_helper->connector_count; i++) {
961
		connector = fb_helper->connector_info[i]->connector;
962
		enabled[i] = drm_connector_enabled(connector, false);
963
	}
964
}
965
 
3746 Serge 966
static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
967
			      struct drm_display_mode **modes,
968
			      bool *enabled, int width, int height)
969
{
970
	int count, i, j;
971
	bool can_clone = false;
972
	struct drm_fb_helper_connector *fb_helper_conn;
973
	struct drm_display_mode *dmt_mode, *mode;
1963 serge 974
 
3746 Serge 975
	/* only contemplate cloning in the single crtc case */
976
	if (fb_helper->crtc_count > 1)
977
		return false;
978
 
979
	count = 0;
980
	for (i = 0; i < fb_helper->connector_count; i++) {
981
		if (enabled[i])
982
			count++;
983
	}
984
 
985
	/* only contemplate cloning if more than one connector is enabled */
986
	if (count <= 1)
987
		return false;
988
 
989
	/* check the command line or if nothing common pick 1024x768 */
990
	can_clone = true;
991
	for (i = 0; i < fb_helper->connector_count; i++) {
992
		if (!enabled[i])
993
			continue;
994
		fb_helper_conn = fb_helper->connector_info[i];
995
		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
996
		if (!modes[i]) {
997
			can_clone = false;
998
			break;
999
		}
1000
		for (j = 0; j < i; j++) {
1001
			if (!enabled[j])
1002
				continue;
1003
			if (!drm_mode_equal(modes[j], modes[i]))
1004
				can_clone = false;
1005
		}
1006
	}
1007
 
1008
	if (can_clone) {
1009
		DRM_DEBUG_KMS("can clone using command line\n");
1010
		return true;
1011
	}
1012
 
1013
	/* try and find a 1024x768 mode on each connector */
1014
	can_clone = true;
1015
	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1016
 
1017
	for (i = 0; i < fb_helper->connector_count; i++) {
1018
 
1019
		if (!enabled[i])
1020
			continue;
1021
 
1022
		fb_helper_conn = fb_helper->connector_info[i];
1023
		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1024
			if (drm_mode_equal(mode, dmt_mode))
1025
				modes[i] = mode;
1026
		}
1027
		if (!modes[i])
1028
			can_clone = false;
1029
	}
1030
 
1031
	if (can_clone) {
1032
		DRM_DEBUG_KMS("can clone using 1024x768\n");
1033
		return true;
1034
	}
1035
	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1036
	return false;
1037
}
1038
 
1963 serge 1039
static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1040
				 struct drm_display_mode **modes,
1041
				 bool *enabled, int width, int height)
1042
{
1043
	struct drm_fb_helper_connector *fb_helper_conn;
1044
	int i;
1045
 
1046
	for (i = 0; i < fb_helper->connector_count; i++) {
1047
		fb_helper_conn = fb_helper->connector_info[i];
1048
 
1049
		if (enabled[i] == false)
1050
			continue;
1051
 
1052
		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1053
			      fb_helper_conn->connector->base.id);
1054
 
1055
		/* got for command line mode first */
3746 Serge 1056
		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1963 serge 1057
		if (!modes[i]) {
1058
			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1059
				      fb_helper_conn->connector->base.id);
1060
			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1061
		}
1062
		/* No preferred modes, pick one off the list */
1063
		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1064
			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1065
				break;
1066
		}
1067
		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1068
			  "none");
1069
	}
1070
	return true;
1071
}
1072
 
1073
static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1074
			  struct drm_fb_helper_crtc **best_crtcs,
1075
			  struct drm_display_mode **modes,
1076
			  int n, int width, int height)
1077
{
1078
	int c, o;
1079
	struct drm_device *dev = fb_helper->dev;
1080
	struct drm_connector *connector;
1081
	struct drm_connector_helper_funcs *connector_funcs;
1082
	struct drm_encoder *encoder;
1083
	struct drm_fb_helper_crtc *best_crtc;
1084
	int my_score, best_score, score;
1085
	struct drm_fb_helper_crtc **crtcs, *crtc;
1086
	struct drm_fb_helper_connector *fb_helper_conn;
1087
 
1088
	if (n == fb_helper->connector_count)
1089
		return 0;
1090
 
1091
	fb_helper_conn = fb_helper->connector_info[n];
1092
	connector = fb_helper_conn->connector;
1093
 
1094
	best_crtcs[n] = NULL;
1095
	best_crtc = NULL;
1096
	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1097
	if (modes[n] == NULL)
1098
		return best_score;
1099
 
1100
	crtcs = kzalloc(dev->mode_config.num_connector *
1101
			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1102
	if (!crtcs)
1103
		return best_score;
1104
 
1105
	my_score = 1;
1106
	if (connector->status == connector_status_connected)
1107
		my_score++;
1108
	if (drm_has_cmdline_mode(fb_helper_conn))
1109
		my_score++;
1110
	if (drm_has_preferred_mode(fb_helper_conn, width, height))
1111
		my_score++;
1112
 
1113
	connector_funcs = connector->helper_private;
1114
	encoder = connector_funcs->best_encoder(connector);
1115
	if (!encoder)
1116
		goto out;
1117
 
1118
	/* select a crtc for this connector and then attempt to configure
1119
	   remaining connectors */
1120
	for (c = 0; c < fb_helper->crtc_count; c++) {
1121
		crtc = &fb_helper->crtc_info[c];
1122
 
3192 Serge 1123
		if ((encoder->possible_crtcs & (1 << c)) == 0)
1963 serge 1124
			continue;
1125
 
1126
		for (o = 0; o < n; o++)
1127
			if (best_crtcs[o] == crtc)
1128
				break;
1129
 
1130
		if (o < n) {
1131
			/* ignore cloning unless only a single crtc */
1132
			if (fb_helper->crtc_count > 1)
1133
				continue;
1134
 
1135
			if (!drm_mode_equal(modes[o], modes[n]))
1136
				continue;
1137
		}
1138
 
1139
		crtcs[n] = crtc;
1140
		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1141
		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1142
						  width, height);
1143
		if (score > best_score) {
1144
			best_crtc = crtc;
1145
			best_score = score;
1146
			memcpy(best_crtcs, crtcs,
1147
			       dev->mode_config.num_connector *
1148
			       sizeof(struct drm_fb_helper_crtc *));
1149
		}
1150
	}
1151
out:
1152
	kfree(crtcs);
1153
	return best_score;
1154
}
1155
 
1156
static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1157
{
1158
	struct drm_device *dev = fb_helper->dev;
1159
	struct drm_fb_helper_crtc **crtcs;
1160
	struct drm_display_mode **modes;
1161
	struct drm_mode_set *modeset;
1162
	bool *enabled;
1163
	int width, height;
3746 Serge 1164
	int i;
1963 serge 1165
 
1166
	DRM_DEBUG_KMS("\n");
1167
 
1168
	width = dev->mode_config.max_width;
1169
	height = dev->mode_config.max_height;
1170
 
1171
	crtcs = kcalloc(dev->mode_config.num_connector,
1172
			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
1173
	modes = kcalloc(dev->mode_config.num_connector,
1174
			sizeof(struct drm_display_mode *), GFP_KERNEL);
1175
	enabled = kcalloc(dev->mode_config.num_connector,
1176
			  sizeof(bool), GFP_KERNEL);
3192 Serge 1177
	if (!crtcs || !modes || !enabled) {
1178
		DRM_ERROR("Memory allocation failed\n");
1179
		goto out;
1180
	}
1963 serge 1181
 
3192 Serge 1182
 
1963 serge 1183
	drm_enable_connectors(fb_helper, enabled);
1184
 
3746 Serge 1185
	if (!(fb_helper->funcs->initial_config &&
1186
	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1187
					       enabled, width, height))) {
1188
		memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1189
		memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1963 serge 1190
 
3746 Serge 1191
		if (!drm_target_cloned(fb_helper,
1192
				       modes, enabled, width, height) &&
1193
		    !drm_target_preferred(fb_helper,
1194
					  modes, enabled, width, height))
1963 serge 1195
			DRM_ERROR("Unable to find initial modes\n");
1196
 
3746 Serge 1197
		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1198
			      width, height);
1963 serge 1199
 
1200
	drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
3746 Serge 1201
	}
1963 serge 1202
 
1203
	/* need to set the modesets up here for use later */
1204
	/* fill out the connector<->crtc mappings into the modesets */
1205
	for (i = 0; i < fb_helper->crtc_count; i++) {
1206
		modeset = &fb_helper->crtc_info[i].mode_set;
1207
		modeset->num_connectors = 0;
3480 Serge 1208
		modeset->fb = NULL;
1963 serge 1209
	}
1210
 
1211
	for (i = 0; i < fb_helper->connector_count; i++) {
1212
		struct drm_display_mode *mode = modes[i];
1213
		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1214
		modeset = &fb_crtc->mode_set;
1215
 
1216
		if (mode && fb_crtc) {
1217
			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1218
				      mode->name, fb_crtc->mode_set.crtc->base.id);
1219
			fb_crtc->desired_mode = mode;
1220
			if (modeset->mode)
1221
				drm_mode_destroy(dev, modeset->mode);
1222
			modeset->mode = drm_mode_duplicate(dev,
1223
							   fb_crtc->desired_mode);
1224
			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
3480 Serge 1225
			modeset->fb = fb_helper->fb;
1963 serge 1226
		}
1227
	}
1228
 
3480 Serge 1229
	/* Clear out any old modes if there are no more connected outputs. */
1230
	for (i = 0; i < fb_helper->crtc_count; i++) {
1231
		modeset = &fb_helper->crtc_info[i].mode_set;
1232
		if (modeset->num_connectors == 0) {
1233
			BUG_ON(modeset->fb);
1234
			BUG_ON(modeset->num_connectors);
1235
			if (modeset->mode)
1236
				drm_mode_destroy(dev, modeset->mode);
1237
			modeset->mode = NULL;
1238
		}
1239
	}
3192 Serge 1240
out:
1963 serge 1241
	kfree(crtcs);
1242
	kfree(modes);
1243
	kfree(enabled);
1244
}
1245
 
1246
/**
3480 Serge 1247
 * drm_fb_helper_initial_config - setup a sane initial connector configuration
3192 Serge 1248
 * @fb_helper: fb_helper device struct
1249
 * @bpp_sel: bpp value to use for the framebuffer configuration
1963 serge 1250
 *
3192 Serge 1251
 * Scans the CRTCs and connectors and tries to put together an initial setup.
1963 serge 1252
 * At the moment, this is a cloned configuration across all heads with
1253
 * a new framebuffer object as the backing store.
1254
 *
3480 Serge 1255
 * Note that this also registers the fbdev and so allows userspace to call into
1256
 * the driver through the fbdev interfaces.
1257
 *
1258
 * This function will call down into the ->fb_probe callback to let
1259
 * the driver allocate and initialize the fbdev info structure and the drm
1260
 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1261
 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1262
 * values for the fbdev info structure.
1263
 *
1963 serge 1264
 * RETURNS:
1265
 * Zero if everything went ok, nonzero otherwise.
1266
 */
1267
bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1268
{
1269
	struct drm_device *dev = fb_helper->dev;
1270
	int count = 0;
1271
 
1272
//   drm_fb_helper_parse_command_line(fb_helper);
1273
 
1274
	count = drm_fb_helper_probe_connector_modes(fb_helper,
1275
						    dev->mode_config.max_width,
1276
						    dev->mode_config.max_height);
1277
	/*
1278
	 * we shouldn't end up with no modes here.
1279
	 */
3192 Serge 1280
	if (count == 0)
1281
		dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
1282
 
1283
	drm_setup_crtcs(fb_helper);
1284
 
3243 Serge 1285
	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
3192 Serge 1286
}
1287
EXPORT_SYMBOL(drm_fb_helper_initial_config);
1288
 
1289
/**
1290
 * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1291
 *                               probing all the outputs attached to the fb
1292
 * @fb_helper: the drm_fb_helper
1293
 *
1294
 * Scan the connectors attached to the fb_helper and try to put together a
1295
 * setup after *notification of a change in output configuration.
1296
 *
3480 Serge 1297
 * Called at runtime, takes the mode config locks to be able to check/change the
1298
 * modeset configuration. Must be run from process context (which usually means
1299
 * either the output polling work or a work item launched from the driver's
1300
 * hotplug interrupt).
1301
 *
1302
 * Note that the driver must ensure that this is only called _after_ the fb has
1303
 * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
1304
 *
3192 Serge 1305
 * RETURNS:
1306
 * 0 on success and a non-zero error code otherwise.
1307
 */
1308
int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1309
{
1310
	struct drm_device *dev = fb_helper->dev;
1311
	int count = 0;
1312
	u32 max_width, max_height, bpp_sel;
1313
 
1314
	if (!fb_helper->fb)
1315
		return 0;
1316
 
3480 Serge 1317
	mutex_lock(&fb_helper->dev->mode_config.mutex);
1318
	if (!drm_fb_helper_is_bound(fb_helper)) {
3192 Serge 1319
		fb_helper->delayed_hotplug = true;
3480 Serge 1320
		mutex_unlock(&fb_helper->dev->mode_config.mutex);
3192 Serge 1321
		return 0;
1322
	}
1323
	DRM_DEBUG_KMS("\n");
1324
 
1325
	max_width = fb_helper->fb->width;
1326
	max_height = fb_helper->fb->height;
1327
	bpp_sel = fb_helper->fb->bits_per_pixel;
1328
 
1329
	count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1330
						    max_height);
3480 Serge 1331
	mutex_unlock(&fb_helper->dev->mode_config.mutex);
1332
 
1333
	drm_modeset_lock_all(dev);
1963 serge 1334
	drm_setup_crtcs(fb_helper);
3480 Serge 1335
	drm_modeset_unlock_all(dev);
1336
	drm_fb_helper_set_par(fb_helper->fbdev);
1963 serge 1337
 
3480 Serge 1338
	return 0;
1963 serge 1339
}
3192 Serge 1340
EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1341