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-chip-ident.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/videobuf2-dma-contig.h>
44 #include <media/blackfin/bfin_capture.h>
45 #include <media/blackfin/ppi.h>
47 #define CAPTURE_DRV_NAME "bfin_capture"
48 #define BCAP_MIN_NUM_BUF 2
53 enum v4l2_mbus_pixelcode mbus_code
;
54 int bpp
; /* bits per pixel */
55 int dlen
; /* data length for ppi in bits */
60 struct list_head list
;
64 /* capture device instance */
65 struct v4l2_device v4l2_dev
;
66 /* v4l2 control handler */
67 struct v4l2_ctrl_handler ctrl_handler
;
68 /* device node data */
69 struct video_device
*video_dev
;
70 /* sub device instance */
71 struct v4l2_subdev
*sd
;
73 struct bfin_capture_config
*cfg
;
77 unsigned int cur_input
;
78 /* current selected standard */
80 /* current selected dv_timings */
81 struct v4l2_dv_timings dv_timings
;
82 /* used to store pixel format */
83 struct v4l2_pix_format fmt
;
86 /* data length for ppi in bits */
88 /* used to store sensor supported format */
89 struct bcap_format
*sensor_formats
;
90 /* number of sensor formats array */
91 int num_sensor_formats
;
92 /* pointing to current video buffer */
93 struct bcap_buffer
*cur_frm
;
94 /* buffer queue used in videobuf2 */
95 struct vb2_queue buffer_queue
;
96 /* allocator-specific contexts for each plane */
97 struct vb2_alloc_ctx
*alloc_ctx
;
98 /* queue of filled frames */
99 struct list_head dma_queue
;
100 /* used in videobuf2 callback */
102 /* used to access capture device */
104 /* used to wait ppi to complete one transfer */
105 struct completion comp
;
106 /* prepare to stop */
112 /* indicates whether this file handle is doing IO */
116 static const struct bcap_format bcap_formats
[] = {
118 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
119 .pixelformat
= V4L2_PIX_FMT_UYVY
,
120 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
125 .desc
= "YCbCr 4:2:2 Interleaved YUYV",
126 .pixelformat
= V4L2_PIX_FMT_YUYV
,
127 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
132 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
133 .pixelformat
= V4L2_PIX_FMT_UYVY
,
134 .mbus_code
= V4L2_MBUS_FMT_UYVY8_1X16
,
140 .pixelformat
= V4L2_PIX_FMT_RGB565
,
141 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
147 .pixelformat
= V4L2_PIX_FMT_RGB444
,
148 .mbus_code
= V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE
,
154 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
156 static irqreturn_t
bcap_isr(int irq
, void *dev_id
);
158 static struct bcap_buffer
*to_bcap_vb(struct vb2_buffer
*vb
)
160 return container_of(vb
, struct bcap_buffer
, vb
);
163 static int bcap_init_sensor_formats(struct bcap_device
*bcap_dev
)
165 enum v4l2_mbus_pixelcode code
;
166 struct bcap_format
*sf
;
167 unsigned int num_formats
= 0;
170 while (!v4l2_subdev_call(bcap_dev
->sd
, video
,
171 enum_mbus_fmt
, num_formats
, &code
))
176 sf
= kzalloc(num_formats
* sizeof(*sf
), GFP_KERNEL
);
180 for (i
= 0; i
< num_formats
; i
++) {
181 v4l2_subdev_call(bcap_dev
->sd
, video
,
182 enum_mbus_fmt
, i
, &code
);
183 for (j
= 0; j
< BCAP_MAX_FMTS
; j
++)
184 if (code
== bcap_formats
[j
].mbus_code
)
186 if (j
== BCAP_MAX_FMTS
) {
187 /* we don't allow this sensor working with our bridge */
191 sf
[i
] = bcap_formats
[j
];
193 bcap_dev
->sensor_formats
= sf
;
194 bcap_dev
->num_sensor_formats
= num_formats
;
198 static void bcap_free_sensor_formats(struct bcap_device
*bcap_dev
)
200 bcap_dev
->num_sensor_formats
= 0;
201 kfree(bcap_dev
->sensor_formats
);
202 bcap_dev
->sensor_formats
= NULL
;
205 static int bcap_open(struct file
*file
)
207 struct bcap_device
*bcap_dev
= video_drvdata(file
);
208 struct video_device
*vfd
= bcap_dev
->video_dev
;
209 struct bcap_fh
*bcap_fh
;
212 v4l2_err(&bcap_dev
->v4l2_dev
, "No sub device registered\n");
216 bcap_fh
= kzalloc(sizeof(*bcap_fh
), GFP_KERNEL
);
218 v4l2_err(&bcap_dev
->v4l2_dev
,
219 "unable to allocate memory for file handle object\n");
223 v4l2_fh_init(&bcap_fh
->fh
, vfd
);
225 /* store pointer to v4l2_fh in private_data member of file */
226 file
->private_data
= &bcap_fh
->fh
;
227 v4l2_fh_add(&bcap_fh
->fh
);
228 bcap_fh
->io_allowed
= false;
232 static int bcap_release(struct file
*file
)
234 struct bcap_device
*bcap_dev
= video_drvdata(file
);
235 struct v4l2_fh
*fh
= file
->private_data
;
236 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
238 /* if this instance is doing IO */
239 if (bcap_fh
->io_allowed
)
240 vb2_queue_release(&bcap_dev
->buffer_queue
);
242 file
->private_data
= NULL
;
243 v4l2_fh_del(&bcap_fh
->fh
);
244 v4l2_fh_exit(&bcap_fh
->fh
);
249 static int bcap_mmap(struct file
*file
, struct vm_area_struct
*vma
)
251 struct bcap_device
*bcap_dev
= video_drvdata(file
);
254 if (mutex_lock_interruptible(&bcap_dev
->mutex
))
256 ret
= vb2_mmap(&bcap_dev
->buffer_queue
, vma
);
257 mutex_unlock(&bcap_dev
->mutex
);
262 static unsigned long bcap_get_unmapped_area(struct file
*file
,
268 struct bcap_device
*bcap_dev
= video_drvdata(file
);
270 return vb2_get_unmapped_area(&bcap_dev
->buffer_queue
,
278 static unsigned int bcap_poll(struct file
*file
, poll_table
*wait
)
280 struct bcap_device
*bcap_dev
= video_drvdata(file
);
283 mutex_lock(&bcap_dev
->mutex
);
284 res
= vb2_poll(&bcap_dev
->buffer_queue
, file
, wait
);
285 mutex_unlock(&bcap_dev
->mutex
);
289 static int bcap_queue_setup(struct vb2_queue
*vq
,
290 const struct v4l2_format
*fmt
,
291 unsigned int *nbuffers
, unsigned int *nplanes
,
292 unsigned int sizes
[], void *alloc_ctxs
[])
294 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
296 if (*nbuffers
< BCAP_MIN_NUM_BUF
)
297 *nbuffers
= BCAP_MIN_NUM_BUF
;
300 sizes
[0] = bcap_dev
->fmt
.sizeimage
;
301 alloc_ctxs
[0] = bcap_dev
->alloc_ctx
;
306 static int bcap_buffer_init(struct vb2_buffer
*vb
)
308 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
310 INIT_LIST_HEAD(&buf
->list
);
314 static int bcap_buffer_prepare(struct vb2_buffer
*vb
)
316 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
317 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
320 size
= bcap_dev
->fmt
.sizeimage
;
321 if (vb2_plane_size(vb
, 0) < size
) {
322 v4l2_err(&bcap_dev
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
323 vb2_plane_size(vb
, 0), size
);
326 vb2_set_plane_payload(&buf
->vb
, 0, size
);
331 static void bcap_buffer_queue(struct vb2_buffer
*vb
)
333 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
334 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
337 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
338 list_add_tail(&buf
->list
, &bcap_dev
->dma_queue
);
339 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
342 static void bcap_buffer_cleanup(struct vb2_buffer
*vb
)
344 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
345 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
348 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
349 list_del_init(&buf
->list
);
350 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
353 static void bcap_lock(struct vb2_queue
*vq
)
355 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
356 mutex_lock(&bcap_dev
->mutex
);
359 static void bcap_unlock(struct vb2_queue
*vq
)
361 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
362 mutex_unlock(&bcap_dev
->mutex
);
365 static int bcap_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
367 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
368 struct ppi_if
*ppi
= bcap_dev
->ppi
;
369 struct ppi_params params
;
372 /* enable streamon on the sub device */
373 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 1);
374 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
375 v4l2_err(&bcap_dev
->v4l2_dev
, "stream on failed in subdev\n");
380 params
.width
= bcap_dev
->fmt
.width
;
381 params
.height
= bcap_dev
->fmt
.height
;
382 params
.bpp
= bcap_dev
->bpp
;
383 params
.dlen
= bcap_dev
->dlen
;
384 params
.ppi_control
= bcap_dev
->cfg
->ppi_control
;
385 params
.int_mask
= bcap_dev
->cfg
->int_mask
;
386 if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
387 & V4L2_IN_CAP_DV_TIMINGS
) {
388 struct v4l2_bt_timings
*bt
= &bcap_dev
->dv_timings
.bt
;
390 params
.hdelay
= bt
->hsync
+ bt
->hbackporch
;
391 params
.vdelay
= bt
->vsync
+ bt
->vbackporch
;
392 params
.line
= bt
->hfrontporch
+ bt
->hsync
393 + bt
->hbackporch
+ bt
->width
;
394 params
.frame
= bt
->vfrontporch
+ bt
->vsync
395 + bt
->vbackporch
+ bt
->height
;
397 params
.frame
+= bt
->il_vfrontporch
+ bt
->il_vsync
399 } else if (bcap_dev
->cfg
->inputs
[bcap_dev
->cur_input
].capabilities
403 if (bcap_dev
->std
& V4L2_STD_525_60
) {
413 params
.line
= params
.width
+ bcap_dev
->cfg
->blank_pixels
;
414 params
.frame
= params
.height
;
416 ret
= ppi
->ops
->set_params(ppi
, ¶ms
);
418 v4l2_err(&bcap_dev
->v4l2_dev
,
419 "Error in setting ppi params\n");
423 /* attach ppi DMA irq handler */
424 ret
= ppi
->ops
->attach_irq(ppi
, bcap_isr
);
426 v4l2_err(&bcap_dev
->v4l2_dev
,
427 "Error in attaching interrupt handler\n");
431 INIT_COMPLETION(bcap_dev
->comp
);
432 bcap_dev
->stop
= false;
436 static int bcap_stop_streaming(struct vb2_queue
*vq
)
438 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
439 struct ppi_if
*ppi
= bcap_dev
->ppi
;
442 if (!vb2_is_streaming(vq
))
445 bcap_dev
->stop
= true;
446 wait_for_completion(&bcap_dev
->comp
);
448 ppi
->ops
->detach_irq(ppi
);
449 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 0);
450 if (ret
&& (ret
!= -ENOIOCTLCMD
))
451 v4l2_err(&bcap_dev
->v4l2_dev
,
452 "stream off failed in subdev\n");
454 /* release all active buffers */
455 while (!list_empty(&bcap_dev
->dma_queue
)) {
456 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
457 struct bcap_buffer
, list
);
458 list_del(&bcap_dev
->cur_frm
->list
);
459 vb2_buffer_done(&bcap_dev
->cur_frm
->vb
, VB2_BUF_STATE_ERROR
);
464 static struct vb2_ops bcap_video_qops
= {
465 .queue_setup
= bcap_queue_setup
,
466 .buf_init
= bcap_buffer_init
,
467 .buf_prepare
= bcap_buffer_prepare
,
468 .buf_cleanup
= bcap_buffer_cleanup
,
469 .buf_queue
= bcap_buffer_queue
,
470 .wait_prepare
= bcap_unlock
,
471 .wait_finish
= bcap_lock
,
472 .start_streaming
= bcap_start_streaming
,
473 .stop_streaming
= bcap_stop_streaming
,
476 static int bcap_reqbufs(struct file
*file
, void *priv
,
477 struct v4l2_requestbuffers
*req_buf
)
479 struct bcap_device
*bcap_dev
= video_drvdata(file
);
480 struct vb2_queue
*vq
= &bcap_dev
->buffer_queue
;
481 struct v4l2_fh
*fh
= file
->private_data
;
482 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
487 bcap_fh
->io_allowed
= true;
489 return vb2_reqbufs(vq
, req_buf
);
492 static int bcap_querybuf(struct file
*file
, void *priv
,
493 struct v4l2_buffer
*buf
)
495 struct bcap_device
*bcap_dev
= video_drvdata(file
);
497 return vb2_querybuf(&bcap_dev
->buffer_queue
, buf
);
500 static int bcap_qbuf(struct file
*file
, void *priv
,
501 struct v4l2_buffer
*buf
)
503 struct bcap_device
*bcap_dev
= video_drvdata(file
);
504 struct v4l2_fh
*fh
= file
->private_data
;
505 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
507 if (!bcap_fh
->io_allowed
)
510 return vb2_qbuf(&bcap_dev
->buffer_queue
, buf
);
513 static int bcap_dqbuf(struct file
*file
, void *priv
,
514 struct v4l2_buffer
*buf
)
516 struct bcap_device
*bcap_dev
= video_drvdata(file
);
517 struct v4l2_fh
*fh
= file
->private_data
;
518 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
520 if (!bcap_fh
->io_allowed
)
523 return vb2_dqbuf(&bcap_dev
->buffer_queue
,
524 buf
, file
->f_flags
& O_NONBLOCK
);
527 static irqreturn_t
bcap_isr(int irq
, void *dev_id
)
529 struct ppi_if
*ppi
= dev_id
;
530 struct bcap_device
*bcap_dev
= ppi
->priv
;
531 struct vb2_buffer
*vb
= &bcap_dev
->cur_frm
->vb
;
534 spin_lock(&bcap_dev
->lock
);
536 if (!list_empty(&bcap_dev
->dma_queue
)) {
537 v4l2_get_timestamp(&vb
->v4l2_buf
.timestamp
);
539 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
542 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
544 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
545 struct bcap_buffer
, list
);
546 list_del(&bcap_dev
->cur_frm
->list
);
548 /* clear error flag, we will get a new frame */
555 if (bcap_dev
->stop
) {
556 complete(&bcap_dev
->comp
);
558 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->cur_frm
->vb
, 0);
559 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
560 ppi
->ops
->start(ppi
);
563 spin_unlock(&bcap_dev
->lock
);
568 static int bcap_streamon(struct file
*file
, void *priv
,
569 enum v4l2_buf_type buf_type
)
571 struct bcap_device
*bcap_dev
= video_drvdata(file
);
572 struct bcap_fh
*fh
= file
->private_data
;
573 struct ppi_if
*ppi
= bcap_dev
->ppi
;
580 /* call streamon to start streaming in videobuf */
581 ret
= vb2_streamon(&bcap_dev
->buffer_queue
, buf_type
);
585 /* if dma queue is empty, return error */
586 if (list_empty(&bcap_dev
->dma_queue
)) {
587 v4l2_err(&bcap_dev
->v4l2_dev
, "dma queue is empty\n");
592 /* get the next frame from the dma queue */
593 bcap_dev
->cur_frm
= list_entry(bcap_dev
->dma_queue
.next
,
594 struct bcap_buffer
, list
);
595 /* remove buffer from the dma queue */
596 list_del(&bcap_dev
->cur_frm
->list
);
597 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->cur_frm
->vb
, 0);
598 /* update DMA address */
599 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
601 ppi
->ops
->start(ppi
);
605 vb2_streamoff(&bcap_dev
->buffer_queue
, buf_type
);
609 static int bcap_streamoff(struct file
*file
, void *priv
,
610 enum v4l2_buf_type buf_type
)
612 struct bcap_device
*bcap_dev
= video_drvdata(file
);
613 struct bcap_fh
*fh
= file
->private_data
;
618 return vb2_streamoff(&bcap_dev
->buffer_queue
, buf_type
);
621 static int bcap_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
623 struct bcap_device
*bcap_dev
= video_drvdata(file
);
625 return v4l2_subdev_call(bcap_dev
->sd
, video
, querystd
, std
);
628 static int bcap_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
630 struct bcap_device
*bcap_dev
= video_drvdata(file
);
632 *std
= bcap_dev
->std
;
636 static int bcap_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
638 struct bcap_device
*bcap_dev
= video_drvdata(file
);
641 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
644 ret
= v4l2_subdev_call(bcap_dev
->sd
, core
, s_std
, std
);
652 static int bcap_g_dv_timings(struct file
*file
, void *priv
,
653 struct v4l2_dv_timings
*timings
)
655 struct bcap_device
*bcap_dev
= video_drvdata(file
);
658 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
,
659 g_dv_timings
, timings
);
663 bcap_dev
->dv_timings
= *timings
;
667 static int bcap_s_dv_timings(struct file
*file
, void *priv
,
668 struct v4l2_dv_timings
*timings
)
670 struct bcap_device
*bcap_dev
= video_drvdata(file
);
672 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
675 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_dv_timings
, timings
);
679 bcap_dev
->dv_timings
= *timings
;
683 static int bcap_enum_input(struct file
*file
, void *priv
,
684 struct v4l2_input
*input
)
686 struct bcap_device
*bcap_dev
= video_drvdata(file
);
687 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
691 if (input
->index
>= config
->num_inputs
)
694 *input
= config
->inputs
[input
->index
];
695 /* get input status */
696 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_input_status
, &status
);
698 input
->status
= status
;
702 static int bcap_g_input(struct file
*file
, void *priv
, unsigned int *index
)
704 struct bcap_device
*bcap_dev
= video_drvdata(file
);
706 *index
= bcap_dev
->cur_input
;
710 static int bcap_s_input(struct file
*file
, void *priv
, unsigned int index
)
712 struct bcap_device
*bcap_dev
= video_drvdata(file
);
713 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
714 struct bcap_route
*route
;
717 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
720 if (index
>= config
->num_inputs
)
723 route
= &config
->routes
[index
];
724 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
725 route
->input
, route
->output
, 0);
726 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
727 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
730 bcap_dev
->cur_input
= index
;
731 /* if this route has specific config, update ppi control */
732 if (route
->ppi_control
)
733 config
->ppi_control
= route
->ppi_control
;
737 static int bcap_try_format(struct bcap_device
*bcap
,
738 struct v4l2_pix_format
*pixfmt
,
739 struct bcap_format
*bcap_fmt
)
741 struct bcap_format
*sf
= bcap
->sensor_formats
;
742 struct bcap_format
*fmt
= NULL
;
743 struct v4l2_mbus_framefmt mbus_fmt
;
746 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
748 if (pixfmt
->pixelformat
== fmt
->pixelformat
)
751 if (i
== bcap
->num_sensor_formats
)
754 v4l2_fill_mbus_format(&mbus_fmt
, pixfmt
, fmt
->mbus_code
);
755 ret
= v4l2_subdev_call(bcap
->sd
, video
,
756 try_mbus_fmt
, &mbus_fmt
);
759 v4l2_fill_pix_format(pixfmt
, &mbus_fmt
);
761 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
763 if (mbus_fmt
.code
== fmt
->mbus_code
)
768 pixfmt
->bytesperline
= pixfmt
->width
* fmt
->bpp
/ 8;
769 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
773 static int bcap_enum_fmt_vid_cap(struct file
*file
, void *priv
,
774 struct v4l2_fmtdesc
*fmt
)
776 struct bcap_device
*bcap_dev
= video_drvdata(file
);
777 struct bcap_format
*sf
= bcap_dev
->sensor_formats
;
779 if (fmt
->index
>= bcap_dev
->num_sensor_formats
)
782 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
783 strlcpy(fmt
->description
,
785 sizeof(fmt
->description
));
786 fmt
->pixelformat
= sf
[fmt
->index
].pixelformat
;
790 static int bcap_try_fmt_vid_cap(struct file
*file
, void *priv
,
791 struct v4l2_format
*fmt
)
793 struct bcap_device
*bcap_dev
= video_drvdata(file
);
794 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
796 return bcap_try_format(bcap_dev
, pixfmt
, NULL
);
799 static int bcap_g_fmt_vid_cap(struct file
*file
, void *priv
,
800 struct v4l2_format
*fmt
)
802 struct bcap_device
*bcap_dev
= video_drvdata(file
);
804 fmt
->fmt
.pix
= bcap_dev
->fmt
;
808 static int bcap_s_fmt_vid_cap(struct file
*file
, void *priv
,
809 struct v4l2_format
*fmt
)
811 struct bcap_device
*bcap_dev
= video_drvdata(file
);
812 struct v4l2_mbus_framefmt mbus_fmt
;
813 struct bcap_format bcap_fmt
;
814 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
817 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
820 /* see if format works */
821 ret
= bcap_try_format(bcap_dev
, pixfmt
, &bcap_fmt
);
825 v4l2_fill_mbus_format(&mbus_fmt
, pixfmt
, bcap_fmt
.mbus_code
);
826 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_mbus_fmt
, &mbus_fmt
);
829 bcap_dev
->fmt
= *pixfmt
;
830 bcap_dev
->bpp
= bcap_fmt
.bpp
;
831 bcap_dev
->dlen
= bcap_fmt
.dlen
;
835 static int bcap_querycap(struct file
*file
, void *priv
,
836 struct v4l2_capability
*cap
)
838 struct bcap_device
*bcap_dev
= video_drvdata(file
);
840 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
841 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
842 strlcpy(cap
->bus_info
, "Blackfin Platform", sizeof(cap
->bus_info
));
843 strlcpy(cap
->card
, bcap_dev
->cfg
->card_name
, sizeof(cap
->card
));
847 static int bcap_g_parm(struct file
*file
, void *fh
,
848 struct v4l2_streamparm
*a
)
850 struct bcap_device
*bcap_dev
= video_drvdata(file
);
852 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
854 return v4l2_subdev_call(bcap_dev
->sd
, video
, g_parm
, a
);
857 static int bcap_s_parm(struct file
*file
, void *fh
,
858 struct v4l2_streamparm
*a
)
860 struct bcap_device
*bcap_dev
= video_drvdata(file
);
862 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
864 return v4l2_subdev_call(bcap_dev
->sd
, video
, s_parm
, a
);
867 static int bcap_g_chip_ident(struct file
*file
, void *priv
,
868 struct v4l2_dbg_chip_ident
*chip
)
870 struct bcap_device
*bcap_dev
= video_drvdata(file
);
872 chip
->ident
= V4L2_IDENT_NONE
;
874 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
875 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
878 return v4l2_subdev_call(bcap_dev
->sd
, core
,
882 #ifdef CONFIG_VIDEO_ADV_DEBUG
883 static int bcap_dbg_g_register(struct file
*file
, void *priv
,
884 struct v4l2_dbg_register
*reg
)
886 struct bcap_device
*bcap_dev
= video_drvdata(file
);
888 return v4l2_subdev_call(bcap_dev
->sd
, core
,
892 static int bcap_dbg_s_register(struct file
*file
, void *priv
,
893 const struct v4l2_dbg_register
*reg
)
895 struct bcap_device
*bcap_dev
= video_drvdata(file
);
897 return v4l2_subdev_call(bcap_dev
->sd
, core
,
902 static int bcap_log_status(struct file
*file
, void *priv
)
904 struct bcap_device
*bcap_dev
= video_drvdata(file
);
905 /* status for sub devices */
906 v4l2_device_call_all(&bcap_dev
->v4l2_dev
, 0, core
, log_status
);
910 static const struct v4l2_ioctl_ops bcap_ioctl_ops
= {
911 .vidioc_querycap
= bcap_querycap
,
912 .vidioc_g_fmt_vid_cap
= bcap_g_fmt_vid_cap
,
913 .vidioc_enum_fmt_vid_cap
= bcap_enum_fmt_vid_cap
,
914 .vidioc_s_fmt_vid_cap
= bcap_s_fmt_vid_cap
,
915 .vidioc_try_fmt_vid_cap
= bcap_try_fmt_vid_cap
,
916 .vidioc_enum_input
= bcap_enum_input
,
917 .vidioc_g_input
= bcap_g_input
,
918 .vidioc_s_input
= bcap_s_input
,
919 .vidioc_querystd
= bcap_querystd
,
920 .vidioc_s_std
= bcap_s_std
,
921 .vidioc_g_std
= bcap_g_std
,
922 .vidioc_s_dv_timings
= bcap_s_dv_timings
,
923 .vidioc_g_dv_timings
= bcap_g_dv_timings
,
924 .vidioc_reqbufs
= bcap_reqbufs
,
925 .vidioc_querybuf
= bcap_querybuf
,
926 .vidioc_qbuf
= bcap_qbuf
,
927 .vidioc_dqbuf
= bcap_dqbuf
,
928 .vidioc_streamon
= bcap_streamon
,
929 .vidioc_streamoff
= bcap_streamoff
,
930 .vidioc_g_parm
= bcap_g_parm
,
931 .vidioc_s_parm
= bcap_s_parm
,
932 .vidioc_g_chip_ident
= bcap_g_chip_ident
,
933 #ifdef CONFIG_VIDEO_ADV_DEBUG
934 .vidioc_g_register
= bcap_dbg_g_register
,
935 .vidioc_s_register
= bcap_dbg_s_register
,
937 .vidioc_log_status
= bcap_log_status
,
940 static struct v4l2_file_operations bcap_fops
= {
941 .owner
= THIS_MODULE
,
943 .release
= bcap_release
,
944 .unlocked_ioctl
= video_ioctl2
,
947 .get_unmapped_area
= bcap_get_unmapped_area
,
952 static int bcap_probe(struct platform_device
*pdev
)
954 struct bcap_device
*bcap_dev
;
955 struct video_device
*vfd
;
956 struct i2c_adapter
*i2c_adap
;
957 struct bfin_capture_config
*config
;
959 struct bcap_route
*route
;
962 config
= pdev
->dev
.platform_data
;
964 v4l2_err(pdev
->dev
.driver
, "Unable to get board config\n");
968 bcap_dev
= kzalloc(sizeof(*bcap_dev
), GFP_KERNEL
);
970 v4l2_err(pdev
->dev
.driver
, "Unable to alloc bcap_dev\n");
974 bcap_dev
->cfg
= config
;
976 bcap_dev
->ppi
= ppi_create_instance(config
->ppi_info
);
977 if (!bcap_dev
->ppi
) {
978 v4l2_err(pdev
->dev
.driver
, "Unable to create ppi\n");
982 bcap_dev
->ppi
->priv
= bcap_dev
;
984 bcap_dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
985 if (IS_ERR(bcap_dev
->alloc_ctx
)) {
986 ret
= PTR_ERR(bcap_dev
->alloc_ctx
);
990 vfd
= video_device_alloc();
993 v4l2_err(pdev
->dev
.driver
, "Unable to alloc video device\n");
994 goto err_cleanup_ctx
;
997 /* initialize field of video device */
998 vfd
->release
= video_device_release
;
999 vfd
->fops
= &bcap_fops
;
1000 vfd
->ioctl_ops
= &bcap_ioctl_ops
;
1002 vfd
->v4l2_dev
= &bcap_dev
->v4l2_dev
;
1003 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
1004 strncpy(vfd
->name
, CAPTURE_DRV_NAME
, sizeof(vfd
->name
));
1005 bcap_dev
->video_dev
= vfd
;
1007 ret
= v4l2_device_register(&pdev
->dev
, &bcap_dev
->v4l2_dev
);
1009 v4l2_err(pdev
->dev
.driver
,
1010 "Unable to register v4l2 device\n");
1011 goto err_release_vdev
;
1013 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 device registered\n");
1015 bcap_dev
->v4l2_dev
.ctrl_handler
= &bcap_dev
->ctrl_handler
;
1016 ret
= v4l2_ctrl_handler_init(&bcap_dev
->ctrl_handler
, 0);
1018 v4l2_err(&bcap_dev
->v4l2_dev
,
1019 "Unable to init control handler\n");
1020 goto err_unreg_v4l2
;
1023 spin_lock_init(&bcap_dev
->lock
);
1024 /* initialize queue */
1025 q
= &bcap_dev
->buffer_queue
;
1026 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1027 q
->io_modes
= VB2_MMAP
;
1028 q
->drv_priv
= bcap_dev
;
1029 q
->buf_struct_size
= sizeof(struct bcap_buffer
);
1030 q
->ops
= &bcap_video_qops
;
1031 q
->mem_ops
= &vb2_dma_contig_memops
;
1032 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1036 mutex_init(&bcap_dev
->mutex
);
1037 init_completion(&bcap_dev
->comp
);
1039 /* init video dma queues */
1040 INIT_LIST_HEAD(&bcap_dev
->dma_queue
);
1042 vfd
->lock
= &bcap_dev
->mutex
;
1044 /* register video device */
1045 ret
= video_register_device(bcap_dev
->video_dev
, VFL_TYPE_GRABBER
, -1);
1047 v4l2_err(&bcap_dev
->v4l2_dev
,
1048 "Unable to register video device\n");
1049 goto err_free_handler
;
1051 video_set_drvdata(bcap_dev
->video_dev
, bcap_dev
);
1052 v4l2_info(&bcap_dev
->v4l2_dev
, "video device registered as: %s\n",
1053 video_device_node_name(vfd
));
1055 /* load up the subdevice */
1056 i2c_adap
= i2c_get_adapter(config
->i2c_adapter_id
);
1058 v4l2_err(&bcap_dev
->v4l2_dev
,
1059 "Unable to find i2c adapter\n");
1061 goto err_unreg_vdev
;
1064 bcap_dev
->sd
= v4l2_i2c_new_subdev_board(&bcap_dev
->v4l2_dev
,
1066 &config
->board_info
,
1070 if (!config
->num_inputs
) {
1071 v4l2_err(&bcap_dev
->v4l2_dev
,
1072 "Unable to work without input\n");
1073 goto err_unreg_vdev
;
1076 /* update tvnorms from the sub devices */
1077 for (i
= 0; i
< config
->num_inputs
; i
++)
1078 vfd
->tvnorms
|= config
->inputs
[i
].std
;
1080 v4l2_err(&bcap_dev
->v4l2_dev
,
1081 "Unable to register sub device\n");
1082 goto err_unreg_vdev
;
1085 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 sub device registered\n");
1088 * explicitly set input, otherwise some boards
1089 * may not work at the state as we expected
1091 route
= &config
->routes
[0];
1092 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
1093 route
->input
, route
->output
, 0);
1094 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
1095 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
1096 goto err_unreg_vdev
;
1098 bcap_dev
->cur_input
= 0;
1099 /* if this route has specific config, update ppi control */
1100 if (route
->ppi_control
)
1101 config
->ppi_control
= route
->ppi_control
;
1103 /* now we can probe the default state */
1104 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_STD
) {
1106 ret
= v4l2_subdev_call(bcap_dev
->sd
, core
, g_std
, &std
);
1108 v4l2_err(&bcap_dev
->v4l2_dev
,
1109 "Unable to get std\n");
1110 goto err_unreg_vdev
;
1112 bcap_dev
->std
= std
;
1114 if (config
->inputs
[0].capabilities
& V4L2_IN_CAP_DV_TIMINGS
) {
1115 struct v4l2_dv_timings dv_timings
;
1116 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
,
1117 g_dv_timings
, &dv_timings
);
1119 v4l2_err(&bcap_dev
->v4l2_dev
,
1120 "Unable to get dv timings\n");
1121 goto err_unreg_vdev
;
1123 bcap_dev
->dv_timings
= dv_timings
;
1125 ret
= bcap_init_sensor_formats(bcap_dev
);
1127 v4l2_err(&bcap_dev
->v4l2_dev
,
1128 "Unable to create sensor formats table\n");
1129 goto err_unreg_vdev
;
1133 video_unregister_device(bcap_dev
->video_dev
);
1134 bcap_dev
->video_dev
= NULL
;
1136 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
1138 v4l2_device_unregister(&bcap_dev
->v4l2_dev
);
1140 if (bcap_dev
->video_dev
)
1141 video_device_release(bcap_dev
->video_dev
);
1143 vb2_dma_contig_cleanup_ctx(bcap_dev
->alloc_ctx
);
1145 ppi_delete_instance(bcap_dev
->ppi
);
1151 static int bcap_remove(struct platform_device
*pdev
)
1153 struct v4l2_device
*v4l2_dev
= platform_get_drvdata(pdev
);
1154 struct bcap_device
*bcap_dev
= container_of(v4l2_dev
,
1155 struct bcap_device
, v4l2_dev
);
1157 bcap_free_sensor_formats(bcap_dev
);
1158 video_unregister_device(bcap_dev
->video_dev
);
1159 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
1160 v4l2_device_unregister(v4l2_dev
);
1161 vb2_dma_contig_cleanup_ctx(bcap_dev
->alloc_ctx
);
1162 ppi_delete_instance(bcap_dev
->ppi
);
1167 static struct platform_driver bcap_driver
= {
1169 .name
= CAPTURE_DRV_NAME
,
1170 .owner
= THIS_MODULE
,
1172 .probe
= bcap_probe
,
1173 .remove
= bcap_remove
,
1175 module_platform_driver(bcap_driver
);
1177 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1178 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1179 MODULE_LICENSE("GPL v2");