1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/interrupt.h>
12#include <linux/of_graph.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
15
16#include <media/v4l2-fwnode.h>
17#include <media/v4l2-ioctl.h>
18#include <media/i2c/tvp514x.h>
19#include <media/v4l2-mediabus.h>
20
21#include <linux/videodev2.h>
22
23#include "vpif.h"
24#include "vpif_capture.h"
25
26MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
27MODULE_LICENSE("GPL");
28MODULE_VERSION(VPIF_CAPTURE_VERSION);
29
30#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
31#define vpif_dbg(level, debug, fmt, arg...) \
32 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
33
34static int debug = 1;
35
36module_param(debug, int, 0644);
37
38MODULE_PARM_DESC(debug, "Debug level 0-1");
39
40#define VPIF_DRIVER_NAME "vpif_capture"
41MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
42
43
44static struct vpif_device vpif_obj = { {NULL} };
45static struct device *vpif_dev;
46static void vpif_calculate_offsets(struct channel_obj *ch);
47static void vpif_config_addr(struct channel_obj *ch, int muxmode);
48
49static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
50
51
52static int ycmux_mode;
53
54static inline
55struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
56{
57 return container_of(vb, struct vpif_cap_buffer, vb);
58}
59
60
61
62
63
64
65
66
67
68static int vpif_buffer_prepare(struct vb2_buffer *vb)
69{
70 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
71 struct vb2_queue *q = vb->vb2_queue;
72 struct channel_obj *ch = vb2_get_drv_priv(q);
73 struct common_obj *common;
74 unsigned long addr;
75
76 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
77
78 common = &ch->common[VPIF_VIDEO_INDEX];
79
80 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
81 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
82 return -EINVAL;
83
84 vbuf->field = common->fmt.fmt.pix.field;
85
86 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
87 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
88 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
89 !IS_ALIGNED((addr + common->ctop_off), 8) ||
90 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
91 vpif_dbg(1, debug, "offset is not aligned\n");
92 return -EINVAL;
93 }
94
95 return 0;
96}
97
98
99
100
101
102
103
104
105
106
107
108
109static int vpif_buffer_queue_setup(struct vb2_queue *vq,
110 unsigned int *nbuffers, unsigned int *nplanes,
111 unsigned int sizes[], struct device *alloc_devs[])
112{
113 struct channel_obj *ch = vb2_get_drv_priv(vq);
114 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
115 unsigned size = common->fmt.fmt.pix.sizeimage;
116
117 vpif_dbg(2, debug, "vpif_buffer_setup\n");
118
119 if (*nplanes) {
120 if (sizes[0] < size)
121 return -EINVAL;
122 size = sizes[0];
123 }
124
125 if (vq->num_buffers + *nbuffers < 3)
126 *nbuffers = 3 - vq->num_buffers;
127
128 *nplanes = 1;
129 sizes[0] = size;
130
131
132 vpif_calculate_offsets(ch);
133
134 return 0;
135}
136
137
138
139
140
141static void vpif_buffer_queue(struct vb2_buffer *vb)
142{
143 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
145 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
146 struct common_obj *common;
147 unsigned long flags;
148
149 common = &ch->common[VPIF_VIDEO_INDEX];
150
151 vpif_dbg(2, debug, "vpif_buffer_queue\n");
152
153 spin_lock_irqsave(&common->irqlock, flags);
154
155 list_add_tail(&buf->list, &common->dma_queue);
156 spin_unlock_irqrestore(&common->irqlock, flags);
157}
158
159
160
161
162
163
164static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
165{
166 struct vpif_capture_config *vpif_config_data =
167 vpif_dev->platform_data;
168 struct channel_obj *ch = vb2_get_drv_priv(vq);
169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
170 struct vpif_params *vpif = &ch->vpifparams;
171 struct vpif_cap_buffer *buf, *tmp;
172 unsigned long addr, flags;
173 int ret;
174
175
176 ch->field_id = 0;
177
178
179 if (vpif_config_data->setup_input_channel_mode) {
180 ret = vpif_config_data->
181 setup_input_channel_mode(vpif->std_info.ycmux_mode);
182 if (ret < 0) {
183 vpif_dbg(1, debug, "can't set vpif channel mode\n");
184 goto err;
185 }
186 }
187
188 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
189 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
190 vpif_dbg(1, debug, "stream on failed in subdev\n");
191 goto err;
192 }
193
194
195 ret = vpif_set_video_params(vpif, ch->channel_id);
196 if (ret < 0) {
197 vpif_dbg(1, debug, "can't set video params\n");
198 goto err;
199 }
200
201 ycmux_mode = ret;
202 vpif_config_addr(ch, ret);
203
204
205 spin_lock_irqsave(&common->irqlock, flags);
206 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
207 struct vpif_cap_buffer, list);
208
209 list_del(&common->cur_frm->list);
210 spin_unlock_irqrestore(&common->irqlock, flags);
211
212 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
213
214 common->set_addr(addr + common->ytop_off,
215 addr + common->ybtm_off,
216 addr + common->ctop_off,
217 addr + common->cbtm_off);
218
219
220
221
222
223 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
224 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
225 channel0_intr_assert();
226 channel0_intr_enable(1);
227 enable_channel0(1);
228 }
229 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
230 ycmux_mode == 2) {
231 channel1_intr_assert();
232 channel1_intr_enable(1);
233 enable_channel1(1);
234 }
235
236 return 0;
237
238err:
239 spin_lock_irqsave(&common->irqlock, flags);
240 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
241 list_del(&buf->list);
242 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
243 }
244 spin_unlock_irqrestore(&common->irqlock, flags);
245
246 return ret;
247}
248
249
250
251
252
253
254
255
256static void vpif_stop_streaming(struct vb2_queue *vq)
257{
258 struct channel_obj *ch = vb2_get_drv_priv(vq);
259 struct common_obj *common;
260 unsigned long flags;
261 int ret;
262
263 common = &ch->common[VPIF_VIDEO_INDEX];
264
265
266 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
267 enable_channel0(0);
268 channel0_intr_enable(0);
269 }
270 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
271 ycmux_mode == 2) {
272 enable_channel1(0);
273 channel1_intr_enable(0);
274 }
275
276 ycmux_mode = 0;
277
278 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
279 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
280 vpif_dbg(1, debug, "stream off failed in subdev\n");
281
282
283 if (common->cur_frm == common->next_frm) {
284 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
285 VB2_BUF_STATE_ERROR);
286 } else {
287 if (common->cur_frm)
288 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
289 VB2_BUF_STATE_ERROR);
290 if (common->next_frm)
291 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
292 VB2_BUF_STATE_ERROR);
293 }
294
295 spin_lock_irqsave(&common->irqlock, flags);
296 while (!list_empty(&common->dma_queue)) {
297 common->next_frm = list_entry(common->dma_queue.next,
298 struct vpif_cap_buffer, list);
299 list_del(&common->next_frm->list);
300 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
301 VB2_BUF_STATE_ERROR);
302 }
303 spin_unlock_irqrestore(&common->irqlock, flags);
304}
305
306static const struct vb2_ops video_qops = {
307 .queue_setup = vpif_buffer_queue_setup,
308 .buf_prepare = vpif_buffer_prepare,
309 .start_streaming = vpif_start_streaming,
310 .stop_streaming = vpif_stop_streaming,
311 .buf_queue = vpif_buffer_queue,
312 .wait_prepare = vb2_ops_wait_prepare,
313 .wait_finish = vb2_ops_wait_finish,
314};
315
316
317
318
319
320
321
322
323
324static void vpif_process_buffer_complete(struct common_obj *common)
325{
326 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
327 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
328
329 common->cur_frm = common->next_frm;
330}
331
332
333
334
335
336
337
338
339
340static void vpif_schedule_next_buffer(struct common_obj *common)
341{
342 unsigned long addr = 0;
343
344 spin_lock(&common->irqlock);
345 common->next_frm = list_entry(common->dma_queue.next,
346 struct vpif_cap_buffer, list);
347
348 list_del(&common->next_frm->list);
349 spin_unlock(&common->irqlock);
350 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
351
352
353 common->set_addr(addr + common->ytop_off,
354 addr + common->ybtm_off,
355 addr + common->ctop_off,
356 addr + common->cbtm_off);
357}
358
359
360
361
362
363
364
365
366
367static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
368{
369 struct vpif_device *dev = &vpif_obj;
370 struct common_obj *common;
371 struct channel_obj *ch;
372 int channel_id;
373 int fid = -1, i;
374
375 channel_id = *(int *)(dev_id);
376 if (!vpif_intr_status(channel_id))
377 return IRQ_NONE;
378
379 ch = dev->dev[channel_id];
380
381 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
382 common = &ch->common[i];
383
384
385 if (1 == ch->vpifparams.std_info.frm_fmt ||
386 common->fmt.fmt.pix.field == V4L2_FIELD_NONE) {
387
388 spin_lock(&common->irqlock);
389 if (list_empty(&common->dma_queue)) {
390 spin_unlock(&common->irqlock);
391 continue;
392 }
393 spin_unlock(&common->irqlock);
394
395 if (!channel_first_int[i][channel_id])
396 vpif_process_buffer_complete(common);
397
398 channel_first_int[i][channel_id] = 0;
399
400 vpif_schedule_next_buffer(common);
401
402
403 channel_first_int[i][channel_id] = 0;
404 } else {
405
406
407
408
409 if (channel_first_int[i][channel_id]) {
410 channel_first_int[i][channel_id] = 0;
411 continue;
412 }
413 if (0 == i) {
414 ch->field_id ^= 1;
415
416 fid = vpif_channel_getfid(ch->channel_id);
417 if (fid != ch->field_id) {
418
419
420
421
422 if (0 == fid)
423 ch->field_id = fid;
424 return IRQ_HANDLED;
425 }
426 }
427
428 if (0 == fid) {
429
430 if (common->cur_frm == common->next_frm)
431 continue;
432
433
434 vpif_process_buffer_complete(common);
435 } else if (1 == fid) {
436
437 spin_lock(&common->irqlock);
438 if (list_empty(&common->dma_queue) ||
439 (common->cur_frm != common->next_frm)) {
440 spin_unlock(&common->irqlock);
441 continue;
442 }
443 spin_unlock(&common->irqlock);
444
445 vpif_schedule_next_buffer(common);
446 }
447 }
448 }
449 return IRQ_HANDLED;
450}
451
452
453
454
455
456
457
458
459static int vpif_update_std_info(struct channel_obj *ch)
460{
461 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
462 struct vpif_params *vpifparams = &ch->vpifparams;
463 const struct vpif_channel_config_params *config;
464 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
465 struct video_obj *vid_ch = &ch->video;
466 int index;
467 struct v4l2_pix_format *pixfmt = &common->fmt.fmt.pix;
468
469 vpif_dbg(2, debug, "vpif_update_std_info\n");
470
471
472
473
474
475 if (pixfmt->width && pixfmt->height) {
476 if (pixfmt->field == V4L2_FIELD_ANY ||
477 pixfmt->field == V4L2_FIELD_NONE)
478 pixfmt->field = V4L2_FIELD_NONE;
479
480 vpifparams->iface.if_type = VPIF_IF_BT656;
481 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
482 pixfmt->pixelformat == V4L2_PIX_FMT_SBGGR8)
483 vpifparams->iface.if_type = VPIF_IF_RAW_BAYER;
484
485 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10)
486 vpifparams->params.data_sz = 1;
487
488
489
490
491
492 if (vpifparams->iface.if_type == VPIF_IF_RAW_BAYER) {
493 memset(std_info, 0, sizeof(struct vpif_channel_config_params));
494 vpifparams->std_info.capture_format = 1;
495 return 0;
496 }
497 }
498
499 for (index = 0; index < vpif_ch_params_count; index++) {
500 config = &vpif_ch_params[index];
501 if (config->hd_sd == 0) {
502 vpif_dbg(2, debug, "SD format\n");
503 if (config->stdid & vid_ch->stdid) {
504 memcpy(std_info, config, sizeof(*config));
505 break;
506 }
507 } else {
508 vpif_dbg(2, debug, "HD format\n");
509 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
510 sizeof(vid_ch->dv_timings))) {
511 memcpy(std_info, config, sizeof(*config));
512 break;
513 }
514 }
515 }
516
517
518 if (index == vpif_ch_params_count)
519 return -EINVAL;
520
521 common->fmt.fmt.pix.width = std_info->width;
522 common->width = std_info->width;
523 common->fmt.fmt.pix.height = std_info->height;
524 common->height = std_info->height;
525 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
526 common->fmt.fmt.pix.bytesperline = std_info->width;
527 vpifparams->video_params.hpitch = std_info->width;
528 vpifparams->video_params.storage_mode = std_info->frm_fmt;
529
530 if (vid_ch->stdid)
531 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
532 else
533 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
534
535 if (ch->vpifparams.std_info.frm_fmt)
536 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
537 else
538 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
539
540 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
541 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
542 else
543 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV16;
544
545 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
546
547 return 0;
548}
549
550
551
552
553
554
555
556
557static void vpif_calculate_offsets(struct channel_obj *ch)
558{
559 unsigned int hpitch, sizeimage;
560 struct video_obj *vid_ch = &(ch->video);
561 struct vpif_params *vpifparams = &ch->vpifparams;
562 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
563 enum v4l2_field field = common->fmt.fmt.pix.field;
564
565 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
566
567 if (V4L2_FIELD_ANY == field) {
568 if (vpifparams->std_info.frm_fmt)
569 vid_ch->buf_field = V4L2_FIELD_NONE;
570 else
571 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
572 } else
573 vid_ch->buf_field = common->fmt.fmt.pix.field;
574
575 sizeimage = common->fmt.fmt.pix.sizeimage;
576
577 hpitch = common->fmt.fmt.pix.bytesperline;
578
579 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
580 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
581
582 common->ytop_off = 0;
583 common->ybtm_off = hpitch;
584 common->ctop_off = sizeimage / 2;
585 common->cbtm_off = sizeimage / 2 + hpitch;
586 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
587
588 common->ytop_off = 0;
589 common->ybtm_off = sizeimage / 4;
590 common->ctop_off = sizeimage / 2;
591 common->cbtm_off = common->ctop_off + sizeimage / 4;
592 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
593
594 common->ybtm_off = 0;
595 common->ytop_off = sizeimage / 4;
596 common->cbtm_off = sizeimage / 2;
597 common->ctop_off = common->cbtm_off + sizeimage / 4;
598 }
599 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
600 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
601 vpifparams->video_params.storage_mode = 1;
602 else
603 vpifparams->video_params.storage_mode = 0;
604
605 if (1 == vpifparams->std_info.frm_fmt)
606 vpifparams->video_params.hpitch =
607 common->fmt.fmt.pix.bytesperline;
608 else {
609 if ((field == V4L2_FIELD_ANY)
610 || (field == V4L2_FIELD_INTERLACED))
611 vpifparams->video_params.hpitch =
612 common->fmt.fmt.pix.bytesperline * 2;
613 else
614 vpifparams->video_params.hpitch =
615 common->fmt.fmt.pix.bytesperline;
616 }
617
618 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
619}
620
621
622
623
624
625static inline enum v4l2_field vpif_get_default_field(
626 struct vpif_interface *iface)
627{
628 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
629 V4L2_FIELD_INTERLACED;
630}
631
632
633
634
635
636
637static void vpif_config_addr(struct channel_obj *ch, int muxmode)
638{
639 struct common_obj *common;
640
641 vpif_dbg(2, debug, "vpif_config_addr\n");
642
643 common = &(ch->common[VPIF_VIDEO_INDEX]);
644
645 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
646 common->set_addr = ch1_set_videobuf_addr;
647 else if (2 == muxmode)
648 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
649 else
650 common->set_addr = ch0_set_videobuf_addr;
651}
652
653
654
655
656
657
658
659
660
661
662
663static int vpif_input_to_subdev(
664 struct vpif_capture_config *vpif_cfg,
665 struct vpif_capture_chan_config *chan_cfg,
666 int input_index)
667{
668 struct vpif_subdev_info *subdev_info;
669 const char *subdev_name;
670 int i;
671
672 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
673
674 if (!chan_cfg)
675 return -1;
676 if (input_index >= chan_cfg->input_count)
677 return -1;
678 subdev_name = chan_cfg->inputs[input_index].subdev_name;
679 if (!subdev_name)
680 return -1;
681
682
683 for (i = 0; i < vpif_cfg->subdev_count; i++) {
684 subdev_info = &vpif_cfg->subdev_info[i];
685 if (subdev_info && !strcmp(subdev_info->name, subdev_name))
686 return i;
687 }
688 return -1;
689}
690
691
692
693
694
695
696
697
698
699static int vpif_set_input(
700 struct vpif_capture_config *vpif_cfg,
701 struct channel_obj *ch,
702 int index)
703{
704 struct vpif_capture_chan_config *chan_cfg =
705 &vpif_cfg->chan_config[ch->channel_id];
706 struct vpif_subdev_info *subdev_info = NULL;
707 struct v4l2_subdev *sd = NULL;
708 u32 input = 0, output = 0;
709 int sd_index;
710 int ret;
711
712 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
713 if (sd_index >= 0) {
714 sd = vpif_obj.sd[sd_index];
715 subdev_info = &vpif_cfg->subdev_info[sd_index];
716 } else {
717
718 return 0;
719 }
720
721
722 if (sd && vpif_cfg->setup_input_path) {
723 ret = vpif_cfg->setup_input_path(ch->channel_id,
724 subdev_info->name);
725 if (ret < 0) {
726 vpif_dbg(1, debug, "couldn't setup input path for the" \
727 " sub device %s, for input index %d\n",
728 subdev_info->name, index);
729 return ret;
730 }
731 }
732
733 if (sd) {
734 input = chan_cfg->inputs[index].input_route;
735 output = chan_cfg->inputs[index].output_route;
736 ret = v4l2_subdev_call(sd, video, s_routing,
737 input, output, 0);
738 if (ret < 0 && ret != -ENOIOCTLCMD) {
739 vpif_dbg(1, debug, "Failed to set input\n");
740 return ret;
741 }
742 }
743 ch->input_idx = index;
744 ch->sd = sd;
745
746 ch->vpifparams.iface = chan_cfg->vpif_if;
747
748
749 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
750 return 0;
751}
752
753
754
755
756
757
758
759
760
761static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
762{
763 struct video_device *vdev = video_devdata(file);
764 struct channel_obj *ch = video_get_drvdata(vdev);
765 int ret;
766
767 vpif_dbg(2, debug, "vpif_querystd\n");
768
769
770 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
771
772 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
773 return -ENODATA;
774 if (ret) {
775 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
776 return ret;
777 }
778
779 return 0;
780}
781
782
783
784
785
786
787
788static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
789{
790 struct vpif_capture_config *config = vpif_dev->platform_data;
791 struct video_device *vdev = video_devdata(file);
792 struct channel_obj *ch = video_get_drvdata(vdev);
793 struct vpif_capture_chan_config *chan_cfg;
794 struct v4l2_input input;
795
796 vpif_dbg(2, debug, "vpif_g_std\n");
797
798 if (!config->chan_config[ch->channel_id].inputs)
799 return -ENODATA;
800
801 chan_cfg = &config->chan_config[ch->channel_id];
802 input = chan_cfg->inputs[ch->input_idx].input;
803 if (input.capabilities != V4L2_IN_CAP_STD)
804 return -ENODATA;
805
806 *std = ch->video.stdid;
807 return 0;
808}
809
810
811
812
813
814
815
816static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
817{
818 struct vpif_capture_config *config = vpif_dev->platform_data;
819 struct video_device *vdev = video_devdata(file);
820 struct channel_obj *ch = video_get_drvdata(vdev);
821 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
822 struct vpif_capture_chan_config *chan_cfg;
823 struct v4l2_input input;
824 int ret;
825
826 vpif_dbg(2, debug, "vpif_s_std\n");
827
828 if (!config->chan_config[ch->channel_id].inputs)
829 return -ENODATA;
830
831 chan_cfg = &config->chan_config[ch->channel_id];
832 input = chan_cfg->inputs[ch->input_idx].input;
833 if (input.capabilities != V4L2_IN_CAP_STD)
834 return -ENODATA;
835
836 if (vb2_is_busy(&common->buffer_queue))
837 return -EBUSY;
838
839
840 ch->video.stdid = std_id;
841 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
842
843
844 if (vpif_update_std_info(ch)) {
845 vpif_err("Error getting the standard info\n");
846 return -EINVAL;
847 }
848
849
850 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
851 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
852 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
853 return ret;
854 }
855 return 0;
856}
857
858
859
860
861
862
863
864static int vpif_enum_input(struct file *file, void *priv,
865 struct v4l2_input *input)
866{
867
868 struct vpif_capture_config *config = vpif_dev->platform_data;
869 struct video_device *vdev = video_devdata(file);
870 struct channel_obj *ch = video_get_drvdata(vdev);
871 struct vpif_capture_chan_config *chan_cfg;
872
873 chan_cfg = &config->chan_config[ch->channel_id];
874
875 if (input->index >= chan_cfg->input_count)
876 return -EINVAL;
877
878 memcpy(input, &chan_cfg->inputs[input->index].input,
879 sizeof(*input));
880 return 0;
881}
882
883
884
885
886
887
888
889static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
890{
891 struct video_device *vdev = video_devdata(file);
892 struct channel_obj *ch = video_get_drvdata(vdev);
893
894 *index = ch->input_idx;
895 return 0;
896}
897
898
899
900
901
902
903
904static int vpif_s_input(struct file *file, void *priv, unsigned int index)
905{
906 struct vpif_capture_config *config = vpif_dev->platform_data;
907 struct video_device *vdev = video_devdata(file);
908 struct channel_obj *ch = video_get_drvdata(vdev);
909 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
910 struct vpif_capture_chan_config *chan_cfg;
911
912 chan_cfg = &config->chan_config[ch->channel_id];
913
914 if (index >= chan_cfg->input_count)
915 return -EINVAL;
916
917 if (vb2_is_busy(&common->buffer_queue))
918 return -EBUSY;
919
920 return vpif_set_input(config, ch, index);
921}
922
923
924
925
926
927
928
929static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_fmtdesc *fmt)
931{
932 struct video_device *vdev = video_devdata(file);
933 struct channel_obj *ch = video_get_drvdata(vdev);
934
935 if (fmt->index != 0) {
936 vpif_dbg(1, debug, "Invalid format index\n");
937 return -EINVAL;
938 }
939
940
941 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
942 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
943 strscpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb",
944 sizeof(fmt->description));
945 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
946 } else {
947 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
948 strscpy(fmt->description, "YCbCr4:2:2 Semi-Planar",
949 sizeof(fmt->description));
950 fmt->pixelformat = V4L2_PIX_FMT_NV16;
951 }
952 return 0;
953}
954
955
956
957
958
959
960
961static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
962 struct v4l2_format *fmt)
963{
964 struct video_device *vdev = video_devdata(file);
965 struct channel_obj *ch = video_get_drvdata(vdev);
966 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
967 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
968
969 common->fmt = *fmt;
970 vpif_update_std_info(ch);
971
972 pixfmt->field = common->fmt.fmt.pix.field;
973 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
974 pixfmt->bytesperline = common->fmt.fmt.pix.width;
975 pixfmt->width = common->fmt.fmt.pix.width;
976 pixfmt->height = common->fmt.fmt.pix.height;
977 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
978 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) {
979 pixfmt->bytesperline = common->fmt.fmt.pix.width * 2;
980 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
981 }
982 pixfmt->priv = 0;
983
984 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d pixelformat=0x%08x, field=%d, size=%d\n", __func__,
985 pixfmt->width, pixfmt->height,
986 pixfmt->bytesperline, pixfmt->pixelformat,
987 pixfmt->field, pixfmt->sizeimage);
988
989 return 0;
990}
991
992
993
994
995
996
997
998
999static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1000 struct v4l2_format *fmt)
1001{
1002 struct video_device *vdev = video_devdata(file);
1003 struct channel_obj *ch = video_get_drvdata(vdev);
1004 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1005 struct v4l2_pix_format *pix_fmt = &fmt->fmt.pix;
1006 struct v4l2_subdev_format format = {
1007 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1008 };
1009 struct v4l2_mbus_framefmt *mbus_fmt = &format.format;
1010 int ret;
1011
1012
1013 if (common->fmt.type != fmt->type)
1014 return -EINVAL;
1015
1016
1017 *fmt = common->fmt;
1018
1019
1020 ret = v4l2_subdev_call(ch->sd, pad, get_fmt, NULL, &format);
1021 if (!ret && mbus_fmt->code) {
1022 v4l2_fill_pix_format(pix_fmt, mbus_fmt);
1023 pix_fmt->bytesperline = pix_fmt->width;
1024 if (mbus_fmt->code == MEDIA_BUS_FMT_SGRBG10_1X10) {
1025
1026 pix_fmt->pixelformat = V4L2_PIX_FMT_SGRBG10;
1027 pix_fmt->bytesperline = pix_fmt->width * 2;
1028 } else if (mbus_fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
1029
1030 pix_fmt->pixelformat = V4L2_PIX_FMT_NV16;
1031 pix_fmt->bytesperline = pix_fmt->width * 2;
1032 } else {
1033 dev_warn(vpif_dev, "%s: Unhandled media-bus format 0x%x\n",
1034 __func__, mbus_fmt->code);
1035 }
1036 pix_fmt->sizeimage = pix_fmt->bytesperline * pix_fmt->height;
1037 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d, pixelformat=0x%08x, code=0x%x, field=%d, size=%d\n", __func__,
1038 pix_fmt->width, pix_fmt->height,
1039 pix_fmt->bytesperline, pix_fmt->pixelformat,
1040 mbus_fmt->code, pix_fmt->field, pix_fmt->sizeimage);
1041
1042 common->fmt = *fmt;
1043 vpif_update_std_info(ch);
1044 }
1045
1046 return 0;
1047}
1048
1049
1050
1051
1052
1053
1054
1055static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1056 struct v4l2_format *fmt)
1057{
1058 struct video_device *vdev = video_devdata(file);
1059 struct channel_obj *ch = video_get_drvdata(vdev);
1060 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1061 int ret;
1062
1063 vpif_dbg(2, debug, "%s\n", __func__);
1064
1065 if (vb2_is_busy(&common->buffer_queue))
1066 return -EBUSY;
1067
1068 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1069 if (ret)
1070 return ret;
1071
1072
1073 common->fmt = *fmt;
1074 return 0;
1075}
1076
1077
1078
1079
1080
1081
1082
1083static int vpif_querycap(struct file *file, void *priv,
1084 struct v4l2_capability *cap)
1085{
1086 struct vpif_capture_config *config = vpif_dev->platform_data;
1087
1088 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1089 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1090 strscpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1091 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1092 dev_name(vpif_dev));
1093 strscpy(cap->card, config->card_name, sizeof(cap->card));
1094
1095 return 0;
1096}
1097
1098
1099
1100
1101
1102
1103
1104static int
1105vpif_enum_dv_timings(struct file *file, void *priv,
1106 struct v4l2_enum_dv_timings *timings)
1107{
1108 struct vpif_capture_config *config = vpif_dev->platform_data;
1109 struct video_device *vdev = video_devdata(file);
1110 struct channel_obj *ch = video_get_drvdata(vdev);
1111 struct vpif_capture_chan_config *chan_cfg;
1112 struct v4l2_input input;
1113 int ret;
1114
1115 if (!config->chan_config[ch->channel_id].inputs)
1116 return -ENODATA;
1117
1118 chan_cfg = &config->chan_config[ch->channel_id];
1119 input = chan_cfg->inputs[ch->input_idx].input;
1120 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1121 return -ENODATA;
1122
1123 timings->pad = 0;
1124
1125 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1126 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1127 return -EINVAL;
1128
1129 return ret;
1130}
1131
1132
1133
1134
1135
1136
1137
1138static int
1139vpif_query_dv_timings(struct file *file, void *priv,
1140 struct v4l2_dv_timings *timings)
1141{
1142 struct vpif_capture_config *config = vpif_dev->platform_data;
1143 struct video_device *vdev = video_devdata(file);
1144 struct channel_obj *ch = video_get_drvdata(vdev);
1145 struct vpif_capture_chan_config *chan_cfg;
1146 struct v4l2_input input;
1147 int ret;
1148
1149 if (!config->chan_config[ch->channel_id].inputs)
1150 return -ENODATA;
1151
1152 chan_cfg = &config->chan_config[ch->channel_id];
1153 input = chan_cfg->inputs[ch->input_idx].input;
1154 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1155 return -ENODATA;
1156
1157 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1158 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1159 return -ENODATA;
1160
1161 return ret;
1162}
1163
1164
1165
1166
1167
1168
1169
1170static int vpif_s_dv_timings(struct file *file, void *priv,
1171 struct v4l2_dv_timings *timings)
1172{
1173 struct vpif_capture_config *config = vpif_dev->platform_data;
1174 struct video_device *vdev = video_devdata(file);
1175 struct channel_obj *ch = video_get_drvdata(vdev);
1176 struct vpif_params *vpifparams = &ch->vpifparams;
1177 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1178 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1179 struct video_obj *vid_ch = &ch->video;
1180 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1181 struct vpif_capture_chan_config *chan_cfg;
1182 struct v4l2_input input;
1183 int ret;
1184
1185 if (!config->chan_config[ch->channel_id].inputs)
1186 return -ENODATA;
1187
1188 chan_cfg = &config->chan_config[ch->channel_id];
1189 input = chan_cfg->inputs[ch->input_idx].input;
1190 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1191 return -ENODATA;
1192
1193 if (timings->type != V4L2_DV_BT_656_1120) {
1194 vpif_dbg(2, debug, "Timing type not defined\n");
1195 return -EINVAL;
1196 }
1197
1198 if (vb2_is_busy(&common->buffer_queue))
1199 return -EBUSY;
1200
1201
1202 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1203 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1204 ret = 0;
1205 if (ret < 0) {
1206 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1207 return ret;
1208 }
1209
1210 if (!(timings->bt.width && timings->bt.height &&
1211 (timings->bt.hbackporch ||
1212 timings->bt.hfrontporch ||
1213 timings->bt.hsync) &&
1214 timings->bt.vfrontporch &&
1215 (timings->bt.vbackporch ||
1216 timings->bt.vsync))) {
1217 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
1218 return -EINVAL;
1219 }
1220
1221 vid_ch->dv_timings = *timings;
1222
1223
1224
1225 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1226 std_info->sav2eav = bt->width;
1227
1228 std_info->l1 = 1;
1229 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1230
1231 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1232 if (bt->interlaced) {
1233 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1234 std_info->l5 = std_info->vsize/2 -
1235 (bt->vfrontporch - 1);
1236 std_info->l7 = std_info->vsize/2 + 1;
1237 std_info->l9 = std_info->l7 + bt->il_vsync +
1238 bt->il_vbackporch + 1;
1239 std_info->l11 = std_info->vsize -
1240 (bt->il_vfrontporch - 1);
1241 } else {
1242 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
1243 return -EINVAL;
1244 }
1245 } else {
1246 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1247 }
1248 strscpy(std_info->name, "Custom timings BT656/1120",
1249 sizeof(std_info->name));
1250 std_info->width = bt->width;
1251 std_info->height = bt->height;
1252 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1253 std_info->ycmux_mode = 0;
1254 std_info->capture_format = 0;
1255 std_info->vbi_supported = 0;
1256 std_info->hd_sd = 1;
1257 std_info->stdid = 0;
1258
1259 vid_ch->stdid = 0;
1260 return 0;
1261}
1262
1263
1264
1265
1266
1267
1268
1269static int vpif_g_dv_timings(struct file *file, void *priv,
1270 struct v4l2_dv_timings *timings)
1271{
1272 struct vpif_capture_config *config = vpif_dev->platform_data;
1273 struct video_device *vdev = video_devdata(file);
1274 struct channel_obj *ch = video_get_drvdata(vdev);
1275 struct video_obj *vid_ch = &ch->video;
1276 struct vpif_capture_chan_config *chan_cfg;
1277 struct v4l2_input input;
1278
1279 if (!config->chan_config[ch->channel_id].inputs)
1280 return -ENODATA;
1281
1282 chan_cfg = &config->chan_config[ch->channel_id];
1283 input = chan_cfg->inputs[ch->input_idx].input;
1284 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1285 return -ENODATA;
1286
1287 *timings = vid_ch->dv_timings;
1288
1289 return 0;
1290}
1291
1292
1293
1294
1295
1296
1297
1298
1299static int vpif_log_status(struct file *filep, void *priv)
1300{
1301
1302 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1303
1304 return 0;
1305}
1306
1307
1308static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1309 .vidioc_querycap = vpif_querycap,
1310 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1311 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1312 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1313 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1314
1315 .vidioc_enum_input = vpif_enum_input,
1316 .vidioc_s_input = vpif_s_input,
1317 .vidioc_g_input = vpif_g_input,
1318
1319 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1320 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1321 .vidioc_querybuf = vb2_ioctl_querybuf,
1322 .vidioc_qbuf = vb2_ioctl_qbuf,
1323 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1324 .vidioc_expbuf = vb2_ioctl_expbuf,
1325 .vidioc_streamon = vb2_ioctl_streamon,
1326 .vidioc_streamoff = vb2_ioctl_streamoff,
1327
1328 .vidioc_querystd = vpif_querystd,
1329 .vidioc_s_std = vpif_s_std,
1330 .vidioc_g_std = vpif_g_std,
1331
1332 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1333 .vidioc_query_dv_timings = vpif_query_dv_timings,
1334 .vidioc_s_dv_timings = vpif_s_dv_timings,
1335 .vidioc_g_dv_timings = vpif_g_dv_timings,
1336
1337 .vidioc_log_status = vpif_log_status,
1338};
1339
1340
1341static const struct v4l2_file_operations vpif_fops = {
1342 .owner = THIS_MODULE,
1343 .open = v4l2_fh_open,
1344 .release = vb2_fop_release,
1345 .unlocked_ioctl = video_ioctl2,
1346 .mmap = vb2_fop_mmap,
1347 .poll = vb2_fop_poll
1348};
1349
1350
1351
1352
1353
1354
1355static int initialize_vpif(void)
1356{
1357 int err, i, j;
1358 int free_channel_objects_index;
1359
1360
1361 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1362 vpif_obj.dev[i] =
1363 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1364
1365 if (!vpif_obj.dev[i]) {
1366 free_channel_objects_index = i;
1367 err = -ENOMEM;
1368 goto vpif_init_free_channel_objects;
1369 }
1370 }
1371 return 0;
1372
1373vpif_init_free_channel_objects:
1374 for (j = 0; j < free_channel_objects_index; j++)
1375 kfree(vpif_obj.dev[j]);
1376 return err;
1377}
1378
1379static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1380 struct v4l2_subdev *subdev,
1381 struct v4l2_async_subdev *asd)
1382{
1383 int i;
1384
1385 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
1386 struct v4l2_async_subdev *_asd = vpif_obj.config->asd[i];
1387 const struct fwnode_handle *fwnode = _asd->match.fwnode;
1388
1389 if (fwnode == subdev->fwnode) {
1390 vpif_obj.sd[i] = subdev;
1391 vpif_obj.config->chan_config->inputs[i].subdev_name =
1392 (char *)to_of_node(subdev->fwnode)->full_name;
1393 vpif_dbg(2, debug,
1394 "%s: setting input %d subdev_name = %s\n",
1395 __func__, i,
1396 vpif_obj.config->chan_config->inputs[i].subdev_name);
1397 return 0;
1398 }
1399 }
1400
1401 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1402 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1403 subdev->name)) {
1404 vpif_obj.sd[i] = subdev;
1405 return 0;
1406 }
1407
1408 return -EINVAL;
1409}
1410
1411static int vpif_probe_complete(void)
1412{
1413 struct common_obj *common;
1414 struct video_device *vdev;
1415 struct channel_obj *ch;
1416 struct vb2_queue *q;
1417 int j, err, k;
1418
1419 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1420 ch = vpif_obj.dev[j];
1421 ch->channel_id = j;
1422 common = &(ch->common[VPIF_VIDEO_INDEX]);
1423 spin_lock_init(&common->irqlock);
1424 mutex_init(&common->lock);
1425
1426
1427 err = vpif_set_input(vpif_obj.config, ch, 0);
1428 if (err)
1429 goto probe_out;
1430
1431
1432 ch->video.stdid = V4L2_STD_525_60;
1433 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1434 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1435 vpif_update_std_info(ch);
1436
1437
1438 q = &common->buffer_queue;
1439 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1440 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1441 q->drv_priv = ch;
1442 q->ops = &video_qops;
1443 q->mem_ops = &vb2_dma_contig_memops;
1444 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1445 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1446 q->min_buffers_needed = 1;
1447 q->lock = &common->lock;
1448 q->dev = vpif_dev;
1449
1450 err = vb2_queue_init(q);
1451 if (err) {
1452 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1453 goto probe_out;
1454 }
1455
1456 INIT_LIST_HEAD(&common->dma_queue);
1457
1458
1459 vdev = &ch->video_dev;
1460 strscpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1461 vdev->release = video_device_release_empty;
1462 vdev->fops = &vpif_fops;
1463 vdev->ioctl_ops = &vpif_ioctl_ops;
1464 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1465 vdev->vfl_dir = VFL_DIR_RX;
1466 vdev->queue = q;
1467 vdev->lock = &common->lock;
1468 video_set_drvdata(&ch->video_dev, ch);
1469 err = video_register_device(vdev,
1470 VFL_TYPE_GRABBER, (j ? 1 : 0));
1471 if (err)
1472 goto probe_out;
1473 }
1474
1475 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1476 return 0;
1477
1478probe_out:
1479 for (k = 0; k < j; k++) {
1480
1481 ch = vpif_obj.dev[k];
1482 common = &ch->common[k];
1483
1484 video_unregister_device(&ch->video_dev);
1485 }
1486 kfree(vpif_obj.sd);
1487 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1488
1489 return err;
1490}
1491
1492static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1493{
1494 return vpif_probe_complete();
1495}
1496
1497static const struct v4l2_async_notifier_operations vpif_async_ops = {
1498 .bound = vpif_async_bound,
1499 .complete = vpif_async_complete,
1500};
1501
1502static struct vpif_capture_config *
1503vpif_capture_get_pdata(struct platform_device *pdev)
1504{
1505 struct device_node *endpoint = NULL;
1506 struct vpif_capture_config *pdata;
1507 struct vpif_subdev_info *sdinfo;
1508 struct vpif_capture_chan_config *chan;
1509 unsigned int i;
1510
1511 v4l2_async_notifier_init(&vpif_obj.notifier);
1512
1513
1514
1515
1516
1517 if (pdev->dev.parent && pdev->dev.parent->of_node)
1518 pdev->dev.of_node = pdev->dev.parent->of_node;
1519 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
1520 return pdev->dev.platform_data;
1521
1522 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1523 if (!pdata)
1524 return NULL;
1525 pdata->subdev_info =
1526 devm_kcalloc(&pdev->dev,
1527 VPIF_CAPTURE_NUM_CHANNELS,
1528 sizeof(*pdata->subdev_info),
1529 GFP_KERNEL);
1530
1531 if (!pdata->subdev_info)
1532 return NULL;
1533
1534 for (i = 0; i < VPIF_CAPTURE_NUM_CHANNELS; i++) {
1535 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
1536 struct device_node *rem;
1537 unsigned int flags;
1538 int err;
1539
1540 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
1541 endpoint);
1542 if (!endpoint)
1543 break;
1544
1545 rem = of_graph_get_remote_port_parent(endpoint);
1546 if (!rem) {
1547 dev_dbg(&pdev->dev, "Remote device at %pOF not found\n",
1548 endpoint);
1549 of_node_put(endpoint);
1550 goto done;
1551 }
1552
1553 sdinfo = &pdata->subdev_info[i];
1554 chan = &pdata->chan_config[i];
1555 chan->inputs = devm_kcalloc(&pdev->dev,
1556 VPIF_CAPTURE_NUM_CHANNELS,
1557 sizeof(*chan->inputs),
1558 GFP_KERNEL);
1559 if (!chan->inputs) {
1560 of_node_put(rem);
1561 of_node_put(endpoint);
1562 goto err_cleanup;
1563 }
1564
1565 chan->input_count++;
1566 chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA;
1567 chan->inputs[i].input.std = V4L2_STD_ALL;
1568 chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD;
1569
1570 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1571 &bus_cfg);
1572 of_node_put(endpoint);
1573 if (err) {
1574 dev_err(&pdev->dev, "Could not parse the endpoint\n");
1575 of_node_put(rem);
1576 goto done;
1577 }
1578
1579 dev_dbg(&pdev->dev, "Endpoint %pOF, bus_width = %d\n",
1580 endpoint, bus_cfg.bus.parallel.bus_width);
1581
1582 flags = bus_cfg.bus.parallel.flags;
1583
1584 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1585 chan->vpif_if.hd_pol = 1;
1586
1587 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1588 chan->vpif_if.vd_pol = 1;
1589
1590 dev_dbg(&pdev->dev, "Remote device %pOF found\n", rem);
1591 sdinfo->name = rem->full_name;
1592
1593 pdata->asd[i] = v4l2_async_notifier_add_fwnode_subdev(
1594 &vpif_obj.notifier, of_fwnode_handle(rem),
1595 sizeof(struct v4l2_async_subdev));
1596 if (IS_ERR(pdata->asd[i])) {
1597 of_node_put(rem);
1598 goto err_cleanup;
1599 }
1600 }
1601
1602done:
1603 pdata->asd_sizes[0] = i;
1604 pdata->subdev_count = i;
1605 pdata->card_name = "DA850/OMAP-L138 Video Capture";
1606
1607 return pdata;
1608
1609err_cleanup:
1610 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1611
1612 return NULL;
1613}
1614
1615
1616
1617
1618
1619
1620
1621
1622static __init int vpif_probe(struct platform_device *pdev)
1623{
1624 struct vpif_subdev_info *subdevdata;
1625 struct i2c_adapter *i2c_adap;
1626 struct resource *res;
1627 int subdev_count;
1628 int res_idx = 0;
1629 int i, err;
1630
1631 pdev->dev.platform_data = vpif_capture_get_pdata(pdev);
1632 if (!pdev->dev.platform_data) {
1633 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1634 return -EINVAL;
1635 }
1636
1637 vpif_dev = &pdev->dev;
1638
1639 err = initialize_vpif();
1640 if (err) {
1641 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1642 goto cleanup;
1643 }
1644
1645 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1646 if (err) {
1647 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1648 goto cleanup;
1649 }
1650
1651 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1652 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1653 IRQF_SHARED, VPIF_DRIVER_NAME,
1654 (void *)(&vpif_obj.dev[res_idx]->
1655 channel_id));
1656 if (err) {
1657 err = -EINVAL;
1658 goto vpif_unregister;
1659 }
1660 res_idx++;
1661 }
1662
1663 vpif_obj.config = pdev->dev.platform_data;
1664
1665 subdev_count = vpif_obj.config->subdev_count;
1666 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
1667 if (!vpif_obj.sd) {
1668 err = -ENOMEM;
1669 goto vpif_unregister;
1670 }
1671
1672 if (!vpif_obj.config->asd_sizes[0]) {
1673 int i2c_id = vpif_obj.config->i2c_adapter_id;
1674
1675 i2c_adap = i2c_get_adapter(i2c_id);
1676 WARN_ON(!i2c_adap);
1677 for (i = 0; i < subdev_count; i++) {
1678 subdevdata = &vpif_obj.config->subdev_info[i];
1679 vpif_obj.sd[i] =
1680 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1681 i2c_adap,
1682 &subdevdata->
1683 board_info,
1684 NULL);
1685
1686 if (!vpif_obj.sd[i]) {
1687 vpif_err("Error registering v4l2 subdevice\n");
1688 err = -ENODEV;
1689 goto probe_subdev_out;
1690 }
1691 v4l2_info(&vpif_obj.v4l2_dev,
1692 "registered sub device %s\n",
1693 subdevdata->name);
1694 }
1695 vpif_probe_complete();
1696 } else {
1697 vpif_obj.notifier.ops = &vpif_async_ops;
1698 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1699 &vpif_obj.notifier);
1700 if (err) {
1701 vpif_err("Error registering async notifier\n");
1702 err = -EINVAL;
1703 goto probe_subdev_out;
1704 }
1705 }
1706
1707 return 0;
1708
1709probe_subdev_out:
1710
1711 kfree(vpif_obj.sd);
1712vpif_unregister:
1713 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1714cleanup:
1715 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1716
1717 return err;
1718}
1719
1720
1721
1722
1723
1724
1725
1726static int vpif_remove(struct platform_device *device)
1727{
1728 struct channel_obj *ch;
1729 int i;
1730
1731 v4l2_async_notifier_unregister(&vpif_obj.notifier);
1732 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1733 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1734
1735 kfree(vpif_obj.sd);
1736
1737 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1738
1739 ch = vpif_obj.dev[i];
1740
1741 video_unregister_device(&ch->video_dev);
1742 kfree(vpif_obj.dev[i]);
1743 }
1744 return 0;
1745}
1746
1747#ifdef CONFIG_PM_SLEEP
1748
1749
1750
1751
1752static int vpif_suspend(struct device *dev)
1753{
1754
1755 struct common_obj *common;
1756 struct channel_obj *ch;
1757 int i;
1758
1759 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1760
1761 ch = vpif_obj.dev[i];
1762 common = &ch->common[VPIF_VIDEO_INDEX];
1763
1764 if (!vb2_start_streaming_called(&common->buffer_queue))
1765 continue;
1766
1767 mutex_lock(&common->lock);
1768
1769 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1770 enable_channel0(0);
1771 channel0_intr_enable(0);
1772 }
1773 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1774 ycmux_mode == 2) {
1775 enable_channel1(0);
1776 channel1_intr_enable(0);
1777 }
1778 mutex_unlock(&common->lock);
1779 }
1780
1781 return 0;
1782}
1783
1784
1785
1786
1787static int vpif_resume(struct device *dev)
1788{
1789 struct common_obj *common;
1790 struct channel_obj *ch;
1791 int i;
1792
1793 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1794
1795 ch = vpif_obj.dev[i];
1796 common = &ch->common[VPIF_VIDEO_INDEX];
1797
1798 if (!vb2_start_streaming_called(&common->buffer_queue))
1799 continue;
1800
1801 mutex_lock(&common->lock);
1802
1803 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1804 enable_channel0(1);
1805 channel0_intr_enable(1);
1806 }
1807 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1808 ycmux_mode == 2) {
1809 enable_channel1(1);
1810 channel1_intr_enable(1);
1811 }
1812 mutex_unlock(&common->lock);
1813 }
1814
1815 return 0;
1816}
1817#endif
1818
1819static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1820
1821static __refdata struct platform_driver vpif_driver = {
1822 .driver = {
1823 .name = VPIF_DRIVER_NAME,
1824 .pm = &vpif_pm_ops,
1825 },
1826 .probe = vpif_probe,
1827 .remove = vpif_remove,
1828};
1829
1830module_platform_driver(vpif_driver);
1831