1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
27#include <media/v4l2-ioctl.h>
28
29#include "vpif.h"
30#include "vpif_capture.h"
31
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
34MODULE_VERSION(VPIF_CAPTURE_VERSION);
35
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
71static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
73
74
75
76
77
78
79
80
81
82static int vpif_buffer_prepare(struct vb2_buffer *vb)
83{
84
85 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
86 struct vb2_queue *q = vb->vb2_queue;
87 struct channel_obj *ch = fh->channel;
88 struct common_obj *common;
89 unsigned long addr;
90
91 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
92
93 common = &ch->common[VPIF_VIDEO_INDEX];
94
95 if (vb->state != VB2_BUF_STATE_ACTIVE &&
96 vb->state != VB2_BUF_STATE_PREPARED) {
97 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
98 if (vb2_plane_vaddr(vb, 0) &&
99 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
100 goto exit;
101 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
102
103 if (q->streaming) {
104 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
105 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
106 !IS_ALIGNED((addr + common->ctop_off), 8) ||
107 !IS_ALIGNED((addr + common->cbtm_off), 8))
108 goto exit;
109 }
110 }
111 return 0;
112exit:
113 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
114 return -EINVAL;
115}
116
117
118
119
120
121
122
123
124
125
126
127
128
129static int vpif_buffer_queue_setup(struct vb2_queue *vq,
130 const struct v4l2_format *fmt,
131 unsigned int *nbuffers, unsigned int *nplanes,
132 unsigned int sizes[], void *alloc_ctxs[])
133{
134
135 struct vpif_fh *fh = vb2_get_drv_priv(vq);
136 struct channel_obj *ch = fh->channel;
137 struct common_obj *common;
138 unsigned long size;
139
140 common = &ch->common[VPIF_VIDEO_INDEX];
141
142 vpif_dbg(2, debug, "vpif_buffer_setup\n");
143
144
145 if (V4L2_MEMORY_MMAP == common->memory) {
146
147 size = config_params.channel_bufsize[ch->channel_id];
148
149
150
151
152
153 if (ch->vpifparams.std_info.ycmux_mode == 0) {
154 if (config_params.video_limit[ch->channel_id])
155 while (size * *nbuffers >
156 (config_params.video_limit[0]
157 + config_params.video_limit[1]))
158 (*nbuffers)--;
159 } else {
160 if (config_params.video_limit[ch->channel_id])
161 while (size * *nbuffers >
162 config_params.video_limit[ch->channel_id])
163 (*nbuffers)--;
164 }
165
166 } else {
167 size = common->fmt.fmt.pix.sizeimage;
168 }
169
170 if (*nbuffers < config_params.min_numbuffers)
171 *nbuffers = config_params.min_numbuffers;
172
173 *nplanes = 1;
174 sizes[0] = size;
175 alloc_ctxs[0] = common->alloc_ctx;
176
177 return 0;
178}
179
180
181
182
183
184static void vpif_buffer_queue(struct vb2_buffer *vb)
185{
186
187 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
188 struct channel_obj *ch = fh->channel;
189 struct vpif_cap_buffer *buf = container_of(vb,
190 struct vpif_cap_buffer, vb);
191 struct common_obj *common;
192 unsigned long flags;
193
194 common = &ch->common[VPIF_VIDEO_INDEX];
195
196 vpif_dbg(2, debug, "vpif_buffer_queue\n");
197
198 spin_lock_irqsave(&common->irqlock, flags);
199
200 list_add_tail(&buf->list, &common->dma_queue);
201 spin_unlock_irqrestore(&common->irqlock, flags);
202}
203
204
205
206
207
208
209
210
211static void vpif_buf_cleanup(struct vb2_buffer *vb)
212{
213
214 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
215 struct vpif_cap_buffer *buf = container_of(vb,
216 struct vpif_cap_buffer, vb);
217 struct channel_obj *ch = fh->channel;
218 struct common_obj *common;
219 unsigned long flags;
220
221 common = &ch->common[VPIF_VIDEO_INDEX];
222
223 spin_lock_irqsave(&common->irqlock, flags);
224 if (vb->state == VB2_BUF_STATE_ACTIVE)
225 list_del_init(&buf->list);
226 spin_unlock_irqrestore(&common->irqlock, flags);
227
228}
229
230static void vpif_wait_prepare(struct vb2_queue *vq)
231{
232 struct vpif_fh *fh = vb2_get_drv_priv(vq);
233 struct channel_obj *ch = fh->channel;
234 struct common_obj *common;
235
236 common = &ch->common[VPIF_VIDEO_INDEX];
237 mutex_unlock(&common->lock);
238}
239
240static void vpif_wait_finish(struct vb2_queue *vq)
241{
242 struct vpif_fh *fh = vb2_get_drv_priv(vq);
243 struct channel_obj *ch = fh->channel;
244 struct common_obj *common;
245
246 common = &ch->common[VPIF_VIDEO_INDEX];
247 mutex_lock(&common->lock);
248}
249
250static int vpif_buffer_init(struct vb2_buffer *vb)
251{
252 struct vpif_cap_buffer *buf = container_of(vb,
253 struct vpif_cap_buffer, vb);
254
255 INIT_LIST_HEAD(&buf->list);
256
257 return 0;
258}
259
260static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
261 { {1, 1} };
262
263static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
264{
265 struct vpif_capture_config *vpif_config_data =
266 vpif_dev->platform_data;
267 struct vpif_fh *fh = vb2_get_drv_priv(vq);
268 struct channel_obj *ch = fh->channel;
269 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
270 struct vpif_params *vpif = &ch->vpifparams;
271 unsigned long addr = 0;
272 unsigned long flags;
273 int ret;
274
275
276 spin_lock_irqsave(&common->irqlock, flags);
277 if (list_empty(&common->dma_queue)) {
278 spin_unlock_irqrestore(&common->irqlock, flags);
279 vpif_dbg(1, debug, "buffer queue is empty\n");
280 return -EIO;
281 }
282
283
284 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
285 struct vpif_cap_buffer, list);
286
287 list_del(&common->cur_frm->list);
288 spin_unlock_irqrestore(&common->irqlock, flags);
289
290 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
291
292 ch->field_id = 0;
293 common->started = 1;
294 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
295
296
297 vpif_calculate_offsets(ch);
298
299 if ((vpif->std_info.frm_fmt &&
300 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
301 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
302 (!vpif->std_info.frm_fmt &&
303 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
304 vpif_dbg(1, debug, "conflict in field format and std format\n");
305 return -EINVAL;
306 }
307
308
309 if (vpif_config_data->setup_input_channel_mode) {
310 ret = vpif_config_data->
311 setup_input_channel_mode(vpif->std_info.ycmux_mode);
312 if (ret < 0) {
313 vpif_dbg(1, debug, "can't set vpif channel mode\n");
314 return ret;
315 }
316 }
317
318
319 ret = vpif_set_video_params(vpif, ch->channel_id);
320
321 if (ret < 0) {
322 vpif_dbg(1, debug, "can't set video params\n");
323 return ret;
324 }
325
326 common->started = ret;
327 vpif_config_addr(ch, ret);
328
329 common->set_addr(addr + common->ytop_off,
330 addr + common->ybtm_off,
331 addr + common->ctop_off,
332 addr + common->cbtm_off);
333
334
335
336
337
338 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
339 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
340 channel0_intr_assert();
341 channel0_intr_enable(1);
342 enable_channel0(1);
343 }
344 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
345 (common->started == 2)) {
346 channel1_intr_assert();
347 channel1_intr_enable(1);
348 enable_channel1(1);
349 }
350
351 return 0;
352}
353
354
355static int vpif_stop_streaming(struct vb2_queue *vq)
356{
357 struct vpif_fh *fh = vb2_get_drv_priv(vq);
358 struct channel_obj *ch = fh->channel;
359 struct common_obj *common;
360 unsigned long flags;
361
362 if (!vb2_is_streaming(vq))
363 return 0;
364
365 common = &ch->common[VPIF_VIDEO_INDEX];
366
367
368 spin_lock_irqsave(&common->irqlock, flags);
369 while (!list_empty(&common->dma_queue)) {
370 common->next_frm = list_entry(common->dma_queue.next,
371 struct vpif_cap_buffer, list);
372 list_del(&common->next_frm->list);
373 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
374 }
375 spin_unlock_irqrestore(&common->irqlock, flags);
376
377 return 0;
378}
379
380static struct vb2_ops video_qops = {
381 .queue_setup = vpif_buffer_queue_setup,
382 .wait_prepare = vpif_wait_prepare,
383 .wait_finish = vpif_wait_finish,
384 .buf_init = vpif_buffer_init,
385 .buf_prepare = vpif_buffer_prepare,
386 .start_streaming = vpif_start_streaming,
387 .stop_streaming = vpif_stop_streaming,
388 .buf_cleanup = vpif_buf_cleanup,
389 .buf_queue = vpif_buffer_queue,
390};
391
392
393
394
395
396
397
398
399
400static void vpif_process_buffer_complete(struct common_obj *common)
401{
402 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
403 vb2_buffer_done(&common->cur_frm->vb,
404 VB2_BUF_STATE_DONE);
405
406 common->cur_frm = common->next_frm;
407}
408
409
410
411
412
413
414
415
416
417static void vpif_schedule_next_buffer(struct common_obj *common)
418{
419 unsigned long addr = 0;
420
421 spin_lock(&common->irqlock);
422 common->next_frm = list_entry(common->dma_queue.next,
423 struct vpif_cap_buffer, list);
424
425 list_del(&common->next_frm->list);
426 spin_unlock(&common->irqlock);
427 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
428 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
429
430
431 common->set_addr(addr + common->ytop_off,
432 addr + common->ybtm_off,
433 addr + common->ctop_off,
434 addr + common->cbtm_off);
435}
436
437
438
439
440
441
442
443
444
445static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
446{
447 struct vpif_device *dev = &vpif_obj;
448 struct common_obj *common;
449 struct channel_obj *ch;
450 enum v4l2_field field;
451 int channel_id = 0;
452 int fid = -1, i;
453
454 channel_id = *(int *)(dev_id);
455 if (!vpif_intr_status(channel_id))
456 return IRQ_NONE;
457
458 ch = dev->dev[channel_id];
459
460 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
461
462 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
463 common = &ch->common[i];
464
465 if (0 == common->started)
466 continue;
467
468
469 if (1 == ch->vpifparams.std_info.frm_fmt) {
470
471 spin_lock(&common->irqlock);
472 if (list_empty(&common->dma_queue)) {
473 spin_unlock(&common->irqlock);
474 continue;
475 }
476 spin_unlock(&common->irqlock);
477
478 if (!channel_first_int[i][channel_id])
479 vpif_process_buffer_complete(common);
480
481 channel_first_int[i][channel_id] = 0;
482
483 vpif_schedule_next_buffer(common);
484
485
486 channel_first_int[i][channel_id] = 0;
487 } else {
488
489
490
491
492 if (channel_first_int[i][channel_id]) {
493 channel_first_int[i][channel_id] = 0;
494 continue;
495 }
496 if (0 == i) {
497 ch->field_id ^= 1;
498
499 fid = vpif_channel_getfid(ch->channel_id);
500 if (fid != ch->field_id) {
501
502
503
504
505 if (0 == fid)
506 ch->field_id = fid;
507 return IRQ_HANDLED;
508 }
509 }
510
511 if (0 == fid) {
512
513 if (common->cur_frm == common->next_frm)
514 continue;
515
516
517 vpif_process_buffer_complete(common);
518 } else if (1 == fid) {
519
520 spin_lock(&common->irqlock);
521 if (list_empty(&common->dma_queue) ||
522 (common->cur_frm != common->next_frm)) {
523 spin_unlock(&common->irqlock);
524 continue;
525 }
526 spin_unlock(&common->irqlock);
527
528 vpif_schedule_next_buffer(common);
529 }
530 }
531 }
532 return IRQ_HANDLED;
533}
534
535
536
537
538
539
540
541
542static int vpif_update_std_info(struct channel_obj *ch)
543{
544 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
545 struct vpif_params *vpifparams = &ch->vpifparams;
546 const struct vpif_channel_config_params *config;
547 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
548 struct video_obj *vid_ch = &ch->video;
549 int index;
550
551 vpif_dbg(2, debug, "vpif_update_std_info\n");
552
553 for (index = 0; index < vpif_ch_params_count; index++) {
554 config = &vpif_ch_params[index];
555 if (config->hd_sd == 0) {
556 vpif_dbg(2, debug, "SD format\n");
557 if (config->stdid & vid_ch->stdid) {
558 memcpy(std_info, config, sizeof(*config));
559 break;
560 }
561 } else {
562 vpif_dbg(2, debug, "HD format\n");
563 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
564 sizeof(vid_ch->dv_timings))) {
565 memcpy(std_info, config, sizeof(*config));
566 break;
567 }
568 }
569 }
570
571
572 if (index == vpif_ch_params_count)
573 return -EINVAL;
574
575 common->fmt.fmt.pix.width = std_info->width;
576 common->width = std_info->width;
577 common->fmt.fmt.pix.height = std_info->height;
578 common->height = std_info->height;
579 common->fmt.fmt.pix.bytesperline = std_info->width;
580 vpifparams->video_params.hpitch = std_info->width;
581 vpifparams->video_params.storage_mode = std_info->frm_fmt;
582
583 return 0;
584}
585
586
587
588
589
590
591
592
593static void vpif_calculate_offsets(struct channel_obj *ch)
594{
595 unsigned int hpitch, vpitch, sizeimage;
596 struct video_obj *vid_ch = &(ch->video);
597 struct vpif_params *vpifparams = &ch->vpifparams;
598 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
599 enum v4l2_field field = common->fmt.fmt.pix.field;
600
601 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
602
603 if (V4L2_FIELD_ANY == field) {
604 if (vpifparams->std_info.frm_fmt)
605 vid_ch->buf_field = V4L2_FIELD_NONE;
606 else
607 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
608 } else
609 vid_ch->buf_field = common->fmt.fmt.pix.field;
610
611 sizeimage = common->fmt.fmt.pix.sizeimage;
612
613 hpitch = common->fmt.fmt.pix.bytesperline;
614 vpitch = sizeimage / (hpitch * 2);
615
616 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
617 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
618
619 common->ytop_off = 0;
620 common->ybtm_off = hpitch;
621 common->ctop_off = sizeimage / 2;
622 common->cbtm_off = sizeimage / 2 + hpitch;
623 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
624
625 common->ytop_off = 0;
626 common->ybtm_off = sizeimage / 4;
627 common->ctop_off = sizeimage / 2;
628 common->cbtm_off = common->ctop_off + sizeimage / 4;
629 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
630
631 common->ybtm_off = 0;
632 common->ytop_off = sizeimage / 4;
633 common->cbtm_off = sizeimage / 2;
634 common->ctop_off = common->cbtm_off + sizeimage / 4;
635 }
636 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
637 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
638 vpifparams->video_params.storage_mode = 1;
639 else
640 vpifparams->video_params.storage_mode = 0;
641
642 if (1 == vpifparams->std_info.frm_fmt)
643 vpifparams->video_params.hpitch =
644 common->fmt.fmt.pix.bytesperline;
645 else {
646 if ((field == V4L2_FIELD_ANY)
647 || (field == V4L2_FIELD_INTERLACED))
648 vpifparams->video_params.hpitch =
649 common->fmt.fmt.pix.bytesperline * 2;
650 else
651 vpifparams->video_params.hpitch =
652 common->fmt.fmt.pix.bytesperline;
653 }
654
655 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
656}
657
658
659
660
661
662static void vpif_config_format(struct channel_obj *ch)
663{
664 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
665
666 vpif_dbg(2, debug, "vpif_config_format\n");
667
668 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
669 if (config_params.numbuffers[ch->channel_id] == 0)
670 common->memory = V4L2_MEMORY_USERPTR;
671 else
672 common->memory = V4L2_MEMORY_MMAP;
673
674 common->fmt.fmt.pix.sizeimage
675 = config_params.channel_bufsize[ch->channel_id];
676
677 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
678 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
679 else
680 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
681 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
682}
683
684
685
686
687
688static inline enum v4l2_field vpif_get_default_field(
689 struct vpif_interface *iface)
690{
691 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
692 V4L2_FIELD_INTERLACED;
693}
694
695
696
697
698
699
700
701
702
703
704
705static int vpif_check_format(struct channel_obj *ch,
706 struct v4l2_pix_format *pixfmt,
707 int update)
708{
709 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
710 struct vpif_params *vpif_params = &ch->vpifparams;
711 enum v4l2_field field = pixfmt->field;
712 u32 sizeimage, hpitch, vpitch;
713 int ret = -EINVAL;
714
715 vpif_dbg(2, debug, "vpif_check_format\n");
716
717
718
719
720
721 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
722 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
723 if (!update) {
724 vpif_dbg(2, debug, "invalid pix format\n");
725 goto exit;
726 }
727 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
728 }
729 } else {
730 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
731 if (!update) {
732 vpif_dbg(2, debug, "invalid pixel format\n");
733 goto exit;
734 }
735 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
736 }
737 }
738
739 if (!(VPIF_VALID_FIELD(field))) {
740 if (!update) {
741 vpif_dbg(2, debug, "invalid field format\n");
742 goto exit;
743 }
744
745
746
747
748 field = vpif_get_default_field(&vpif_params->iface);
749 } else if (field == V4L2_FIELD_ANY)
750
751 field = vpif_get_default_field(&vpif_params->iface);
752
753
754 hpitch = pixfmt->bytesperline;
755 if (hpitch < vpif_params->std_info.width) {
756 if (!update) {
757 vpif_dbg(2, debug, "invalid hpitch\n");
758 goto exit;
759 }
760 hpitch = vpif_params->std_info.width;
761 }
762
763 sizeimage = pixfmt->sizeimage;
764
765 vpitch = sizeimage / (hpitch * 2);
766
767
768 if (vpitch < vpif_params->std_info.height) {
769 if (!update) {
770 vpif_dbg(2, debug, "Invalid vpitch\n");
771 goto exit;
772 }
773 vpitch = vpif_params->std_info.height;
774 }
775
776
777 if (!ALIGN(hpitch, 8)) {
778 if (!update) {
779 vpif_dbg(2, debug, "invalid pitch alignment\n");
780 goto exit;
781 }
782
783 hpitch = (((hpitch + 7) / 8) * 8);
784 }
785
786 if (update) {
787 pixfmt->bytesperline = hpitch;
788 pixfmt->sizeimage = hpitch * vpitch * 2;
789 }
790
791
792
793
794 pixfmt->width = common->fmt.fmt.pix.width;
795 pixfmt->height = common->fmt.fmt.pix.height;
796 return 0;
797exit:
798 return ret;
799}
800
801
802
803
804
805
806static void vpif_config_addr(struct channel_obj *ch, int muxmode)
807{
808 struct common_obj *common;
809
810 vpif_dbg(2, debug, "vpif_config_addr\n");
811
812 common = &(ch->common[VPIF_VIDEO_INDEX]);
813
814 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
815 common->set_addr = ch1_set_videobuf_addr;
816 else if (2 == muxmode)
817 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
818 else
819 common->set_addr = ch0_set_videobuf_addr;
820}
821
822
823
824
825
826
827static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
828{
829
830 struct vpif_fh *fh = filep->private_data;
831 struct channel_obj *ch = fh->channel;
832 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
833 int ret;
834
835 vpif_dbg(2, debug, "vpif_mmap\n");
836
837 if (mutex_lock_interruptible(&common->lock))
838 return -ERESTARTSYS;
839 ret = vb2_mmap(&common->buffer_queue, vma);
840 mutex_unlock(&common->lock);
841 return ret;
842}
843
844
845
846
847
848
849static unsigned int vpif_poll(struct file *filep, poll_table * wait)
850{
851 struct vpif_fh *fh = filep->private_data;
852 struct channel_obj *channel = fh->channel;
853 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
854 unsigned int res = 0;
855
856 vpif_dbg(2, debug, "vpif_poll\n");
857
858 if (common->started) {
859 mutex_lock(&common->lock);
860 res = vb2_poll(&common->buffer_queue, filep, wait);
861 mutex_unlock(&common->lock);
862 }
863 return res;
864}
865
866
867
868
869
870
871
872
873static int vpif_open(struct file *filep)
874{
875 struct video_device *vdev = video_devdata(filep);
876 struct common_obj *common;
877 struct video_obj *vid_ch;
878 struct channel_obj *ch;
879 struct vpif_fh *fh;
880
881 vpif_dbg(2, debug, "vpif_open\n");
882
883 ch = video_get_drvdata(vdev);
884
885 vid_ch = &ch->video;
886 common = &ch->common[VPIF_VIDEO_INDEX];
887
888
889 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
890 if (NULL == fh) {
891 vpif_err("unable to allocate memory for file handle object\n");
892 return -ENOMEM;
893 }
894
895 if (mutex_lock_interruptible(&common->lock)) {
896 kfree(fh);
897 return -ERESTARTSYS;
898 }
899
900 filep->private_data = fh;
901 fh->channel = ch;
902 fh->initialized = 0;
903
904 if (!ch->initialized) {
905 fh->initialized = 1;
906 ch->initialized = 1;
907 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
908 }
909
910 ch->usrs++;
911
912 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
913
914 fh->prio = V4L2_PRIORITY_UNSET;
915 v4l2_prio_open(&ch->prio, &fh->prio);
916 mutex_unlock(&common->lock);
917 return 0;
918}
919
920
921
922
923
924
925
926
927static int vpif_release(struct file *filep)
928{
929 struct vpif_fh *fh = filep->private_data;
930 struct channel_obj *ch = fh->channel;
931 struct common_obj *common;
932
933 vpif_dbg(2, debug, "vpif_release\n");
934
935 common = &ch->common[VPIF_VIDEO_INDEX];
936
937 mutex_lock(&common->lock);
938
939 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
940
941 common->io_usrs = 0;
942
943 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
944 enable_channel0(0);
945 channel0_intr_enable(0);
946 }
947 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
948 (2 == common->started)) {
949 enable_channel1(0);
950 channel1_intr_enable(0);
951 }
952 common->started = 0;
953
954 vb2_queue_release(&common->buffer_queue);
955 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
956 }
957
958
959 ch->usrs--;
960
961
962 v4l2_prio_close(&ch->prio, fh->prio);
963
964 if (fh->initialized)
965 ch->initialized = 0;
966
967 mutex_unlock(&common->lock);
968 filep->private_data = NULL;
969 kfree(fh);
970 return 0;
971}
972
973
974
975
976
977
978
979static int vpif_reqbufs(struct file *file, void *priv,
980 struct v4l2_requestbuffers *reqbuf)
981{
982 struct vpif_fh *fh = priv;
983 struct channel_obj *ch = fh->channel;
984 struct common_obj *common;
985 u8 index = 0;
986 struct vb2_queue *q;
987 int ret;
988
989 vpif_dbg(2, debug, "vpif_reqbufs\n");
990
991
992
993
994
995 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
996 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
997 if (!fh->initialized) {
998 vpif_dbg(1, debug, "Channel Busy\n");
999 return -EBUSY;
1000 }
1001 }
1002
1003 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
1004 return -EINVAL;
1005
1006 index = VPIF_VIDEO_INDEX;
1007
1008 common = &ch->common[index];
1009
1010 if (0 != common->io_usrs)
1011 return -EBUSY;
1012
1013
1014 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1015 if (IS_ERR(common->alloc_ctx)) {
1016 vpif_err("Failed to get the context\n");
1017 return PTR_ERR(common->alloc_ctx);
1018 }
1019 q = &common->buffer_queue;
1020 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1021 q->io_modes = VB2_MMAP | VB2_USERPTR;
1022 q->drv_priv = fh;
1023 q->ops = &video_qops;
1024 q->mem_ops = &vb2_dma_contig_memops;
1025 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1026 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1027
1028 ret = vb2_queue_init(q);
1029 if (ret) {
1030 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1031 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1032 return ret;
1033 }
1034
1035 fh->io_allowed[index] = 1;
1036
1037 common->io_usrs = 1;
1038
1039 common->memory = reqbuf->memory;
1040 INIT_LIST_HEAD(&common->dma_queue);
1041
1042
1043 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1044}
1045
1046
1047
1048
1049
1050
1051
1052static int vpif_querybuf(struct file *file, void *priv,
1053 struct v4l2_buffer *buf)
1054{
1055 struct vpif_fh *fh = priv;
1056 struct channel_obj *ch = fh->channel;
1057 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1058
1059 vpif_dbg(2, debug, "vpif_querybuf\n");
1060
1061 if (common->fmt.type != buf->type)
1062 return -EINVAL;
1063
1064 if (common->memory != V4L2_MEMORY_MMAP) {
1065 vpif_dbg(1, debug, "Invalid memory\n");
1066 return -EINVAL;
1067 }
1068
1069 return vb2_querybuf(&common->buffer_queue, buf);
1070}
1071
1072
1073
1074
1075
1076
1077
1078static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1079{
1080
1081 struct vpif_fh *fh = priv;
1082 struct channel_obj *ch = fh->channel;
1083 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1084 struct v4l2_buffer tbuf = *buf;
1085
1086 vpif_dbg(2, debug, "vpif_qbuf\n");
1087
1088 if (common->fmt.type != tbuf.type) {
1089 vpif_err("invalid buffer type\n");
1090 return -EINVAL;
1091 }
1092
1093 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1094 vpif_err("fh io not allowed\n");
1095 return -EACCES;
1096 }
1097
1098 return vb2_qbuf(&common->buffer_queue, buf);
1099}
1100
1101
1102
1103
1104
1105
1106
1107static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1108{
1109 struct vpif_fh *fh = priv;
1110 struct channel_obj *ch = fh->channel;
1111 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1112
1113 vpif_dbg(2, debug, "vpif_dqbuf\n");
1114
1115 return vb2_dqbuf(&common->buffer_queue, buf,
1116 (file->f_flags & O_NONBLOCK));
1117}
1118
1119
1120
1121
1122
1123
1124
1125static int vpif_streamon(struct file *file, void *priv,
1126 enum v4l2_buf_type buftype)
1127{
1128
1129 struct vpif_fh *fh = priv;
1130 struct channel_obj *ch = fh->channel;
1131 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1132 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1133 struct vpif_params *vpif;
1134 int ret = 0;
1135
1136 vpif_dbg(2, debug, "vpif_streamon\n");
1137
1138 vpif = &ch->vpifparams;
1139
1140 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1141 vpif_dbg(1, debug, "buffer type not supported\n");
1142 return -EINVAL;
1143 }
1144
1145
1146 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1147 vpif_dbg(1, debug, "io not allowed\n");
1148 return -EACCES;
1149 }
1150
1151
1152 if (common->started) {
1153 vpif_dbg(1, debug, "channel->started\n");
1154 return -EBUSY;
1155 }
1156
1157 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1158 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1159 vpif->std_info.ycmux_mode == 0) ||
1160 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1161 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1162 vpif_dbg(1, debug, "other channel is being used\n");
1163 return -EBUSY;
1164 }
1165
1166 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1167 if (ret)
1168 return ret;
1169
1170
1171 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
1172
1173 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1174 vpif_dbg(1, debug, "stream on failed in subdev\n");
1175 return ret;
1176 }
1177
1178
1179 ret = vb2_streamon(&common->buffer_queue, buftype);
1180 if (ret) {
1181 vpif_dbg(1, debug, "vb2_streamon\n");
1182 return ret;
1183 }
1184
1185 return ret;
1186}
1187
1188
1189
1190
1191
1192
1193
1194static int vpif_streamoff(struct file *file, void *priv,
1195 enum v4l2_buf_type buftype)
1196{
1197
1198 struct vpif_fh *fh = priv;
1199 struct channel_obj *ch = fh->channel;
1200 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1201 int ret;
1202
1203 vpif_dbg(2, debug, "vpif_streamoff\n");
1204
1205 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1206 vpif_dbg(1, debug, "buffer type not supported\n");
1207 return -EINVAL;
1208 }
1209
1210
1211 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1212 vpif_dbg(1, debug, "io not allowed\n");
1213 return -EACCES;
1214 }
1215
1216
1217 if (!common->started) {
1218 vpif_dbg(1, debug, "channel->started\n");
1219 return -EINVAL;
1220 }
1221
1222
1223 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1224 enable_channel0(0);
1225 channel0_intr_enable(0);
1226 } else {
1227 enable_channel1(0);
1228 channel1_intr_enable(0);
1229 }
1230
1231 common->started = 0;
1232
1233 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
1234
1235 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1236 vpif_dbg(1, debug, "stream off failed in subdev\n");
1237
1238 return vb2_streamoff(&common->buffer_queue, buftype);
1239}
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251static int vpif_input_to_subdev(
1252 struct vpif_capture_config *vpif_cfg,
1253 struct vpif_capture_chan_config *chan_cfg,
1254 int input_index)
1255{
1256 struct vpif_subdev_info *subdev_info;
1257 const char *subdev_name;
1258 int i;
1259
1260 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
1261
1262 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1263 if (subdev_name == NULL)
1264 return -1;
1265
1266
1267 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1268 subdev_info = &vpif_cfg->subdev_info[i];
1269 if (!strcmp(subdev_info->name, subdev_name))
1270 return i;
1271 }
1272 return -1;
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283static int vpif_set_input(
1284 struct vpif_capture_config *vpif_cfg,
1285 struct channel_obj *ch,
1286 int index)
1287{
1288 struct vpif_capture_chan_config *chan_cfg =
1289 &vpif_cfg->chan_config[ch->channel_id];
1290 struct vpif_subdev_info *subdev_info = NULL;
1291 struct v4l2_subdev *sd = NULL;
1292 u32 input = 0, output = 0;
1293 int sd_index;
1294 int ret;
1295
1296 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1297 if (sd_index >= 0) {
1298 sd = vpif_obj.sd[sd_index];
1299 subdev_info = &vpif_cfg->subdev_info[sd_index];
1300 }
1301
1302
1303 if (sd && vpif_cfg->setup_input_path) {
1304 ret = vpif_cfg->setup_input_path(ch->channel_id,
1305 subdev_info->name);
1306 if (ret < 0) {
1307 vpif_dbg(1, debug, "couldn't setup input path for the" \
1308 " sub device %s, for input index %d\n",
1309 subdev_info->name, index);
1310 return ret;
1311 }
1312 }
1313
1314 if (sd) {
1315 input = chan_cfg->inputs[index].input_route;
1316 output = chan_cfg->inputs[index].output_route;
1317 ret = v4l2_subdev_call(sd, video, s_routing,
1318 input, output, 0);
1319 if (ret < 0 && ret != -ENOIOCTLCMD) {
1320 vpif_dbg(1, debug, "Failed to set input\n");
1321 return ret;
1322 }
1323 }
1324 ch->input_idx = index;
1325 ch->sd = sd;
1326
1327 ch->vpifparams.iface = chan_cfg->vpif_if;
1328
1329
1330 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1331 return 0;
1332}
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1343{
1344 struct vpif_fh *fh = priv;
1345 struct channel_obj *ch = fh->channel;
1346 int ret = 0;
1347
1348 vpif_dbg(2, debug, "vpif_querystd\n");
1349
1350
1351 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
1352
1353 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1354 return -ENODATA;
1355 if (ret) {
1356 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1357 return ret;
1358 }
1359
1360 return 0;
1361}
1362
1363
1364
1365
1366
1367
1368
1369static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1370{
1371 struct vpif_fh *fh = priv;
1372 struct channel_obj *ch = fh->channel;
1373
1374 vpif_dbg(2, debug, "vpif_g_std\n");
1375
1376 *std = ch->video.stdid;
1377 return 0;
1378}
1379
1380
1381
1382
1383
1384
1385
1386static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1387{
1388 struct vpif_fh *fh = priv;
1389 struct channel_obj *ch = fh->channel;
1390 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1391 int ret = 0;
1392
1393 vpif_dbg(2, debug, "vpif_s_std\n");
1394
1395 if (common->started) {
1396 vpif_err("streaming in progress\n");
1397 return -EBUSY;
1398 }
1399
1400 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1401 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1402 if (!fh->initialized) {
1403 vpif_dbg(1, debug, "Channel Busy\n");
1404 return -EBUSY;
1405 }
1406 }
1407
1408 ret = v4l2_prio_check(&ch->prio, fh->prio);
1409 if (0 != ret)
1410 return ret;
1411
1412 fh->initialized = 1;
1413
1414
1415 ch->video.stdid = std_id;
1416 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1417
1418
1419 if (vpif_update_std_info(ch)) {
1420 vpif_err("Error getting the standard info\n");
1421 return -EINVAL;
1422 }
1423
1424
1425 vpif_config_format(ch);
1426
1427
1428 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
1429 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
1430 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1431 return ret;
1432 }
1433 return 0;
1434}
1435
1436
1437
1438
1439
1440
1441
1442static int vpif_enum_input(struct file *file, void *priv,
1443 struct v4l2_input *input)
1444{
1445
1446 struct vpif_capture_config *config = vpif_dev->platform_data;
1447 struct vpif_capture_chan_config *chan_cfg;
1448 struct vpif_fh *fh = priv;
1449 struct channel_obj *ch = fh->channel;
1450
1451 chan_cfg = &config->chan_config[ch->channel_id];
1452
1453 if (input->index >= chan_cfg->input_count) {
1454 vpif_dbg(1, debug, "Invalid input index\n");
1455 return -EINVAL;
1456 }
1457
1458 memcpy(input, &chan_cfg->inputs[input->index].input,
1459 sizeof(*input));
1460 return 0;
1461}
1462
1463
1464
1465
1466
1467
1468
1469static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1470{
1471 struct vpif_fh *fh = priv;
1472 struct channel_obj *ch = fh->channel;
1473
1474 *index = ch->input_idx;
1475 return 0;
1476}
1477
1478
1479
1480
1481
1482
1483
1484static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1485{
1486 struct vpif_capture_config *config = vpif_dev->platform_data;
1487 struct vpif_capture_chan_config *chan_cfg;
1488 struct vpif_fh *fh = priv;
1489 struct channel_obj *ch = fh->channel;
1490 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1491 int ret;
1492
1493 chan_cfg = &config->chan_config[ch->channel_id];
1494
1495 if (index >= chan_cfg->input_count)
1496 return -EINVAL;
1497
1498 if (common->started) {
1499 vpif_err("Streaming in progress\n");
1500 return -EBUSY;
1501 }
1502
1503 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1504 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1505 if (!fh->initialized) {
1506 vpif_dbg(1, debug, "Channel Busy\n");
1507 return -EBUSY;
1508 }
1509 }
1510
1511 ret = v4l2_prio_check(&ch->prio, fh->prio);
1512 if (0 != ret)
1513 return ret;
1514
1515 fh->initialized = 1;
1516 return vpif_set_input(config, ch, index);
1517}
1518
1519
1520
1521
1522
1523
1524
1525static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1526 struct v4l2_fmtdesc *fmt)
1527{
1528 struct vpif_fh *fh = priv;
1529 struct channel_obj *ch = fh->channel;
1530
1531 if (fmt->index != 0) {
1532 vpif_dbg(1, debug, "Invalid format index\n");
1533 return -EINVAL;
1534 }
1535
1536
1537 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1538 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1539 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1540 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1541 } else {
1542 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1543 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1544 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1545 }
1546 return 0;
1547}
1548
1549
1550
1551
1552
1553
1554
1555static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1556 struct v4l2_format *fmt)
1557{
1558 struct vpif_fh *fh = priv;
1559 struct channel_obj *ch = fh->channel;
1560 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1561
1562 return vpif_check_format(ch, pixfmt, 1);
1563}
1564
1565
1566
1567
1568
1569
1570
1571
1572static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1573 struct v4l2_format *fmt)
1574{
1575 struct vpif_fh *fh = priv;
1576 struct channel_obj *ch = fh->channel;
1577 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1578
1579
1580 if (common->fmt.type != fmt->type)
1581 return -EINVAL;
1582
1583
1584 *fmt = common->fmt;
1585 return 0;
1586}
1587
1588
1589
1590
1591
1592
1593
1594static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1595 struct v4l2_format *fmt)
1596{
1597 struct vpif_fh *fh = priv;
1598 struct channel_obj *ch = fh->channel;
1599 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1600 struct v4l2_pix_format *pixfmt;
1601 int ret = 0;
1602
1603 vpif_dbg(2, debug, "%s\n", __func__);
1604
1605
1606 if (common->started) {
1607 vpif_dbg(1, debug, "Streaming is started\n");
1608 return -EBUSY;
1609 }
1610
1611 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1612 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1613 if (!fh->initialized) {
1614 vpif_dbg(1, debug, "Channel Busy\n");
1615 return -EBUSY;
1616 }
1617 }
1618
1619 ret = v4l2_prio_check(&ch->prio, fh->prio);
1620 if (0 != ret)
1621 return ret;
1622
1623 fh->initialized = 1;
1624
1625 pixfmt = &fmt->fmt.pix;
1626
1627 ret = vpif_check_format(ch, pixfmt, 0);
1628
1629 if (ret)
1630 return ret;
1631
1632 common->fmt = *fmt;
1633 return 0;
1634}
1635
1636
1637
1638
1639
1640
1641
1642static int vpif_querycap(struct file *file, void *priv,
1643 struct v4l2_capability *cap)
1644{
1645 struct vpif_capture_config *config = vpif_dev->platform_data;
1646
1647 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1648 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1649 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1650 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1651 dev_name(vpif_dev));
1652 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1653
1654 return 0;
1655}
1656
1657
1658
1659
1660
1661
1662
1663static int vpif_g_priority(struct file *file, void *priv,
1664 enum v4l2_priority *prio)
1665{
1666 struct vpif_fh *fh = priv;
1667 struct channel_obj *ch = fh->channel;
1668
1669 *prio = v4l2_prio_max(&ch->prio);
1670
1671 return 0;
1672}
1673
1674
1675
1676
1677
1678
1679
1680static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1681{
1682 struct vpif_fh *fh = priv;
1683 struct channel_obj *ch = fh->channel;
1684
1685 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1686}
1687
1688
1689
1690
1691
1692
1693
1694static int vpif_cropcap(struct file *file, void *priv,
1695 struct v4l2_cropcap *crop)
1696{
1697 struct vpif_fh *fh = priv;
1698 struct channel_obj *ch = fh->channel;
1699 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1700
1701 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1702 return -EINVAL;
1703
1704 crop->bounds.left = 0;
1705 crop->bounds.top = 0;
1706 crop->bounds.height = common->height;
1707 crop->bounds.width = common->width;
1708 crop->defrect = crop->bounds;
1709 return 0;
1710}
1711
1712
1713
1714
1715
1716
1717
1718static int
1719vpif_enum_dv_timings(struct file *file, void *priv,
1720 struct v4l2_enum_dv_timings *timings)
1721{
1722 struct vpif_fh *fh = priv;
1723 struct channel_obj *ch = fh->channel;
1724 int ret;
1725
1726 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1727 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1728 return -EINVAL;
1729 return ret;
1730}
1731
1732
1733
1734
1735
1736
1737
1738static int
1739vpif_query_dv_timings(struct file *file, void *priv,
1740 struct v4l2_dv_timings *timings)
1741{
1742 struct vpif_fh *fh = priv;
1743 struct channel_obj *ch = fh->channel;
1744 int ret;
1745
1746 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1747 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1748 return -ENODATA;
1749 return ret;
1750}
1751
1752
1753
1754
1755
1756
1757
1758static int vpif_s_dv_timings(struct file *file, void *priv,
1759 struct v4l2_dv_timings *timings)
1760{
1761 struct vpif_fh *fh = priv;
1762 struct channel_obj *ch = fh->channel;
1763 struct vpif_params *vpifparams = &ch->vpifparams;
1764 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1765 struct video_obj *vid_ch = &ch->video;
1766 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1767 int ret;
1768
1769 if (timings->type != V4L2_DV_BT_656_1120) {
1770 vpif_dbg(2, debug, "Timing type not defined\n");
1771 return -EINVAL;
1772 }
1773
1774
1775 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1776 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1777 ret = 0;
1778 if (ret < 0) {
1779 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1780 return ret;
1781 }
1782
1783 if (!(timings->bt.width && timings->bt.height &&
1784 (timings->bt.hbackporch ||
1785 timings->bt.hfrontporch ||
1786 timings->bt.hsync) &&
1787 timings->bt.vfrontporch &&
1788 (timings->bt.vbackporch ||
1789 timings->bt.vsync))) {
1790 vpif_dbg(2, debug, "Timings for width, height, "
1791 "horizontal back porch, horizontal sync, "
1792 "horizontal front porch, vertical back porch, "
1793 "vertical sync and vertical back porch "
1794 "must be defined\n");
1795 return -EINVAL;
1796 }
1797
1798 vid_ch->dv_timings = *timings;
1799
1800
1801
1802 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1803 std_info->sav2eav = bt->width;
1804
1805 std_info->l1 = 1;
1806 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1807
1808 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1809 if (bt->interlaced) {
1810 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1811 std_info->l5 = std_info->vsize/2 -
1812 (bt->vfrontporch - 1);
1813 std_info->l7 = std_info->vsize/2 + 1;
1814 std_info->l9 = std_info->l7 + bt->il_vsync +
1815 bt->il_vbackporch + 1;
1816 std_info->l11 = std_info->vsize -
1817 (bt->il_vfrontporch - 1);
1818 } else {
1819 vpif_dbg(2, debug, "Required timing values for "
1820 "interlaced BT format missing\n");
1821 return -EINVAL;
1822 }
1823 } else {
1824 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1825 }
1826 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1827 std_info->width = bt->width;
1828 std_info->height = bt->height;
1829 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1830 std_info->ycmux_mode = 0;
1831 std_info->capture_format = 0;
1832 std_info->vbi_supported = 0;
1833 std_info->hd_sd = 1;
1834 std_info->stdid = 0;
1835
1836 vid_ch->stdid = 0;
1837 return 0;
1838}
1839
1840
1841
1842
1843
1844
1845
1846static int vpif_g_dv_timings(struct file *file, void *priv,
1847 struct v4l2_dv_timings *timings)
1848{
1849 struct vpif_fh *fh = priv;
1850 struct channel_obj *ch = fh->channel;
1851 struct video_obj *vid_ch = &ch->video;
1852
1853 *timings = vid_ch->dv_timings;
1854
1855 return 0;
1856}
1857
1858
1859
1860
1861
1862
1863
1864
1865static int vpif_log_status(struct file *filep, void *priv)
1866{
1867
1868 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1869
1870 return 0;
1871}
1872
1873
1874static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1875 .vidioc_querycap = vpif_querycap,
1876 .vidioc_g_priority = vpif_g_priority,
1877 .vidioc_s_priority = vpif_s_priority,
1878 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1879 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1880 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1881 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1882 .vidioc_enum_input = vpif_enum_input,
1883 .vidioc_s_input = vpif_s_input,
1884 .vidioc_g_input = vpif_g_input,
1885 .vidioc_reqbufs = vpif_reqbufs,
1886 .vidioc_querybuf = vpif_querybuf,
1887 .vidioc_querystd = vpif_querystd,
1888 .vidioc_s_std = vpif_s_std,
1889 .vidioc_g_std = vpif_g_std,
1890 .vidioc_qbuf = vpif_qbuf,
1891 .vidioc_dqbuf = vpif_dqbuf,
1892 .vidioc_streamon = vpif_streamon,
1893 .vidioc_streamoff = vpif_streamoff,
1894 .vidioc_cropcap = vpif_cropcap,
1895 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1896 .vidioc_query_dv_timings = vpif_query_dv_timings,
1897 .vidioc_s_dv_timings = vpif_s_dv_timings,
1898 .vidioc_g_dv_timings = vpif_g_dv_timings,
1899 .vidioc_log_status = vpif_log_status,
1900};
1901
1902
1903static struct v4l2_file_operations vpif_fops = {
1904 .owner = THIS_MODULE,
1905 .open = vpif_open,
1906 .release = vpif_release,
1907 .unlocked_ioctl = video_ioctl2,
1908 .mmap = vpif_mmap,
1909 .poll = vpif_poll
1910};
1911
1912
1913static struct video_device vpif_video_template = {
1914 .name = "vpif",
1915 .fops = &vpif_fops,
1916 .minor = -1,
1917 .ioctl_ops = &vpif_ioctl_ops,
1918};
1919
1920
1921
1922
1923
1924
1925static int initialize_vpif(void)
1926{
1927 int err = 0, i, j;
1928 int free_channel_objects_index;
1929
1930
1931 if ((ch0_numbuffers > 0) &&
1932 (ch0_numbuffers < config_params.min_numbuffers))
1933 ch0_numbuffers = config_params.min_numbuffers;
1934 if ((ch1_numbuffers > 0) &&
1935 (ch1_numbuffers < config_params.min_numbuffers))
1936 ch1_numbuffers = config_params.min_numbuffers;
1937
1938
1939 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1940 ch0_bufsize =
1941 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1942 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1943 ch1_bufsize =
1944 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1945
1946 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1947 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1948 if (ch0_numbuffers) {
1949 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1950 = ch0_bufsize;
1951 }
1952 if (ch1_numbuffers) {
1953 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1954 = ch1_bufsize;
1955 }
1956
1957
1958 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1959 vpif_obj.dev[i] =
1960 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1961
1962 if (!vpif_obj.dev[i]) {
1963 free_channel_objects_index = i;
1964 err = -ENOMEM;
1965 goto vpif_init_free_channel_objects;
1966 }
1967 }
1968 return 0;
1969
1970vpif_init_free_channel_objects:
1971 for (j = 0; j < free_channel_objects_index; j++)
1972 kfree(vpif_obj.dev[j]);
1973 return err;
1974}
1975
1976static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1977 struct v4l2_subdev *subdev,
1978 struct v4l2_async_subdev *asd)
1979{
1980 int i;
1981
1982 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1983 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1984 subdev->name)) {
1985 vpif_obj.sd[i] = subdev;
1986 return 0;
1987 }
1988
1989 return -EINVAL;
1990}
1991
1992static int vpif_probe_complete(void)
1993{
1994 struct common_obj *common;
1995 struct channel_obj *ch;
1996 int i, j, err, k;
1997
1998 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1999 ch = vpif_obj.dev[j];
2000 ch->channel_id = j;
2001 common = &(ch->common[VPIF_VIDEO_INDEX]);
2002 spin_lock_init(&common->irqlock);
2003 mutex_init(&common->lock);
2004 ch->video_dev->lock = &common->lock;
2005
2006 v4l2_prio_init(&ch->prio);
2007 video_set_drvdata(ch->video_dev, ch);
2008
2009
2010 err = vpif_set_input(vpif_obj.config, ch, 0);
2011 if (err)
2012 goto probe_out;
2013
2014 err = video_register_device(ch->video_dev,
2015 VFL_TYPE_GRABBER, (j ? 1 : 0));
2016 if (err)
2017 goto probe_out;
2018 }
2019
2020 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2021 return 0;
2022
2023probe_out:
2024 for (k = 0; k < j; k++) {
2025
2026 ch = vpif_obj.dev[k];
2027
2028 video_unregister_device(ch->video_dev);
2029 }
2030 kfree(vpif_obj.sd);
2031 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2032 ch = vpif_obj.dev[i];
2033
2034 video_device_release(ch->video_dev);
2035 }
2036 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2037
2038 return err;
2039}
2040
2041static int vpif_async_complete(struct v4l2_async_notifier *notifier)
2042{
2043 return vpif_probe_complete();
2044}
2045
2046
2047
2048
2049
2050
2051
2052
2053static __init int vpif_probe(struct platform_device *pdev)
2054{
2055 struct vpif_subdev_info *subdevdata;
2056 int i, j, err;
2057 int res_idx = 0;
2058 struct i2c_adapter *i2c_adap;
2059 struct channel_obj *ch;
2060 struct video_device *vfd;
2061 struct resource *res;
2062 int subdev_count;
2063 size_t size;
2064
2065 vpif_dev = &pdev->dev;
2066
2067 err = initialize_vpif();
2068 if (err) {
2069 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2070 return err;
2071 }
2072
2073 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2074 if (err) {
2075 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2076 return err;
2077 }
2078
2079 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
2080 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2081 IRQF_SHARED, "VPIF_Capture",
2082 (void *)(&vpif_obj.dev[res_idx]->
2083 channel_id));
2084 if (err) {
2085 err = -EINVAL;
2086 goto vpif_unregister;
2087 }
2088 res_idx++;
2089 }
2090
2091 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2092
2093 ch = vpif_obj.dev[i];
2094
2095 vfd = video_device_alloc();
2096 if (NULL == vfd) {
2097 for (j = 0; j < i; j++) {
2098 ch = vpif_obj.dev[j];
2099 video_device_release(ch->video_dev);
2100 }
2101 err = -ENOMEM;
2102 goto vpif_unregister;
2103 }
2104
2105
2106 *vfd = vpif_video_template;
2107 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2108 vfd->release = video_device_release;
2109 snprintf(vfd->name, sizeof(vfd->name),
2110 "VPIF_Capture_DRIVER_V%s",
2111 VPIF_CAPTURE_VERSION);
2112
2113 ch->video_dev = vfd;
2114 }
2115
2116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2117 if (res) {
2118 size = resource_size(res);
2119
2120
2121
2122 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2123 ch = vpif_obj.dev[j];
2124 ch->channel_id = j;
2125
2126 config_params.video_limit[ch->channel_id] = 0;
2127 if (size)
2128 config_params.video_limit[ch->channel_id] =
2129 size/2;
2130 }
2131 }
2132
2133 vpif_obj.config = pdev->dev.platform_data;
2134
2135 subdev_count = vpif_obj.config->subdev_count;
2136 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2137 GFP_KERNEL);
2138 if (vpif_obj.sd == NULL) {
2139 vpif_err("unable to allocate memory for subdevice pointers\n");
2140 err = -ENOMEM;
2141 goto vpif_sd_error;
2142 }
2143
2144 if (!vpif_obj.config->asd_sizes) {
2145 i2c_adap = i2c_get_adapter(1);
2146 for (i = 0; i < subdev_count; i++) {
2147 subdevdata = &vpif_obj.config->subdev_info[i];
2148 vpif_obj.sd[i] =
2149 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2150 i2c_adap,
2151 &subdevdata->
2152 board_info,
2153 NULL);
2154
2155 if (!vpif_obj.sd[i]) {
2156 vpif_err("Error registering v4l2 subdevice\n");
2157 err = -ENOMEM;
2158 goto probe_subdev_out;
2159 }
2160 v4l2_info(&vpif_obj.v4l2_dev,
2161 "registered sub device %s\n",
2162 subdevdata->name);
2163 }
2164 vpif_probe_complete();
2165 } else {
2166 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
2167 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2168 vpif_obj.notifier.bound = vpif_async_bound;
2169 vpif_obj.notifier.complete = vpif_async_complete;
2170 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2171 &vpif_obj.notifier);
2172 if (err) {
2173 vpif_err("Error registering async notifier\n");
2174 err = -EINVAL;
2175 goto probe_subdev_out;
2176 }
2177 }
2178
2179 return 0;
2180
2181probe_subdev_out:
2182
2183 kfree(vpif_obj.sd);
2184
2185vpif_sd_error:
2186 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2187 ch = vpif_obj.dev[i];
2188
2189 video_device_release(ch->video_dev);
2190 }
2191vpif_unregister:
2192 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2193
2194 return err;
2195}
2196
2197
2198
2199
2200
2201
2202
2203static int vpif_remove(struct platform_device *device)
2204{
2205 struct channel_obj *ch;
2206 int i;
2207
2208 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2209
2210 kfree(vpif_obj.sd);
2211
2212 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2213
2214 ch = vpif_obj.dev[i];
2215
2216 video_unregister_device(ch->video_dev);
2217 kfree(vpif_obj.dev[i]);
2218 }
2219 return 0;
2220}
2221
2222#ifdef CONFIG_PM
2223
2224
2225
2226static int vpif_suspend(struct device *dev)
2227{
2228
2229 struct common_obj *common;
2230 struct channel_obj *ch;
2231 int i;
2232
2233 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2234
2235 ch = vpif_obj.dev[i];
2236 common = &ch->common[VPIF_VIDEO_INDEX];
2237 mutex_lock(&common->lock);
2238 if (ch->usrs && common->io_usrs) {
2239
2240 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2241 enable_channel0(0);
2242 channel0_intr_enable(0);
2243 }
2244 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2245 common->started == 2) {
2246 enable_channel1(0);
2247 channel1_intr_enable(0);
2248 }
2249 }
2250 mutex_unlock(&common->lock);
2251 }
2252
2253 return 0;
2254}
2255
2256
2257
2258
2259static int vpif_resume(struct device *dev)
2260{
2261 struct common_obj *common;
2262 struct channel_obj *ch;
2263 int i;
2264
2265 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2266
2267 ch = vpif_obj.dev[i];
2268 common = &ch->common[VPIF_VIDEO_INDEX];
2269 mutex_lock(&common->lock);
2270 if (ch->usrs && common->io_usrs) {
2271
2272 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2273 enable_channel0(1);
2274 channel0_intr_enable(1);
2275 }
2276 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2277 common->started == 2) {
2278 enable_channel1(1);
2279 channel1_intr_enable(1);
2280 }
2281 }
2282 mutex_unlock(&common->lock);
2283 }
2284
2285 return 0;
2286}
2287
2288static const struct dev_pm_ops vpif_dev_pm_ops = {
2289 .suspend = vpif_suspend,
2290 .resume = vpif_resume,
2291};
2292
2293#define vpif_pm_ops (&vpif_dev_pm_ops)
2294#else
2295#define vpif_pm_ops NULL
2296#endif
2297
2298static __refdata struct platform_driver vpif_driver = {
2299 .driver = {
2300 .name = "vpif_capture",
2301 .owner = THIS_MODULE,
2302 .pm = vpif_pm_ops,
2303 },
2304 .probe = vpif_probe,
2305 .remove = vpif_remove,
2306};
2307
2308module_platform_driver(vpif_driver);
2309