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 */
59 struct list_head list
;
63 /* capture device instance */
64 struct v4l2_device v4l2_dev
;
65 /* v4l2 control handler */
66 struct v4l2_ctrl_handler ctrl_handler
;
67 /* device node data */
68 struct video_device
*video_dev
;
69 /* sub device instance */
70 struct v4l2_subdev
*sd
;
72 struct bfin_capture_config
*cfg
;
76 unsigned int cur_input
;
77 /* current selected standard */
79 /* used to store pixel format */
80 struct v4l2_pix_format fmt
;
83 /* used to store sensor supported format */
84 struct bcap_format
*sensor_formats
;
85 /* number of sensor formats array */
86 int num_sensor_formats
;
87 /* pointing to current video buffer */
88 struct bcap_buffer
*cur_frm
;
89 /* pointing to next video buffer */
90 struct bcap_buffer
*next_frm
;
91 /* buffer queue used in videobuf2 */
92 struct vb2_queue buffer_queue
;
93 /* allocator-specific contexts for each plane */
94 struct vb2_alloc_ctx
*alloc_ctx
;
95 /* queue of filled frames */
96 struct list_head dma_queue
;
97 /* used in videobuf2 callback */
99 /* used to access capture device */
101 /* used to wait ppi to complete one transfer */
102 struct completion comp
;
103 /* prepare to stop */
109 /* indicates whether this file handle is doing IO */
113 static const struct bcap_format bcap_formats
[] = {
115 .desc
= "YCbCr 4:2:2 Interleaved UYVY",
116 .pixelformat
= V4L2_PIX_FMT_UYVY
,
117 .mbus_code
= V4L2_MBUS_FMT_UYVY8_2X8
,
121 .desc
= "YCbCr 4:2:2 Interleaved YUYV",
122 .pixelformat
= V4L2_PIX_FMT_YUYV
,
123 .mbus_code
= V4L2_MBUS_FMT_YUYV8_2X8
,
128 .pixelformat
= V4L2_PIX_FMT_RGB565
,
129 .mbus_code
= V4L2_MBUS_FMT_RGB565_2X8_LE
,
134 .pixelformat
= V4L2_PIX_FMT_RGB444
,
135 .mbus_code
= V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE
,
140 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
142 static irqreturn_t
bcap_isr(int irq
, void *dev_id
);
144 static struct bcap_buffer
*to_bcap_vb(struct vb2_buffer
*vb
)
146 return container_of(vb
, struct bcap_buffer
, vb
);
149 static int bcap_init_sensor_formats(struct bcap_device
*bcap_dev
)
151 enum v4l2_mbus_pixelcode code
;
152 struct bcap_format
*sf
;
153 unsigned int num_formats
= 0;
156 while (!v4l2_subdev_call(bcap_dev
->sd
, video
,
157 enum_mbus_fmt
, num_formats
, &code
))
162 sf
= kzalloc(num_formats
* sizeof(*sf
), GFP_KERNEL
);
166 for (i
= 0; i
< num_formats
; i
++) {
167 v4l2_subdev_call(bcap_dev
->sd
, video
,
168 enum_mbus_fmt
, i
, &code
);
169 for (j
= 0; j
< BCAP_MAX_FMTS
; j
++)
170 if (code
== bcap_formats
[j
].mbus_code
)
172 if (j
== BCAP_MAX_FMTS
) {
173 /* we don't allow this sensor working with our bridge */
177 sf
[i
] = bcap_formats
[j
];
179 bcap_dev
->sensor_formats
= sf
;
180 bcap_dev
->num_sensor_formats
= num_formats
;
184 static void bcap_free_sensor_formats(struct bcap_device
*bcap_dev
)
186 bcap_dev
->num_sensor_formats
= 0;
187 kfree(bcap_dev
->sensor_formats
);
188 bcap_dev
->sensor_formats
= NULL
;
191 static int bcap_open(struct file
*file
)
193 struct bcap_device
*bcap_dev
= video_drvdata(file
);
194 struct video_device
*vfd
= bcap_dev
->video_dev
;
195 struct bcap_fh
*bcap_fh
;
198 v4l2_err(&bcap_dev
->v4l2_dev
, "No sub device registered\n");
202 bcap_fh
= kzalloc(sizeof(*bcap_fh
), GFP_KERNEL
);
204 v4l2_err(&bcap_dev
->v4l2_dev
,
205 "unable to allocate memory for file handle object\n");
209 v4l2_fh_init(&bcap_fh
->fh
, vfd
);
211 /* store pointer to v4l2_fh in private_data member of file */
212 file
->private_data
= &bcap_fh
->fh
;
213 v4l2_fh_add(&bcap_fh
->fh
);
214 bcap_fh
->io_allowed
= false;
218 static int bcap_release(struct file
*file
)
220 struct bcap_device
*bcap_dev
= video_drvdata(file
);
221 struct v4l2_fh
*fh
= file
->private_data
;
222 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
224 /* if this instance is doing IO */
225 if (bcap_fh
->io_allowed
)
226 vb2_queue_release(&bcap_dev
->buffer_queue
);
228 file
->private_data
= NULL
;
229 v4l2_fh_del(&bcap_fh
->fh
);
230 v4l2_fh_exit(&bcap_fh
->fh
);
235 static int bcap_mmap(struct file
*file
, struct vm_area_struct
*vma
)
237 struct bcap_device
*bcap_dev
= video_drvdata(file
);
240 if (mutex_lock_interruptible(&bcap_dev
->mutex
))
242 ret
= vb2_mmap(&bcap_dev
->buffer_queue
, vma
);
243 mutex_unlock(&bcap_dev
->mutex
);
248 static unsigned long bcap_get_unmapped_area(struct file
*file
,
254 struct bcap_device
*bcap_dev
= video_drvdata(file
);
256 return vb2_get_unmapped_area(&bcap_dev
->buffer_queue
,
264 static unsigned int bcap_poll(struct file
*file
, poll_table
*wait
)
266 struct bcap_device
*bcap_dev
= video_drvdata(file
);
269 mutex_lock(&bcap_dev
->mutex
);
270 res
= vb2_poll(&bcap_dev
->buffer_queue
, file
, wait
);
271 mutex_unlock(&bcap_dev
->mutex
);
275 static int bcap_queue_setup(struct vb2_queue
*vq
,
276 const struct v4l2_format
*fmt
,
277 unsigned int *nbuffers
, unsigned int *nplanes
,
278 unsigned int sizes
[], void *alloc_ctxs
[])
280 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
282 if (*nbuffers
< BCAP_MIN_NUM_BUF
)
283 *nbuffers
= BCAP_MIN_NUM_BUF
;
286 sizes
[0] = bcap_dev
->fmt
.sizeimage
;
287 alloc_ctxs
[0] = bcap_dev
->alloc_ctx
;
292 static int bcap_buffer_init(struct vb2_buffer
*vb
)
294 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
296 INIT_LIST_HEAD(&buf
->list
);
300 static int bcap_buffer_prepare(struct vb2_buffer
*vb
)
302 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
303 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
306 size
= bcap_dev
->fmt
.sizeimage
;
307 if (vb2_plane_size(vb
, 0) < size
) {
308 v4l2_err(&bcap_dev
->v4l2_dev
, "buffer too small (%lu < %lu)\n",
309 vb2_plane_size(vb
, 0), size
);
312 vb2_set_plane_payload(&buf
->vb
, 0, size
);
317 static void bcap_buffer_queue(struct vb2_buffer
*vb
)
319 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
320 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
323 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
324 list_add_tail(&buf
->list
, &bcap_dev
->dma_queue
);
325 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
328 static void bcap_buffer_cleanup(struct vb2_buffer
*vb
)
330 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vb
->vb2_queue
);
331 struct bcap_buffer
*buf
= to_bcap_vb(vb
);
334 spin_lock_irqsave(&bcap_dev
->lock
, flags
);
335 list_del_init(&buf
->list
);
336 spin_unlock_irqrestore(&bcap_dev
->lock
, flags
);
339 static void bcap_lock(struct vb2_queue
*vq
)
341 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
342 mutex_lock(&bcap_dev
->mutex
);
345 static void bcap_unlock(struct vb2_queue
*vq
)
347 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
348 mutex_unlock(&bcap_dev
->mutex
);
351 static int bcap_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
353 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
354 struct ppi_if
*ppi
= bcap_dev
->ppi
;
355 struct ppi_params params
;
358 /* enable streamon on the sub device */
359 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 1);
360 if (ret
&& (ret
!= -ENOIOCTLCMD
)) {
361 v4l2_err(&bcap_dev
->v4l2_dev
, "stream on failed in subdev\n");
366 params
.width
= bcap_dev
->fmt
.width
;
367 params
.height
= bcap_dev
->fmt
.height
;
368 params
.bpp
= bcap_dev
->bpp
;
369 params
.ppi_control
= bcap_dev
->cfg
->ppi_control
;
370 params
.int_mask
= bcap_dev
->cfg
->int_mask
;
371 params
.blank_clocks
= bcap_dev
->cfg
->blank_clocks
;
372 ret
= ppi
->ops
->set_params(ppi
, ¶ms
);
374 v4l2_err(&bcap_dev
->v4l2_dev
,
375 "Error in setting ppi params\n");
379 /* attach ppi DMA irq handler */
380 ret
= ppi
->ops
->attach_irq(ppi
, bcap_isr
);
382 v4l2_err(&bcap_dev
->v4l2_dev
,
383 "Error in attaching interrupt handler\n");
387 INIT_COMPLETION(bcap_dev
->comp
);
388 bcap_dev
->stop
= false;
392 static int bcap_stop_streaming(struct vb2_queue
*vq
)
394 struct bcap_device
*bcap_dev
= vb2_get_drv_priv(vq
);
395 struct ppi_if
*ppi
= bcap_dev
->ppi
;
398 if (!vb2_is_streaming(vq
))
401 bcap_dev
->stop
= true;
402 wait_for_completion(&bcap_dev
->comp
);
404 ppi
->ops
->detach_irq(ppi
);
405 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_stream
, 0);
406 if (ret
&& (ret
!= -ENOIOCTLCMD
))
407 v4l2_err(&bcap_dev
->v4l2_dev
,
408 "stream off failed in subdev\n");
410 /* release all active buffers */
411 while (!list_empty(&bcap_dev
->dma_queue
)) {
412 bcap_dev
->next_frm
= list_entry(bcap_dev
->dma_queue
.next
,
413 struct bcap_buffer
, list
);
414 list_del(&bcap_dev
->next_frm
->list
);
415 vb2_buffer_done(&bcap_dev
->next_frm
->vb
, VB2_BUF_STATE_ERROR
);
420 static struct vb2_ops bcap_video_qops
= {
421 .queue_setup
= bcap_queue_setup
,
422 .buf_init
= bcap_buffer_init
,
423 .buf_prepare
= bcap_buffer_prepare
,
424 .buf_cleanup
= bcap_buffer_cleanup
,
425 .buf_queue
= bcap_buffer_queue
,
426 .wait_prepare
= bcap_unlock
,
427 .wait_finish
= bcap_lock
,
428 .start_streaming
= bcap_start_streaming
,
429 .stop_streaming
= bcap_stop_streaming
,
432 static int bcap_reqbufs(struct file
*file
, void *priv
,
433 struct v4l2_requestbuffers
*req_buf
)
435 struct bcap_device
*bcap_dev
= video_drvdata(file
);
436 struct vb2_queue
*vq
= &bcap_dev
->buffer_queue
;
437 struct v4l2_fh
*fh
= file
->private_data
;
438 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
443 bcap_fh
->io_allowed
= true;
445 return vb2_reqbufs(vq
, req_buf
);
448 static int bcap_querybuf(struct file
*file
, void *priv
,
449 struct v4l2_buffer
*buf
)
451 struct bcap_device
*bcap_dev
= video_drvdata(file
);
453 return vb2_querybuf(&bcap_dev
->buffer_queue
, buf
);
456 static int bcap_qbuf(struct file
*file
, void *priv
,
457 struct v4l2_buffer
*buf
)
459 struct bcap_device
*bcap_dev
= video_drvdata(file
);
460 struct v4l2_fh
*fh
= file
->private_data
;
461 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
463 if (!bcap_fh
->io_allowed
)
466 return vb2_qbuf(&bcap_dev
->buffer_queue
, buf
);
469 static int bcap_dqbuf(struct file
*file
, void *priv
,
470 struct v4l2_buffer
*buf
)
472 struct bcap_device
*bcap_dev
= video_drvdata(file
);
473 struct v4l2_fh
*fh
= file
->private_data
;
474 struct bcap_fh
*bcap_fh
= container_of(fh
, struct bcap_fh
, fh
);
476 if (!bcap_fh
->io_allowed
)
479 return vb2_dqbuf(&bcap_dev
->buffer_queue
,
480 buf
, file
->f_flags
& O_NONBLOCK
);
483 static irqreturn_t
bcap_isr(int irq
, void *dev_id
)
485 struct ppi_if
*ppi
= dev_id
;
486 struct bcap_device
*bcap_dev
= ppi
->priv
;
487 struct timeval timevalue
;
488 struct vb2_buffer
*vb
= &bcap_dev
->cur_frm
->vb
;
491 spin_lock(&bcap_dev
->lock
);
493 if (bcap_dev
->cur_frm
!= bcap_dev
->next_frm
) {
494 do_gettimeofday(&timevalue
);
495 vb
->v4l2_buf
.timestamp
= timevalue
;
496 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
497 bcap_dev
->cur_frm
= bcap_dev
->next_frm
;
502 if (bcap_dev
->stop
) {
503 complete(&bcap_dev
->comp
);
505 if (!list_empty(&bcap_dev
->dma_queue
)) {
506 bcap_dev
->next_frm
= list_entry(bcap_dev
->dma_queue
.next
,
507 struct bcap_buffer
, list
);
508 list_del(&bcap_dev
->next_frm
->list
);
509 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->next_frm
->vb
, 0);
510 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
512 ppi
->ops
->start(ppi
);
515 spin_unlock(&bcap_dev
->lock
);
520 static int bcap_streamon(struct file
*file
, void *priv
,
521 enum v4l2_buf_type buf_type
)
523 struct bcap_device
*bcap_dev
= video_drvdata(file
);
524 struct bcap_fh
*fh
= file
->private_data
;
525 struct ppi_if
*ppi
= bcap_dev
->ppi
;
532 /* call streamon to start streaming in videobuf */
533 ret
= vb2_streamon(&bcap_dev
->buffer_queue
, buf_type
);
537 /* if dma queue is empty, return error */
538 if (list_empty(&bcap_dev
->dma_queue
)) {
539 v4l2_err(&bcap_dev
->v4l2_dev
, "dma queue is empty\n");
544 /* get the next frame from the dma queue */
545 bcap_dev
->next_frm
= list_entry(bcap_dev
->dma_queue
.next
,
546 struct bcap_buffer
, list
);
547 bcap_dev
->cur_frm
= bcap_dev
->next_frm
;
548 /* remove buffer from the dma queue */
549 list_del(&bcap_dev
->cur_frm
->list
);
550 addr
= vb2_dma_contig_plane_dma_addr(&bcap_dev
->cur_frm
->vb
, 0);
551 /* update DMA address */
552 ppi
->ops
->update_addr(ppi
, (unsigned long)addr
);
554 ppi
->ops
->start(ppi
);
558 vb2_streamoff(&bcap_dev
->buffer_queue
, buf_type
);
562 static int bcap_streamoff(struct file
*file
, void *priv
,
563 enum v4l2_buf_type buf_type
)
565 struct bcap_device
*bcap_dev
= video_drvdata(file
);
566 struct bcap_fh
*fh
= file
->private_data
;
571 return vb2_streamoff(&bcap_dev
->buffer_queue
, buf_type
);
574 static int bcap_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
576 struct bcap_device
*bcap_dev
= video_drvdata(file
);
578 return v4l2_subdev_call(bcap_dev
->sd
, video
, querystd
, std
);
581 static int bcap_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
583 struct bcap_device
*bcap_dev
= video_drvdata(file
);
585 *std
= bcap_dev
->std
;
589 static int bcap_s_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
591 struct bcap_device
*bcap_dev
= video_drvdata(file
);
594 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
597 ret
= v4l2_subdev_call(bcap_dev
->sd
, core
, s_std
, *std
);
601 bcap_dev
->std
= *std
;
605 static int bcap_enum_input(struct file
*file
, void *priv
,
606 struct v4l2_input
*input
)
608 struct bcap_device
*bcap_dev
= video_drvdata(file
);
609 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
613 if (input
->index
>= config
->num_inputs
)
616 *input
= config
->inputs
[input
->index
];
617 /* get input status */
618 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, g_input_status
, &status
);
620 input
->status
= status
;
624 static int bcap_g_input(struct file
*file
, void *priv
, unsigned int *index
)
626 struct bcap_device
*bcap_dev
= video_drvdata(file
);
628 *index
= bcap_dev
->cur_input
;
632 static int bcap_s_input(struct file
*file
, void *priv
, unsigned int index
)
634 struct bcap_device
*bcap_dev
= video_drvdata(file
);
635 struct bfin_capture_config
*config
= bcap_dev
->cfg
;
636 struct bcap_route
*route
;
639 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
642 if (index
>= config
->num_inputs
)
645 route
= &config
->routes
[index
];
646 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_routing
,
647 route
->input
, route
->output
, 0);
648 if ((ret
< 0) && (ret
!= -ENOIOCTLCMD
)) {
649 v4l2_err(&bcap_dev
->v4l2_dev
, "Failed to set input\n");
652 bcap_dev
->cur_input
= index
;
656 static int bcap_try_format(struct bcap_device
*bcap
,
657 struct v4l2_pix_format
*pixfmt
,
658 enum v4l2_mbus_pixelcode
*mbus_code
,
661 struct bcap_format
*sf
= bcap
->sensor_formats
;
662 struct bcap_format
*fmt
= NULL
;
663 struct v4l2_mbus_framefmt mbus_fmt
;
666 for (i
= 0; i
< bcap
->num_sensor_formats
; i
++) {
668 if (pixfmt
->pixelformat
== fmt
->pixelformat
)
671 if (i
== bcap
->num_sensor_formats
)
675 *mbus_code
= fmt
->mbus_code
;
678 v4l2_fill_mbus_format(&mbus_fmt
, pixfmt
, fmt
->mbus_code
);
679 ret
= v4l2_subdev_call(bcap
->sd
, video
,
680 try_mbus_fmt
, &mbus_fmt
);
683 v4l2_fill_pix_format(pixfmt
, &mbus_fmt
);
684 pixfmt
->bytesperline
= pixfmt
->width
* fmt
->bpp
/ 8;
685 pixfmt
->sizeimage
= pixfmt
->bytesperline
* pixfmt
->height
;
689 static int bcap_enum_fmt_vid_cap(struct file
*file
, void *priv
,
690 struct v4l2_fmtdesc
*fmt
)
692 struct bcap_device
*bcap_dev
= video_drvdata(file
);
693 struct bcap_format
*sf
= bcap_dev
->sensor_formats
;
695 if (fmt
->index
>= bcap_dev
->num_sensor_formats
)
698 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
699 strlcpy(fmt
->description
,
701 sizeof(fmt
->description
));
702 fmt
->pixelformat
= sf
[fmt
->index
].pixelformat
;
706 static int bcap_try_fmt_vid_cap(struct file
*file
, void *priv
,
707 struct v4l2_format
*fmt
)
709 struct bcap_device
*bcap_dev
= video_drvdata(file
);
710 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
712 return bcap_try_format(bcap_dev
, pixfmt
, NULL
, NULL
);
715 static int bcap_g_fmt_vid_cap(struct file
*file
, void *priv
,
716 struct v4l2_format
*fmt
)
718 struct bcap_device
*bcap_dev
= video_drvdata(file
);
720 fmt
->fmt
.pix
= bcap_dev
->fmt
;
724 static int bcap_s_fmt_vid_cap(struct file
*file
, void *priv
,
725 struct v4l2_format
*fmt
)
727 struct bcap_device
*bcap_dev
= video_drvdata(file
);
728 struct v4l2_mbus_framefmt mbus_fmt
;
729 enum v4l2_mbus_pixelcode mbus_code
;
730 struct v4l2_pix_format
*pixfmt
= &fmt
->fmt
.pix
;
733 if (vb2_is_busy(&bcap_dev
->buffer_queue
))
736 /* see if format works */
737 ret
= bcap_try_format(bcap_dev
, pixfmt
, &mbus_code
, &bpp
);
741 v4l2_fill_mbus_format(&mbus_fmt
, pixfmt
, mbus_code
);
742 ret
= v4l2_subdev_call(bcap_dev
->sd
, video
, s_mbus_fmt
, &mbus_fmt
);
745 bcap_dev
->fmt
= *pixfmt
;
750 static int bcap_querycap(struct file
*file
, void *priv
,
751 struct v4l2_capability
*cap
)
753 struct bcap_device
*bcap_dev
= video_drvdata(file
);
755 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
;
756 strlcpy(cap
->driver
, CAPTURE_DRV_NAME
, sizeof(cap
->driver
));
757 strlcpy(cap
->bus_info
, "Blackfin Platform", sizeof(cap
->bus_info
));
758 strlcpy(cap
->card
, bcap_dev
->cfg
->card_name
, sizeof(cap
->card
));
762 static int bcap_g_parm(struct file
*file
, void *fh
,
763 struct v4l2_streamparm
*a
)
765 struct bcap_device
*bcap_dev
= video_drvdata(file
);
767 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
769 return v4l2_subdev_call(bcap_dev
->sd
, video
, g_parm
, a
);
772 static int bcap_s_parm(struct file
*file
, void *fh
,
773 struct v4l2_streamparm
*a
)
775 struct bcap_device
*bcap_dev
= video_drvdata(file
);
777 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
779 return v4l2_subdev_call(bcap_dev
->sd
, video
, s_parm
, a
);
782 static int bcap_g_chip_ident(struct file
*file
, void *priv
,
783 struct v4l2_dbg_chip_ident
*chip
)
785 struct bcap_device
*bcap_dev
= video_drvdata(file
);
787 chip
->ident
= V4L2_IDENT_NONE
;
789 if (chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_DRIVER
&&
790 chip
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
793 return v4l2_subdev_call(bcap_dev
->sd
, core
,
797 #ifdef CONFIG_VIDEO_ADV_DEBUG
798 static int bcap_dbg_g_register(struct file
*file
, void *priv
,
799 struct v4l2_dbg_register
*reg
)
801 struct bcap_device
*bcap_dev
= video_drvdata(file
);
803 return v4l2_subdev_call(bcap_dev
->sd
, core
,
807 static int bcap_dbg_s_register(struct file
*file
, void *priv
,
808 struct v4l2_dbg_register
*reg
)
810 struct bcap_device
*bcap_dev
= video_drvdata(file
);
812 return v4l2_subdev_call(bcap_dev
->sd
, core
,
817 static int bcap_log_status(struct file
*file
, void *priv
)
819 struct bcap_device
*bcap_dev
= video_drvdata(file
);
820 /* status for sub devices */
821 v4l2_device_call_all(&bcap_dev
->v4l2_dev
, 0, core
, log_status
);
825 static const struct v4l2_ioctl_ops bcap_ioctl_ops
= {
826 .vidioc_querycap
= bcap_querycap
,
827 .vidioc_g_fmt_vid_cap
= bcap_g_fmt_vid_cap
,
828 .vidioc_enum_fmt_vid_cap
= bcap_enum_fmt_vid_cap
,
829 .vidioc_s_fmt_vid_cap
= bcap_s_fmt_vid_cap
,
830 .vidioc_try_fmt_vid_cap
= bcap_try_fmt_vid_cap
,
831 .vidioc_enum_input
= bcap_enum_input
,
832 .vidioc_g_input
= bcap_g_input
,
833 .vidioc_s_input
= bcap_s_input
,
834 .vidioc_querystd
= bcap_querystd
,
835 .vidioc_s_std
= bcap_s_std
,
836 .vidioc_g_std
= bcap_g_std
,
837 .vidioc_reqbufs
= bcap_reqbufs
,
838 .vidioc_querybuf
= bcap_querybuf
,
839 .vidioc_qbuf
= bcap_qbuf
,
840 .vidioc_dqbuf
= bcap_dqbuf
,
841 .vidioc_streamon
= bcap_streamon
,
842 .vidioc_streamoff
= bcap_streamoff
,
843 .vidioc_g_parm
= bcap_g_parm
,
844 .vidioc_s_parm
= bcap_s_parm
,
845 .vidioc_g_chip_ident
= bcap_g_chip_ident
,
846 #ifdef CONFIG_VIDEO_ADV_DEBUG
847 .vidioc_g_register
= bcap_dbg_g_register
,
848 .vidioc_s_register
= bcap_dbg_s_register
,
850 .vidioc_log_status
= bcap_log_status
,
853 static struct v4l2_file_operations bcap_fops
= {
854 .owner
= THIS_MODULE
,
856 .release
= bcap_release
,
857 .unlocked_ioctl
= video_ioctl2
,
860 .get_unmapped_area
= bcap_get_unmapped_area
,
865 static int __devinit
bcap_probe(struct platform_device
*pdev
)
867 struct bcap_device
*bcap_dev
;
868 struct video_device
*vfd
;
869 struct i2c_adapter
*i2c_adap
;
870 struct bfin_capture_config
*config
;
874 config
= pdev
->dev
.platform_data
;
876 v4l2_err(pdev
->dev
.driver
, "Unable to get board config\n");
880 bcap_dev
= kzalloc(sizeof(*bcap_dev
), GFP_KERNEL
);
882 v4l2_err(pdev
->dev
.driver
, "Unable to alloc bcap_dev\n");
886 bcap_dev
->cfg
= config
;
888 bcap_dev
->ppi
= ppi_create_instance(config
->ppi_info
);
889 if (!bcap_dev
->ppi
) {
890 v4l2_err(pdev
->dev
.driver
, "Unable to create ppi\n");
894 bcap_dev
->ppi
->priv
= bcap_dev
;
896 bcap_dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&pdev
->dev
);
897 if (IS_ERR(bcap_dev
->alloc_ctx
)) {
898 ret
= PTR_ERR(bcap_dev
->alloc_ctx
);
902 vfd
= video_device_alloc();
905 v4l2_err(pdev
->dev
.driver
, "Unable to alloc video device\n");
906 goto err_cleanup_ctx
;
909 /* initialize field of video device */
910 vfd
->release
= video_device_release
;
911 vfd
->fops
= &bcap_fops
;
912 vfd
->ioctl_ops
= &bcap_ioctl_ops
;
914 vfd
->v4l2_dev
= &bcap_dev
->v4l2_dev
;
915 set_bit(V4L2_FL_USE_FH_PRIO
, &vfd
->flags
);
916 strncpy(vfd
->name
, CAPTURE_DRV_NAME
, sizeof(vfd
->name
));
917 bcap_dev
->video_dev
= vfd
;
919 ret
= v4l2_device_register(&pdev
->dev
, &bcap_dev
->v4l2_dev
);
921 v4l2_err(pdev
->dev
.driver
,
922 "Unable to register v4l2 device\n");
923 goto err_release_vdev
;
925 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 device registered\n");
927 bcap_dev
->v4l2_dev
.ctrl_handler
= &bcap_dev
->ctrl_handler
;
928 ret
= v4l2_ctrl_handler_init(&bcap_dev
->ctrl_handler
, 0);
930 v4l2_err(&bcap_dev
->v4l2_dev
,
931 "Unable to init control handler\n");
935 spin_lock_init(&bcap_dev
->lock
);
936 /* initialize queue */
937 q
= &bcap_dev
->buffer_queue
;
938 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
939 q
->io_modes
= VB2_MMAP
;
940 q
->drv_priv
= bcap_dev
;
941 q
->buf_struct_size
= sizeof(struct bcap_buffer
);
942 q
->ops
= &bcap_video_qops
;
943 q
->mem_ops
= &vb2_dma_contig_memops
;
947 mutex_init(&bcap_dev
->mutex
);
948 init_completion(&bcap_dev
->comp
);
950 /* init video dma queues */
951 INIT_LIST_HEAD(&bcap_dev
->dma_queue
);
953 vfd
->lock
= &bcap_dev
->mutex
;
955 /* register video device */
956 ret
= video_register_device(bcap_dev
->video_dev
, VFL_TYPE_GRABBER
, -1);
958 v4l2_err(&bcap_dev
->v4l2_dev
,
959 "Unable to register video device\n");
960 goto err_free_handler
;
962 video_set_drvdata(bcap_dev
->video_dev
, bcap_dev
);
963 v4l2_info(&bcap_dev
->v4l2_dev
, "video device registered as: %s\n",
964 video_device_node_name(vfd
));
966 /* load up the subdevice */
967 i2c_adap
= i2c_get_adapter(config
->i2c_adapter_id
);
969 v4l2_err(&bcap_dev
->v4l2_dev
,
970 "Unable to find i2c adapter\n");
975 bcap_dev
->sd
= v4l2_i2c_new_subdev_board(&bcap_dev
->v4l2_dev
,
981 /* update tvnorms from the sub devices */
982 for (i
= 0; i
< config
->num_inputs
; i
++)
983 vfd
->tvnorms
|= config
->inputs
[i
].std
;
985 v4l2_err(&bcap_dev
->v4l2_dev
,
986 "Unable to register sub device\n");
990 v4l2_info(&bcap_dev
->v4l2_dev
, "v4l2 sub device registered\n");
992 /* now we can probe the default state */
995 ret
= v4l2_subdev_call(bcap_dev
->sd
, core
, g_std
, &std
);
997 v4l2_err(&bcap_dev
->v4l2_dev
,
998 "Unable to get std\n");
1001 bcap_dev
->std
= std
;
1003 ret
= bcap_init_sensor_formats(bcap_dev
);
1005 v4l2_err(&bcap_dev
->v4l2_dev
,
1006 "Unable to create sensor formats table\n");
1007 goto err_unreg_vdev
;
1011 video_unregister_device(bcap_dev
->video_dev
);
1012 bcap_dev
->video_dev
= NULL
;
1014 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
1016 v4l2_device_unregister(&bcap_dev
->v4l2_dev
);
1018 if (bcap_dev
->video_dev
)
1019 video_device_release(bcap_dev
->video_dev
);
1021 vb2_dma_contig_cleanup_ctx(bcap_dev
->alloc_ctx
);
1023 ppi_delete_instance(bcap_dev
->ppi
);
1029 static int __devexit
bcap_remove(struct platform_device
*pdev
)
1031 struct v4l2_device
*v4l2_dev
= platform_get_drvdata(pdev
);
1032 struct bcap_device
*bcap_dev
= container_of(v4l2_dev
,
1033 struct bcap_device
, v4l2_dev
);
1035 bcap_free_sensor_formats(bcap_dev
);
1036 video_unregister_device(bcap_dev
->video_dev
);
1037 v4l2_ctrl_handler_free(&bcap_dev
->ctrl_handler
);
1038 v4l2_device_unregister(v4l2_dev
);
1039 vb2_dma_contig_cleanup_ctx(bcap_dev
->alloc_ctx
);
1040 ppi_delete_instance(bcap_dev
->ppi
);
1045 static struct platform_driver bcap_driver
= {
1047 .name
= CAPTURE_DRV_NAME
,
1048 .owner
= THIS_MODULE
,
1050 .probe
= bcap_probe
,
1051 .remove
= __devexit_p(bcap_remove
),
1054 static __init
int bcap_init(void)
1056 return platform_driver_register(&bcap_driver
);
1059 static __exit
void bcap_exit(void)
1061 platform_driver_unregister(&bcap_driver
);
1064 module_init(bcap_init
);
1065 module_exit(bcap_exit
);
1067 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1068 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1069 MODULE_LICENSE("GPL v2");