Free Electrons

Embedded Linux Experts

   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
/*
 * Analog Devices video capture driver
 *
 * Copyright (c) 2011 Analog Devices Inc.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/types.h>

#include <media/v4l2-chip-ident.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf2-dma-contig.h>

#include <asm/dma.h>

#include <media/blackfin/bfin_capture.h>
#include <media/blackfin/ppi.h>

#define CAPTURE_DRV_NAME        "bfin_capture"
#define BCAP_MIN_NUM_BUF        2

struct bcap_format {
	char *desc;
	u32 pixelformat;
	enum v4l2_mbus_pixelcode mbus_code;
	int bpp; /* bits per pixel */
};

struct bcap_buffer {
	struct vb2_buffer vb;
	struct list_head list;
};

struct bcap_device {
	/* capture device instance */
	struct v4l2_device v4l2_dev;
	/* v4l2 control handler */
	struct v4l2_ctrl_handler ctrl_handler;
	/* device node data */
	struct video_device *video_dev;
	/* sub device instance */
	struct v4l2_subdev *sd;
	/* capture config */
	struct bfin_capture_config *cfg;
	/* ppi interface */
	struct ppi_if *ppi;
	/* current input */
	unsigned int cur_input;
	/* current selected standard */
	v4l2_std_id std;
	/* used to store pixel format */
	struct v4l2_pix_format fmt;
	/* bits per pixel*/
	int bpp;
	/* used to store sensor supported format */
	struct bcap_format *sensor_formats;
	/* number of sensor formats array */
	int num_sensor_formats;
	/* pointing to current video buffer */
	struct bcap_buffer *cur_frm;
	/* pointing to next video buffer */
	struct bcap_buffer *next_frm;
	/* buffer queue used in videobuf2 */
	struct vb2_queue buffer_queue;
	/* allocator-specific contexts for each plane */
	struct vb2_alloc_ctx *alloc_ctx;
	/* queue of filled frames */
	struct list_head dma_queue;
	/* used in videobuf2 callback */
	spinlock_t lock;
	/* used to access capture device */
	struct mutex mutex;
	/* used to wait ppi to complete one transfer */
	struct completion comp;
	/* prepare to stop */
	bool stop;
};

struct bcap_fh {
	struct v4l2_fh fh;
	/* indicates whether this file handle is doing IO */
	bool io_allowed;
};

static const struct bcap_format bcap_formats[] = {
	{
		.desc        = "YCbCr 4:2:2 Interleaved UYVY",
		.pixelformat = V4L2_PIX_FMT_UYVY,
		.mbus_code   = V4L2_MBUS_FMT_UYVY8_2X8,
		.bpp         = 16,
	},
	{
		.desc        = "YCbCr 4:2:2 Interleaved YUYV",
		.pixelformat = V4L2_PIX_FMT_YUYV,
		.mbus_code   = V4L2_MBUS_FMT_YUYV8_2X8,
		.bpp         = 16,
	},
	{
		.desc        = "RGB 565",
		.pixelformat = V4L2_PIX_FMT_RGB565,
		.mbus_code   = V4L2_MBUS_FMT_RGB565_2X8_LE,
		.bpp         = 16,
	},
	{
		.desc        = "RGB 444",
		.pixelformat = V4L2_PIX_FMT_RGB444,
		.mbus_code   = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
		.bpp         = 16,
	},

};
#define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)

static irqreturn_t bcap_isr(int irq, void *dev_id);

static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
{
	return container_of(vb, struct bcap_buffer, vb);
}

static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
{
	enum v4l2_mbus_pixelcode code;
	struct bcap_format *sf;
	unsigned int num_formats = 0;
	int i, j;

	while (!v4l2_subdev_call(bcap_dev->sd, video,
				enum_mbus_fmt, num_formats, &code))
		num_formats++;
	if (!num_formats)
		return -ENXIO;

	sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
	if (!sf)
		return -ENOMEM;

	for (i = 0; i < num_formats; i++) {
		v4l2_subdev_call(bcap_dev->sd, video,
				enum_mbus_fmt, i, &code);
		for (j = 0; j < BCAP_MAX_FMTS; j++)
			if (code == bcap_formats[j].mbus_code)
				break;
		if (j == BCAP_MAX_FMTS) {
			/* we don't allow this sensor working with our bridge */
			kfree(sf);
			return -EINVAL;
		}
		sf[i] = bcap_formats[j];
	}
	bcap_dev->sensor_formats = sf;
	bcap_dev->num_sensor_formats = num_formats;
	return 0;
}

static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
{
	bcap_dev->num_sensor_formats = 0;
	kfree(bcap_dev->sensor_formats);
	bcap_dev->sensor_formats = NULL;
}

static int bcap_open(struct file *file)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct video_device *vfd = bcap_dev->video_dev;
	struct bcap_fh *bcap_fh;

	if (!bcap_dev->sd) {
		v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n");
		return -ENODEV;
	}

	bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL);
	if (!bcap_fh) {
		v4l2_err(&bcap_dev->v4l2_dev,
			 "unable to allocate memory for file handle object\n");
		return -ENOMEM;
	}

	v4l2_fh_init(&bcap_fh->fh, vfd);

	/* store pointer to v4l2_fh in private_data member of file */
	file->private_data = &bcap_fh->fh;
	v4l2_fh_add(&bcap_fh->fh);
	bcap_fh->io_allowed = false;
	return 0;
}

static int bcap_release(struct file *file)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct v4l2_fh *fh = file->private_data;
	struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);

	/* if this instance is doing IO */
	if (bcap_fh->io_allowed)
		vb2_queue_release(&bcap_dev->buffer_queue);

	file->private_data = NULL;
	v4l2_fh_del(&bcap_fh->fh);
	v4l2_fh_exit(&bcap_fh->fh);
	kfree(bcap_fh);
	return 0;
}

static int bcap_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return vb2_mmap(&bcap_dev->buffer_queue, vma);
}

#ifndef CONFIG_MMU
static unsigned long bcap_get_unmapped_area(struct file *file,
					    unsigned long addr,
					    unsigned long len,
					    unsigned long pgoff,
					    unsigned long flags)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
				     addr,
				     len,
				     pgoff,
				     flags);
}
#endif

static unsigned int bcap_poll(struct file *file, poll_table *wait)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return vb2_poll(&bcap_dev->buffer_queue, file, wait);
}

static int bcap_queue_setup(struct vb2_queue *vq,
				const struct v4l2_format *fmt,
				unsigned int *nbuffers, unsigned int *nplanes,
				unsigned int sizes[], void *alloc_ctxs[])
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);

	if (*nbuffers < BCAP_MIN_NUM_BUF)
		*nbuffers = BCAP_MIN_NUM_BUF;

	*nplanes = 1;
	sizes[0] = bcap_dev->fmt.sizeimage;
	alloc_ctxs[0] = bcap_dev->alloc_ctx;

	return 0;
}

static int bcap_buffer_init(struct vb2_buffer *vb)
{
	struct bcap_buffer *buf = to_bcap_vb(vb);

	INIT_LIST_HEAD(&buf->list);
	return 0;
}

static int bcap_buffer_prepare(struct vb2_buffer *vb)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
	struct bcap_buffer *buf = to_bcap_vb(vb);
	unsigned long size;

	size = bcap_dev->fmt.sizeimage;
	if (vb2_plane_size(vb, 0) < size) {
		v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
				vb2_plane_size(vb, 0), size);
		return -EINVAL;
	}
	vb2_set_plane_payload(&buf->vb, 0, size);

	return 0;
}

static void bcap_buffer_queue(struct vb2_buffer *vb)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
	struct bcap_buffer *buf = to_bcap_vb(vb);
	unsigned long flags;

	spin_lock_irqsave(&bcap_dev->lock, flags);
	list_add_tail(&buf->list, &bcap_dev->dma_queue);
	spin_unlock_irqrestore(&bcap_dev->lock, flags);
}

static void bcap_buffer_cleanup(struct vb2_buffer *vb)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
	struct bcap_buffer *buf = to_bcap_vb(vb);
	unsigned long flags;

	spin_lock_irqsave(&bcap_dev->lock, flags);
	list_del_init(&buf->list);
	spin_unlock_irqrestore(&bcap_dev->lock, flags);
}

static void bcap_lock(struct vb2_queue *vq)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
	mutex_lock(&bcap_dev->mutex);
}

static void bcap_unlock(struct vb2_queue *vq)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
	mutex_unlock(&bcap_dev->mutex);
}

static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
	struct ppi_if *ppi = bcap_dev->ppi;
	struct ppi_params params;
	int ret;

	/* enable streamon on the sub device */
	ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
	if (ret && (ret != -ENOIOCTLCMD)) {
		v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
		return ret;
	}

	/* set ppi params */
	params.width = bcap_dev->fmt.width;
	params.height = bcap_dev->fmt.height;
	params.bpp = bcap_dev->bpp;
	params.ppi_control = bcap_dev->cfg->ppi_control;
	params.int_mask = bcap_dev->cfg->int_mask;
	params.blank_clocks = bcap_dev->cfg->blank_clocks;
	ret = ppi->ops->set_params(ppi, &params);
	if (ret < 0) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Error in setting ppi params\n");
		return ret;
	}

	/* attach ppi DMA irq handler */
	ret = ppi->ops->attach_irq(ppi, bcap_isr);
	if (ret < 0) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Error in attaching interrupt handler\n");
		return ret;
	}

	INIT_COMPLETION(bcap_dev->comp);
	bcap_dev->stop = false;
	return 0;
}

static int bcap_stop_streaming(struct vb2_queue *vq)
{
	struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
	struct ppi_if *ppi = bcap_dev->ppi;
	int ret;

	if (!vb2_is_streaming(vq))
		return 0;

	bcap_dev->stop = true;
	wait_for_completion(&bcap_dev->comp);
	ppi->ops->stop(ppi);
	ppi->ops->detach_irq(ppi);
	ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
	if (ret && (ret != -ENOIOCTLCMD))
		v4l2_err(&bcap_dev->v4l2_dev,
				"stream off failed in subdev\n");

	/* release all active buffers */
	while (!list_empty(&bcap_dev->dma_queue)) {
		bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
						struct bcap_buffer, list);
		list_del(&bcap_dev->next_frm->list);
		vb2_buffer_done(&bcap_dev->next_frm->vb, VB2_BUF_STATE_ERROR);
	}
	return 0;
}

static struct vb2_ops bcap_video_qops = {
	.queue_setup            = bcap_queue_setup,
	.buf_init               = bcap_buffer_init,
	.buf_prepare            = bcap_buffer_prepare,
	.buf_cleanup            = bcap_buffer_cleanup,
	.buf_queue              = bcap_buffer_queue,
	.wait_prepare           = bcap_unlock,
	.wait_finish            = bcap_lock,
	.start_streaming        = bcap_start_streaming,
	.stop_streaming         = bcap_stop_streaming,
};

static int bcap_reqbufs(struct file *file, void *priv,
			struct v4l2_requestbuffers *req_buf)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct vb2_queue *vq = &bcap_dev->buffer_queue;
	struct v4l2_fh *fh = file->private_data;
	struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);

	if (vb2_is_busy(vq))
		return -EBUSY;

	bcap_fh->io_allowed = true;

	return vb2_reqbufs(vq, req_buf);
}

static int bcap_querybuf(struct file *file, void *priv,
				struct v4l2_buffer *buf)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return vb2_querybuf(&bcap_dev->buffer_queue, buf);
}

static int bcap_qbuf(struct file *file, void *priv,
			struct v4l2_buffer *buf)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct v4l2_fh *fh = file->private_data;
	struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);

	if (!bcap_fh->io_allowed)
		return -EBUSY;

	return vb2_qbuf(&bcap_dev->buffer_queue, buf);
}

static int bcap_dqbuf(struct file *file, void *priv,
			struct v4l2_buffer *buf)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct v4l2_fh *fh = file->private_data;
	struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);

	if (!bcap_fh->io_allowed)
		return -EBUSY;

	return vb2_dqbuf(&bcap_dev->buffer_queue,
				buf, file->f_flags & O_NONBLOCK);
}

static irqreturn_t bcap_isr(int irq, void *dev_id)
{
	struct ppi_if *ppi = dev_id;
	struct bcap_device *bcap_dev = ppi->priv;
	struct timeval timevalue;
	struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
	dma_addr_t addr;

	spin_lock(&bcap_dev->lock);

	if (bcap_dev->cur_frm != bcap_dev->next_frm) {
		do_gettimeofday(&timevalue);
		vb->v4l2_buf.timestamp = timevalue;
		vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
		bcap_dev->cur_frm = bcap_dev->next_frm;
	}

	ppi->ops->stop(ppi);

	if (bcap_dev->stop) {
		complete(&bcap_dev->comp);
	} else {
		if (!list_empty(&bcap_dev->dma_queue)) {
			bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
						struct bcap_buffer, list);
			list_del(&bcap_dev->next_frm->list);
			addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->next_frm->vb, 0);
			ppi->ops->update_addr(ppi, (unsigned long)addr);
		}
		ppi->ops->start(ppi);
	}

	spin_unlock(&bcap_dev->lock);

	return IRQ_HANDLED;
}

static int bcap_streamon(struct file *file, void *priv,
				enum v4l2_buf_type buf_type)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct bcap_fh *fh = file->private_data;
	struct ppi_if *ppi = bcap_dev->ppi;
	dma_addr_t addr;
	int ret;

	if (!fh->io_allowed)
		return -EBUSY;

	/* call streamon to start streaming in videobuf */
	ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
	if (ret)
		return ret;

	/* if dma queue is empty, return error */
	if (list_empty(&bcap_dev->dma_queue)) {
		v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
		ret = -EINVAL;
		goto err;
	}

	/* get the next frame from the dma queue */
	bcap_dev->next_frm = list_entry(bcap_dev->dma_queue.next,
					struct bcap_buffer, list);
	bcap_dev->cur_frm = bcap_dev->next_frm;
	/* remove buffer from the dma queue */
	list_del(&bcap_dev->cur_frm->list);
	addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
	/* update DMA address */
	ppi->ops->update_addr(ppi, (unsigned long)addr);
	/* enable ppi */
	ppi->ops->start(ppi);

	return 0;
err:
	vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
	return ret;
}

static int bcap_streamoff(struct file *file, void *priv,
				enum v4l2_buf_type buf_type)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct bcap_fh *fh = file->private_data;

	if (!fh->io_allowed)
		return -EBUSY;

	return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
}

static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
}

static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	*std = bcap_dev->std;
	return 0;
}

static int bcap_s_std(struct file *file, void *priv, v4l2_std_id *std)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	int ret;

	if (vb2_is_busy(&bcap_dev->buffer_queue))
		return -EBUSY;

	ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, *std);
	if (ret < 0)
		return ret;

	bcap_dev->std = *std;
	return 0;
}

static int bcap_enum_input(struct file *file, void *priv,
				struct v4l2_input *input)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct bfin_capture_config *config = bcap_dev->cfg;
	int ret;
	u32 status;

	if (input->index >= config->num_inputs)
		return -EINVAL;

	*input = config->inputs[input->index];
	/* get input status */
	ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
	if (!ret)
		input->status = status;
	return 0;
}

static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	*index = bcap_dev->cur_input;
	return 0;
}

static int bcap_s_input(struct file *file, void *priv, unsigned int index)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct bfin_capture_config *config = bcap_dev->cfg;
	struct bcap_route *route;
	int ret;

	if (vb2_is_busy(&bcap_dev->buffer_queue))
		return -EBUSY;

	if (index >= config->num_inputs)
		return -EINVAL;

	route = &config->routes[index];
	ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
				route->input, route->output, 0);
	if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
		v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
		return ret;
	}
	bcap_dev->cur_input = index;
	return 0;
}

static int bcap_try_format(struct bcap_device *bcap,
				struct v4l2_pix_format *pixfmt,
				enum v4l2_mbus_pixelcode *mbus_code,
				int *bpp)
{
	struct bcap_format *sf = bcap->sensor_formats;
	struct bcap_format *fmt = NULL;
	struct v4l2_mbus_framefmt mbus_fmt;
	int ret, i;

	for (i = 0; i < bcap->num_sensor_formats; i++) {
		fmt = &sf[i];
		if (pixfmt->pixelformat == fmt->pixelformat)
			break;
	}
	if (i == bcap->num_sensor_formats)
		fmt = &sf[0];

	if (mbus_code)
		*mbus_code = fmt->mbus_code;
	if (bpp)
		*bpp = fmt->bpp;
	v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
	ret = v4l2_subdev_call(bcap->sd, video,
				try_mbus_fmt, &mbus_fmt);
	if (ret < 0)
		return ret;
	v4l2_fill_pix_format(pixfmt, &mbus_fmt);
	pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
	pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
	return 0;
}

static int bcap_enum_fmt_vid_cap(struct file *file, void  *priv,
					struct v4l2_fmtdesc *fmt)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct bcap_format *sf = bcap_dev->sensor_formats;

	if (fmt->index >= bcap_dev->num_sensor_formats)
		return -EINVAL;

	fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	strlcpy(fmt->description,
		sf[fmt->index].desc,
		sizeof(fmt->description));
	fmt->pixelformat = sf[fmt->index].pixelformat;
	return 0;
}

static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
					struct v4l2_format *fmt)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;

	return bcap_try_format(bcap_dev, pixfmt, NULL, NULL);
}

static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *fmt)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	fmt->fmt.pix = bcap_dev->fmt;
	return 0;
}

static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *fmt)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	struct v4l2_mbus_framefmt mbus_fmt;
	enum v4l2_mbus_pixelcode mbus_code;
	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
	int ret, bpp;

	if (vb2_is_busy(&bcap_dev->buffer_queue))
		return -EBUSY;

	/* see if format works */
	ret = bcap_try_format(bcap_dev, pixfmt, &mbus_code, &bpp);
	if (ret < 0)
		return ret;

	v4l2_fill_mbus_format(&mbus_fmt, pixfmt, mbus_code);
	ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
	if (ret < 0)
		return ret;
	bcap_dev->fmt = *pixfmt;
	bcap_dev->bpp = bpp;
	return 0;
}

static int bcap_querycap(struct file *file, void  *priv,
				struct v4l2_capability *cap)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
	strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
	strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
	strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
	return 0;
}

static int bcap_g_parm(struct file *file, void *fh,
				struct v4l2_streamparm *a)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
	return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
}

static int bcap_s_parm(struct file *file, void *fh,
				struct v4l2_streamparm *a)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
	return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
}

static int bcap_g_chip_ident(struct file *file, void *priv,
		struct v4l2_dbg_chip_ident *chip)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	chip->ident = V4L2_IDENT_NONE;
	chip->revision = 0;
	if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
			chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
		return -EINVAL;

	return v4l2_subdev_call(bcap_dev->sd, core,
			g_chip_ident, chip);
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
static int bcap_dbg_g_register(struct file *file, void *priv,
		struct v4l2_dbg_register *reg)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return v4l2_subdev_call(bcap_dev->sd, core,
			g_register, reg);
}

static int bcap_dbg_s_register(struct file *file, void *priv,
		struct v4l2_dbg_register *reg)
{
	struct bcap_device *bcap_dev = video_drvdata(file);

	return v4l2_subdev_call(bcap_dev->sd, core,
			s_register, reg);
}
#endif

static int bcap_log_status(struct file *file, void *priv)
{
	struct bcap_device *bcap_dev = video_drvdata(file);
	/* status for sub devices */
	v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
	return 0;
}

static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
	.vidioc_querycap         = bcap_querycap,
	.vidioc_g_fmt_vid_cap    = bcap_g_fmt_vid_cap,
	.vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap    = bcap_s_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap  = bcap_try_fmt_vid_cap,
	.vidioc_enum_input       = bcap_enum_input,
	.vidioc_g_input          = bcap_g_input,
	.vidioc_s_input          = bcap_s_input,
	.vidioc_querystd         = bcap_querystd,
	.vidioc_s_std            = bcap_s_std,
	.vidioc_g_std            = bcap_g_std,
	.vidioc_reqbufs          = bcap_reqbufs,
	.vidioc_querybuf         = bcap_querybuf,
	.vidioc_qbuf             = bcap_qbuf,
	.vidioc_dqbuf            = bcap_dqbuf,
	.vidioc_streamon         = bcap_streamon,
	.vidioc_streamoff        = bcap_streamoff,
	.vidioc_g_parm           = bcap_g_parm,
	.vidioc_s_parm           = bcap_s_parm,
	.vidioc_g_chip_ident     = bcap_g_chip_ident,
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register       = bcap_dbg_g_register,
	.vidioc_s_register       = bcap_dbg_s_register,
#endif
	.vidioc_log_status       = bcap_log_status,
};

static struct v4l2_file_operations bcap_fops = {
	.owner = THIS_MODULE,
	.open = bcap_open,
	.release = bcap_release,
	.unlocked_ioctl = video_ioctl2,
	.mmap = bcap_mmap,
#ifndef CONFIG_MMU
	.get_unmapped_area = bcap_get_unmapped_area,
#endif
	.poll = bcap_poll
};

static int __devinit bcap_probe(struct platform_device *pdev)
{
	struct bcap_device *bcap_dev;
	struct video_device *vfd;
	struct i2c_adapter *i2c_adap;
	struct bfin_capture_config *config;
	struct vb2_queue *q;
	int ret;

	config = pdev->dev.platform_data;
	if (!config) {
		v4l2_err(pdev->dev.driver, "Unable to get board config\n");
		return -ENODEV;
	}

	bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
	if (!bcap_dev) {
		v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
		return -ENOMEM;
	}

	bcap_dev->cfg = config;

	bcap_dev->ppi = ppi_create_instance(config->ppi_info);
	if (!bcap_dev->ppi) {
		v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
		ret = -ENODEV;
		goto err_free_dev;
	}
	bcap_dev->ppi->priv = bcap_dev;

	bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
	if (IS_ERR(bcap_dev->alloc_ctx)) {
		ret = PTR_ERR(bcap_dev->alloc_ctx);
		goto err_free_ppi;
	}

	vfd = video_device_alloc();
	if (!vfd) {
		ret = -ENOMEM;
		v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
		goto err_cleanup_ctx;
	}

	/* initialize field of video device */
	vfd->release            = video_device_release;
	vfd->fops               = &bcap_fops;
	vfd->ioctl_ops          = &bcap_ioctl_ops;
	vfd->tvnorms            = 0;
	vfd->v4l2_dev           = &bcap_dev->v4l2_dev;
	set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
	strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
	bcap_dev->video_dev     = vfd;

	ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
	if (ret) {
		v4l2_err(pdev->dev.driver,
				"Unable to register v4l2 device\n");
		goto err_release_vdev;
	}
	v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");

	bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
	ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
	if (ret) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Unable to init control handler\n");
		goto err_unreg_v4l2;
	}

	spin_lock_init(&bcap_dev->lock);
	/* initialize queue */
	q = &bcap_dev->buffer_queue;
	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	q->io_modes = VB2_MMAP;
	q->drv_priv = bcap_dev;
	q->buf_struct_size = sizeof(struct bcap_buffer);
	q->ops = &bcap_video_qops;
	q->mem_ops = &vb2_dma_contig_memops;

	vb2_queue_init(q);

	mutex_init(&bcap_dev->mutex);
	init_completion(&bcap_dev->comp);

	/* init video dma queues */
	INIT_LIST_HEAD(&bcap_dev->dma_queue);

	vfd->lock = &bcap_dev->mutex;

	/* register video device */
	ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
	if (ret) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Unable to register video device\n");
		goto err_free_handler;
	}
	video_set_drvdata(bcap_dev->video_dev, bcap_dev);
	v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
			video_device_node_name(vfd));

	/* load up the subdevice */
	i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
	if (!i2c_adap) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Unable to find i2c adapter\n");
		goto err_unreg_vdev;

	}
	bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
						 i2c_adap,
						 &config->board_info,
						 NULL);
	if (bcap_dev->sd) {
		int i;
		/* update tvnorms from the sub devices */
		for (i = 0; i < config->num_inputs; i++)
			vfd->tvnorms |= config->inputs[i].std;
	} else {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Unable to register sub device\n");
		goto err_unreg_vdev;
	}

	v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");

	/* now we can probe the default state */
	if (vfd->tvnorms) {
		v4l2_std_id std;
		ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
		if (ret) {
			v4l2_err(&bcap_dev->v4l2_dev,
					"Unable to get std\n");
			goto err_unreg_vdev;
		}
		bcap_dev->std = std;
	}
	ret = bcap_init_sensor_formats(bcap_dev);
	if (ret) {
		v4l2_err(&bcap_dev->v4l2_dev,
				"Unable to create sensor formats table\n");
		goto err_unreg_vdev;
	}
	return 0;
err_unreg_vdev:
	video_unregister_device(bcap_dev->video_dev);
	bcap_dev->video_dev = NULL;
err_free_handler:
	v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
err_unreg_v4l2:
	v4l2_device_unregister(&bcap_dev->v4l2_dev);
err_release_vdev:
	if (bcap_dev->video_dev)
		video_device_release(bcap_dev->video_dev);
err_cleanup_ctx:
	vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
err_free_ppi:
	ppi_delete_instance(bcap_dev->ppi);
err_free_dev:
	kfree(bcap_dev);
	return ret;
}

static int __devexit bcap_remove(struct platform_device *pdev)
{
	struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
	struct bcap_device *bcap_dev = container_of(v4l2_dev,
						struct bcap_device, v4l2_dev);

	bcap_free_sensor_formats(bcap_dev);
	video_unregister_device(bcap_dev->video_dev);
	v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
	v4l2_device_unregister(v4l2_dev);
	vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
	ppi_delete_instance(bcap_dev->ppi);
	kfree(bcap_dev);
	return 0;
}

static struct platform_driver bcap_driver = {
	.driver = {
		.name  = CAPTURE_DRV_NAME,
		.owner = THIS_MODULE,
	},
	.probe = bcap_probe,
	.remove = __devexit_p(bcap_remove),
};

static __init int bcap_init(void)
{
	return platform_driver_register(&bcap_driver);
}

static __exit void bcap_exit(void)
{
	platform_driver_unregister(&bcap_driver);
}

module_init(bcap_init);
module_exit(bcap_exit);

MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
MODULE_LICENSE("GPL v2");