aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/pvr/display/omap_display.c
blob: d3859084b83f4f126d3f684115025f3def871f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
/*
 * drivers/gpu/pvr/display/omap_display.c
 *
 * Copyright (C) 2010 Texas Instruments
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fb.h>

#include <plat/vrfb.h>
#include <plat/display.h>

/* Workaround for DEBUG macro clash in framebuffer */
#ifdef RELEASE
#include <../drivers/video/omap2/omapfb/omapfb.h>
#undef DEBUG
#else
#undef DEBUG
#include <../drivers/video/omap2/omapfb/omapfb.h>
#endif

#define OMAP_DISP_DRV_NAME "omap_display"
#define OMAP_DISP_FRAMEBUFFER_COUNT num_registered_fb

#define OMAP_DISP_PAGE_MASK (PAGE_SIZE - 1)
#define OMAP_DISP_PAGE_TRUNCATE (~OMAP_DISP_PAGE_MASK)
#define OMAP_DISP_PAGE_ROUND_UP(x) \
	(((x)+OMAP_DISP_PAGE_MASK) & OMAP_DISP_PAGE_TRUNCATE)

#define OMAP_DISP_IRQ_TIMEOUT 500

#ifdef DEBUG
#define DBG_PRINT(format, ...) printk(KERN_INFO OMAP_DISP_DRV_NAME \
	" (%s %i): " format "\n", __func__, __LINE__, ## __VA_ARGS__)
#define WRN_PRINT(format, ...) printk(KERN_WARNING OMAP_DISP_DRV_NAME \
	" (%s %i): " format "\n", __func__, __LINE__, ## __VA_ARGS__)
#define ERR_PRINT(format, ...) printk(KERN_ERR OMAP_DISP_DRV_NAME \
	" (%s %i): " format "\n", __func__, __LINE__, ## __VA_ARGS__)
#else
#define DBG_PRINT(format, ...)
#define WRN_PRINT(format, ...)
#define ERR_PRINT(format, ...)
#endif

#include "omap_display.h"

/* List for the available displays */
static struct omap_display_device *omap_display_list;
static unsigned int omap_display_number;

/* Workqueues for virtual display (primary, seconday)*/
static struct workqueue_struct *vdisp_wq_primary;
static struct workqueue_struct *vdisp_wq_secondary;
static struct omap_display_sync_item vdisp_sync_primary;
static struct omap_display_sync_item vdisp_sync_secondary;

/* Forward declarations */
static struct omap_display_buffer *create_main_buffer(
	struct omap_display_device *display);
static int display_destroy_buffer(struct omap_display_buffer *buffer);
static void vdisp_sync_handler(struct work_struct *work);

static int open_display(struct omap_display_device *display,
	enum omap_display_feature features)
{
	int i;

	DBG_PRINT("Opening display '%s'", display->name);

	/* TODO: Support horizontal orientation */
	if (features & ORIENTATION_HORIZONTAL) {
		DBG_PRINT("Horizontal orientation is not supported yet , "
			"falling back to vertical orientation");
		features = ORIENTATION_VERTICAL;
	}

	display->features = features;
	display->reference_count++;
	for (i = 0; i < display->overlay_managers_count; i++)
		omap_dss_get_device(display->overlay_managers[i]->device);

	/* If the main buffer doesn't exist create it */
	if (!display->main_buffer) {
		DBG_PRINT("Main buffer doesn't exist for display '%s', create"
			" one", display->name);
		display->main_buffer = create_main_buffer(display);
		if (!display->main_buffer) {
			ERR_PRINT("Failed to create main buffer for '%s'",
				display->name);
			return 1;
		}
	}

	return 0;
}

static int close_display(struct omap_display_device *display)
{
	int err;
	int i;

	/* TODO: Is it the same thing to close a virtual and single display? */
	DBG_PRINT("Closing display '%s'", display->name);

	display->reference_count--;
	for (i = 0; i < display->overlay_managers_count; i++)
		omap_dss_put_device(display->overlay_managers[i]->device);

	if (display->flip_chain) {
		err = display->destroy_flip_chain(display);
		display->flip_chain = 0;
		if (err)
			WRN_PRINT("An error happened when destroying flip "
				"chain for '%s'", display->name);
	}

	return 0;
}

static int get_max_buffers(struct omap_display_device *display)
{
	/* TODO: If TILER is wanted to be used how do you calculate this? */
	int fb_idx;
	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
		fb_idx = 0;
		break;
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unknown display id %i", display->id);
		BUG();
	}

	/* Use the framebuffer memory */
	if (fb_idx >= 0 && fb_idx < num_registered_fb) {
		struct fb_info *framebuffer = registered_fb[fb_idx];
		unsigned long buffer_size;

		/* Single buffer size */
		buffer_size = display->width * display->height *
			display->bytes_per_pixel;
		/* Page align the buffer size, round up to the page size */
		buffer_size = OMAP_DISP_PAGE_ROUND_UP(buffer_size);

		return (int) (framebuffer->fix.smem_len / buffer_size);
	} else {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return 0;
	}
}

static int create_flip_chain(struct omap_display_device *display,
	unsigned int buffer_count)
{
	int fb_idx;

	/* TODO: What about TILER buffers */
	if (buffer_count <= 1) {
		ERR_PRINT("Flip chains with %i buffers not supported",
			buffer_count);
		return 1;
	} else if (buffer_count > display->buffers_available) {
		ERR_PRINT("Requesting %i buffers when there is %i available"
			" for '%s'", buffer_count, display->buffers_available,
			display->name);
		return 1;
	} else if (display->flip_chain) {
		ERR_PRINT("Flip chain already exists for '%s'", display->name);
		return 1;
	}

	/* Create the flip chain with the framebuffer memory */
	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
		fb_idx = 0;
		break;
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unknown display id %i", display->id);
		BUG();
	}

	/* Use the framebuffer memory */
	if (fb_idx >= 0 && fb_idx < num_registered_fb) {
		struct fb_info *framebuffer = registered_fb[fb_idx];
		unsigned long buffer_size;
		struct omap_display_flip_chain *flip_chain;
		int i;

		if (!framebuffer || !framebuffer->fix.smem_start ||
			!framebuffer->screen_base) {
			ERR_PRINT("Framebuffer %i doesn't seem to be "
				"initialized", fb_idx);
			return 1;
		}

		/*
		 * Check if there is enough memory in the fb for the requested
		 * buffers
		 */
		buffer_size = display->width * display->height *
			display->bytes_per_pixel;
		/* Page align the buffer size, round up to the page size */
		buffer_size = OMAP_DISP_PAGE_ROUND_UP(buffer_size);

		if (buffer_size * buffer_count > framebuffer->fix.smem_len) {
			ERR_PRINT("Not enough memory to allocate %i buffers "
				"(%lu bytes each), memory available %lu for "
				"display '%s'", buffer_count, buffer_size,
				(unsigned long)framebuffer->fix.smem_len,
				display->name);
			return 1;
		}

		flip_chain = kzalloc(sizeof(*flip_chain), GFP_KERNEL);

		if (!flip_chain) {
			ERR_PRINT("Out of memory");
			return 1;
		}

		for (i = 0; i < buffer_count; i++) {
			struct omap_display_buffer *buffer;

			/*
			 * Reuse the main buffer as the first buffer in the
			 * flip chain
			 */
			if (i == 0) {
				buffer = display->main_buffer;
				flip_chain->buffers[i] = buffer;
				DBG_PRINT("Flip chain buffer %i has address "
					"%lx for display '%s'", i,
					buffer->physical_addr, display->name);
				continue;
			}

			buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);

			if (!buffer) {
				/*
				 * FIXME: If one buffer allocation fails,
				 * deallocate flip chain and buffers
				 */
				ERR_PRINT("Out of memory");
				return 1;
			}

			buffer->physical_addr = framebuffer->fix.smem_start +
				(buffer_size * i);
			buffer->virtual_addr =
				(unsigned long) framebuffer->screen_base +
				(buffer_size * i);
			buffer->size = buffer_size;
			buffer->display = display;
			flip_chain->buffers[i] = buffer;

			DBG_PRINT("Flip chain buffer %i has address %lx for"
				" display '%s'", i, buffer->physical_addr,
				display->name);
		}

		display->flip_chain = flip_chain;
		return 0;
	} else {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return 1;
	}

	return 0;
}

static int destroy_flip_chain(struct omap_display_device *display)
{
	int i;
	int err;

	if (!display->flip_chain) {
		DBG_PRINT("No flip chain to destroy for '%s'", display->name);
		return 0;
	}

	for (i = 0; i < display->flip_chain->buffer_count; i++) {
		struct omap_display_buffer *buffer =
			display->flip_chain->buffers[i];
		/* If buffer is main buffer don't touch it */
		if (display->main_buffer == buffer)
			continue;

		err = display_destroy_buffer(buffer);
		if (err) {
			ERR_PRINT("Error destroying buffer in flip chain for"
			" '%s'", display->name);
			return 1;
		}
	}

	DBG_PRINT("Destroying flip chain for '%s'", display->name);
	kfree(display->flip_chain);
	display->flip_chain = 0;

	return 0;
}

static int rotate_display(struct omap_display_device *display,
	unsigned int rotation)
{
	ERR_PRINT("Not supported yet");
	return 1;
}

static int display_destroy_buffer(struct omap_display_buffer *buffer)
{
	kfree(buffer);
	return 0;
}

static int present_buffer_virtual(struct omap_display_buffer *buffer)
{
	/*
	 * TODO: Support for ORIENTATION_VERTICAL is in place,
	 * ORIENTATION_HORIZONTAL is missing
	 */
	struct omap_display_device *display_virtual = buffer->display;
	struct omap_display_device *display_primary;
	struct omap_display_device *display_secondary;
	struct omap_display_buffer temp_buffer;
	unsigned int buffer_offset;

	if (display_virtual->id != OMAP_DISPID_VIRTUAL) {
		ERR_PRINT("Not a virtual display");
		BUG();
	}

	display_primary = omap_display_get(OMAP_DISPID_PRIMARY);
	display_secondary = omap_display_get(OMAP_DISPID_SECONDARY);
	/*
	 * Calculate offset without page alignment round up otherwise second
	 * display may see incorrect data
	 */
	buffer_offset = display_primary->height * display_virtual->byte_stride;

	/* The first buffer will be the base */
	temp_buffer.physical_addr = buffer->physical_addr;
	temp_buffer.virtual_addr = buffer->virtual_addr;
	temp_buffer.size = buffer->size >> 1;

	if (display_virtual->features & ORIENTATION_INVERT) {
		/* Secondary display has the base */
		temp_buffer.display = display_secondary;
		display_secondary->present_buffer(&temp_buffer);
	} else {
		/* Primary display has the base */
		temp_buffer.display = display_primary;
		display_primary->present_buffer(&temp_buffer);
	}

	/* Remaining display will show the rest */
	temp_buffer.physical_addr = buffer->physical_addr + buffer_offset;
	temp_buffer.virtual_addr = buffer->virtual_addr + buffer_offset;

	if (display_virtual->features & ORIENTATION_INVERT) {
		temp_buffer.display = display_primary;
		display_primary->present_buffer(&temp_buffer);
	} else {
		temp_buffer.display = display_secondary;
		display_secondary->present_buffer(&temp_buffer);
	}

	return 0;
}

static int present_buffer(struct omap_display_buffer *buffer)
{
	struct omap_display_device *display = buffer->display;
	struct fb_info *framebuffer;
	struct omapfb_info *ofbi;
	struct omapfb2_device *fbdev;
	int i;
	int fb_idx;

	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unable to handle display %i", display->id);
		BUG();
	}

	if (fb_idx < 0 || fb_idx >= num_registered_fb) {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return 1;
	}

	framebuffer = registered_fb[fb_idx];
	ofbi = FB2OFB(framebuffer);
	fbdev = ofbi->fbdev;

	omapfb_lock(fbdev);

	/* Get the overlays attached to the framebuffer */
	for (i = 0; i < ofbi->num_overlays ; i++) {
		struct omap_dss_device *display = NULL;
		struct omap_dss_driver *driver = NULL;
		struct omap_overlay_manager *manager;
		struct omap_overlay *overlay;
		struct omap_overlay_info overlay_info;

		overlay = ofbi->overlays[i];
		manager = overlay->manager;
		overlay->get_overlay_info(overlay, &overlay_info);

		overlay_info.paddr = buffer->physical_addr;
		overlay_info.vaddr = (void *) buffer->virtual_addr;
		overlay->set_overlay_info(overlay, &overlay_info);

		if (manager) {
			manager->apply(manager);
			display = manager->device;
			driver = display ? display->driver : NULL;
		}

		if (dss_ovl_manually_updated(overlay)) {
			if (driver->sched_update)
				driver->sched_update(display, 0, 0,
							overlay_info.width,
							overlay_info.height);
			else if (driver->update)
				driver->update(display, 0, 0,
							overlay_info.width,
							overlay_info.height);
		}
	}

	omapfb_unlock(fbdev);


	return 0;
}

static int present_buffer_sync(struct omap_display_buffer *buffer)
{
	/* TODO: Cloning may tear with this implementation */
	struct omap_display_device *display = buffer->display;
	struct fb_info *framebuffer;
	struct omap_dss_device *dss_device;
	struct omap_dss_driver *driver;
	struct omap_overlay_manager *manager;
	int fb_idx;
	int err = 1;

	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unable to handle display %i", display->id);
		BUG();
	}

	if (fb_idx < 0 || fb_idx >= num_registered_fb) {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return 1;
	}

	framebuffer = registered_fb[fb_idx];
	dss_device = fb2display(framebuffer);

	if (!dss_device) {
		WRN_PRINT("No DSS device to sync with display '%s'!",
				display->name);
		return 1;
	}

	driver = dss_device->driver;
	manager = dss_device->manager;

	if (driver && driver->sync &&
		driver->get_update_mode(dss_device) ==
		OMAP_DSS_UPDATE_MANUAL) {
		err = driver->sync(dss_device);
		err |= display->present_buffer(buffer);
	} else if (manager && manager->wait_for_vsync) {
		err = manager->wait_for_vsync(manager);
		err |= display->present_buffer(buffer);
	}

	if (err)
		WRN_PRINT("Unable to sync with display '%s'!", display->name);

	return err;
}

static void vdisp_sync_handler(struct work_struct *work)
{
	struct omap_display_sync_item *sync_item =
		(struct omap_display_sync_item *) work;
	struct omap_display_device *display = sync_item->buffer->display;
	display->present_buffer_sync(sync_item->buffer);
}

static int present_buffer_sync_virtual(struct omap_display_buffer *buffer)
{
	/*
	 * TODO: Support for ORIENTATION_VERTICAL is in place,
	 * ORIENTATION_HORIZONTAL is missing. Some code can be reduced here,
	 * it will be simplified in the future.
	 */
	struct omap_display_device *display_virtual = buffer->display;
	struct omap_display_device *display_primary;
	struct omap_display_device *display_secondary;
	struct omap_display_buffer temp_buffer_top;
	struct omap_display_buffer temp_buffer_bottom;
	unsigned int buffer_offset;

	if (display_virtual->id != OMAP_DISPID_VIRTUAL) {
		ERR_PRINT("Not a virtual display");
		BUG();
	}

	display_primary = omap_display_get(OMAP_DISPID_PRIMARY);
	display_secondary = omap_display_get(OMAP_DISPID_SECONDARY);
	/*
	 * Calculate offset without page alignment round up otherwise second
	 * display may see incorrect data
	 */
	buffer_offset = display_primary->height * display_virtual->byte_stride;

	/* The first buffer will be the top */
	temp_buffer_top.physical_addr = buffer->physical_addr;
	temp_buffer_top.virtual_addr = buffer->virtual_addr;
	temp_buffer_top.size = buffer->size >> 1;
	/* Then the bottom */
	temp_buffer_bottom.physical_addr = buffer->physical_addr +
		buffer_offset;
	temp_buffer_bottom.virtual_addr = buffer->virtual_addr + buffer_offset;
	temp_buffer_bottom.size = buffer->size >> 1;

	if (display_virtual->features & ORIENTATION_INVERT) {
		/* Secondary display has the base */
		temp_buffer_top.display = display_secondary;
		temp_buffer_bottom.display = display_primary;
		vdisp_sync_primary.buffer = &temp_buffer_bottom;
		vdisp_sync_secondary.buffer = &temp_buffer_top;
	} else {
		/* Primary display has the base */
		temp_buffer_top.display = display_primary;
		temp_buffer_bottom.display = display_secondary;
		vdisp_sync_primary.buffer = &temp_buffer_top;
		vdisp_sync_secondary.buffer = &temp_buffer_bottom;
	}

	/* Launch the workqueues for each display to present independently */
	queue_work(vdisp_wq_primary,
		(struct work_struct *)&vdisp_sync_primary);
	queue_work(vdisp_wq_secondary,
		(struct work_struct *)&vdisp_sync_secondary);

	/* Wait until each display sync and present */
	flush_workqueue(vdisp_wq_primary);
	flush_workqueue(vdisp_wq_secondary);

	return 0;
}

static int display_sync(struct omap_display_device *display)
{
	/* TODO: Synchronize properly with multiple managers */
	struct fb_info *framebuffer;
	struct omap_dss_device *dss_device;
	struct omap_dss_driver *driver;
	struct omap_overlay_manager *manager;
	int fb_idx;
	int err = 1;

	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unable to handle display %i", display->id);
		BUG();
	}

	if (fb_idx < 0 || fb_idx >= num_registered_fb) {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return 1;
	}

	framebuffer = registered_fb[fb_idx];
	dss_device = fb2display(framebuffer);

	if (!dss_device) {
		WRN_PRINT("No DSS device to sync with display '%s'!",
				display->name);
		return 1;
	}

	driver = dss_device->driver;
	manager = dss_device->manager;

	if (driver && driver->sync &&
		driver->get_update_mode(dss_device) == OMAP_DSS_UPDATE_MANUAL)
		err = driver->sync(dss_device);
	else if (manager && manager->wait_for_vsync)
		err = manager->wait_for_vsync(manager);

	if (err)
		WRN_PRINT("Unable to sync with display '%s'!", display->name);

	return err;
}

static int display_sync_virtual(struct omap_display_device *display_virtual)
{
	/*
	 * XXX: This function only waits for the primary display it should
	 * be adapted to the customer needs since waiting for the primary
	 * AND the secondary display may take too long for a single sync.
	 */
	struct omap_display_device *display_primary;

	if (display_virtual->id != OMAP_DISPID_VIRTUAL) {
		ERR_PRINT("Not a virtual display");
		BUG();
	}

	display_primary = omap_display_get(OMAP_DISPID_PRIMARY);
	return display_primary->sync(display_primary);
}

static struct omap_display_buffer *create_main_buffer(
	struct omap_display_device *display)
{
	int fb_idx;
	switch (display->id) {
	case OMAP_DISPID_PRIMARY:
		fb_idx = 0;
		break;
	case OMAP_DISPID_SECONDARY:
		fb_idx = 1;
		break;
	case OMAP_DISPID_TERTIARY:
		fb_idx = 2;
		break;
	case OMAP_DISPID_VIRTUAL:
		/* Use fb0 for virtual display */
		fb_idx = 0;
		break;
	case OMAP_DISPID_BADSTATE:
	default:
		ERR_PRINT("Unknown display id %i", display->id);
		BUG();
	}

	/* Use the framebuffer memory */
	if (fb_idx >= 0 && fb_idx < num_registered_fb) {
		struct fb_info *framebuffer = registered_fb[fb_idx];
		unsigned long buffer_size;
		struct omap_display_buffer *buffer;

		if (!framebuffer || !framebuffer->fix.smem_start ||
			!framebuffer->screen_base) {
			ERR_PRINT("Framebuffer %i doesn't seem to be "
				"initialized", fb_idx);
			return NULL;
		}

		/*
		 * Check if there is enough memory in the fb for the
		 * main buffer
		 */
		buffer_size = display->width * display->height *
			display->bytes_per_pixel;
		/* Page align the buffer size */
		buffer_size = OMAP_DISP_PAGE_ROUND_UP(buffer_size);

		if (buffer_size > framebuffer->fix.smem_len) {
			ERR_PRINT("Main buffer needs %lu bytes while the "
				"framebuffer %i has only %lu bytes for display"
				" '%s'", buffer_size, fb_idx,
				(unsigned long)framebuffer->fix.smem_len,
				display->name);
			return NULL;
		}

		buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);

		if (!buffer) {
			ERR_PRINT("Out of memory");
			return NULL;
		}

		/* Use base addresses reported by the framebuffer */
		buffer->physical_addr = framebuffer->fix.smem_start;
		buffer->virtual_addr =
			(unsigned long) framebuffer->screen_base;
		buffer->size = buffer_size;
		buffer->display = display;

		DBG_PRINT("Created main buffer %lx for display '%s'",
			buffer->physical_addr, display->name);

		return buffer;
	} else {
		ERR_PRINT("Framebuffer %i doesn't exist for display '%s'",
			fb_idx, display->name);
		return NULL;
	}
}

static int populate_display_info(struct omap_display_device *display,
	struct omap_overlay_manager *overlay_manager)
{
	struct omap_dss_device *dss_device = overlay_manager->device;
	u16 xres;
	u16 yres;
	int i;

	if (!strcmp(dss_device->name, "lcd")) {
		display->id = OMAP_DISPID_PRIMARY;
		display->name = "primary";
	} else if (!strcmp(dss_device->name, "lcd2")) {
		display->id = OMAP_DISPID_SECONDARY;
		display->name = "secondary";
	} else if (!strcmp(dss_device->name, "hdmi")) {
		display->id = OMAP_DISPID_TERTIARY;
		display->name = "tertiary";
	} else {
		ERR_PRINT("Display id '%s' not supported", dss_device->name);
		return 1;
	}

	dss_device->driver->get_resolution(dss_device, &xres, &yres);
	if (xres == 0 || yres == 0) {
		ERR_PRINT("Unable to handle display '%s' with width %i "
		"and height %i", dss_device->name, xres, yres);
		return 1;
	}

	display->width = xres;
	display->height = yres;

	display->bits_per_pixel =
		dss_device->driver->get_recommended_bpp(dss_device);
	switch (display->bits_per_pixel) {
	case 16:
		/*
		 * TODO: Asume RGB_565, maybe need to double check in
		 * the DSS if this is true
		 */
		display->pixel_format = RGB_565;
		display->bytes_per_pixel = 2;
		break;
	case 24: /* 24 bits are encapsulated with 32 bits */
	case 32:
		/*
		 * TODO: Asume ARGB_8888, maybe need to double check in
		 * the DSS if this is true
		 */
		display->pixel_format = ARGB_8888;
		display->bytes_per_pixel = 4;
		break;
	default:
		ERR_PRINT("Unable to handle %i bpp", display->bits_per_pixel);
		return 1;
	}

	display->byte_stride = display->bytes_per_pixel * display->width;
	display->rotation = OMAP_DSS_ROT_0; /* Asume rotation 0 degrees */
	display->main_buffer = 0;
	display->flip_chain = 0;

	/* Add the manager to the list */
	for (i = 0; i < OMAP_DISP_MAX_MANAGERS; i++)
		display->overlay_managers[i] = 0;

	display->overlay_managers[0] = overlay_manager;
	display->overlay_managers_count = 1;

	/* Assign function pointers for display operations */
	display->open = open_display;
	display->close = close_display;
	display->create_flip_chain = create_flip_chain;
	display->destroy_flip_chain = destroy_flip_chain;
	display->rotate = rotate_display;
	display->present_buffer = present_buffer;
	display->sync = display_sync;
	display->present_buffer_sync = present_buffer_sync;

	display->main_buffer = create_main_buffer(display);
	if (!display->main_buffer)
		WRN_PRINT("Failed to create main buffer for '%s'",
			display->name);

	display->buffers_available = get_max_buffers(display);

	/* Just print some display info */
	DBG_PRINT("Found display '%s-%s' (%i,%i) %i bpp (%i bytes per pixel)"
		" rotation %i", display->name, dss_device->name,
		display->width, display->height, display->bits_per_pixel,
		display->bytes_per_pixel, display->rotation);

	return 0;
}

static int populate_virtual_display_info(struct omap_display_device *display)
{
	struct omap_display_device *display_primary ;
	struct omap_display_device *display_secondary;
	int i;

	display->id = OMAP_DISPID_VIRTUAL;
	display->name = "virtual";

	display_primary = omap_display_get(OMAP_DISPID_PRIMARY);
	display_secondary = omap_display_get(OMAP_DISPID_SECONDARY);

	if (!display_primary) {
		ERR_PRINT("Primary display doesn't exist");
		return 1;
	} else if (!display_secondary) {
		ERR_PRINT("Secondary display doesn't exist");
		return 1;
	}

	/* Combine primary and secondary display resolutions */
	if (display_primary->width != display_secondary->width ||
		display_primary->height != display_secondary->height) {
		ERR_PRINT("Primary and seconday displays resolution are not"
			" the same");
		return 1;
	}

	/*
	 * TODO: Here it is hardcoded the resolution asumming a vertical
	 * virtual config, what about horizontal?
	 */
	display->width = display_primary->width;
	display->height = display_primary->height * 2;

	if (display_primary->bits_per_pixel !=
		display_secondary->bits_per_pixel) {
		ERR_PRINT("Primary and seconday displays format are"
			" not the same");
		return 1;
	}

	display->bits_per_pixel = display_primary->bits_per_pixel;
	switch (display->bits_per_pixel) {
	case 16:
		/*
		 * TODO: Asume RGB_565, maybe need to double check in
		 * the DSS if this is true
		 */
		display->pixel_format = RGB_565;
		display->bytes_per_pixel = 2;
		break;
	case 24: /* 24 bits are encapsulated with 32 bits */
	case 32:
		/*
		 * TODO: Asume ARGB_8888, maybe need to double check in
		 * the DSS if this is true
		 */
		display->pixel_format = ARGB_8888;
		display->bytes_per_pixel = 4;
		break;
	default:
		ERR_PRINT("Unable to handle %i bpp",
			display->bits_per_pixel);
		return 1;
	}

	/* TODO: Asumming a vertical virtual config too for stride */
	display->byte_stride = display->bytes_per_pixel * display->width;
	display->rotation = OMAP_DSS_ROT_0; /* Asume rotation 0 degrees */
	display->main_buffer = 0;
	display->flip_chain = 0;

	/* Add the primary and secondary overlay managers */
	for (i = 0; i < OMAP_DISP_MAX_MANAGERS; i++)
		display->overlay_managers[i] = 0;

	display->overlay_managers[0] = display_primary->overlay_managers[0];
	display->overlay_managers[1] = display_secondary->overlay_managers[0];
	display->overlay_managers_count = 2;

	/* Assign function pointers for display operations */
	display->open = open_display;
	display->close = close_display;
	display->create_flip_chain = create_flip_chain;
	display->destroy_flip_chain = destroy_flip_chain;
	display->rotate = rotate_display;
	display->present_buffer = present_buffer_virtual;
	display->sync = display_sync_virtual;
	display->present_buffer_sync = present_buffer_sync_virtual;

	display->main_buffer = create_main_buffer(display);
	if (!display->main_buffer)
		WRN_PRINT("Failed to create main buffer for '%s'",
			display->name);

	display->buffers_available = get_max_buffers(display);

	/* Just print some display info */
	DBG_PRINT("Found display '%s' (%i,%i) %i bpp (%i bytes per pixel)"
		" rotation %i", display->name, display->width, display->height,
		display->bits_per_pixel, display->bytes_per_pixel,
		display->rotation);

	return 0;
}

static int create_display_list(void)
{
	int i;
	struct omap_display_device *display;

	/* Query number of possible displays available first */
	omap_display_number = omap_dss_get_num_overlay_managers();
	/* For virtual display */
	omap_display_number++;

	/* Allocate custom display list */
	omap_display_list = kzalloc(
		sizeof(*display) * omap_display_number, GFP_KERNEL);

	if (!omap_display_list) {
		ERR_PRINT("Out of memory");
		return 1;
	}

	/* Populate each display info */
	for (i = 0; i < omap_display_number - 1; i++) {
		struct omap_overlay_manager *overlay_manager =
			omap_dss_get_overlay_manager(i);
		display = &omap_display_list[i];
		if (!overlay_manager->device) {
			WRN_PRINT("Display '%s' doesn't have a dss device "
				"attached to it, ignoring",
				overlay_manager->name);
			display->id = OMAP_DISPID_BADSTATE;
			continue;
		}
		if (populate_display_info(display, overlay_manager)) {
			ERR_PRINT("Error populating display %i info with "
				"manager '%s'", i,
				overlay_manager->device->name);
			display->id = OMAP_DISPID_BADSTATE;
			continue;
		}
	}

	/* Populate virtual display */
	display = &omap_display_list[omap_display_number - 1];
	if (populate_virtual_display_info(display)) {
		ERR_PRINT("Error populating virtual display info");
		display->id = OMAP_DISPID_BADSTATE;
	}

	return 0;
}

struct omap_display_device *omap_display_get(enum omap_display_id id)
{
	int i;
	struct omap_display_device *display;

	if (id == OMAP_DISPID_BADSTATE) {
		ERR_PRINT("Oops.. user must never request a bad display");
		BUG();
	}

	for (i = 0; i < omap_display_number; i++) {
		display = &omap_display_list[i];
		if (display->id == id)
			return display;
	}

	ERR_PRINT("Unknown display %i requested", id);
	return 0;
}
EXPORT_SYMBOL(omap_display_get);

int omap_display_count(void)
{
	return omap_display_number;
}
EXPORT_SYMBOL(omap_display_count);

int omap_display_initialize(void)
{
	/*
	 * TODO: Is there a better way to check if list is already created?
	 */
	if (!omap_display_list) {
		DBG_PRINT("Initializing driver");
		if (create_display_list()) {
			ERR_PRINT("Error loading driver");
			return 1;
		}
	}

	vdisp_wq_primary = __create_workqueue("vdisp_wq_primary", 1, 1, 1);
	vdisp_wq_secondary = __create_workqueue("vdisp_wq_secondary", 1, 1, 1);
	INIT_WORK((struct work_struct *)&vdisp_sync_primary,
		vdisp_sync_handler);
	INIT_WORK((struct work_struct *)&vdisp_sync_secondary,
		vdisp_sync_handler);

	return 0;
}
EXPORT_SYMBOL(omap_display_initialize);

int omap_display_deinitialize(void)
{
	int i;
	int err = 0;
	DBG_PRINT("Driver exiting");

	for (i = 0; i < omap_display_number; i++) {
		struct omap_display_device *display = &omap_display_list[i];

		if (!display)
			continue;

		if (display->main_buffer) {
			err = display_destroy_buffer(display->main_buffer);
			display->main_buffer = 0;
			if (err)
				WRN_PRINT("An error happened when destroying "
					"main buffer for '%s'", display->name);
		}

		err = display->close(display);

		if (err)
			ERR_PRINT("Unable to close display '%s'",
				display->name);
	}

	kfree(omap_display_list);
	omap_display_list = 0;

	destroy_workqueue(vdisp_wq_primary);
	destroy_workqueue(vdisp_wq_secondary);
	vdisp_wq_primary = NULL;
	vdisp_wq_secondary = NULL;

	return err;
}
EXPORT_SYMBOL(omap_display_deinitialize);