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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/completion.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/types.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ctrls.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/videobuf2-dma-contig.h>
43 #include <media/blackfin/bfin_capture.h>
44 #include <media/blackfin/ppi.h>
46 #define CAPTURE_DRV_NAME "bfin_capture"
52 int bpp
; /* bits per pixel */
53 int dlen
; /* data length for ppi in bits */
57 struct vb2_v4l2_buffer vb
;
58 struct list_head list
;
62 /* capture device instance */
63 struct v4l2_device v4l2_dev
;
64 /* v4l2 control handler */
65 struct v4l2_ctrl_handler ctrl_handler
;
66 /* device node data */
67 struct video_device video_dev
;
68 /* sub device instance */
69 struct v4l2_subdev
*sd
;
71 struct bfin_capture_config
*cfg
;
75 unsigned int cur_input
;
76 /* current selected standard */
78 /* current selected dv_timings */
79 struct v4l2_dv_timings dv_timings
;
80 /* used to store pixel format */
81 struct v4l2_pix_format fmt
;
84 /* data length for ppi in bits */
86 /* used to store sensor supported format */
87 struct bcap_format
*sensor_formats
;
88 /* number of sensor formats array */
89 int num_sensor_formats
;
90 /* pointing to current video buffer */
91 struct bcap_buffer
*cur_frm
;
92 /* buffer queue used in videobuf2 */
93 struct vb2_queue buffer_queue
;
94 /* queue of filled frames */
95 struct list_head dma_queue
;
96 /* used in videobuf2 callback */
98 /* used to access capture device */
100 /* used to wait ppi to complete one transfer */
101 struct completion comp
;
102 /* prepare to stop */
104 /* vb2 buffer sequence counter */
108 static const struct bcap_format bcap_formats
[] = {
110 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
111 .pixelformat
= V4L2_PIX_FMT_UYVY
,
112 .mbus_code
= MEDIA_BUS_FMT_UYVY8_2X8
,
117 .desc
= "YCbCr 4:2:2 Interleaved YUYV",
118 .pixelformat
= V4L2_PIX_FMT_YUYV
,
119 .mbus_code
= MEDIA_BUS_FMT_YUYV8_2X8
,
124 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
125 .pixelformat
= V4L2_PIX_FMT_UYVY
,
126 .mbus_code
= MEDIA_BUS_FMT_UYVY8_1X16
,
132 .pixelformat
= V4L2_PIX_FMT_RGB565
,
133 .mbus_code
= MEDIA_BUS_FMT_RGB565_2X8_LE
,
139 .pixelformat
= V4L2_PIX_FMT_RGB444
,
140 .mbus_code
= MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE
,
146 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
148 static irqreturn_t
bcap_isr(int irq
, void *dev_id
);
150 static struct bcap_buffer
*to_bcap_vb(struct vb2_v4l2_buffer
*vb
)
152 return container_of(vb
, struct bcap_buffer
, vb
);
155 static int bcap_init_sensor_formats(struct bcap_device
*bcap_dev
)
157 struct v4l2_subdev_mbus_code_enum code
= {
158 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
160 struct bcap_format
*sf
;
161 unsigned int num_formats
= 0;
164 while (!v4l2_subdev_call(bcap_dev
->sd
, pad
,
165 enum_mbus_code
, NULL
, &code
)) {
172 sf
= kcalloc(num_formats
, sizeof(*sf
), GFP_KERNEL
);
176 for (i
= 0; i
< num_formats
; i
++) {
178 v4l2_subdev_call(bcap_dev
->sd
, pad
,
179 enum_mbus_code
, NULL
, &code
);
180 for (j
= 0; j
< BCAP_MAX_FMTS
; j
++)
181 if (code
.code
== bcap_formats
[j
].mbus_code
)
183 if (j
== BCAP_MAX_FMTS
) {
184 /* we don't allow this sensor working with our bridge */
188 sf
[i
] = bcap_formats
[j
];
190 bcap_dev
->sensor_formats
= sf
;
191 bcap_dev
->num_sensor_formats
= num_formats
;
195 static void bcap_free_sensor_formats(struct bcap_device
*bcap_dev
)
197 bcap_dev
->num_sensor_formats
= 0;
198 kfree(bcap_dev
->sensor_formats
);
199 bcap_dev
->sensor_formats
= NULL
;
202 static int bcap_queue_setup(struct vb2_queue
*vq
,
203 unsigned int *nbuffers
, unsigned int *nplanes
,
204 unsigned int sizes
[], struct device
*alloc_devs
[])
206 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
208 if (vq
->num_buffers
+ *nbuffers
< 2)
212 return sizes
[0] < bcap_dev
->fmt
.sizeimage
? -EINVAL
: 0;
215 sizes
[0] = bcap_dev
->fmt
.sizeimage
;
220 static int bcap_buffer_prepare(struct vb2_buffer
*vb
)
222 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
223 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
224 unsigned long size
= bcap_dev
->fmt
.sizeimage
;
226 if (vb2_plane_size(vb
, 0) < size
) {
227 v4l2_err(&bcap_dev
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
228 vb2_plane_size(vb
, 0), size
);
231 vb2_set_plane_payload(vb
, 0, size
);
233 vbuf
->field
= bcap_dev
->fmt
.field
;
238 static void bcap_buffer_queue(struct vb2_buffer
*vb
)
240 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
241 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
242 struct bcap_buffer
*buf
= to_bcap_vb(vbuf
);
245 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
246 list_add_tail(&buf
->list
, &bcap_dev
->dma_queue
);
247 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
250 static void bcap_buffer_cleanup(struct vb2_buffer
*vb
)
252 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
253 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
254 struct bcap_buffer
*buf
= to_bcap_vb(vbuf
);
257 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
258 list_del_init(&buf
->list
);
259 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
262 static int bcap_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
264 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
265 struct ppi_if
*ppi
= bcap_dev
->ppi
;
266 struct bcap_buffer
*buf
, *tmp
;
267 struct ppi_params params
;
271 /* enable streamon on the sub device */
272 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 1);
273 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
274 v4l2_err(&bcap_dev
->v4l2_dev
, "stream on failed in subdev\n");
279 params
.width
= bcap_dev
->fmt
.width
;
280 params
.height
= bcap_dev
->fmt
.height
;
281 params
.bpp
= bcap_dev
->bpp
;
282 params
.dlen
= bcap_dev
->dlen
;
283 params
.ppi_control
= bcap_dev
->cfg
->ppi_control
;
284 params
.int_mask
= bcap_dev
->cfg
->int_mask
;
285 if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
286 & V4L2_IN_CAP_DV_TIMINGS
) {
287 struct v4l2_bt_timings
*bt
= &bcap_dev
->dv_timings
.bt
;
289 params
.hdelay
= bt
->hsync
+ bt
->hbackporch
;
290 params
.vdelay
= bt
->vsync
+ bt
->vbackporch
;
291 params
.line
= V4L2_DV_BT_FRAME_WIDTH(bt
);
292 params
.frame
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
293 } else if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
297 if (bcap_dev
->std
& V4L2_STD_525_60
) {
307 params
.line
= params
.width
+ bcap_dev
->cfg
->blank_pixels
;
308 params
.frame
= params
.height
;
310 ret
= ppi
->ops
->set_params(ppi
, ¶ms
);
312 v4l2_err(&bcap_dev
->v4l2_dev
,
313 "Error in setting ppi params\n");
317 /* attach ppi DMA irq handler */
318 ret
= ppi
->ops
->attach_irq(ppi
, bcap_isr
);
320 v4l2_err(&bcap_dev
->v4l2_dev
,
321 "Error in attaching interrupt handler\n");
325 bcap_dev
->sequence
= 0;
327 reinit_completion(&bcap_dev
->comp
);
328 bcap_dev
->stop
= false;
330 /* get the next frame from the dma queue */
331 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
332 struct bcap_buffer
, list
);
333 /* remove buffer from the dma queue */
334 list_del_init(&bcap_dev
->cur_frm
->list
);
335 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
337 /* update DMA address */
338 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
340 ppi
->ops
->start(ppi
);
345 list_for_each_entry_safe(buf
, tmp
, &bcap_dev
->dma_queue
, list
) {
346 list_del(&buf
->list
);
347 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_QUEUED
);
353 static void bcap_stop_streaming(struct vb2_queue
*vq
)
355 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
356 struct ppi_if
*ppi
= bcap_dev
->ppi
;
359 bcap_dev
->stop
= true;
360 wait_for_completion(&bcap_dev
->comp
);
362 ppi
->ops
->detach_irq(ppi
);
363 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 0);
364 if (ret
&& (ret
!= -ENOIOCTLCMD
))
365 v4l2_err(&bcap_dev
->v4l2_dev
,
366 "stream off failed in subdev\n");
368 /* release all active buffers */
369 if (bcap_dev
->cur_frm
)
370 vb2_buffer_done(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
371 VB2_BUF_STATE_ERROR
);
373 while (!list_empty(&bcap_dev
->dma_queue
)) {
374 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
375 struct bcap_buffer
, list
);
376 list_del_init(&bcap_dev
->cur_frm
->list
);
377 vb2_buffer_done(&bcap_dev
->cur_frm
->vb
.vb2_buf
,
378 VB2_BUF_STATE_ERROR
);
382 static struct vb2_ops bcap_video_qops
= {
383 .queue_setup
= bcap_queue_setup
,
384 .buf_prepare
= bcap_buffer_prepare
,
385 .buf_cleanup
= bcap_buffer_cleanup
,
386 .buf_queue
= bcap_buffer_queue
,
387 .wait_prepare
= vb2_ops_wait_prepare
,
388 .wait_finish
= vb2_ops_wait_finish
,
389 .start_streaming
= bcap_start_streaming
,
390 .stop_streaming
= bcap_stop_streaming
,
393 static irqreturn_t
bcap_isr(int irq
, void *dev_id
)
395 struct ppi_if
*ppi
= dev_id
;
396 struct bcap_device
*bcap_dev
= ppi
->priv
;
397 struct vb2_v4l2_buffer
*vbuf
= &bcap_dev
->cur_frm
->vb
;
398 struct vb2_buffer
*vb
= &vbuf
->vb2_buf
;
401 spin_lock(&bcap_dev
->lock
);
403 if (!list_empty(&bcap_dev
->dma_queue
)) {
404 vb
->timestamp
= ktime_get_ns();
406 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
409 vbuf
->sequence
= bcap_dev
->sequence
++;
410 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
412 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
413 struct bcap_buffer
, list
);
414 list_del_init(&bcap_dev
->cur_frm
->list
);
416 /* clear error flag, we will get a new frame */
423 if (bcap_dev
->stop
) {
424 complete(&bcap_dev
->comp
);
426 addr
= vb2_dma_contig_plane_dma_addr(
427 &bcap_dev
->cur_frm
->vb
.vb2_buf
, 0);
428 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
429 ppi
->ops
->start(ppi
);
432 spin_unlock(&bcap_dev
->lock
);
437 static int bcap_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
439 struct bcap_device
*bcap_dev
= video_drvdata(file
);
440 struct v4l2_input input
;
442 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
443 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
446 return v4l2_subdev_call(bcap_dev
->sd
, video
, querystd
, std
);
449 static int bcap_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
451 struct bcap_device
*bcap_dev
= video_drvdata(file
);
452 struct v4l2_input input
;
454 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
455 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
458 *std
= bcap_dev
->std
;
462 static int bcap_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
464 struct bcap_device
*bcap_dev
= video_drvdata(file
);
465 struct v4l2_input input
;
468 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
469 if (!(input
.capabilities
& V4L2_IN_CAP_STD
))
472 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
475 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_std
, std
);
483 static int bcap_enum_dv_timings(struct file
*file
, void *priv
,
484 struct v4l2_enum_dv_timings
*timings
)
486 struct bcap_device
*bcap_dev
= video_drvdata(file
);
487 struct v4l2_input input
;
489 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
490 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
495 return v4l2_subdev_call(bcap_dev
->sd
, pad
,
496 enum_dv_timings
, timings
);
499 static int bcap_query_dv_timings(struct file
*file
, void *priv
,
500 struct v4l2_dv_timings
*timings
)
502 struct bcap_device
*bcap_dev
= video_drvdata(file
);
503 struct v4l2_input input
;
505 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
506 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
509 return v4l2_subdev_call(bcap_dev
->sd
, video
,
510 query_dv_timings
, timings
);
513 static int bcap_g_dv_timings(struct file
*file
, void *priv
,
514 struct v4l2_dv_timings
*timings
)
516 struct bcap_device
*bcap_dev
= video_drvdata(file
);
517 struct v4l2_input input
;
519 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
520 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
523 *timings
= bcap_dev
->dv_timings
;
527 static int bcap_s_dv_timings(struct file
*file
, void *priv
,
528 struct v4l2_dv_timings
*timings
)
530 struct bcap_device
*bcap_dev
= video_drvdata(file
);
531 struct v4l2_input input
;
534 input
= bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
];
535 if (!(input
.capabilities
& V4L2_IN_CAP_DV_TIMINGS
))
538 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
541 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_dv_timings
, timings
);
545 bcap_dev
->dv_timings
= *timings
;
549 static int bcap_enum_input(struct file
*file
, void *priv
,
550 struct v4l2_input
*input
)
552 struct bcap_device
*bcap_dev
= video_drvdata(file
);
553 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
557 if (input
->index
>= config
->num_inputs
)
560 *input
= config
->inputs
[input
->index
];
561 /* get input status */
562 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_input_status
, &status
);
564 input
->status
= status
;
568 static int bcap_g_input(struct file
*file
, void *priv
, unsigned int *index
)
570 struct bcap_device
*bcap_dev
= video_drvdata(file
);
572 *index
= bcap_dev
->cur_input
;
576 static int bcap_s_input(struct file
*file
, void *priv
, unsigned int index
)
578 struct bcap_device
*bcap_dev
= video_drvdata(file
);
579 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
580 struct bcap_route
*route
;
583 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
586 if (index
>= config
->num_inputs
)
589 route
= &config
->routes
[index
];
590 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
591 route
->input
, route
->output
, 0);
592 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
593 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
596 bcap_dev
->cur_input
= index
;
597 /* if this route has specific config, update ppi control */
598 if (route
->ppi_control
)
599 config
->ppi_control
= route
->ppi_control
;
603 static int bcap_try_format(struct bcap_device
*bcap
,
604 struct v4l2_pix_format
*pixfmt
,
605 struct bcap_format
*bcap_fmt
)
607 struct bcap_format
*sf
= bcap
->sensor_formats
;
608 struct bcap_format
*fmt
= NULL
;
609 struct v4l2_subdev_pad_config pad_cfg
;
610 struct v4l2_subdev_format format
= {
611 .which
= V4L2_SUBDEV_FORMAT_TRY
,
615 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
617 if (pixfmt
->pixelformat
== fmt
->pixelformat
)
620 if (i
== bcap
->num_sensor_formats
)
623 v4l2_fill_mbus_format(&format
.format
, pixfmt
, fmt
->mbus_code
);
624 ret
= v4l2_subdev_call(bcap
->sd
, pad
, set_fmt
, &pad_cfg
,
628 v4l2_fill_pix_format(pixfmt
, &format
.format
);
630 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
632 if (format
.format
.code
== fmt
->mbus_code
)
637 pixfmt
->bytesperline
= pixfmt
->width
* fmt
->bpp
/ 8;
638 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
642 static int bcap_enum_fmt_vid_cap(struct file
*file
, void *priv
,
643 struct v4l2_fmtdesc
*fmt
)
645 struct bcap_device
*bcap_dev
= video_drvdata(file
);
646 struct bcap_format
*sf
= bcap_dev
->sensor_formats
;
648 if (fmt
->index
>= bcap_dev
->num_sensor_formats
)
651 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
652 strlcpy(fmt
->description
,
654 sizeof(fmt
->description
));
655 fmt
->pixelformat
= sf
[fmt
->index
].pixelformat
;
659 static int bcap_try_fmt_vid_cap(struct file
*file
, void *priv
,
660 struct v4l2_format
*fmt
)
662 struct bcap_device
*bcap_dev
= video_drvdata(file
);
663 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
665 return bcap_try_format(bcap_dev
, pixfmt
, NULL
);
668 static int bcap_g_fmt_vid_cap(struct file
*file
, void *priv
,
669 struct v4l2_format
*fmt
)
671 struct bcap_device
*bcap_dev
= video_drvdata(file
);
673 fmt
->fmt
.pix
= bcap_dev
->fmt
;
677 static int bcap_s_fmt_vid_cap(struct file
*file
, void *priv
,
678 struct v4l2_format
*fmt
)
680 struct bcap_device
*bcap_dev
= video_drvdata(file
);
681 struct v4l2_subdev_format format
= {
682 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
684 struct bcap_format bcap_fmt
;
685 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
688 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
691 /* see if format works */
692 ret
= bcap_try_format(bcap_dev
, pixfmt
, &bcap_fmt
);
696 v4l2_fill_mbus_format(&format
.format
, pixfmt
, bcap_fmt
.mbus_code
);
697 ret
= v4l2_subdev_call(bcap_dev
->sd
, pad
, set_fmt
, NULL
, &format
);
700 bcap_dev
->fmt
= *pixfmt
;
701 bcap_dev
->bpp
= bcap_fmt
.bpp
;
702 bcap_dev
->dlen
= bcap_fmt
.dlen
;
706 static int bcap_querycap(struct file
*file
, void *priv
,
707 struct v4l2_capability
*cap
)
709 struct bcap_device
*bcap_dev
= video_drvdata(file
);
711 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
712 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
713 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
714 strlcpy(cap
->bus_info
, "Blackfin Platform", sizeof(cap
->bus_info
));
715 strlcpy(cap
->card
, bcap_dev
->cfg
->card_name
, sizeof(cap
->card
));
719 static int bcap_g_parm(struct file
*file
, void *fh
,
720 struct v4l2_streamparm
*a
)
722 struct bcap_device
*bcap_dev
= video_drvdata(file
);
724 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
726 return v4l2_subdev_call(bcap_dev
->sd
, video
, g_parm
, a
);
729 static int bcap_s_parm(struct file
*file
, void *fh
,
730 struct v4l2_streamparm
*a
)
732 struct bcap_device
*bcap_dev
= video_drvdata(file
);
734 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
736 return v4l2_subdev_call(bcap_dev
->sd
, video
, s_parm
, a
);
739 static int bcap_log_status(struct file
*file
, void *priv
)
741 struct bcap_device
*bcap_dev
= video_drvdata(file
);
742 /* status for sub devices */
743 v4l2_device_call_all(&bcap_dev
->v4l2_dev
, 0, core
, log_status
);
747 static const struct v4l2_ioctl_ops bcap_ioctl_ops
= {
748 .vidioc_querycap
= bcap_querycap
,
749 .vidioc_g_fmt_vid_cap
= bcap_g_fmt_vid_cap
,
750 .vidioc_enum_fmt_vid_cap
= bcap_enum_fmt_vid_cap
,
751 .vidioc_s_fmt_vid_cap
= bcap_s_fmt_vid_cap
,
752 .vidioc_try_fmt_vid_cap
= bcap_try_fmt_vid_cap
,
753 .vidioc_enum_input
= bcap_enum_input
,
754 .vidioc_g_input
= bcap_g_input
,
755 .vidioc_s_input
= bcap_s_input
,
756 .vidioc_querystd
= bcap_querystd
,
757 .vidioc_s_std
= bcap_s_std
,
758 .vidioc_g_std
= bcap_g_std
,
759 .vidioc_s_dv_timings
= bcap_s_dv_timings
,
760 .vidioc_g_dv_timings
= bcap_g_dv_timings
,
761 .vidioc_query_dv_timings
= bcap_query_dv_timings
,
762 .vidioc_enum_dv_timings
= bcap_enum_dv_timings
,
763 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
764 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
765 .vidioc_querybuf
= vb2_ioctl_querybuf
,
766 .vidioc_qbuf
= vb2_ioctl_qbuf
,
767 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
768 .vidioc_expbuf
= vb2_ioctl_expbuf
,
769 .vidioc_streamon
= vb2_ioctl_streamon
,
770 .vidioc_streamoff
= vb2_ioctl_streamoff
,
771 .vidioc_g_parm
= bcap_g_parm
,
772 .vidioc_s_parm
= bcap_s_parm
,
773 .vidioc_log_status
= bcap_log_status
,
776 static struct v4l2_file_operations bcap_fops
= {
777 .owner
= THIS_MODULE
,
778 .open
= v4l2_fh_open
,
779 .release
= vb2_fop_release
,
780 .unlocked_ioctl
= video_ioctl2
,
781 .mmap
= vb2_fop_mmap
,
783 .get_unmapped_area
= vb2_fop_get_unmapped_area
,
788 static int bcap_probe(struct platform_device
*pdev
)
790 struct bcap_device
*bcap_dev
;
791 struct video_device
*vfd
;
792 struct i2c_adapter
*i2c_adap
;
793 struct bfin_capture_config
*config
;
795 struct bcap_route
*route
;
798 config
= pdev
->dev
.platform_data
;
799 if (!config
|| !config
->num_inputs
) {
800 v4l2_err(pdev
->dev
.driver
, "Unable to get board config\n");
804 bcap_dev
= kzalloc(sizeof(*bcap_dev
), GFP_KERNEL
);
808 bcap_dev
->cfg
= config
;
810 bcap_dev
->ppi
= ppi_create_instance(pdev
, config
->ppi_info
);
811 if (!bcap_dev
->ppi
) {
812 v4l2_err(pdev
->dev
.driver
, "Unable to create ppi\n");
816 bcap_dev
->ppi
->priv
= bcap_dev
;
818 vfd
= &bcap_dev
->video_dev
;
819 /* initialize field of video device */
820 vfd
->release
= video_device_release_empty
;
821 vfd
->fops
= &bcap_fops
;
822 vfd
->ioctl_ops
= &bcap_ioctl_ops
;
824 vfd
->v4l2_dev
= &bcap_dev
->v4l2_dev
;
825 strncpy(vfd
->name
, CAPTURE_DRV_NAME
, sizeof(vfd
->name
));
827 ret
= v4l2_device_register(&pdev
->dev
, &bcap_dev
->v4l2_dev
);
829 v4l2_err(pdev
->dev
.driver
,
830 "Unable to register v4l2 device\n");
833 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 device registered\n");
835 bcap_dev
->v4l2_dev
.ctrl_handler
= &bcap_dev
->ctrl_handler
;
836 ret
= v4l2_ctrl_handler_init(&bcap_dev
->ctrl_handler
, 0);
838 v4l2_err(&bcap_dev
->v4l2_dev
,
839 "Unable to init control handler\n");
843 spin_lock_init(&bcap_dev
->lock
);
844 /* initialize queue */
845 q
= &bcap_dev
->buffer_queue
;
846 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
847 q
->io_modes
= VB2_MMAP
| VB2_DMABUF
;
848 q
->drv_priv
= bcap_dev
;
849 q
->buf_struct_size
= sizeof(struct bcap_buffer
);
850 q
->ops
= &bcap_video_qops
;
851 q
->mem_ops
= &vb2_dma_contig_memops
;
852 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
853 q
->lock
= &bcap_dev
->mutex
;
854 q
->min_buffers_needed
= 1;
857 ret
= vb2_queue_init(q
);
859 goto err_free_handler
;
861 mutex_init(&bcap_dev
->mutex
);
862 init_completion(&bcap_dev
->comp
);
864 /* init video dma queues */
865 INIT_LIST_HEAD(&bcap_dev
->dma_queue
);
867 vfd
->lock
= &bcap_dev
->mutex
;
870 /* register video device */
871 ret
= video_register_device(&bcap_dev
->video_dev
, VFL_TYPE_GRABBER
, -1);
873 v4l2_err(&bcap_dev
->v4l2_dev
,
874 "Unable to register video device\n");
875 goto err_free_handler
;
877 video_set_drvdata(&bcap_dev
->video_dev
, bcap_dev
);
878 v4l2_info(&bcap_dev
->v4l2_dev
, "video device registered as: %s\n",
879 video_device_node_name(vfd
));
881 /* load up the subdevice */
882 i2c_adap
= i2c_get_adapter(config
->i2c_adapter_id
);
884 v4l2_err(&bcap_dev
->v4l2_dev
,
885 "Unable to find i2c adapter\n");
890 bcap_dev
->sd
= v4l2_i2c_new_subdev_board(&bcap_dev
->v4l2_dev
,
897 /* update tvnorms from the sub devices */
898 for (i
= 0; i
< config
->num_inputs
; i
++)
899 vfd
->tvnorms
|= config
->inputs
[i
].std
;
901 v4l2_err(&bcap_dev
->v4l2_dev
,
902 "Unable to register sub device\n");
907 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 sub device registered\n");
910 * explicitly set input, otherwise some boards
911 * may not work at the state as we expected
913 route
= &config
->routes
[0];
914 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
915 route
->input
, route
->output
, 0);
916 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
917 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
920 bcap_dev
->cur_input
= 0;
921 /* if this route has specific config, update ppi control */
922 if (route
->ppi_control
)
923 config
->ppi_control
= route
->ppi_control
;
925 /* now we can probe the default state */
926 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_STD
) {
928 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_std
, &std
);
930 v4l2_err(&bcap_dev
->v4l2_dev
,
931 "Unable to get std\n");
936 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_DV_TIMINGS
) {
937 struct v4l2_dv_timings dv_timings
;
938 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
,
939 g_dv_timings
, &dv_timings
);
941 v4l2_err(&bcap_dev
->v4l2_dev
,
942 "Unable to get dv timings\n");
945 bcap_dev
->dv_timings
= dv_timings
;
947 ret
= bcap_init_sensor_formats(bcap_dev
);
949 v4l2_err(&bcap_dev
->v4l2_dev
,
950 "Unable to create sensor formats table\n");
955 video_unregister_device(&bcap_dev
->video_dev
);
957 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
959 v4l2_device_unregister(&bcap_dev
->v4l2_dev
);
961 ppi_delete_instance(bcap_dev
->ppi
);
967 static int bcap_remove(struct platform_device
*pdev
)
969 struct v4l2_device
*v4l2_dev
= platform_get_drvdata(pdev
);
970 struct bcap_device
*bcap_dev
= container_of(v4l2_dev
,
971 struct bcap_device
, v4l2_dev
);
973 bcap_free_sensor_formats(bcap_dev
);
974 video_unregister_device(&bcap_dev
->video_dev
);
975 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
976 v4l2_device_unregister(v4l2_dev
);
977 ppi_delete_instance(bcap_dev
->ppi
);
982 static struct platform_driver bcap_driver
= {
984 .name
= CAPTURE_DRV_NAME
,
987 .remove
= bcap_remove
,
989 module_platform_driver(bcap_driver
);
991 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
992 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
993 MODULE_LICENSE("GPL v2");