2 * Analog Devices video capture driver
4 * Copyright (c) 2011 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/types.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/videobuf2-dma-contig.h>
39 #include <media/blackfin/bfin_capture.h>
40 #include <media/blackfin/ppi.h>
42 #define CAPTURE_DRV_NAME "bfin_capture"
48 int bpp
; /* bits per pixel */
49 int dlen
; /* data length for ppi in bits */
53 struct vb2_v4l2_buffer vb
;
54 struct list_head list
;
58 /* capture device instance */
59 struct v4l2_device v4l2_dev
;
60 /* v4l2 control handler */
61 struct v4l2_ctrl_handler ctrl_handler
;
62 /* device node data */
63 struct video_device video_dev
;
64 /* sub device instance */
65 struct v4l2_subdev
*sd
;
67 struct bfin_capture_config
*cfg
;
71 unsigned int cur_input
;
72 /* current selected standard */
74 /* current selected dv_timings */
75 struct v4l2_dv_timings dv_timings
;
76 /* used to store pixel format */
77 struct v4l2_pix_format fmt
;
80 /* data length for ppi in bits */
82 /* used to store sensor supported format */
83 struct bcap_format
*sensor_formats
;
84 /* number of sensor formats array */
85 int num_sensor_formats
;
86 /* pointing to current video buffer */
87 struct bcap_buffer
*cur_frm
;
88 /* buffer queue used in videobuf2 */
89 struct vb2_queue buffer_queue
;
90 /* queue of filled frames */
91 struct list_head dma_queue
;
92 /* used in videobuf2 callback */
94 /* used to access capture device */
96 /* used to wait ppi to complete one transfer */
97 struct completion comp
;
100 /* vb2 buffer sequence counter */
104 static const struct bcap_format bcap_formats
[] = {
106 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
107 .pixelformat
= V4L2_PIX_FMT_UYVY
,
108 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
113 .desc
= "YCbCr 4:2:2 Interleaved YUYV",
114 .pixelformat
= V4L2_PIX_FMT_YUYV
,
115 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
120 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
121 .pixelformat
= V4L2_PIX_FMT_UYVY
,
122 .mbus_code
= MEDIA_BUS_FMT_UYVY8_1X16
,
128 .pixelformat
= V4L2_PIX_FMT_RGB565
,
129 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
135 .pixelformat
= V4L2_PIX_FMT_RGB444
,
136 .mbus_code
= MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE
,
142 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
144 static irqreturn_t
bcap_isr(int irq
, void *dev_id
);
146 static struct bcap_buffer
*to_bcap_vb(struct vb2_v4l2_buffer
*vb
)
148 return container_of(vb
, struct bcap_buffer
, vb
);
151 static int bcap_init_sensor_formats(struct bcap_device
*bcap_dev
)
153 struct v4l2_subdev_mbus_code_enum code
= {
154 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
156 struct bcap_format
*sf
;
157 unsigned int num_formats
= 0;
160 while (!v4l2_subdev_call(bcap_dev
->sd
, pad
,
161 enum_mbus_code
, NULL
, &code
)) {
168 sf
= kcalloc(num_formats
, sizeof(*sf
), GFP_KERNEL
);
172 for (i
= 0; i
< num_formats
; i
++) {
174 v4l2_subdev_call(bcap_dev
->sd
, pad
,
175 enum_mbus_code
, NULL
, &code
);
176 for (j
= 0; j
< BCAP_MAX_FMTS
; j
++)
177 if (code
.code
== bcap_formats
[j
].mbus_code
)
179 if (j
== BCAP_MAX_FMTS
) {
180 /* we don't allow this sensor working with our bridge */
184 sf
[i
] = bcap_formats
[j
];
186 bcap_dev
->sensor_formats
= sf
;
187 bcap_dev
->num_sensor_formats
= num_formats
;
191 static void bcap_free_sensor_formats(struct bcap_device
*bcap_dev
)
193 bcap_dev
->num_sensor_formats
= 0;
194 kfree(bcap_dev
->sensor_formats
);
195 bcap_dev
->sensor_formats
= NULL
;
198 static int bcap_queue_setup(struct vb2_queue
*vq
,
199 unsigned int *nbuffers
, unsigned int *nplanes
,
200 unsigned int sizes
[], struct device
*alloc_devs
[])
202 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
204 if (vq
->num_buffers
+ *nbuffers
< 2)
208 return sizes
[0] < bcap_dev
->fmt
.sizeimage
? -EINVAL
: 0;
211 sizes
[0] = bcap_dev
->fmt
.sizeimage
;
216 static int bcap_buffer_prepare(struct vb2_buffer
*vb
)
218 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
219 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
220 unsigned long size
= bcap_dev
->fmt
.sizeimage
;
222 if (vb2_plane_size(vb
, 0) < size
) {
223 v4l2_err(&bcap_dev
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
224 vb2_plane_size(vb
, 0), size
);
227 vb2_set_plane_payload(vb
, 0, size
);
229 vbuf
->field
= bcap_dev
->fmt
.field
;
234 static void bcap_buffer_queue(struct vb2_buffer
*vb
)
236 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
237 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
238 struct bcap_buffer
*buf
= to_bcap_vb(vbuf
);
241 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
242 list_add_tail(&buf
->list
, &bcap_dev
->dma_queue
);
243 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
246 static void bcap_buffer_cleanup(struct vb2_buffer
*vb
)
248 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
249 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
250 struct bcap_buffer
*buf
= to_bcap_vb(vbuf
);
253 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
254 list_del_init(&buf
->list
);
255 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
258 static int bcap_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
260 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
261 struct ppi_if
*ppi
= bcap_dev
->ppi
;
262 struct bcap_buffer
*buf
, *tmp
;
263 struct ppi_params params
;
267 /* enable streamon on the sub device */
268 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 1);
269 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
270 v4l2_err(&bcap_dev
->v4l2_dev
, "stream on failed in subdev\n");
275 params
.width
= bcap_dev
->fmt
.width
;
276 params
.height
= bcap_dev
->fmt
.height
;
277 params
.bpp
= bcap_dev
->bpp
;
278 params
.dlen
= bcap_dev
->dlen
;
279 params
.ppi_control
= bcap_dev
->cfg
->ppi_control
;
280 params
.int_mask
= bcap_dev
->cfg
->int_mask
;
281 if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
282 & V4L2_IN_CAP_DV_TIMINGS
) {
283 struct v4l2_bt_timings
*bt
= &bcap_dev
->dv_timings
.bt
;
285 params
.hdelay
= bt
->hsync
+ bt
->hbackporch
;
286 params
.vdelay
= bt
->vsync
+ bt
->vbackporch
;
287 params
.line
= V4L2_DV_BT_FRAME_WIDTH(bt
);
288 params
.frame
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
289 } else if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
293 if (bcap_dev
->std
& V4L2_STD_525_60
) {
303 params
.line
= params
.width
+ bcap_dev
->cfg
->blank_pixels
;
304 params
.frame
= params
.height
;
306 ret
= ppi
->ops
->set_params(ppi
, ¶ms
);
308 v4l2_err(&bcap_dev
->v4l2_dev
,
309 "Error in setting ppi params\n");
313 /* attach ppi DMA irq handler */
314 ret
= ppi
->ops
->attach_irq(ppi
, bcap_isr
);
316 v4l2_err(&bcap_dev
->v4l2_dev
,
317 "Error in attaching interrupt handler\n");
321 bcap_dev
->sequence
= 0;
323 reinit_completion(&bcap_dev
->comp
);
324 bcap_dev
->stop
= false;
326 /* get the next frame from the dma queue */
327 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
328 struct bcap_buffer
, list
);
329 /* remove buffer from the dma queue */
330 list_del_init(&bcap_dev
->cur_frm
->list
);
331 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
333 /* update DMA address */
334 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
336 ppi
->ops
->start(ppi
);
341 list_for_each_entry_safe(buf
, tmp
, &bcap_dev
->dma_queue
, list
) {
342 list_del(&buf
->list
);
343 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
349 static void bcap_stop_streaming(struct vb2_queue
*vq
)
351 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
352 struct ppi_if
*ppi
= bcap_dev
->ppi
;
355 bcap_dev
->stop
= true;
356 wait_for_completion(&bcap_dev
->comp
);
358 ppi
->ops
->detach_irq(ppi
);
359 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 0);
360 if (ret
&& (ret
!= -ENOIOCTLCMD
))
361 v4l2_err(&bcap_dev
->v4l2_dev
,
362 "stream off failed in subdev\n");
364 /* release all active buffers */
365 if (bcap_dev
->cur_frm
)
366 vb2_buffer_done(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
367 VB2_BUF_STATE_ERROR
);
369 while (!list_empty(&bcap_dev
->dma_queue
)) {
370 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
371 struct bcap_buffer
, list
);
372 list_del_init(&bcap_dev
->cur_frm
->list
);
373 vb2_buffer_done(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
374 VB2_BUF_STATE_ERROR
);
378 static struct vb2_ops bcap_video_qops
= {
379 .queue_setup
= bcap_queue_setup
,
380 .buf_prepare
= bcap_buffer_prepare
,
381 .buf_cleanup
= bcap_buffer_cleanup
,
382 .buf_queue
= bcap_buffer_queue
,
383 .wait_prepare
= vb2_ops_wait_prepare
,
384 .wait_finish
= vb2_ops_wait_finish
,
385 .start_streaming
= bcap_start_streaming
,
386 .stop_streaming
= bcap_stop_streaming
,
389 static irqreturn_t
bcap_isr(int irq
, void *dev_id
)
391 struct ppi_if
*ppi
= dev_id
;
392 struct bcap_device
*bcap_dev
= ppi
->priv
;
393 struct vb2_v4l2_buffer
*vbuf
= &bcap_dev
->cur_frm
->vb
;
394 struct vb2_buffer
*vb
= &vbuf
->vb2_buf
;
397 spin_lock(&bcap_dev
->lock
);
399 if (!list_empty(&bcap_dev
->dma_queue
)) {
400 vb
->timestamp
= ktime_get_ns();
402 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
405 vbuf
->sequence
= bcap_dev
->sequence
++;
406 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
408 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
409 struct bcap_buffer
, list
);
410 list_del_init(&bcap_dev
->cur_frm
->list
);
412 /* clear error flag, we will get a new frame */
419 if (bcap_dev
->stop
) {
420 complete(&bcap_dev
->comp
);
422 addr
= vb2_dma_contig_plane_dma_addr(
423 &bcap_dev
->cur_frm
->vb
.vb2_buf
, 0);
424 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
425 ppi
->ops
->start(ppi
);
428 spin_unlock(&bcap_dev
->lock
);
433 static int bcap_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
435 struct bcap_device
*bcap_dev
= video_drvdata(file
);
436 struct v4l2_input input
;
438 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
439 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
442 return v4l2_subdev_call(bcap_dev
->sd
, video
, querystd
, std
);
445 static int bcap_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
447 struct bcap_device
*bcap_dev
= video_drvdata(file
);
448 struct v4l2_input input
;
450 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
451 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
454 *std
= bcap_dev
->std
;
458 static int bcap_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
460 struct bcap_device
*bcap_dev
= video_drvdata(file
);
461 struct v4l2_input input
;
464 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
465 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
468 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
471 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_std
, std
);
479 static int bcap_enum_dv_timings(struct file
*file
, void *priv
,
480 struct v4l2_enum_dv_timings
*timings
)
482 struct bcap_device
*bcap_dev
= video_drvdata(file
);
483 struct v4l2_input input
;
485 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
486 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
491 return v4l2_subdev_call(bcap_dev
->sd
, pad
,
492 enum_dv_timings
, timings
);
495 static int bcap_query_dv_timings(struct file
*file
, void *priv
,
496 struct v4l2_dv_timings
*timings
)
498 struct bcap_device
*bcap_dev
= video_drvdata(file
);
499 struct v4l2_input input
;
501 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
502 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
505 return v4l2_subdev_call(bcap_dev
->sd
, video
,
506 query_dv_timings
, timings
);
509 static int bcap_g_dv_timings(struct file
*file
, void *priv
,
510 struct v4l2_dv_timings
*timings
)
512 struct bcap_device
*bcap_dev
= video_drvdata(file
);
513 struct v4l2_input input
;
515 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
516 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
519 *timings
= bcap_dev
->dv_timings
;
523 static int bcap_s_dv_timings(struct file
*file
, void *priv
,
524 struct v4l2_dv_timings
*timings
)
526 struct bcap_device
*bcap_dev
= video_drvdata(file
);
527 struct v4l2_input input
;
530 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
531 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
534 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
537 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_dv_timings
, timings
);
541 bcap_dev
->dv_timings
= *timings
;
545 static int bcap_enum_input(struct file
*file
, void *priv
,
546 struct v4l2_input
*input
)
548 struct bcap_device
*bcap_dev
= video_drvdata(file
);
549 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
553 if (input
->index
>= config
->num_inputs
)
556 *input
= config
->inputs
[input
->index
];
557 /* get input status */
558 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_input_status
, &status
);
560 input
->status
= status
;
564 static int bcap_g_input(struct file
*file
, void *priv
, unsigned int *index
)
566 struct bcap_device
*bcap_dev
= video_drvdata(file
);
568 *index
= bcap_dev
->cur_input
;
572 static int bcap_s_input(struct file
*file
, void *priv
, unsigned int index
)
574 struct bcap_device
*bcap_dev
= video_drvdata(file
);
575 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
576 struct bcap_route
*route
;
579 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
582 if (index
>= config
->num_inputs
)
585 route
= &config
->routes
[index
];
586 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
587 route
->input
, route
->output
, 0);
588 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
589 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
592 bcap_dev
->cur_input
= index
;
593 /* if this route has specific config, update ppi control */
594 if (route
->ppi_control
)
595 config
->ppi_control
= route
->ppi_control
;
599 static int bcap_try_format(struct bcap_device
*bcap
,
600 struct v4l2_pix_format
*pixfmt
,
601 struct bcap_format
*bcap_fmt
)
603 struct bcap_format
*sf
= bcap
->sensor_formats
;
604 struct bcap_format
*fmt
= NULL
;
605 struct v4l2_subdev_pad_config pad_cfg
;
606 struct v4l2_subdev_format format
= {
607 .which
= V4L2_SUBDEV_FORMAT_TRY
,
611 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
613 if (pixfmt
->pixelformat
== fmt
->pixelformat
)
616 if (i
== bcap
->num_sensor_formats
)
619 v4l2_fill_mbus_format(&format
.format
, pixfmt
, fmt
->mbus_code
);
620 ret
= v4l2_subdev_call(bcap
->sd
, pad
, set_fmt
, &pad_cfg
,
624 v4l2_fill_pix_format(pixfmt
, &format
.format
);
626 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
628 if (format
.format
.code
== fmt
->mbus_code
)
633 pixfmt
->bytesperline
= pixfmt
->width
* fmt
->bpp
/ 8;
634 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
638 static int bcap_enum_fmt_vid_cap(struct file
*file
, void *priv
,
639 struct v4l2_fmtdesc
*fmt
)
641 struct bcap_device
*bcap_dev
= video_drvdata(file
);
642 struct bcap_format
*sf
= bcap_dev
->sensor_formats
;
644 if (fmt
->index
>= bcap_dev
->num_sensor_formats
)
647 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
648 strlcpy(fmt
->description
,
650 sizeof(fmt
->description
));
651 fmt
->pixelformat
= sf
[fmt
->index
].pixelformat
;
655 static int bcap_try_fmt_vid_cap(struct file
*file
, void *priv
,
656 struct v4l2_format
*fmt
)
658 struct bcap_device
*bcap_dev
= video_drvdata(file
);
659 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
661 return bcap_try_format(bcap_dev
, pixfmt
, NULL
);
664 static int bcap_g_fmt_vid_cap(struct file
*file
, void *priv
,
665 struct v4l2_format
*fmt
)
667 struct bcap_device
*bcap_dev
= video_drvdata(file
);
669 fmt
->fmt
.pix
= bcap_dev
->fmt
;
673 static int bcap_s_fmt_vid_cap(struct file
*file
, void *priv
,
674 struct v4l2_format
*fmt
)
676 struct bcap_device
*bcap_dev
= video_drvdata(file
);
677 struct v4l2_subdev_format format
= {
678 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
680 struct bcap_format bcap_fmt
;
681 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
684 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
687 /* see if format works */
688 ret
= bcap_try_format(bcap_dev
, pixfmt
, &bcap_fmt
);
692 v4l2_fill_mbus_format(&format
.format
, pixfmt
, bcap_fmt
.mbus_code
);
693 ret
= v4l2_subdev_call(bcap_dev
->sd
, pad
, set_fmt
, NULL
, &format
);
696 bcap_dev
->fmt
= *pixfmt
;
697 bcap_dev
->bpp
= bcap_fmt
.bpp
;
698 bcap_dev
->dlen
= bcap_fmt
.dlen
;
702 static int bcap_querycap(struct file
*file
, void *priv
,
703 struct v4l2_capability
*cap
)
705 struct bcap_device
*bcap_dev
= video_drvdata(file
);
707 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
708 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
709 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
710 strlcpy(cap
->bus_info
, "Blackfin Platform", sizeof(cap
->bus_info
));
711 strlcpy(cap
->card
, bcap_dev
->cfg
->card_name
, sizeof(cap
->card
));
715 static int bcap_g_parm(struct file
*file
, void *fh
,
716 struct v4l2_streamparm
*a
)
718 struct bcap_device
*bcap_dev
= video_drvdata(file
);
720 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
722 return v4l2_subdev_call(bcap_dev
->sd
, video
, g_parm
, a
);
725 static int bcap_s_parm(struct file
*file
, void *fh
,
726 struct v4l2_streamparm
*a
)
728 struct bcap_device
*bcap_dev
= video_drvdata(file
);
730 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
732 return v4l2_subdev_call(bcap_dev
->sd
, video
, s_parm
, a
);
735 static int bcap_log_status(struct file
*file
, void *priv
)
737 struct bcap_device
*bcap_dev
= video_drvdata(file
);
738 /* status for sub devices */
739 v4l2_device_call_all(&bcap_dev
->v4l2_dev
, 0, core
, log_status
);
743 static const struct v4l2_ioctl_ops bcap_ioctl_ops
= {
744 .vidioc_querycap
= bcap_querycap
,
745 .vidioc_g_fmt_vid_cap
= bcap_g_fmt_vid_cap
,
746 .vidioc_enum_fmt_vid_cap
= bcap_enum_fmt_vid_cap
,
747 .vidioc_s_fmt_vid_cap
= bcap_s_fmt_vid_cap
,
748 .vidioc_try_fmt_vid_cap
= bcap_try_fmt_vid_cap
,
749 .vidioc_enum_input
= bcap_enum_input
,
750 .vidioc_g_input
= bcap_g_input
,
751 .vidioc_s_input
= bcap_s_input
,
752 .vidioc_querystd
= bcap_querystd
,
753 .vidioc_s_std
= bcap_s_std
,
754 .vidioc_g_std
= bcap_g_std
,
755 .vidioc_s_dv_timings
= bcap_s_dv_timings
,
756 .vidioc_g_dv_timings
= bcap_g_dv_timings
,
757 .vidioc_query_dv_timings
= bcap_query_dv_timings
,
758 .vidioc_enum_dv_timings
= bcap_enum_dv_timings
,
759 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
760 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
761 .vidioc_querybuf
= vb2_ioctl_querybuf
,
762 .vidioc_qbuf
= vb2_ioctl_qbuf
,
763 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
764 .vidioc_expbuf
= vb2_ioctl_expbuf
,
765 .vidioc_streamon
= vb2_ioctl_streamon
,
766 .vidioc_streamoff
= vb2_ioctl_streamoff
,
767 .vidioc_g_parm
= bcap_g_parm
,
768 .vidioc_s_parm
= bcap_s_parm
,
769 .vidioc_log_status
= bcap_log_status
,
772 static struct v4l2_file_operations bcap_fops
= {
773 .owner
= THIS_MODULE
,
774 .open
= v4l2_fh_open
,
775 .release
= vb2_fop_release
,
776 .unlocked_ioctl
= video_ioctl2
,
777 .mmap
= vb2_fop_mmap
,
779 .get_unmapped_area
= vb2_fop_get_unmapped_area
,
784 static int bcap_probe(struct platform_device
*pdev
)
786 struct bcap_device
*bcap_dev
;
787 struct video_device
*vfd
;
788 struct i2c_adapter
*i2c_adap
;
789 struct bfin_capture_config
*config
;
791 struct bcap_route
*route
;
794 config
= pdev
->dev
.platform_data
;
795 if (!config
|| !config
->num_inputs
) {
796 v4l2_err(pdev
->dev
.driver
, "Unable to get board config\n");
800 bcap_dev
= kzalloc(sizeof(*bcap_dev
), GFP_KERNEL
);
804 bcap_dev
->cfg
= config
;
806 bcap_dev
->ppi
= ppi_create_instance(pdev
, config
->ppi_info
);
807 if (!bcap_dev
->ppi
) {
808 v4l2_err(pdev
->dev
.driver
, "Unable to create ppi\n");
812 bcap_dev
->ppi
->priv
= bcap_dev
;
814 vfd
= &bcap_dev
->video_dev
;
815 /* initialize field of video device */
816 vfd
->release
= video_device_release_empty
;
817 vfd
->fops
= &bcap_fops
;
818 vfd
->ioctl_ops
= &bcap_ioctl_ops
;
820 vfd
->v4l2_dev
= &bcap_dev
->v4l2_dev
;
821 strncpy(vfd
->name
, CAPTURE_DRV_NAME
, sizeof(vfd
->name
));
823 ret
= v4l2_device_register(&pdev
->dev
, &bcap_dev
->v4l2_dev
);
825 v4l2_err(pdev
->dev
.driver
,
826 "Unable to register v4l2 device\n");
829 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 device registered\n");
831 bcap_dev
->v4l2_dev
.ctrl_handler
= &bcap_dev
->ctrl_handler
;
832 ret
= v4l2_ctrl_handler_init(&bcap_dev
->ctrl_handler
, 0);
834 v4l2_err(&bcap_dev
->v4l2_dev
,
835 "Unable to init control handler\n");
839 spin_lock_init(&bcap_dev
->lock
);
840 /* initialize queue */
841 q
= &bcap_dev
->buffer_queue
;
842 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
843 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
844 q
->drv_priv
= bcap_dev
;
845 q
->buf_struct_size
= sizeof(struct bcap_buffer
);
846 q
->ops
= &bcap_video_qops
;
847 q
->mem_ops
= &vb2_dma_contig_memops
;
848 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
849 q
->lock
= &bcap_dev
->mutex
;
850 q
->min_buffers_needed
= 1;
853 ret
= vb2_queue_init(q
);
855 goto err_free_handler
;
857 mutex_init(&bcap_dev
->mutex
);
858 init_completion(&bcap_dev
->comp
);
860 /* init video dma queues */
861 INIT_LIST_HEAD(&bcap_dev
->dma_queue
);
863 vfd
->lock
= &bcap_dev
->mutex
;
866 /* register video device */
867 ret
= video_register_device(&bcap_dev
->video_dev
, VFL_TYPE_GRABBER
, -1);
869 v4l2_err(&bcap_dev
->v4l2_dev
,
870 "Unable to register video device\n");
871 goto err_free_handler
;
873 video_set_drvdata(&bcap_dev
->video_dev
, bcap_dev
);
874 v4l2_info(&bcap_dev
->v4l2_dev
, "video device registered as: %s\n",
875 video_device_node_name(vfd
));
877 /* load up the subdevice */
878 i2c_adap
= i2c_get_adapter(config
->i2c_adapter_id
);
880 v4l2_err(&bcap_dev
->v4l2_dev
,
881 "Unable to find i2c adapter\n");
886 bcap_dev
->sd
= v4l2_i2c_new_subdev_board(&bcap_dev
->v4l2_dev
,
893 /* update tvnorms from the sub devices */
894 for (i
= 0; i
< config
->num_inputs
; i
++)
895 vfd
->tvnorms
|= config
->inputs
[i
].std
;
897 v4l2_err(&bcap_dev
->v4l2_dev
,
898 "Unable to register sub device\n");
903 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 sub device registered\n");
906 * explicitly set input, otherwise some boards
907 * may not work at the state as we expected
909 route
= &config
->routes
[0];
910 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
911 route
->input
, route
->output
, 0);
912 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
913 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
916 bcap_dev
->cur_input
= 0;
917 /* if this route has specific config, update ppi control */
918 if (route
->ppi_control
)
919 config
->ppi_control
= route
->ppi_control
;
921 /* now we can probe the default state */
922 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_STD
) {
924 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_std
, &std
);
926 v4l2_err(&bcap_dev
->v4l2_dev
,
927 "Unable to get std\n");
932 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_DV_TIMINGS
) {
933 struct v4l2_dv_timings dv_timings
;
934 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
,
935 g_dv_timings
, &dv_timings
);
937 v4l2_err(&bcap_dev
->v4l2_dev
,
938 "Unable to get dv timings\n");
941 bcap_dev
->dv_timings
= dv_timings
;
943 ret
= bcap_init_sensor_formats(bcap_dev
);
945 v4l2_err(&bcap_dev
->v4l2_dev
,
946 "Unable to create sensor formats table\n");
951 video_unregister_device(&bcap_dev
->video_dev
);
953 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
955 v4l2_device_unregister(&bcap_dev
->v4l2_dev
);
957 ppi_delete_instance(bcap_dev
->ppi
);
963 static int bcap_remove(struct platform_device
*pdev
)
965 struct v4l2_device
*v4l2_dev
= platform_get_drvdata(pdev
);
966 struct bcap_device
*bcap_dev
= container_of(v4l2_dev
,
967 struct bcap_device
, v4l2_dev
);
969 bcap_free_sensor_formats(bcap_dev
);
970 video_unregister_device(&bcap_dev
->video_dev
);
971 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
972 v4l2_device_unregister(v4l2_dev
);
973 ppi_delete_instance(bcap_dev
->ppi
);
978 static struct platform_driver bcap_driver
= {
980 .name
= CAPTURE_DRV_NAME
,
983 .remove
= bcap_remove
,
985 module_platform_driver(bcap_driver
);
987 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
988 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
989 MODULE_LICENSE("GPL v2");