summaryrefslogtreecommitdiff
path: root/drivers/media/video/omap/omap_wb.c
blob: ca7480de0de1a826e86d04be0b15a79d15698e9a (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
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
/*
 * drivers/media/video/omap/omap_wb.c
 *
 * Copyright (C) 2005-2009 Texas Instruments.
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2. This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 *
 * Leveraged code from the OMAP2 camera driver
 * Video-for-Linux (Version 2) camera capture driver for
 * the OMAP24xx camera controller.
 *
 * Author: Andy Lowe (source@mvista.com)
 *
 * Copyright (C) 2004 MontaVista Software, Inc.
 * Copyright (C) 2009 Texas Instruments.
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/vmalloc.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/videodev2.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <media/videobuf-dma-contig.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#include <media/v4l2-device.h>

#include <asm/processor.h>
#include <plat/dma.h>
#include <plat/vram.h>
#include <plat/display.h>

#include "omap_wbdef.h"
#include "omap_voutlib.h"
#include <mach/tiler.h>

#define WB_NAME "omap_wb"

#define QQVGA_WIDTH		160
#define QQVGA_HEIGHT	120
#define WB_MIN_WIDTH		2
#define WB_MIN_HEIGHT		2
#define WB_MAX_WIDTH		2048
#define WB_MAX_HEIGHT		2048


struct mutex wb_lock;
static struct videobuf_queue_ops video_vbq_ops;

static int debug_wb;

module_param(debug_wb, bool, S_IRUGO);
MODULE_PARM_DESC(debug_wb, "Debug level (0-1)");

int omap_vout_try_format(struct v4l2_pix_format *pix);
enum omap_color_mode video_mode_to_dss_mode(
		struct v4l2_pix_format *pix);
void omap_wb_isr(void *arg, unsigned int irqstatus);
int	omap_dss_wb_apply(struct omap_overlay_manager *mgr, struct omap_writeback *wb);
int omap_dss_wb_flush(void);

int omap_setup_wb(struct omap_wb_device *wb_device, u32 addr, u32 uv_addr)
{
	struct omap_writeback *wb;
	struct omap_overlay_manager *mgr = NULL;
	struct omap_writeback_info wb_info;
	struct omap_overlay *ovl = NULL;
	int r = 0;
	int i = 0;
	wb_info.enabled = true;
	wb_info.info_dirty = true;
	wb_info.capturemode = wb_device->capturemode;
	wb_info.dss_mode = wb_device->dss_mode;
	wb_info.out_height = wb_device->pix.height;
	wb_info.out_width = wb_device->pix.width;
	wb_info.source = wb_device->source;
	wb_info.source_type = wb_device->source_type;
	wb_info.paddr = addr;
	wb_info.puv_addr = uv_addr;

	ovl = omap_dss_get_overlay(wb_info.source - 3);

	wb_info.height = ovl->info.out_height;
	wb_info.width = ovl->info.out_width;

	v4l2_dbg(1, debug_wb, &wb_device->wb_dev->v4l2_dev,
			"omap_write back struct contents :\n"
			"enabled = %d\n infodirty = %d\n"
			"capturemode = %d\n dss_mode = %d\n"
			"height = %ld\n width = %ld\n out_height = %ld\n"
			"out_width = %ld\n source = %d\n"
			"source_type = %d\n paddr =%lx\n puvaddr = %lx\n",
			wb_info.enabled, wb_info.info_dirty, wb_info.capturemode,
			wb_info.dss_mode, wb_info.height, wb_info.width,
			wb_info.out_height, wb_info.out_width, wb_info.source,
			wb_info.source_type, wb_info.paddr, wb_info.puv_addr);

	for (i = 0; i < omap_dss_get_num_overlay_managers(); ++i) {
			/* Fix : checking for mgr will shift to DSS2 */
			mgr = omap_dss_get_overlay_manager(i);
			if (strcmp(mgr->name, "lcd") == 0)
				break;
	}

	wb = omap_dss_get_wb(0);

	if (!wb) {
		printk(KERN_ERR WB_NAME "couldn't get wb struct\n");
		r = -EINVAL;
		goto err;
	}
	wb->enabled = true;
	wb->info_dirty = true;

	r = wb->set_wb_info(wb, &wb_info);
	if (r) {
		printk(KERN_ERR WB_NAME "wb_info not set\n");
		goto err;
	}
	r = omap_dss_wb_apply(mgr, wb);
err:
	return r;
}

int omap_wb_try_format(enum omap_color_mode *color_mode,
			struct v4l2_pix_format *pix)
{
	int bpp = 0;

	pix->height = clamp(pix->height, (u32)WB_MIN_HEIGHT,
			(u32)WB_MAX_HEIGHT);
	pix->width = clamp(pix->width, (u32)WB_MIN_WIDTH, (u32)WB_MAX_WIDTH);

	switch (pix->pixelformat) {
	case V4L2_PIX_FMT_RGB565:
	case V4L2_PIX_FMT_RGB565X:
		*color_mode = OMAP_DSS_COLOR_RGB16;
		bpp = RGB565_BPP;
		break;
	case V4L2_PIX_FMT_RGB24:
		*color_mode = OMAP_DSS_COLOR_RGB24P;
		bpp = RGB24_BPP;
		break;
	case V4L2_PIX_FMT_BGR32:
		*color_mode = OMAP_DSS_COLOR_RGBX32;
		bpp = RGB32_BPP;
	case V4L2_PIX_FMT_YUYV:
		*color_mode = OMAP_DSS_COLOR_YUV2;
		bpp = YUYV_BPP;
		break;
	case V4L2_PIX_FMT_UYVY:
		*color_mode = OMAP_DSS_COLOR_UYVY;
		bpp = YUYV_BPP;
		break;
	case V4L2_PIX_FMT_NV12:
		*color_mode = OMAP_DSS_COLOR_NV12;
		bpp = 1; /* TODO: check this? */
		break;
	case V4L2_PIX_FMT_RGB32:
	default:
		*color_mode = OMAP_DSS_COLOR_ARGB32;
		bpp = RGB32_BPP;
		break;
	}

	pix->colorspace = V4L2_COLORSPACE_SRGB;
	pix->field = V4L2_FIELD_ANY;
	pix->priv = 0;

	pix->bytesperline = pix->width * bpp;
	pix->bytesperline = (pix->bytesperline + PAGE_SIZE - 1) &
		~(PAGE_SIZE - 1);
	pix->sizeimage = pix->bytesperline * pix->height;

	return bpp;
}

/*
 *
 * IOCTL interface.
 *
 */

static int vidioc_querycap(struct file *file, void *fh,
		struct v4l2_capability *cap)
{
	struct omap_wb_device *wb = fh;

	strlcpy(cap->driver, WB_NAME,
		sizeof(cap->driver));
	strlcpy(cap->card, wb->vfd->name, sizeof(cap->card));
	cap->bus_info[0] = '\0';
	cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
	return 0;
}

static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
			struct v4l2_format *f)
{
	struct omap_wb_device *wb = fh;

	f->fmt.pix = wb->pix;
	return 0;

}

static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
			struct v4l2_format *f)
{
	struct omap_wb_device *wb = fh;
	int bpp;
	enum omap_color_mode color_mode;

	if (wb->streaming)
		return -EBUSY;

	mutex_lock(&wb->lock);

	bpp = omap_wb_try_format(&color_mode, &f->fmt.pix);

	/* try & set the new output format */
	wb->bpp = bpp;
	wb->pix = f->fmt.pix;

	wb->win.w.width = wb->pix.width;
	wb->win.w.height = wb->pix.height;

	/* we need to get curr_display dimensions for wb pipeline */
	wb->win.w.left = ((864 - wb->win.w.width) >> 1) & ~1;
	wb->win.w.top = ((480 - wb->win.w.height) >> 1) & ~1;

	wb->dss_mode = color_mode;

	mutex_unlock(&wb->lock);

	return 0;
}

static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
			struct v4l2_format *f)
{
	int ret = 0;
	struct omap_wb_device *wb = fh;
	struct v4l2_window *win = &f->fmt.win;

	mutex_lock(&wb->lock);
	/* No boundry checks for wb window for now */
	wb->win.w.left = win->w.left;
	wb->win.w.top = win->w.top;
	wb->win.w.width = win->w.width;
	wb->win.w.height = win->w.height;
	mutex_unlock(&wb->lock);

	return ret;
}

static int vidioc_reqbufs(struct file *file, void *fh,
			struct v4l2_requestbuffers *req)
{
	struct omap_wb_device *wb = fh;
	struct videobuf_queue *q = &wb->vbq;
	unsigned int i;
	int ret = 0;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "entered REQbuf: \n");

	if ((req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) || (req->count < 0))
		return -EINVAL;

	/* if memory is not mmp or userptr
	   return error */
	if ((V4L2_MEMORY_MMAP != req->memory) &&
		(V4L2_MEMORY_USERPTR != req->memory))
		return -EINVAL;

	mutex_lock(&wb->lock);

	/* Cannot be requested when streaming is on */
	if (wb->streaming) {
		mutex_unlock(&wb->lock);
		return -EBUSY;
	}

	/* If buffers are already allocated free them */
	if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
		if (wb->mmap_count) {
			mutex_unlock(&wb->lock);
			return -EBUSY;
		}

		videobuf_mmap_free(q);
	} else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
		if (wb->buffer_allocated) {
			videobuf_mmap_free(q);
			for (i = 0; i < wb->buffer_allocated; i++) {
				kfree(q->bufs[i]);
				q->bufs[i] = NULL;
			}
			wb->buffer_allocated = 0;
		}
	}
	/*store the memory type in data structure */
	wb->memory = req->memory;

	INIT_LIST_HEAD(&wb->dma_queue);

	/* call videobuf_reqbufs api */
	ret = videobuf_reqbufs(q, req);
	if (ret < 0) {
		mutex_unlock(&wb->lock);
		return ret;
	}

	wb->buffer_allocated = req->count;
	mutex_unlock(&wb->lock);
	return 0;
}

static int vidioc_querybuf(struct file *file, void *fh,
			struct v4l2_buffer *b)
{
	struct omap_wb_device *wb = fh;

	return videobuf_querybuf(&wb->vbq, b);
}

static int vidioc_qbuf(struct file *file, void *fh,
			struct v4l2_buffer *buffer)
{
	struct omap_wb_device *wb = fh;
	struct videobuf_queue *q = &wb->vbq;
	int ret = 0;
	u32 addr = 0, uv_addr = 0;
	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
		"entered qbuf: buffer address: %x\n", (unsigned int) buffer);

	if ((V4L2_BUF_TYPE_VIDEO_CAPTURE != buffer->type) ||
			(buffer->index >= wb->buffer_allocated) ||

			(q->bufs[buffer->index]->memory != buffer->memory)) {
		return -EINVAL;
	}
	if (V4L2_MEMORY_USERPTR == buffer->memory) {
		if ((buffer->length < wb->pix.sizeimage) ||
			(0 == buffer->m.userptr)) {
			return -EINVAL;
		}
	}

	ret = videobuf_qbuf(q, buffer);

	if (wb->streaming && wb->buf_empty) {
		addr = (unsigned long)wb->queued_buf_addr[wb->next_frm->i];
		uv_addr = (unsigned long) wb->queued_buf_uv_addr[wb->cur_frm->i];
		wb->buf_empty = 0;
		omap_setup_wb(wb, addr, uv_addr);
	}
	return ret;
}

static int vidioc_dqbuf(struct file *file, void *fh,
			struct v4l2_buffer *b)
{
	struct omap_wb_device *wb = fh;
	struct videobuf_queue *q = &wb->vbq;
	int ret = 0;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
		"entered DQbuf: buffer address: %x \n", (unsigned int) b);

	if (!wb->streaming)
		return -EINVAL;

	if (file->f_flags & O_NONBLOCK)
		/* Call videobuf_dqbuf for non blocking mode */
		ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
	else
		/* Call videobuf_dqbuf for  blocking mode */
		ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
	return ret;
}

static int vidioc_streamon(struct file *file, void *fh,
			enum v4l2_buf_type i)
{
	struct omap_wb_device *wb = fh;
	struct videobuf_queue *q = &wb->vbq;
	u32 addr = 0, uv_addr = 0;
	int r = 0;
	u32 mask = 0;

	mutex_lock(&wb->lock);

	if (wb->streaming) {
		mutex_unlock(&wb->lock);
		return -EBUSY;
	}

	r = videobuf_streamon(q);
	if (r < 0) {
		mutex_unlock(&wb->lock);
		return r;
	}

	if (list_empty(&wb->dma_queue)) {
		mutex_unlock(&wb->lock);
		return -EIO;
	}
	/* Get the next frame from the buffer queue */
	wb->next_frm = wb->cur_frm = list_entry(wb->dma_queue.next,
				struct videobuf_buffer, queue);
	/* Remove buffer from the buffer queue */
	list_del(&wb->cur_frm->queue);
	/* Mark state of the current frame to active */
	wb->cur_frm->state = VIDEOBUF_ACTIVE;

	/* set flag here. Next QBUF will start DMA */
	wb->streaming = 1;

	wb->first_int = 1;

	addr = (unsigned long) wb->queued_buf_addr[wb->cur_frm->i];

	uv_addr = (unsigned long) wb->queued_buf_uv_addr[wb->cur_frm->i];

	mask = DISPC_IRQ_FRAMEDONE_WB;

	r = omap_dispc_register_isr(omap_wb_isr, wb, mask);
	if (r) {
		printk(KERN_ERR WB_NAME "Failed to register wb_isr\n");
		return r;
	}

	r = omap_setup_wb(wb, addr, uv_addr);
	if (r)
		printk(KERN_ERR WB_NAME "error in setup_wb\n");

	mutex_unlock(&wb->lock);
	return r;
}

static int vidioc_streamoff(struct file *file, void *fh,
			enum v4l2_buf_type i)
{
	struct omap_wb_device *wb = fh;
	u32 mask = 0;
	int r = 0;

	if (!wb->streaming)
		return -EINVAL;

	wb->streaming = 0;
	mask = DISPC_IRQ_FRAMEDONE_WB;

	r = omap_dispc_unregister_isr(omap_wb_isr, wb, mask);
	if (r) {
		printk(KERN_ERR WB_NAME "Failed to unregister wb_isr\n");
		return r;
	}
	omap_dss_wb_flush();

	INIT_LIST_HEAD(&wb->dma_queue);
	videobuf_streamoff(&wb->vbq);
	videobuf_queue_cancel(&wb->vbq);
	return 0;
}

static long vidioc_default(struct file *file, void *fh,
			     int cmd, void *arg)
{
	struct v4l2_writeback_ioctl_data *wb_data = NULL;
	struct omap_wb_device *wb = fh;
	wb_data  = (struct v4l2_writeback_ioctl_data *)arg;

	switch (cmd) {
	case VIDIOC_CUSTOM_S_WB:
		if (!wb_data) {
			printk(KERN_ERR WB_NAME "error in S_WB\n");
			goto err;
		}
		mutex_lock(&wb->lock);
		wb->enabled = wb_data->enabled;
		wb->source = wb_data->source;
		wb->capturemode = wb_data->capturemode;
		wb->source_type = wb_data->source_type;
		mutex_unlock(&wb->lock);

		break;

	case VIDIOC_CUSTOM_G_WB:
		if (!wb_data) {
			printk(KERN_ERR WB_NAME "error in G_WB\n");
			goto err;
		}
		mutex_lock(&wb->lock);
		wb_data->source = wb->source;
		wb_data->capturemode = wb->capturemode;
		wb_data->source_type = wb->source_type;
		mutex_unlock(&wb->lock);

		break;
	default:
		BUG();
	}
	return 0;

err:
	mutex_unlock(&wb->lock);
	return -EINVAL;
}

static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
			struct v4l2_format *f)
{

	struct omap_wb_device *wb = fh;
	struct v4l2_window *win = &f->fmt.win;

	win->w = wb->win.w;
	win->field = wb->win.field;
	win->global_alpha = wb->win.global_alpha;

	return 0;
}

static const struct v4l2_ioctl_ops wb_ioctl_fops = {
	.vidioc_querycap	= vidioc_querycap,
	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
	.vidioc_reqbufs		= vidioc_reqbufs,
	.vidioc_querybuf	= vidioc_querybuf,
	.vidioc_qbuf		= vidioc_qbuf,
	.vidioc_dqbuf		= vidioc_dqbuf,
	.vidioc_streamon	= vidioc_streamon,
	.vidioc_streamoff	= vidioc_streamoff,
	.vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
	.vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
	.vidioc_default		= vidioc_default,
};

static void omap_wb_tiler_buffer_free(struct omap_wb_device *wb,
					unsigned int count,
					unsigned int startindex)
{
	int i;

	if (startindex < 0)
		startindex = 0;
	if (startindex + count > VIDEO_MAX_FRAME)
		count = VIDEO_MAX_FRAME - startindex;

	for (i = startindex; i < startindex + count; i++) {
		if (wb->buf_phy_addr_alloced[i])
			tiler_free(wb->buf_phy_addr_alloced[i]);
		if (wb->buf_phy_uv_addr_alloced[i])
			tiler_free(wb->buf_phy_uv_addr_alloced[i]);
		wb->buf_phy_addr[i] = 0;
		wb->buf_phy_addr_alloced[i] = 0;
		wb->buf_phy_uv_addr[i] = 0;
		wb->buf_phy_uv_addr_alloced[i] = 0;
	}
}

/* Allocate the buffers for  TILER space.  Ideally, the buffers will be ONLY
 in tiler space, with different rotated views available by just a convert.
 */
static int omap_wb_tiler_buffer_setup(struct omap_wb_device *wb,
					unsigned int *count, unsigned int startindex,
					struct v4l2_pix_format *pix)
{
	int i, aligned = 1;
	enum tiler_fmt fmt;

	/* normalize buffers to allocate so we stay within bounds */
	int start = (startindex < 0) ? 0 : startindex;
	int n_alloc = (start + *count > VIDEO_MAX_FRAME)
		? VIDEO_MAX_FRAME - start : *count;
	int bpp = omap_vout_try_format(pix);

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "tiler buffer alloc:\n"
		"count - %d, start -%d :\n", *count, startindex);

	/* special allocation scheme for NV12 format */
	if (OMAP_DSS_COLOR_NV12 == video_mode_to_dss_mode(pix)) {
		tiler_alloc_packed_nv12(&n_alloc, pix->width,
			pix->height,
			(void **) wb->buf_phy_addr + start,
			(void **) wb->buf_phy_uv_addr + start,
			(void **) wb->buf_phy_addr_alloced + start,
			(void **) wb->buf_phy_uv_addr_alloced + start,
			aligned);
	} else {
		/* Only bpp of 1, 2, and 4 is supported by tiler */
		fmt = (bpp == 1 ? TILFMT_8BIT :
			bpp == 2 ? TILFMT_16BIT :
			bpp == 4 ? TILFMT_32BIT : TILFMT_INVALID);
		if (fmt == TILFMT_INVALID)
			return -ENOMEM;

		tiler_alloc_packed(&n_alloc, fmt, pix->width,
			pix->height,
			(void **) wb->buf_phy_addr + start,
			(void **) wb->buf_phy_addr_alloced + start,
			aligned);
	}

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
			"allocated %d buffers\n", n_alloc);

	if (n_alloc < *count) {
		if (n_alloc && (startindex == -1 ||
			V4L2_MEMORY_MMAP != wb->memory)) {
			/* TODO: check this condition's logic */
			omap_wb_tiler_buffer_free(wb, n_alloc, start);
			*count = 0;
			return -ENOMEM;
		}
	}

	for (i = start; i < start + n_alloc; i++) {
		v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
				"y=%08lx (%d) uv=%08lx (%d)\n",
				wb->buf_phy_addr[i],
				wb->buf_phy_addr_alloced[i] ? 1 : 0,
				wb->buf_phy_uv_addr[i],
				wb->buf_phy_uv_addr_alloced[i] ? 1 : 0);
	}

	*count = n_alloc;

	return 0;
}

/* Free tiler buffers */
static void omap_wb_free_tiler_buffers(struct omap_wb_device *wb)
{
	omap_wb_tiler_buffer_free(wb, wb->buffer_allocated, 0);
	wb->buffer_allocated = 0;
}


/* Video buffer call backs */

/* Buffer setup function is called by videobuf layer when REQBUF ioctl is
 * called. This is used to setup buffers and return size and count of
 * buffers allocated. After the call to this buffer, videobuf layer will
 * setup buffer queue depending on the size and count of buffers
 */
static int omap_wb_buffer_setup(struct videobuf_queue *q, unsigned int *count,
			  unsigned int *size)
{
	struct omap_wb_device *wb = q->priv_data;
	int i;

	if (!wb)
		return -EINVAL;

	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != q->type)
		return -EINVAL;

	/* Now allocated the V4L2 buffers */
	/* i is the block-width - either 4K or 8K, depending upon input width*/
	i = (wb->pix.width * wb->bpp +
		TILER_PAGE - 1) & ~(TILER_PAGE - 1);

	/* for NV12 format, buffer is height + height / 2*/
	if (OMAP_DSS_COLOR_NV12 == wb->dss_mode)
		*size = wb->buffer_size = (wb->pix.height * 3/2 * i);
	else
		*size = wb->buffer_size = (wb->pix.height * i);

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
			"\nheight=%d, size = %d, wb->buffer_sz=%d\n",
			wb->pix.height, *size, wb->buffer_size);
	if (omap_wb_tiler_buffer_setup(wb, count, 0, &wb->pix))
			return -ENOMEM;

	if (V4L2_MEMORY_MMAP != wb->memory)
		return 0;

	return 0;
}

/* This function will be called when VIDIOC_QBUF ioctl is called.
 * It prepare buffers before give out for the display. This function
 * user space virtual address into physical address if userptr memory
 * exchange mechanism is used. If rotation is enabled, it copies entire
 * buffer into VRFB memory space before giving it to the DSS.
 */
static int omap_wb_buffer_prepare(struct videobuf_queue *q,
			    struct videobuf_buffer *vb,
			    enum v4l2_field field)
{
	struct omap_wb_device *wb = q->priv_data;

	if (VIDEOBUF_NEEDS_INIT == vb->state) {
		vb->width = wb->pix.width;
		vb->height = wb->pix.height;
		vb->size = vb->width * vb->height * wb->bpp;
		vb->field = field;
	}
	vb->state = VIDEOBUF_PREPARED;

	/* Here, we need to use the physical addresses given by Tiler:
	*/
	wb->queued_buf_addr[vb->i] = (u8 *) wb->buf_phy_addr[vb->i]; 
	wb->queued_buf_uv_addr[vb->i] = (u8 *) wb->buf_phy_uv_addr[vb->i];
	return 0;
}

/* Buffer queue funtion will be called from the videobuf layer when _QBUF
 * ioctl is called. It is used to enqueue buffer, which is ready to be
 * displayed. */
static void omap_wb_buffer_queue(struct videobuf_queue *q,
			  struct videobuf_buffer *vb)
{
	struct omap_wb_device *wb = q->priv_data;

	/* Driver is also maintainig a queue. So enqueue buffer in the driver
	 * queue */
	list_add_tail(&vb->queue, &wb->dma_queue);

	vb->state = VIDEOBUF_QUEUED;
}

/* Buffer release function is called from videobuf layer to release buffer
 * which are already allocated */
static void omap_wb_buffer_release(struct videobuf_queue *q,
			    struct videobuf_buffer *vb)
{
	struct omap_wb_device *wb = q->priv_data;

	vb->state = VIDEOBUF_NEEDS_INIT;

	if (V4L2_MEMORY_MMAP != wb->memory)
		return;
}

/*
 *  File operations
 */
static void omap_wb_vm_open(struct vm_area_struct *vma)
{
	struct omap_wb_device *wb = vma->vm_private_data;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
	"vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
	wb->mmap_count++;
}

static void omap_wb_vm_close(struct vm_area_struct *vma)
{
	struct omap_wb_device *wb = vma->vm_private_data;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
	"vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
	wb->mmap_count--;
}

static struct vm_operations_struct omap_wb_vm_ops = {
	.open = omap_wb_vm_open,
	.close = omap_wb_vm_close,
};

static int omap_wb_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct omap_wb_device *wb = file->private_data;
	struct videobuf_queue *q = &wb->vbq;
	int i;
	void *pos;

	int j = 0, k = 0, m = 0, p = 0, m_increment = 0;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
		" %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
		vma->vm_pgoff, vma->vm_start, vma->vm_end);

	/* look for the buffer to map */
	for (i = 0; i < VIDEO_MAX_FRAME; i++) {
		if (NULL == q->bufs[i])
			continue;
		if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
			continue;
		if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
			break;
	}

	if (VIDEO_MAX_FRAME == i) {
		v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev,
		"offset invalid [offset=0x%lx]\n",
			(vma->vm_pgoff << PAGE_SHIFT));
		return -EINVAL;
	}
	q->bufs[i]->baddr = vma->vm_start;

	vma->vm_flags |= VM_RESERVED;
	vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
	vma->vm_ops = &omap_wb_vm_ops;
	vma->vm_private_data = (void *) wb;

	pos = (void*) wb->buf_phy_addr[i];
	/* get line width */
	/* for NV12, Y buffer is 1bpp*/

	if (OMAP_DSS_COLOR_NV12 == wb->dss_mode) {
		p = (wb->pix.width +
			TILER_PAGE - 1) & ~(TILER_PAGE - 1);
		m_increment = 64 * TILER_WIDTH;
	} else {
		p = (wb->pix.width * wb->bpp +
			TILER_PAGE - 1) & ~(TILER_PAGE - 1);

		if (wb->bpp > 1)
			m_increment = 2*64*TILER_WIDTH;
		else
			m_increment = 64 * TILER_WIDTH;
	}

	for (j = 0; j < wb->pix.height; j++) {
		/* map each page of the line */

		vma->vm_pgoff =
			((unsigned long)pos + m) >> PAGE_SHIFT;

		if (remap_pfn_range(vma, vma->vm_start + k,
			((unsigned long)pos + m) >> PAGE_SHIFT,
			p, vma->vm_page_prot))
				return -EAGAIN;
		k += p;
		m += m_increment;
	}
	m = 0;

	/* UV Buffer in case of NV12 format */
	if (OMAP_DSS_COLOR_NV12 == wb->dss_mode) {
		pos = (void*) wb->buf_phy_uv_addr[i];
		/* UV buffer is 2 bpp, but half size, so p remains */
		m_increment = 2*64*TILER_WIDTH;

		/* UV buffer is height / 2*/
		for (j = 0; j < wb->pix.height / 2; j++) {
			/* map each page of the line */
			vma->vm_pgoff =
				((unsigned long)pos + m) >> PAGE_SHIFT;

			if (remap_pfn_range(vma, vma->vm_start + k,
				((unsigned long)pos + m) >> PAGE_SHIFT,
				p, vma->vm_page_prot))
				return -EAGAIN;
			k += p;
			m += m_increment;
		}
	}

	vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
	wb->mmap_count++;
	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "Exiting %s\n", __func__);
	return 0;
}

static int omap_wb_release(struct file *file)
{

	struct omap_wb_device *wb = file->private_data;
	struct videobuf_queue *q;
	unsigned int r = 0;

	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "Entering %s\n", __func__);

	if (!wb)
		return 0;

	q = &wb->vbq;

	omap_wb_free_tiler_buffers(wb);

	videobuf_mmap_free(q);

	/* Even if apply changes fails we should continue
	   freeing allocated memeory */
	if (wb->streaming) {
		u32 mask = 0;

		mask = DISPC_IRQ_FRAMEDONE_WB;

		omap_dispc_unregister_isr(omap_wb_isr, wb, mask);
		wb->streaming = 0;

		videobuf_streamoff(q);
		videobuf_queue_cancel(q);
	}

	if (wb->mmap_count != 0)
		wb->mmap_count = 0;

	wb->opened -= 1;
	file->private_data = NULL;

	if (wb->buffer_allocated)
		videobuf_mmap_free(q);
	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "Exiting %s\n", __func__);
	return r;
}

static int omap_wb_open(struct file *file)
{
	struct omap_wb_device *wb = NULL;
	struct videobuf_queue *q;

	wb = video_drvdata(file);
	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "Entering %s\n", __func__);

	if (wb == NULL)
		return -ENODEV;

	/* for now, we only support single open */
	if (wb->opened)
		return -EBUSY;

	wb->opened += 1;
	wb->enabled = true;
	file->private_data = wb;
	wb->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

	q = &wb->vbq;
	video_vbq_ops.buf_setup = omap_wb_buffer_setup;
	video_vbq_ops.buf_prepare = omap_wb_buffer_prepare;
	video_vbq_ops.buf_release = omap_wb_buffer_release;
	video_vbq_ops.buf_queue = omap_wb_buffer_queue;
	spin_lock_init(&wb->vbq_lock);

	videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev, &wb->vbq_lock,
			       wb->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer),
				wb);	
	v4l2_dbg(1, debug_wb, &wb->wb_dev->v4l2_dev, "Exiting %s\n", __func__);
	return 0;
}

static struct v4l2_file_operations wb_fops = {
	.owner		= THIS_MODULE,
	.ioctl	 = video_ioctl2,
	.mmap	 = omap_wb_mmap,
	.open	 = omap_wb_open,
	.release = omap_wb_release,
};


/* Init functions used during driver intitalization */
/* Initial setup of video_data */
static int __init omap_wb_setup_video_data(struct omap_wb_device *wb)
{
	struct v4l2_pix_format *pix;
	struct video_device *vfd;
	/* set the default pix */
	pix = &wb->pix;

	/* Set the default picture of QVGA  */
	pix->width = QQVGA_WIDTH;
	pix->height = QQVGA_HEIGHT;

	/* Default pixel format is RGB 5-6-5 */
	pix->pixelformat = V4L2_PIX_FMT_RGB32;
	pix->field = V4L2_FIELD_ANY;
	pix->bytesperline = pix->width * 2;
	pix->sizeimage = pix->bytesperline * pix->height;
	pix->priv = 0;
	pix->colorspace = V4L2_COLORSPACE_JPEG;

	wb->bpp = RGB32_BPP;
	wb->enabled = false;
	wb->buffer_allocated = 0;
	wb->capturemode = V4L2_WB_CAPTURE_ALL;
	wb->source = V4L2_WB_OVERLAY0;
	wb->source_type =  V4L2_WB_SOURCE_OVERLAY;
	wb->dss_mode = OMAP_DSS_COLOR_ARGB32;
	/* initialize the video_device struct */
	vfd = wb->vfd = video_device_alloc();

	if (!vfd) {
		printk(KERN_ERR WB_NAME ": could not allocate"
				" WB device struct\n");
		return -ENOMEM;
	}
	vfd->release = video_device_release;
	vfd->ioctl_ops = &wb_ioctl_fops;

	strlcpy(vfd->name, WB_NAME, sizeof(vfd->name));
	vfd->vfl_type = VFL_TYPE_GRABBER;

	/* need to register for a VID_HARDWARE_* ID in videodev.h */
	vfd->fops = &wb_fops;
	mutex_init(&wb->lock);

	vfd->minor = -1;
	return 0;
}


/* Create wb devices */
static int __init omap_wb_create_video_devices(struct platform_device *pdev)
{
	int r = 0, k;
	struct omap_wb_device *wb;
	struct video_device *vfd = NULL;
	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);

	struct omap2wb_device *wb_dev = container_of(v4l2_dev, struct
			omap2wb_device, v4l2_dev);

	for (k = 0; k < pdev->num_resources; k++) {

		wb = kmalloc(sizeof(struct omap_wb_device), GFP_KERNEL);
		if (!wb) {
			printk(KERN_ERR WB_NAME
					": could not allocate memory\n");
			return -ENOMEM;
		}

		memset(wb, 0, sizeof(struct omap_wb_device));

		wb->wid = k;
		wb->wb_dev = wb_dev;

		/* Setup the default configuration for the wb devices
		 */
		if (omap_wb_setup_video_data(wb) != 0) {
			r = -ENOMEM;
			goto error;
		}

		/* Register the Video device with V4L2
		 */
		vfd = wb->vfd;
		if (video_register_device(vfd, VFL_TYPE_GRABBER, k + 1) < 0) {
			printk(KERN_ERR WB_NAME ": could not register "
					"Video for Linux device\n");
			vfd->minor = -1;
			r = -ENODEV;
			goto error1;
		}
		video_set_drvdata(vfd, wb);

		goto success;

error1:
	video_device_release(vfd);
error:
	kfree(wb);
	return r;

success:
	printk(KERN_INFO WB_NAME ": registered and initialized "
			"wb device %d [v4l2]\n", vfd->minor);
	if (k == (pdev->num_resources - 1))
		return 0;
	}
	return -ENODEV;

}

/* Driver functions */

static void omap_wb_cleanup_device(struct omap_wb_device *wb)
{
	struct video_device *vfd;

	if (!wb)
		return;

	vfd = wb->vfd;

	if (vfd) {
		if (vfd->minor == -1) {
			/*
			 * The device was never registered, so release the
			 * video_device struct directly.
			 */
			video_device_release(vfd);
		} else {
			/*
			 * The unregister function will release the video_device
			 * struct as well as unregistering it.
			 */
			video_unregister_device(vfd);
		}
	}

	omap_wb_free_tiler_buffers(wb);

	kfree(wb);
}

static int omap_wb_remove(struct platform_device *pdev)
{

	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
	struct omap2wb_device *wb_dev = container_of(v4l2_dev, struct
			omap2wb_device, v4l2_dev);
	int k;

	v4l2_device_unregister(v4l2_dev);
	for (k = 0; k < pdev->num_resources; k++)
		omap_wb_cleanup_device(wb_dev->wb);

	kfree(wb_dev);

	return 0;
}

static int __init omap_wb_probe(struct platform_device *pdev)
{
	int r = 0;
	struct omap2wb_device *wb_dev = NULL;

	if (pdev->num_resources == 0) {
		dev_err(&pdev->dev, "probed for an unknown device\n");
		r = -ENODEV;
		return r;
	}

	wb_dev = kzalloc(sizeof(struct omap2wb_device), GFP_KERNEL);
	if (wb_dev == NULL) {
		r = -ENOMEM;
		return r;
	}

	if (v4l2_device_register(&pdev->dev, &wb_dev->v4l2_dev) < 0) {
		printk(KERN_ERR WB_NAME "v4l2_device_register failed\n");
		return -ENODEV;
	}

	r = omap_wb_create_video_devices(pdev);
	if (r)
		goto error0;

	return 0;

error0:
	kfree(wb_dev);
	return r;
}

static struct platform_driver omap_wb_driver = {
	.driver = {
		   .name = WB_NAME,
		   },
	.probe = omap_wb_probe,
	.remove = omap_wb_remove,
};
void omap_wb_isr(void *arg, unsigned int irqstatus)
{
	struct timeval timevalue = {0};
	int r = 0;
	struct omap_wb_device *wb =
	    (struct omap_wb_device *) arg;
	u32 addr, uv_addr;
	unsigned long flags;

	spin_lock_irqsave(&wb->vbq_lock, flags);

	if (!wb->first_int && (wb->cur_frm != wb->next_frm)) {
		wb->cur_frm->ts = timevalue;
		wb->cur_frm->state = VIDEOBUF_DONE;
		wake_up_interruptible(&wb->cur_frm->done);
		wb->cur_frm = wb->next_frm;
		}

	wb->first_int = 0;
	if (list_empty(&wb->dma_queue)) {
		wb->buf_empty = true;
		spin_unlock_irqrestore(&wb->vbq_lock, flags);
		return;
	}

	wb->next_frm = list_entry(wb->dma_queue.next,
		struct videobuf_buffer, queue);
	list_del(&wb->next_frm->queue);

	wb->next_frm->state = VIDEOBUF_ACTIVE;
	addr = (unsigned long)wb->queued_buf_addr[wb->next_frm->i];

#ifdef CONFIG_ARCH_OMAP4
	uv_addr = (unsigned long)wb->queued_buf_uv_addr[
		wb->next_frm->i];
#endif
	r = omap_setup_wb(wb, addr, uv_addr);
	if (r)
		printk(KERN_ERR WB_NAME "error in setup_wb %d\n", r);

	spin_unlock_irqrestore(&wb->vbq_lock, flags);
}

static int __init omap_wb_init(void)
{

	if (platform_driver_register(&omap_wb_driver) != 0) {
		printk(KERN_ERR WB_NAME ": could not register \
				WB driver\n");
		return -EINVAL;
	}
	mutex_init(&wb_lock);
	return 0;
}

static void omap_wb_cleanup(void)
{
	platform_driver_unregister(&omap_wb_driver);
}

late_initcall(omap_wb_init);
module_exit(omap_wb_cleanup);