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#include <linux/module.h>
66#include <linux/slab.h>
67#include <linux/init.h>
68#include <linux/platform_device.h>
69#include <linux/interrupt.h>
70#include <media/v4l2-common.h>
71#include <linux/io.h>
72#include <media/davinci/vpfe_capture.h>
73#include "ccdc_hw_device.h"
74
75static int debug;
76static u32 numbuffers = 3;
77static u32 bufsize = (720 * 576 * 2);
78
79module_param(numbuffers, uint, S_IRUGO);
80module_param(bufsize, uint, S_IRUGO);
81module_param(debug, int, 0644);
82
83MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
84MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
85MODULE_PARM_DESC(debug, "Debug level 0-1");
86
87MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
88MODULE_LICENSE("GPL");
89MODULE_AUTHOR("Texas Instruments");
90
91
92struct vpfe_standard {
93 v4l2_std_id std_id;
94 unsigned int width;
95 unsigned int height;
96 struct v4l2_fract pixelaspect;
97
98 int frame_format;
99};
100
101
102struct ccdc_config {
103
104 int vpfe_probed;
105
106 char name[32];
107};
108
109
110static struct vpfe_config_params config_params = {
111 .min_numbuffers = 3,
112 .numbuffers = 3,
113 .min_bufsize = 720 * 480 * 2,
114 .device_bufsize = 720 * 576 * 2,
115};
116
117
118static const struct ccdc_hw_device *ccdc_dev;
119
120static DEFINE_MUTEX(ccdc_lock);
121
122static struct ccdc_config *ccdc_cfg;
123
124static const struct vpfe_standard vpfe_standards[] = {
125 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
126 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
127};
128
129
130static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
131 {
132 .fmtdesc = {
133 .index = 0,
134 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
135 .description = "Bayer GrRBGb 8bit A-Law compr.",
136 .pixelformat = V4L2_PIX_FMT_SBGGR8,
137 },
138 .bpp = 1,
139 },
140 {
141 .fmtdesc = {
142 .index = 1,
143 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
144 .description = "Bayer GrRBGb - 16bit",
145 .pixelformat = V4L2_PIX_FMT_SBGGR16,
146 },
147 .bpp = 2,
148 },
149 {
150 .fmtdesc = {
151 .index = 2,
152 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
153 .description = "Bayer GrRBGb 8bit DPCM compr.",
154 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
155 },
156 .bpp = 1,
157 },
158 {
159 .fmtdesc = {
160 .index = 3,
161 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
162 .description = "YCbCr 4:2:2 Interleaved UYVY",
163 .pixelformat = V4L2_PIX_FMT_UYVY,
164 },
165 .bpp = 2,
166 },
167 {
168 .fmtdesc = {
169 .index = 4,
170 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
171 .description = "YCbCr 4:2:2 Interleaved YUYV",
172 .pixelformat = V4L2_PIX_FMT_YUYV,
173 },
174 .bpp = 2,
175 },
176 {
177 .fmtdesc = {
178 .index = 5,
179 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
180 .description = "Y/CbCr 4:2:0 - Semi planar",
181 .pixelformat = V4L2_PIX_FMT_NV12,
182 },
183 .bpp = 1,
184 },
185};
186
187
188
189
190
191static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
192{
193 int i;
194
195 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
196 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
197 return &vpfe_pix_fmts[i];
198 }
199 return NULL;
200}
201
202
203
204
205
206int vpfe_register_ccdc_device(const struct ccdc_hw_device *dev)
207{
208 int ret = 0;
209 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
210
211 BUG_ON(!dev->hw_ops.open);
212 BUG_ON(!dev->hw_ops.enable);
213 BUG_ON(!dev->hw_ops.set_hw_if_params);
214 BUG_ON(!dev->hw_ops.configure);
215 BUG_ON(!dev->hw_ops.set_buftype);
216 BUG_ON(!dev->hw_ops.get_buftype);
217 BUG_ON(!dev->hw_ops.enum_pix);
218 BUG_ON(!dev->hw_ops.set_frame_format);
219 BUG_ON(!dev->hw_ops.get_frame_format);
220 BUG_ON(!dev->hw_ops.get_pixel_format);
221 BUG_ON(!dev->hw_ops.set_pixel_format);
222 BUG_ON(!dev->hw_ops.set_image_window);
223 BUG_ON(!dev->hw_ops.get_image_window);
224 BUG_ON(!dev->hw_ops.get_line_length);
225 BUG_ON(!dev->hw_ops.getfid);
226
227 mutex_lock(&ccdc_lock);
228 if (!ccdc_cfg) {
229
230
231
232
233
234 printk(KERN_ERR "vpfe capture not initialized\n");
235 ret = -EFAULT;
236 goto unlock;
237 }
238
239 if (strcmp(dev->name, ccdc_cfg->name)) {
240
241 ret = -EINVAL;
242 goto unlock;
243 }
244
245 if (ccdc_dev) {
246 printk(KERN_ERR "ccdc already registered\n");
247 ret = -EINVAL;
248 goto unlock;
249 }
250
251 ccdc_dev = dev;
252unlock:
253 mutex_unlock(&ccdc_lock);
254 return ret;
255}
256EXPORT_SYMBOL(vpfe_register_ccdc_device);
257
258
259
260
261
262void vpfe_unregister_ccdc_device(const struct ccdc_hw_device *dev)
263{
264 if (!dev) {
265 printk(KERN_ERR "invalid ccdc device ptr\n");
266 return;
267 }
268
269 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
270 dev->name);
271
272 if (strcmp(dev->name, ccdc_cfg->name)) {
273
274 return;
275 }
276
277 mutex_lock(&ccdc_lock);
278 ccdc_dev = NULL;
279 mutex_unlock(&ccdc_lock);
280}
281EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
282
283
284
285
286
287static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
288{
289 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
290 int ret = 0;
291
292 if (ccdc_dev->hw_ops.set_pixel_format(
293 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
294 v4l2_err(&vpfe_dev->v4l2_dev,
295 "couldn't set pix format in ccdc\n");
296 return -EINVAL;
297 }
298
299 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
300
301 switch (vpfe_dev->fmt.fmt.pix.field) {
302 case V4L2_FIELD_INTERLACED:
303
304 ret = ccdc_dev->hw_ops.set_buftype(
305 CCDC_BUFTYPE_FLD_INTERLEAVED);
306 break;
307 case V4L2_FIELD_NONE:
308 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
309
310 break;
311 case V4L2_FIELD_SEQ_TB:
312 ret = ccdc_dev->hw_ops.set_buftype(
313 CCDC_BUFTYPE_FLD_SEPARATED);
314 break;
315 default:
316 return -EINVAL;
317 }
318
319
320 if (!ret)
321 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
322 return ret;
323}
324
325
326
327
328
329
330
331
332
333
334static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
335 v4l2_std_id std_id)
336{
337 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
338 struct v4l2_subdev_format fmt = {
339 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
340 };
341 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
342 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
343 int i, ret;
344
345 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
346 if (vpfe_standards[i].std_id & std_id) {
347 vpfe_dev->std_info.active_pixels =
348 vpfe_standards[i].width;
349 vpfe_dev->std_info.active_lines =
350 vpfe_standards[i].height;
351 vpfe_dev->std_info.frame_format =
352 vpfe_standards[i].frame_format;
353 vpfe_dev->std_index = i;
354 break;
355 }
356 }
357
358 if (i == ARRAY_SIZE(vpfe_standards)) {
359 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
360 return -EINVAL;
361 }
362
363 vpfe_dev->crop.top = 0;
364 vpfe_dev->crop.left = 0;
365 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
366 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
367 pix->width = vpfe_dev->crop.width;
368 pix->height = vpfe_dev->crop.height;
369
370
371 if (vpfe_dev->std_info.frame_format) {
372 pix->field = V4L2_FIELD_INTERLACED;
373
374 pix->pixelformat = V4L2_PIX_FMT_UYVY;
375 v4l2_fill_mbus_format(mbus_fmt, pix,
376 MEDIA_BUS_FMT_YUYV10_2X10);
377 } else {
378 pix->field = V4L2_FIELD_NONE;
379
380 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
381 v4l2_fill_mbus_format(mbus_fmt, pix,
382 MEDIA_BUS_FMT_SBGGR8_1X8);
383 }
384
385
386 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
387 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
388
389 if (ret && ret != -ENOIOCTLCMD) {
390 v4l2_err(&vpfe_dev->v4l2_dev,
391 "error in getting get_fmt from sub device\n");
392 return ret;
393 }
394 v4l2_fill_pix_format(pix, mbus_fmt);
395 pix->bytesperline = pix->width * 2;
396 pix->sizeimage = pix->bytesperline * pix->height;
397
398
399 ret = vpfe_config_ccdc_image_format(vpfe_dev);
400 if (ret)
401 return ret;
402
403
404 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
405 pix->sizeimage = pix->bytesperline * pix->height;
406
407 return 0;
408}
409
410static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
411{
412 int ret;
413
414
415 vpfe_dev->current_input = 0;
416
417
418 vpfe_dev->std_index = 0;
419
420
421 ret = vpfe_config_image_format(vpfe_dev,
422 vpfe_standards[vpfe_dev->std_index].std_id);
423 if (ret)
424 return ret;
425
426
427 mutex_lock(&ccdc_lock);
428 if (!ccdc_dev) {
429 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
430 ret = -ENODEV;
431 goto unlock;
432 }
433
434 if (!try_module_get(ccdc_dev->owner)) {
435 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
436 ret = -ENODEV;
437 goto unlock;
438 }
439 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
440 if (!ret)
441 vpfe_dev->initialized = 1;
442
443
444 if (vpfe_dev->cfg->clr_intr)
445 vpfe_dev->cfg->clr_intr(-1);
446
447unlock:
448 mutex_unlock(&ccdc_lock);
449 return ret;
450}
451
452
453
454
455
456static int vpfe_open(struct file *file)
457{
458 struct vpfe_device *vpfe_dev = video_drvdata(file);
459 struct video_device *vdev = video_devdata(file);
460 struct vpfe_fh *fh;
461
462 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
463
464 if (!vpfe_dev->cfg->num_subdevs) {
465 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
466 return -ENODEV;
467 }
468
469
470 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
471 if (!fh)
472 return -ENOMEM;
473
474
475 file->private_data = fh;
476 fh->vpfe_dev = vpfe_dev;
477 v4l2_fh_init(&fh->fh, vdev);
478 mutex_lock(&vpfe_dev->lock);
479
480 if (!vpfe_dev->initialized) {
481 if (vpfe_initialize_device(vpfe_dev)) {
482 mutex_unlock(&vpfe_dev->lock);
483 v4l2_fh_exit(&fh->fh);
484 kfree(fh);
485 return -ENODEV;
486 }
487 }
488
489 vpfe_dev->usrs++;
490
491 fh->io_allowed = 0;
492 v4l2_fh_add(&fh->fh);
493 mutex_unlock(&vpfe_dev->lock);
494 return 0;
495}
496
497static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
498{
499 unsigned long addr;
500
501 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
502 struct videobuf_buffer, queue);
503 list_del(&vpfe_dev->next_frm->queue);
504 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
505 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
506
507 ccdc_dev->hw_ops.setfbaddr(addr);
508}
509
510static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
511{
512 unsigned long addr;
513
514 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
515 addr += vpfe_dev->field_off;
516 ccdc_dev->hw_ops.setfbaddr(addr);
517}
518
519static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
520{
521 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
522 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
523 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
524 wake_up_interruptible(&vpfe_dev->cur_frm->done);
525 vpfe_dev->cur_frm = vpfe_dev->next_frm;
526}
527
528
529static irqreturn_t vpfe_isr(int irq, void *dev_id)
530{
531 struct vpfe_device *vpfe_dev = dev_id;
532 enum v4l2_field field;
533 int fid;
534
535 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
536 field = vpfe_dev->fmt.fmt.pix.field;
537
538
539 if (!vpfe_dev->started)
540 goto clear_intr;
541
542
543 if (ccdc_dev->hw_ops.reset)
544 ccdc_dev->hw_ops.reset();
545
546 if (field == V4L2_FIELD_NONE) {
547
548 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
549 "frame format is progressive...\n");
550 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
551 vpfe_process_buffer_complete(vpfe_dev);
552 goto clear_intr;
553 }
554
555
556 fid = ccdc_dev->hw_ops.getfid();
557
558
559 vpfe_dev->field_id ^= 1;
560 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
561 fid, vpfe_dev->field_id);
562 if (fid == vpfe_dev->field_id) {
563
564 if (fid == 0) {
565
566
567
568
569 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
570 vpfe_process_buffer_complete(vpfe_dev);
571
572
573
574
575
576 if (field == V4L2_FIELD_SEQ_TB)
577 vpfe_schedule_bottom_field(vpfe_dev);
578 goto clear_intr;
579 }
580
581
582
583
584
585
586 spin_lock(&vpfe_dev->dma_queue_lock);
587 if (!list_empty(&vpfe_dev->dma_queue) &&
588 vpfe_dev->cur_frm == vpfe_dev->next_frm)
589 vpfe_schedule_next_buffer(vpfe_dev);
590 spin_unlock(&vpfe_dev->dma_queue_lock);
591 } else if (fid == 0) {
592
593
594
595
596 vpfe_dev->field_id = fid;
597 }
598clear_intr:
599 if (vpfe_dev->cfg->clr_intr)
600 vpfe_dev->cfg->clr_intr(irq);
601
602 return IRQ_HANDLED;
603}
604
605
606static irqreturn_t vdint1_isr(int irq, void *dev_id)
607{
608 struct vpfe_device *vpfe_dev = dev_id;
609
610 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
611
612
613 if (!vpfe_dev->started) {
614 if (vpfe_dev->cfg->clr_intr)
615 vpfe_dev->cfg->clr_intr(irq);
616 return IRQ_HANDLED;
617 }
618
619 spin_lock(&vpfe_dev->dma_queue_lock);
620 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
621 !list_empty(&vpfe_dev->dma_queue) &&
622 vpfe_dev->cur_frm == vpfe_dev->next_frm)
623 vpfe_schedule_next_buffer(vpfe_dev);
624 spin_unlock(&vpfe_dev->dma_queue_lock);
625
626 if (vpfe_dev->cfg->clr_intr)
627 vpfe_dev->cfg->clr_intr(irq);
628
629 return IRQ_HANDLED;
630}
631
632static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
633{
634 enum ccdc_frmfmt frame_format;
635
636 frame_format = ccdc_dev->hw_ops.get_frame_format();
637 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
638 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
639}
640
641static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
642{
643 enum ccdc_frmfmt frame_format;
644
645 frame_format = ccdc_dev->hw_ops.get_frame_format();
646 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
647 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
648 0, "vpfe_capture1",
649 vpfe_dev);
650 }
651 return 0;
652}
653
654
655static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
656{
657 vpfe_dev->started = 0;
658 ccdc_dev->hw_ops.enable(0);
659 if (ccdc_dev->hw_ops.enable_out_to_sdram)
660 ccdc_dev->hw_ops.enable_out_to_sdram(0);
661}
662
663
664
665
666
667static int vpfe_release(struct file *file)
668{
669 struct vpfe_device *vpfe_dev = video_drvdata(file);
670 struct vpfe_fh *fh = file->private_data;
671 struct vpfe_subdev_info *sdinfo;
672 int ret;
673
674 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
675
676
677 mutex_lock(&vpfe_dev->lock);
678
679 if (fh->io_allowed) {
680 if (vpfe_dev->started) {
681 sdinfo = vpfe_dev->current_subdev;
682 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
683 sdinfo->grp_id,
684 video, s_stream, 0);
685 if (ret && (ret != -ENOIOCTLCMD))
686 v4l2_err(&vpfe_dev->v4l2_dev,
687 "stream off failed in subdev\n");
688 vpfe_stop_ccdc_capture(vpfe_dev);
689 vpfe_detach_irq(vpfe_dev);
690 videobuf_streamoff(&vpfe_dev->buffer_queue);
691 }
692 vpfe_dev->io_usrs = 0;
693 vpfe_dev->numbuffers = config_params.numbuffers;
694 videobuf_stop(&vpfe_dev->buffer_queue);
695 videobuf_mmap_free(&vpfe_dev->buffer_queue);
696 }
697
698
699 vpfe_dev->usrs--;
700 v4l2_fh_del(&fh->fh);
701 v4l2_fh_exit(&fh->fh);
702
703 if (!vpfe_dev->usrs) {
704 vpfe_dev->initialized = 0;
705 if (ccdc_dev->hw_ops.close)
706 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
707 module_put(ccdc_dev->owner);
708 }
709 mutex_unlock(&vpfe_dev->lock);
710 file->private_data = NULL;
711
712 kfree(fh);
713 return 0;
714}
715
716
717
718
719
720static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
721{
722
723 struct vpfe_device *vpfe_dev = video_drvdata(file);
724
725 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
726
727 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
728}
729
730
731
732
733static __poll_t vpfe_poll(struct file *file, poll_table *wait)
734{
735 struct vpfe_device *vpfe_dev = video_drvdata(file);
736
737 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
738
739 if (vpfe_dev->started)
740 return videobuf_poll_stream(file,
741 &vpfe_dev->buffer_queue, wait);
742 return 0;
743}
744
745
746static const struct v4l2_file_operations vpfe_fops = {
747 .owner = THIS_MODULE,
748 .open = vpfe_open,
749 .release = vpfe_release,
750 .unlocked_ioctl = video_ioctl2,
751 .mmap = vpfe_mmap,
752 .poll = vpfe_poll
753};
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772static const struct vpfe_pixel_format *
773 vpfe_check_format(struct vpfe_device *vpfe_dev,
774 struct v4l2_pix_format *pixfmt)
775{
776 u32 min_height = 1, min_width = 32, max_width, max_height;
777 const struct vpfe_pixel_format *vpfe_pix_fmt;
778 u32 pix;
779 int temp, found;
780
781 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
782 if (!vpfe_pix_fmt) {
783
784
785
786
787 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
788 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
789 }
790
791
792 temp = 0;
793 found = 0;
794 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
795 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
796 found = 1;
797 break;
798 }
799 temp++;
800 }
801
802 if (!found) {
803
804 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
805
806
807
808
809 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
810 }
811
812
813 if (pixfmt->field == V4L2_FIELD_ANY) {
814
815 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
816 }
817
818
819
820
821
822 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
823
824
825
826
827 switch (pixfmt->field) {
828 case V4L2_FIELD_INTERLACED:
829 case V4L2_FIELD_SEQ_TB:
830
831 if (!vpfe_dev->std_info.frame_format)
832 pixfmt->field = V4L2_FIELD_NONE;
833 break;
834 case V4L2_FIELD_NONE:
835 if (vpfe_dev->std_info.frame_format)
836 pixfmt->field = V4L2_FIELD_INTERLACED;
837 break;
838
839 default:
840
841 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
842 break;
843 }
844 }
845
846
847 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
848 pixfmt->field == V4L2_FIELD_SEQ_TB)
849 min_height = 2;
850
851 max_width = vpfe_dev->std_info.active_pixels;
852 max_height = vpfe_dev->std_info.active_lines;
853 min_width /= vpfe_pix_fmt->bpp;
854
855 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
856 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
857
858 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
859 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
860
861
862 if (pixfmt->field == V4L2_FIELD_INTERLACED)
863 pixfmt->height &= (~1);
864
865
866
867
868 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
869 & ~31);
870 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
871 pixfmt->sizeimage =
872 pixfmt->bytesperline * pixfmt->height +
873 ((pixfmt->bytesperline * pixfmt->height) >> 1);
874 else
875 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
876
877 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
878 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
879 pixfmt->bytesperline, pixfmt->sizeimage);
880 return vpfe_pix_fmt;
881}
882
883static int vpfe_querycap(struct file *file, void *priv,
884 struct v4l2_capability *cap)
885{
886 struct vpfe_device *vpfe_dev = video_drvdata(file);
887
888 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
889
890 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
891 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
892 strscpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
893 strscpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
894 strscpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
895 return 0;
896}
897
898static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
899 struct v4l2_format *fmt)
900{
901 struct vpfe_device *vpfe_dev = video_drvdata(file);
902
903 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
904
905 *fmt = vpfe_dev->fmt;
906 return 0;
907}
908
909static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
910 struct v4l2_fmtdesc *fmt)
911{
912 struct vpfe_device *vpfe_dev = video_drvdata(file);
913 const struct vpfe_pixel_format *pix_fmt;
914 int temp_index;
915 u32 pix;
916
917 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
918
919 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
920 return -EINVAL;
921
922
923 pix_fmt = vpfe_lookup_pix_format(pix);
924 if (pix_fmt) {
925 temp_index = fmt->index;
926 *fmt = pix_fmt->fmtdesc;
927 fmt->index = temp_index;
928 return 0;
929 }
930 return -EINVAL;
931}
932
933static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
934 struct v4l2_format *fmt)
935{
936 struct vpfe_device *vpfe_dev = video_drvdata(file);
937 const struct vpfe_pixel_format *pix_fmts;
938 int ret;
939
940 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
941
942
943 if (vpfe_dev->started) {
944 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
945 return -EBUSY;
946 }
947
948
949 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
950 if (!pix_fmts)
951 return -EINVAL;
952
953
954 ret = mutex_lock_interruptible(&vpfe_dev->lock);
955 if (ret)
956 return ret;
957
958
959 vpfe_detach_irq(vpfe_dev);
960 vpfe_dev->fmt = *fmt;
961
962 ret = vpfe_config_ccdc_image_format(vpfe_dev);
963 mutex_unlock(&vpfe_dev->lock);
964 return ret;
965}
966
967static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
968 struct v4l2_format *f)
969{
970 struct vpfe_device *vpfe_dev = video_drvdata(file);
971 const struct vpfe_pixel_format *pix_fmts;
972
973 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
974
975 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
976 if (!pix_fmts)
977 return -EINVAL;
978 return 0;
979}
980
981
982
983
984
985static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
986 int *subdev_index,
987 int *subdev_input_index,
988 int app_input_index)
989{
990 struct vpfe_config *cfg = vpfe_dev->cfg;
991 struct vpfe_subdev_info *sdinfo;
992 int i, j = 0;
993
994 for (i = 0; i < cfg->num_subdevs; i++) {
995 sdinfo = &cfg->sub_devs[i];
996 if (app_input_index < (j + sdinfo->num_inputs)) {
997 *subdev_index = i;
998 *subdev_input_index = app_input_index - j;
999 return 0;
1000 }
1001 j += sdinfo->num_inputs;
1002 }
1003 return -EINVAL;
1004}
1005
1006
1007
1008
1009
1010
1011static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1012 int *app_input_index)
1013{
1014 struct vpfe_config *cfg = vpfe_dev->cfg;
1015 struct vpfe_subdev_info *sdinfo;
1016 int i, j = 0;
1017
1018 for (i = 0; i < cfg->num_subdevs; i++) {
1019 sdinfo = &cfg->sub_devs[i];
1020 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1021 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1022 return -1;
1023 *app_input_index = j + vpfe_dev->current_input;
1024 return 0;
1025 }
1026 j += sdinfo->num_inputs;
1027 }
1028 return -EINVAL;
1029}
1030
1031static int vpfe_enum_input(struct file *file, void *priv,
1032 struct v4l2_input *inp)
1033{
1034 struct vpfe_device *vpfe_dev = video_drvdata(file);
1035 struct vpfe_subdev_info *sdinfo;
1036 int subdev, index ;
1037
1038 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1039
1040 if (vpfe_get_subdev_input_index(vpfe_dev,
1041 &subdev,
1042 &index,
1043 inp->index) < 0) {
1044 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
1045 return -EINVAL;
1046 }
1047 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1048 *inp = sdinfo->inputs[index];
1049 return 0;
1050}
1051
1052static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1053{
1054 struct vpfe_device *vpfe_dev = video_drvdata(file);
1055
1056 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1057
1058 return vpfe_get_app_input_index(vpfe_dev, index);
1059}
1060
1061
1062static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1063{
1064 struct vpfe_device *vpfe_dev = video_drvdata(file);
1065 struct v4l2_subdev *sd;
1066 struct vpfe_subdev_info *sdinfo;
1067 int subdev_index, inp_index;
1068 struct vpfe_route *route;
1069 u32 input, output;
1070 int ret;
1071
1072 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1073
1074 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1075 if (ret)
1076 return ret;
1077
1078
1079
1080
1081
1082 if (vpfe_dev->started) {
1083 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1084 ret = -EBUSY;
1085 goto unlock_out;
1086 }
1087 ret = vpfe_get_subdev_input_index(vpfe_dev,
1088 &subdev_index,
1089 &inp_index,
1090 index);
1091 if (ret < 0) {
1092 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1093 goto unlock_out;
1094 }
1095
1096 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1097 sd = vpfe_dev->sd[subdev_index];
1098 route = &sdinfo->routes[inp_index];
1099 if (route && sdinfo->can_route) {
1100 input = route->input;
1101 output = route->output;
1102 } else {
1103 input = 0;
1104 output = 0;
1105 }
1106
1107 if (sd)
1108 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1109
1110 if (ret) {
1111 v4l2_err(&vpfe_dev->v4l2_dev,
1112 "vpfe_doioctl:error in setting input in decoder\n");
1113 ret = -EINVAL;
1114 goto unlock_out;
1115 }
1116 vpfe_dev->current_subdev = sdinfo;
1117 if (sd)
1118 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1119 vpfe_dev->current_input = index;
1120 vpfe_dev->std_index = 0;
1121
1122
1123 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1124 if (ret)
1125 goto unlock_out;
1126
1127
1128 ret = vpfe_config_image_format(vpfe_dev,
1129 vpfe_standards[vpfe_dev->std_index].std_id);
1130unlock_out:
1131 mutex_unlock(&vpfe_dev->lock);
1132 return ret;
1133}
1134
1135static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1136{
1137 struct vpfe_device *vpfe_dev = video_drvdata(file);
1138 struct vpfe_subdev_info *sdinfo;
1139 int ret;
1140
1141 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1142
1143 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1144 sdinfo = vpfe_dev->current_subdev;
1145 if (ret)
1146 return ret;
1147
1148 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1149 video, querystd, std_id);
1150 mutex_unlock(&vpfe_dev->lock);
1151 return ret;
1152}
1153
1154static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1155{
1156 struct vpfe_device *vpfe_dev = video_drvdata(file);
1157 struct vpfe_subdev_info *sdinfo;
1158 int ret;
1159
1160 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1161
1162
1163 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1164 if (ret)
1165 return ret;
1166
1167 sdinfo = vpfe_dev->current_subdev;
1168
1169 if (vpfe_dev->started) {
1170 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1171 ret = -EBUSY;
1172 goto unlock_out;
1173 }
1174
1175 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1176 video, s_std, std_id);
1177 if (ret < 0) {
1178 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1179 goto unlock_out;
1180 }
1181 ret = vpfe_config_image_format(vpfe_dev, std_id);
1182
1183unlock_out:
1184 mutex_unlock(&vpfe_dev->lock);
1185 return ret;
1186}
1187
1188static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1189{
1190 struct vpfe_device *vpfe_dev = video_drvdata(file);
1191
1192 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1193
1194 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1195 return 0;
1196}
1197
1198
1199
1200static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1201 unsigned int *count,
1202 unsigned int *size)
1203{
1204 struct vpfe_fh *fh = vq->priv_data;
1205 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1206
1207 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1208 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1209 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1210 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1211 *size = config_params.device_bufsize;
1212
1213 if (*count < config_params.min_numbuffers)
1214 *count = config_params.min_numbuffers;
1215 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1216 "count=%d, size=%d\n", *count, *size);
1217 return 0;
1218}
1219
1220static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1221 struct videobuf_buffer *vb,
1222 enum v4l2_field field)
1223{
1224 struct vpfe_fh *fh = vq->priv_data;
1225 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1226 unsigned long addr;
1227 int ret;
1228
1229 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1230
1231
1232 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1233 vb->width = vpfe_dev->fmt.fmt.pix.width;
1234 vb->height = vpfe_dev->fmt.fmt.pix.height;
1235 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1236 vb->field = field;
1237
1238 ret = videobuf_iolock(vq, vb, NULL);
1239 if (ret < 0)
1240 return ret;
1241
1242 addr = videobuf_to_dma_contig(vb);
1243
1244 if (!ALIGN(addr, 32))
1245 return -EINVAL;
1246
1247 vb->state = VIDEOBUF_PREPARED;
1248 }
1249 return 0;
1250}
1251
1252static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1253 struct videobuf_buffer *vb)
1254{
1255
1256 struct vpfe_fh *fh = vq->priv_data;
1257 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1258 unsigned long flags;
1259
1260 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1261
1262
1263 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1264 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1265 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1266
1267
1268 vb->state = VIDEOBUF_QUEUED;
1269}
1270
1271static void vpfe_videobuf_release(struct videobuf_queue *vq,
1272 struct videobuf_buffer *vb)
1273{
1274 struct vpfe_fh *fh = vq->priv_data;
1275 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1276 unsigned long flags;
1277
1278 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1279
1280
1281
1282
1283
1284 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1285 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1286 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1287 videobuf_dma_contig_free(vq, vb);
1288 vb->state = VIDEOBUF_NEEDS_INIT;
1289}
1290
1291static const struct videobuf_queue_ops vpfe_videobuf_qops = {
1292 .buf_setup = vpfe_videobuf_setup,
1293 .buf_prepare = vpfe_videobuf_prepare,
1294 .buf_queue = vpfe_videobuf_queue,
1295 .buf_release = vpfe_videobuf_release,
1296};
1297
1298
1299
1300
1301
1302static int vpfe_reqbufs(struct file *file, void *priv,
1303 struct v4l2_requestbuffers *req_buf)
1304{
1305 struct vpfe_device *vpfe_dev = video_drvdata(file);
1306 struct vpfe_fh *fh = file->private_data;
1307 int ret;
1308
1309 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1310
1311 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1312 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1313 return -EINVAL;
1314 }
1315
1316 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1317 if (ret)
1318 return ret;
1319
1320 if (vpfe_dev->io_usrs != 0) {
1321 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1322 ret = -EBUSY;
1323 goto unlock_out;
1324 }
1325
1326 vpfe_dev->memory = req_buf->memory;
1327 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1328 &vpfe_videobuf_qops,
1329 vpfe_dev->pdev,
1330 &vpfe_dev->irqlock,
1331 req_buf->type,
1332 vpfe_dev->fmt.fmt.pix.field,
1333 sizeof(struct videobuf_buffer),
1334 fh, NULL);
1335
1336 fh->io_allowed = 1;
1337 vpfe_dev->io_usrs = 1;
1338 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1339 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1340unlock_out:
1341 mutex_unlock(&vpfe_dev->lock);
1342 return ret;
1343}
1344
1345static int vpfe_querybuf(struct file *file, void *priv,
1346 struct v4l2_buffer *buf)
1347{
1348 struct vpfe_device *vpfe_dev = video_drvdata(file);
1349
1350 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1351
1352 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1353 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1354 return -EINVAL;
1355 }
1356
1357 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1358 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1359 return -EINVAL;
1360 }
1361
1362 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1363}
1364
1365static int vpfe_qbuf(struct file *file, void *priv,
1366 struct v4l2_buffer *p)
1367{
1368 struct vpfe_device *vpfe_dev = video_drvdata(file);
1369 struct vpfe_fh *fh = file->private_data;
1370
1371 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1372
1373 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1374 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1375 return -EINVAL;
1376 }
1377
1378
1379
1380
1381
1382 if (!fh->io_allowed) {
1383 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1384 return -EACCES;
1385 }
1386 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1387}
1388
1389static int vpfe_dqbuf(struct file *file, void *priv,
1390 struct v4l2_buffer *buf)
1391{
1392 struct vpfe_device *vpfe_dev = video_drvdata(file);
1393
1394 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1395
1396 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1397 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1398 return -EINVAL;
1399 }
1400 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1401 buf, file->f_flags & O_NONBLOCK);
1402}
1403
1404
1405
1406
1407
1408static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1409{
1410 struct v4l2_rect image_win;
1411
1412 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1413
1414 ccdc_dev->hw_ops.get_image_window(&image_win);
1415 vpfe_dev->field_off = image_win.height * image_win.width;
1416}
1417
1418
1419static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1420{
1421 ccdc_dev->hw_ops.enable(1);
1422 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1423 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1424 vpfe_dev->started = 1;
1425}
1426
1427
1428
1429
1430
1431
1432static int vpfe_streamon(struct file *file, void *priv,
1433 enum v4l2_buf_type buf_type)
1434{
1435 struct vpfe_device *vpfe_dev = video_drvdata(file);
1436 struct vpfe_fh *fh = file->private_data;
1437 struct vpfe_subdev_info *sdinfo;
1438 unsigned long addr;
1439 int ret;
1440
1441 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1442
1443 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1444 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1445 return -EINVAL;
1446 }
1447
1448
1449 if (!fh->io_allowed) {
1450 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1451 return -EACCES;
1452 }
1453
1454 sdinfo = vpfe_dev->current_subdev;
1455 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1456 video, s_stream, 1);
1457
1458 if (ret && (ret != -ENOIOCTLCMD)) {
1459 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1460 return -EINVAL;
1461 }
1462
1463
1464 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1465 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1466 return -EIO;
1467 }
1468
1469
1470 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1471 if (ret)
1472 return ret;
1473
1474
1475 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1476 if (ret)
1477 goto streamoff;
1478
1479 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1480 struct videobuf_buffer, queue);
1481 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1482
1483 list_del(&vpfe_dev->cur_frm->queue);
1484
1485 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1486
1487 vpfe_dev->field_id = 0;
1488 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1489
1490
1491 vpfe_calculate_offsets(vpfe_dev);
1492
1493 if (vpfe_attach_irq(vpfe_dev) < 0) {
1494 v4l2_err(&vpfe_dev->v4l2_dev,
1495 "Error in attaching interrupt handle\n");
1496 ret = -EFAULT;
1497 goto unlock_out;
1498 }
1499 if (ccdc_dev->hw_ops.configure() < 0) {
1500 v4l2_err(&vpfe_dev->v4l2_dev,
1501 "Error in configuring ccdc\n");
1502 ret = -EINVAL;
1503 goto unlock_out;
1504 }
1505 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1506 vpfe_start_ccdc_capture(vpfe_dev);
1507 mutex_unlock(&vpfe_dev->lock);
1508 return ret;
1509unlock_out:
1510 mutex_unlock(&vpfe_dev->lock);
1511streamoff:
1512 videobuf_streamoff(&vpfe_dev->buffer_queue);
1513 return ret;
1514}
1515
1516static int vpfe_streamoff(struct file *file, void *priv,
1517 enum v4l2_buf_type buf_type)
1518{
1519 struct vpfe_device *vpfe_dev = video_drvdata(file);
1520 struct vpfe_fh *fh = file->private_data;
1521 struct vpfe_subdev_info *sdinfo;
1522 int ret;
1523
1524 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1525
1526 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1527 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1528 return -EINVAL;
1529 }
1530
1531
1532 if (!fh->io_allowed) {
1533 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1534 return -EACCES;
1535 }
1536
1537
1538 if (!vpfe_dev->started) {
1539 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1540 return -EINVAL;
1541 }
1542
1543 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1544 if (ret)
1545 return ret;
1546
1547 vpfe_stop_ccdc_capture(vpfe_dev);
1548 vpfe_detach_irq(vpfe_dev);
1549
1550 sdinfo = vpfe_dev->current_subdev;
1551 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1552 video, s_stream, 0);
1553
1554 if (ret && (ret != -ENOIOCTLCMD))
1555 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1556 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1557 mutex_unlock(&vpfe_dev->lock);
1558 return ret;
1559}
1560
1561static int vpfe_cropcap(struct file *file, void *priv,
1562 struct v4l2_cropcap *crop)
1563{
1564 struct vpfe_device *vpfe_dev = video_drvdata(file);
1565
1566 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1567
1568 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1569 return -EINVAL;
1570
1571 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1572 return 0;
1573
1574 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1575 return 0;
1576}
1577
1578static int vpfe_g_selection(struct file *file, void *priv,
1579 struct v4l2_selection *sel)
1580{
1581 struct vpfe_device *vpfe_dev = video_drvdata(file);
1582
1583 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1584
1585 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1586 return -EINVAL;
1587
1588 switch (sel->target) {
1589 case V4L2_SEL_TGT_CROP:
1590 sel->r = vpfe_dev->crop;
1591 break;
1592 case V4L2_SEL_TGT_CROP_DEFAULT:
1593 case V4L2_SEL_TGT_CROP_BOUNDS:
1594 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1595 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1596 break;
1597 default:
1598 return -EINVAL;
1599 }
1600 return 0;
1601}
1602
1603static int vpfe_s_selection(struct file *file, void *priv,
1604 struct v4l2_selection *sel)
1605{
1606 struct vpfe_device *vpfe_dev = video_drvdata(file);
1607 struct v4l2_rect rect = sel->r;
1608 int ret;
1609
1610 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1611
1612 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1613 sel->target != V4L2_SEL_TGT_CROP)
1614 return -EINVAL;
1615
1616 if (vpfe_dev->started) {
1617
1618 v4l2_err(&vpfe_dev->v4l2_dev,
1619 "Cannot change crop when streaming is ON\n");
1620 return -EBUSY;
1621 }
1622
1623 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1624 if (ret)
1625 return ret;
1626
1627 if (rect.top < 0 || rect.left < 0) {
1628 v4l2_err(&vpfe_dev->v4l2_dev,
1629 "doesn't support negative values for top & left\n");
1630 ret = -EINVAL;
1631 goto unlock_out;
1632 }
1633
1634
1635 rect.width = ((rect.width + 15) & ~0xf);
1636
1637
1638 if ((rect.left + rect.width >
1639 vpfe_dev->std_info.active_pixels) ||
1640 (rect.top + rect.height >
1641 vpfe_dev->std_info.active_lines)) {
1642 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1643 ret = -EINVAL;
1644 goto unlock_out;
1645 }
1646 ccdc_dev->hw_ops.set_image_window(&rect);
1647 vpfe_dev->fmt.fmt.pix.width = rect.width;
1648 vpfe_dev->fmt.fmt.pix.height = rect.height;
1649 vpfe_dev->fmt.fmt.pix.bytesperline =
1650 ccdc_dev->hw_ops.get_line_length();
1651 vpfe_dev->fmt.fmt.pix.sizeimage =
1652 vpfe_dev->fmt.fmt.pix.bytesperline *
1653 vpfe_dev->fmt.fmt.pix.height;
1654 vpfe_dev->crop = rect;
1655 sel->r = rect;
1656unlock_out:
1657 mutex_unlock(&vpfe_dev->lock);
1658 return ret;
1659}
1660
1661
1662static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1663 .vidioc_querycap = vpfe_querycap,
1664 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1665 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1666 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1667 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1668 .vidioc_enum_input = vpfe_enum_input,
1669 .vidioc_g_input = vpfe_g_input,
1670 .vidioc_s_input = vpfe_s_input,
1671 .vidioc_querystd = vpfe_querystd,
1672 .vidioc_s_std = vpfe_s_std,
1673 .vidioc_g_std = vpfe_g_std,
1674 .vidioc_reqbufs = vpfe_reqbufs,
1675 .vidioc_querybuf = vpfe_querybuf,
1676 .vidioc_qbuf = vpfe_qbuf,
1677 .vidioc_dqbuf = vpfe_dqbuf,
1678 .vidioc_streamon = vpfe_streamon,
1679 .vidioc_streamoff = vpfe_streamoff,
1680 .vidioc_cropcap = vpfe_cropcap,
1681 .vidioc_g_selection = vpfe_g_selection,
1682 .vidioc_s_selection = vpfe_s_selection,
1683};
1684
1685static struct vpfe_device *vpfe_initialize(void)
1686{
1687 struct vpfe_device *vpfe_dev;
1688
1689
1690 if ((numbuffers > 0) &&
1691 (numbuffers < config_params.min_numbuffers))
1692 numbuffers = config_params.min_numbuffers;
1693
1694
1695
1696
1697
1698 if (bufsize < config_params.min_bufsize)
1699 bufsize = config_params.min_bufsize;
1700
1701 config_params.numbuffers = numbuffers;
1702
1703 if (numbuffers)
1704 config_params.device_bufsize = bufsize;
1705
1706
1707 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1708
1709 return vpfe_dev;
1710}
1711
1712
1713
1714
1715
1716
1717static int vpfe_probe(struct platform_device *pdev)
1718{
1719 struct vpfe_subdev_info *sdinfo;
1720 struct vpfe_config *vpfe_cfg;
1721 struct resource *res1;
1722 struct vpfe_device *vpfe_dev;
1723 struct i2c_adapter *i2c_adap;
1724 struct video_device *vfd;
1725 int ret, i, j;
1726 int num_subdevs = 0;
1727
1728
1729 vpfe_dev = vpfe_initialize();
1730
1731 if (!vpfe_dev) {
1732 v4l2_err(pdev->dev.driver,
1733 "Failed to allocate memory for vpfe_dev\n");
1734 return -ENOMEM;
1735 }
1736
1737 vpfe_dev->pdev = &pdev->dev;
1738
1739 if (!pdev->dev.platform_data) {
1740 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1741 ret = -ENODEV;
1742 goto probe_free_dev_mem;
1743 }
1744
1745 vpfe_cfg = pdev->dev.platform_data;
1746 vpfe_dev->cfg = vpfe_cfg;
1747 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
1748 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1749 ret = -ENOENT;
1750 goto probe_free_dev_mem;
1751 }
1752
1753
1754 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
1755 if (!ccdc_cfg) {
1756 ret = -ENOMEM;
1757 goto probe_free_dev_mem;
1758 }
1759
1760 mutex_lock(&ccdc_lock);
1761
1762 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1763
1764 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1765 if (!res1) {
1766 v4l2_err(pdev->dev.driver,
1767 "Unable to get interrupt for VINT0\n");
1768 ret = -ENODEV;
1769 goto probe_free_ccdc_cfg_mem;
1770 }
1771 vpfe_dev->ccdc_irq0 = res1->start;
1772
1773
1774 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1775 if (!res1) {
1776 v4l2_err(pdev->dev.driver,
1777 "Unable to get interrupt for VINT1\n");
1778 ret = -ENODEV;
1779 goto probe_free_ccdc_cfg_mem;
1780 }
1781 vpfe_dev->ccdc_irq1 = res1->start;
1782
1783 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1784 "vpfe_capture0", vpfe_dev);
1785
1786 if (0 != ret) {
1787 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1788 goto probe_free_ccdc_cfg_mem;
1789 }
1790
1791 vfd = &vpfe_dev->video_dev;
1792
1793 vfd->release = video_device_release_empty;
1794 vfd->fops = &vpfe_fops;
1795 vfd->ioctl_ops = &vpfe_ioctl_ops;
1796 vfd->tvnorms = 0;
1797 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1798 snprintf(vfd->name, sizeof(vfd->name),
1799 "%s_V%d.%d.%d",
1800 CAPTURE_DRV_NAME,
1801 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1802 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1803 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1804
1805 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1806 if (ret) {
1807 v4l2_err(pdev->dev.driver,
1808 "Unable to register v4l2 device.\n");
1809 goto probe_out_release_irq;
1810 }
1811 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1812 spin_lock_init(&vpfe_dev->irqlock);
1813 spin_lock_init(&vpfe_dev->dma_queue_lock);
1814 mutex_init(&vpfe_dev->lock);
1815
1816
1817 vpfe_dev->numbuffers = config_params.numbuffers;
1818
1819
1820 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1821 "trying to register vpfe device.\n");
1822 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1823 "video_dev=%p\n", &vpfe_dev->video_dev);
1824 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1825 ret = video_register_device(&vpfe_dev->video_dev,
1826 VFL_TYPE_GRABBER, -1);
1827
1828 if (ret) {
1829 v4l2_err(pdev->dev.driver,
1830 "Unable to register video device.\n");
1831 goto probe_out_v4l2_unregister;
1832 }
1833
1834 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1835
1836 platform_set_drvdata(pdev, vpfe_dev);
1837
1838 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1839 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1840 num_subdevs = vpfe_cfg->num_subdevs;
1841 vpfe_dev->sd = kmalloc_array(num_subdevs,
1842 sizeof(*vpfe_dev->sd),
1843 GFP_KERNEL);
1844 if (!vpfe_dev->sd) {
1845 ret = -ENOMEM;
1846 goto probe_out_video_unregister;
1847 }
1848
1849 for (i = 0; i < num_subdevs; i++) {
1850 struct v4l2_input *inps;
1851
1852 sdinfo = &vpfe_cfg->sub_devs[i];
1853
1854
1855 vpfe_dev->sd[i] =
1856 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1857 i2c_adap,
1858 &sdinfo->board_info,
1859 NULL);
1860 if (vpfe_dev->sd[i]) {
1861 v4l2_info(&vpfe_dev->v4l2_dev,
1862 "v4l2 sub device %s registered\n",
1863 sdinfo->name);
1864 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1865
1866 for (j = 0; j < sdinfo->num_inputs; j++) {
1867 inps = &sdinfo->inputs[j];
1868 vfd->tvnorms |= inps->std;
1869 }
1870 } else {
1871 v4l2_info(&vpfe_dev->v4l2_dev,
1872 "v4l2 sub device %s register fails\n",
1873 sdinfo->name);
1874 ret = -ENXIO;
1875 goto probe_sd_out;
1876 }
1877 }
1878
1879
1880 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1881 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1882
1883
1884 mutex_unlock(&ccdc_lock);
1885 return 0;
1886
1887probe_sd_out:
1888 kfree(vpfe_dev->sd);
1889probe_out_video_unregister:
1890 video_unregister_device(&vpfe_dev->video_dev);
1891probe_out_v4l2_unregister:
1892 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1893probe_out_release_irq:
1894 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1895probe_free_ccdc_cfg_mem:
1896 kfree(ccdc_cfg);
1897 mutex_unlock(&ccdc_lock);
1898probe_free_dev_mem:
1899 kfree(vpfe_dev);
1900 return ret;
1901}
1902
1903
1904
1905
1906static int vpfe_remove(struct platform_device *pdev)
1907{
1908 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1909
1910 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1911
1912 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1913 kfree(vpfe_dev->sd);
1914 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1915 video_unregister_device(&vpfe_dev->video_dev);
1916 kfree(vpfe_dev);
1917 kfree(ccdc_cfg);
1918 return 0;
1919}
1920
1921static int vpfe_suspend(struct device *dev)
1922{
1923 return 0;
1924}
1925
1926static int vpfe_resume(struct device *dev)
1927{
1928 return 0;
1929}
1930
1931static const struct dev_pm_ops vpfe_dev_pm_ops = {
1932 .suspend = vpfe_suspend,
1933 .resume = vpfe_resume,
1934};
1935
1936static struct platform_driver vpfe_driver = {
1937 .driver = {
1938 .name = CAPTURE_DRV_NAME,
1939 .pm = &vpfe_dev_pm_ops,
1940 },
1941 .probe = vpfe_probe,
1942 .remove = vpfe_remove,
1943};
1944
1945module_platform_driver(vpfe_driver);
1946