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"
47 #define BCAP_MIN_NUM_BUF 2
52 enum v4l2_mbus_pixelcode mbus_code;
53 int bpp; /* bits per pixel */
54 int dlen; /* data length for ppi in bits */
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 /* current selected dv_timings */
80 struct v4l2_dv_timings dv_timings;
81 /* used to store pixel format */
82 struct v4l2_pix_format fmt;
85 /* data length for ppi in bits */
87 /* used to store sensor supported format */
88 struct bcap_format *sensor_formats;
89 /* number of sensor formats array */
90 int num_sensor_formats;
91 /* pointing to current video buffer */
92 struct bcap_buffer *cur_frm;
93 /* buffer queue used in videobuf2 */
94 struct vb2_queue buffer_queue;
95 /* allocator-specific contexts for each plane */
96 struct vb2_alloc_ctx *alloc_ctx;
97 /* queue of filled frames */
98 struct list_head dma_queue;
99 /* used in videobuf2 callback */
101 /* used to access capture device */
103 /* used to wait ppi to complete one transfer */
104 struct completion comp;
105 /* prepare to stop */
111 /* indicates whether this file handle is doing IO */
115 static const struct bcap_format bcap_formats[] = {
117 .desc = "YCbCr 4:2:2 Interleaved UYVY",
118 .pixelformat = V4L2_PIX_FMT_UYVY,
119 .mbus_code = V4L2_MBUS_FMT_UYVY8_2X8,
124 .desc = "YCbCr 4:2:2 Interleaved YUYV",
125 .pixelformat = V4L2_PIX_FMT_YUYV,
126 .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
131 .desc = "YCbCr 4:2:2 Interleaved UYVY",
132 .pixelformat = V4L2_PIX_FMT_UYVY,
133 .mbus_code = V4L2_MBUS_FMT_UYVY8_1X16,
139 .pixelformat = V4L2_PIX_FMT_RGB565,
140 .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
146 .pixelformat = V4L2_PIX_FMT_RGB444,
147 .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
153 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
155 static irqreturn_t bcap_isr(int irq, void *dev_id);
157 static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
159 return container_of(vb, struct bcap_buffer, vb);
162 static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
164 enum v4l2_mbus_pixelcode code;
165 struct bcap_format *sf;
166 unsigned int num_formats = 0;
169 while (!v4l2_subdev_call(bcap_dev->sd, video,
170 enum_mbus_fmt, num_formats, &code))
175 sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
179 for (i = 0; i < num_formats; i++) {
180 v4l2_subdev_call(bcap_dev->sd, video,
181 enum_mbus_fmt, i, &code);
182 for (j = 0; j < BCAP_MAX_FMTS; j++)
183 if (code == bcap_formats[j].mbus_code)
185 if (j == BCAP_MAX_FMTS) {
186 /* we don't allow this sensor working with our bridge */
190 sf[i] = bcap_formats[j];
192 bcap_dev->sensor_formats = sf;
193 bcap_dev->num_sensor_formats = num_formats;
197 static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
199 bcap_dev->num_sensor_formats = 0;
200 kfree(bcap_dev->sensor_formats);
201 bcap_dev->sensor_formats = NULL;
204 static int bcap_open(struct file *file)
206 struct bcap_device *bcap_dev = video_drvdata(file);
207 struct video_device *vfd = bcap_dev->video_dev;
208 struct bcap_fh *bcap_fh;
211 v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n");
215 bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL);
217 v4l2_err(&bcap_dev->v4l2_dev,
218 "unable to allocate memory for file handle object\n");
222 v4l2_fh_init(&bcap_fh->fh, vfd);
224 /* store pointer to v4l2_fh in private_data member of file */
225 file->private_data = &bcap_fh->fh;
226 v4l2_fh_add(&bcap_fh->fh);
227 bcap_fh->io_allowed = false;
231 static int bcap_release(struct file *file)
233 struct bcap_device *bcap_dev = video_drvdata(file);
234 struct v4l2_fh *fh = file->private_data;
235 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
237 /* if this instance is doing IO */
238 if (bcap_fh->io_allowed)
239 vb2_queue_release(&bcap_dev->buffer_queue);
241 file->private_data = NULL;
242 v4l2_fh_del(&bcap_fh->fh);
243 v4l2_fh_exit(&bcap_fh->fh);
248 static int bcap_mmap(struct file *file, struct vm_area_struct *vma)
250 struct bcap_device *bcap_dev = video_drvdata(file);
253 if (mutex_lock_interruptible(&bcap_dev->mutex))
255 ret = vb2_mmap(&bcap_dev->buffer_queue, vma);
256 mutex_unlock(&bcap_dev->mutex);
261 static unsigned long bcap_get_unmapped_area(struct file *file,
267 struct bcap_device *bcap_dev = video_drvdata(file);
269 return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
277 static unsigned int bcap_poll(struct file *file, poll_table *wait)
279 struct bcap_device *bcap_dev = video_drvdata(file);
282 mutex_lock(&bcap_dev->mutex);
283 res = vb2_poll(&bcap_dev->buffer_queue, file, wait);
284 mutex_unlock(&bcap_dev->mutex);
288 static int bcap_queue_setup(struct vb2_queue *vq,
289 const struct v4l2_format *fmt,
290 unsigned int *nbuffers, unsigned int *nplanes,
291 unsigned int sizes[], void *alloc_ctxs[])
293 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
295 if (*nbuffers < BCAP_MIN_NUM_BUF)
296 *nbuffers = BCAP_MIN_NUM_BUF;
299 sizes[0] = bcap_dev->fmt.sizeimage;
300 alloc_ctxs[0] = bcap_dev->alloc_ctx;
305 static int bcap_buffer_init(struct vb2_buffer *vb)
307 struct bcap_buffer *buf = to_bcap_vb(vb);
309 INIT_LIST_HEAD(&buf->list);
313 static int bcap_buffer_prepare(struct vb2_buffer *vb)
315 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
316 struct bcap_buffer *buf = to_bcap_vb(vb);
319 size = bcap_dev->fmt.sizeimage;
320 if (vb2_plane_size(vb, 0) < size) {
321 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
322 vb2_plane_size(vb, 0), size);
325 vb2_set_plane_payload(&buf->vb, 0, size);
330 static void bcap_buffer_queue(struct vb2_buffer *vb)
332 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
333 struct bcap_buffer *buf = to_bcap_vb(vb);
336 spin_lock_irqsave(&bcap_dev->lock, flags);
337 list_add_tail(&buf->list, &bcap_dev->dma_queue);
338 spin_unlock_irqrestore(&bcap_dev->lock, flags);
341 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
343 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
344 struct bcap_buffer *buf = to_bcap_vb(vb);
347 spin_lock_irqsave(&bcap_dev->lock, flags);
348 list_del_init(&buf->list);
349 spin_unlock_irqrestore(&bcap_dev->lock, flags);
352 static void bcap_lock(struct vb2_queue *vq)
354 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
355 mutex_lock(&bcap_dev->mutex);
358 static void bcap_unlock(struct vb2_queue *vq)
360 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
361 mutex_unlock(&bcap_dev->mutex);
364 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
366 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
367 struct ppi_if *ppi = bcap_dev->ppi;
368 struct ppi_params params;
371 /* enable streamon on the sub device */
372 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
373 if (ret && (ret != -ENOIOCTLCMD)) {
374 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
379 params.width = bcap_dev->fmt.width;
380 params.height = bcap_dev->fmt.height;
381 params.bpp = bcap_dev->bpp;
382 params.dlen = bcap_dev->dlen;
383 params.ppi_control = bcap_dev->cfg->ppi_control;
384 params.int_mask = bcap_dev->cfg->int_mask;
385 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
386 & V4L2_IN_CAP_DV_TIMINGS) {
387 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
389 params.hdelay = bt->hsync + bt->hbackporch;
390 params.vdelay = bt->vsync + bt->vbackporch;
391 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
392 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
393 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
397 if (bcap_dev->std & V4L2_STD_525_60) {
407 params.line = params.width + bcap_dev->cfg->blank_pixels;
408 params.frame = params.height;
410 ret = ppi->ops->set_params(ppi, ¶ms);
412 v4l2_err(&bcap_dev->v4l2_dev,
413 "Error in setting ppi params\n");
417 /* attach ppi DMA irq handler */
418 ret = ppi->ops->attach_irq(ppi, bcap_isr);
420 v4l2_err(&bcap_dev->v4l2_dev,
421 "Error in attaching interrupt handler\n");
425 reinit_completion(&bcap_dev->comp);
426 bcap_dev->stop = false;
430 static void bcap_stop_streaming(struct vb2_queue *vq)
432 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
433 struct ppi_if *ppi = bcap_dev->ppi;
436 if (!vb2_is_streaming(vq))
439 bcap_dev->stop = true;
440 wait_for_completion(&bcap_dev->comp);
442 ppi->ops->detach_irq(ppi);
443 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
444 if (ret && (ret != -ENOIOCTLCMD))
445 v4l2_err(&bcap_dev->v4l2_dev,
446 "stream off failed in subdev\n");
448 /* release all active buffers */
449 while (!list_empty(&bcap_dev->dma_queue)) {
450 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
451 struct bcap_buffer, list);
452 list_del(&bcap_dev->cur_frm->list);
453 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
457 static struct vb2_ops bcap_video_qops = {
458 .queue_setup = bcap_queue_setup,
459 .buf_init = bcap_buffer_init,
460 .buf_prepare = bcap_buffer_prepare,
461 .buf_cleanup = bcap_buffer_cleanup,
462 .buf_queue = bcap_buffer_queue,
463 .wait_prepare = bcap_unlock,
464 .wait_finish = bcap_lock,
465 .start_streaming = bcap_start_streaming,
466 .stop_streaming = bcap_stop_streaming,
469 static int bcap_reqbufs(struct file *file, void *priv,
470 struct v4l2_requestbuffers *req_buf)
472 struct bcap_device *bcap_dev = video_drvdata(file);
473 struct vb2_queue *vq = &bcap_dev->buffer_queue;
474 struct v4l2_fh *fh = file->private_data;
475 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
480 bcap_fh->io_allowed = true;
482 return vb2_reqbufs(vq, req_buf);
485 static int bcap_querybuf(struct file *file, void *priv,
486 struct v4l2_buffer *buf)
488 struct bcap_device *bcap_dev = video_drvdata(file);
490 return vb2_querybuf(&bcap_dev->buffer_queue, buf);
493 static int bcap_qbuf(struct file *file, void *priv,
494 struct v4l2_buffer *buf)
496 struct bcap_device *bcap_dev = video_drvdata(file);
497 struct v4l2_fh *fh = file->private_data;
498 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
500 if (!bcap_fh->io_allowed)
503 return vb2_qbuf(&bcap_dev->buffer_queue, buf);
506 static int bcap_dqbuf(struct file *file, void *priv,
507 struct v4l2_buffer *buf)
509 struct bcap_device *bcap_dev = video_drvdata(file);
510 struct v4l2_fh *fh = file->private_data;
511 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
513 if (!bcap_fh->io_allowed)
516 return vb2_dqbuf(&bcap_dev->buffer_queue,
517 buf, file->f_flags & O_NONBLOCK);
520 static irqreturn_t bcap_isr(int irq, void *dev_id)
522 struct ppi_if *ppi = dev_id;
523 struct bcap_device *bcap_dev = ppi->priv;
524 struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
527 spin_lock(&bcap_dev->lock);
529 if (!list_empty(&bcap_dev->dma_queue)) {
530 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
532 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
535 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
537 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
538 struct bcap_buffer, list);
539 list_del(&bcap_dev->cur_frm->list);
541 /* clear error flag, we will get a new frame */
548 if (bcap_dev->stop) {
549 complete(&bcap_dev->comp);
551 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
552 ppi->ops->update_addr(ppi, (unsigned long)addr);
553 ppi->ops->start(ppi);
556 spin_unlock(&bcap_dev->lock);
561 static int bcap_streamon(struct file *file, void *priv,
562 enum v4l2_buf_type buf_type)
564 struct bcap_device *bcap_dev = video_drvdata(file);
565 struct bcap_fh *fh = file->private_data;
566 struct ppi_if *ppi = bcap_dev->ppi;
573 /* call streamon to start streaming in videobuf */
574 ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
578 /* if dma queue is empty, return error */
579 if (list_empty(&bcap_dev->dma_queue)) {
580 v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
585 /* get the next frame from the dma queue */
586 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
587 struct bcap_buffer, list);
588 /* remove buffer from the dma queue */
589 list_del(&bcap_dev->cur_frm->list);
590 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
591 /* update DMA address */
592 ppi->ops->update_addr(ppi, (unsigned long)addr);
594 ppi->ops->start(ppi);
598 vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
602 static int bcap_streamoff(struct file *file, void *priv,
603 enum v4l2_buf_type buf_type)
605 struct bcap_device *bcap_dev = video_drvdata(file);
606 struct bcap_fh *fh = file->private_data;
611 return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
614 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
616 struct bcap_device *bcap_dev = video_drvdata(file);
618 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
621 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
623 struct bcap_device *bcap_dev = video_drvdata(file);
625 *std = bcap_dev->std;
629 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
631 struct bcap_device *bcap_dev = video_drvdata(file);
634 if (vb2_is_busy(&bcap_dev->buffer_queue))
637 ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, std);
645 static int bcap_enum_dv_timings(struct file *file, void *priv,
646 struct v4l2_enum_dv_timings *timings)
648 struct bcap_device *bcap_dev = video_drvdata(file);
650 return v4l2_subdev_call(bcap_dev->sd, video,
651 enum_dv_timings, timings);
654 static int bcap_query_dv_timings(struct file *file, void *priv,
655 struct v4l2_dv_timings *timings)
657 struct bcap_device *bcap_dev = video_drvdata(file);
659 return v4l2_subdev_call(bcap_dev->sd, video,
660 query_dv_timings, timings);
663 static int bcap_g_dv_timings(struct file *file, void *priv,
664 struct v4l2_dv_timings *timings)
666 struct bcap_device *bcap_dev = video_drvdata(file);
668 *timings = bcap_dev->dv_timings;
672 static int bcap_s_dv_timings(struct file *file, void *priv,
673 struct v4l2_dv_timings *timings)
675 struct bcap_device *bcap_dev = video_drvdata(file);
677 if (vb2_is_busy(&bcap_dev->buffer_queue))
680 ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
684 bcap_dev->dv_timings = *timings;
688 static int bcap_enum_input(struct file *file, void *priv,
689 struct v4l2_input *input)
691 struct bcap_device *bcap_dev = video_drvdata(file);
692 struct bfin_capture_config *config = bcap_dev->cfg;
696 if (input->index >= config->num_inputs)
699 *input = config->inputs[input->index];
700 /* get input status */
701 ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
703 input->status = status;
707 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
709 struct bcap_device *bcap_dev = video_drvdata(file);
711 *index = bcap_dev->cur_input;
715 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
717 struct bcap_device *bcap_dev = video_drvdata(file);
718 struct bfin_capture_config *config = bcap_dev->cfg;
719 struct bcap_route *route;
722 if (vb2_is_busy(&bcap_dev->buffer_queue))
725 if (index >= config->num_inputs)
728 route = &config->routes[index];
729 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
730 route->input, route->output, 0);
731 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
732 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
735 bcap_dev->cur_input = index;
736 /* if this route has specific config, update ppi control */
737 if (route->ppi_control)
738 config->ppi_control = route->ppi_control;
742 static int bcap_try_format(struct bcap_device *bcap,
743 struct v4l2_pix_format *pixfmt,
744 struct bcap_format *bcap_fmt)
746 struct bcap_format *sf = bcap->sensor_formats;
747 struct bcap_format *fmt = NULL;
748 struct v4l2_mbus_framefmt mbus_fmt;
751 for (i = 0; i < bcap->num_sensor_formats; i++) {
753 if (pixfmt->pixelformat == fmt->pixelformat)
756 if (i == bcap->num_sensor_formats)
759 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
760 ret = v4l2_subdev_call(bcap->sd, video,
761 try_mbus_fmt, &mbus_fmt);
764 v4l2_fill_pix_format(pixfmt, &mbus_fmt);
766 for (i = 0; i < bcap->num_sensor_formats; i++) {
768 if (mbus_fmt.code == fmt->mbus_code)
773 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
774 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
778 static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
779 struct v4l2_fmtdesc *fmt)
781 struct bcap_device *bcap_dev = video_drvdata(file);
782 struct bcap_format *sf = bcap_dev->sensor_formats;
784 if (fmt->index >= bcap_dev->num_sensor_formats)
787 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
788 strlcpy(fmt->description,
790 sizeof(fmt->description));
791 fmt->pixelformat = sf[fmt->index].pixelformat;
795 static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
796 struct v4l2_format *fmt)
798 struct bcap_device *bcap_dev = video_drvdata(file);
799 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
801 return bcap_try_format(bcap_dev, pixfmt, NULL);
804 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
805 struct v4l2_format *fmt)
807 struct bcap_device *bcap_dev = video_drvdata(file);
809 fmt->fmt.pix = bcap_dev->fmt;
813 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
814 struct v4l2_format *fmt)
816 struct bcap_device *bcap_dev = video_drvdata(file);
817 struct v4l2_mbus_framefmt mbus_fmt;
818 struct bcap_format bcap_fmt;
819 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
822 if (vb2_is_busy(&bcap_dev->buffer_queue))
825 /* see if format works */
826 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
830 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
831 ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
834 bcap_dev->fmt = *pixfmt;
835 bcap_dev->bpp = bcap_fmt.bpp;
836 bcap_dev->dlen = bcap_fmt.dlen;
840 static int bcap_querycap(struct file *file, void *priv,
841 struct v4l2_capability *cap)
843 struct bcap_device *bcap_dev = video_drvdata(file);
845 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
846 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
847 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
848 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
852 static int bcap_g_parm(struct file *file, void *fh,
853 struct v4l2_streamparm *a)
855 struct bcap_device *bcap_dev = video_drvdata(file);
857 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
859 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
862 static int bcap_s_parm(struct file *file, void *fh,
863 struct v4l2_streamparm *a)
865 struct bcap_device *bcap_dev = video_drvdata(file);
867 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
869 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
872 static int bcap_log_status(struct file *file, void *priv)
874 struct bcap_device *bcap_dev = video_drvdata(file);
875 /* status for sub devices */
876 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
880 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
881 .vidioc_querycap = bcap_querycap,
882 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
883 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
884 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
885 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
886 .vidioc_enum_input = bcap_enum_input,
887 .vidioc_g_input = bcap_g_input,
888 .vidioc_s_input = bcap_s_input,
889 .vidioc_querystd = bcap_querystd,
890 .vidioc_s_std = bcap_s_std,
891 .vidioc_g_std = bcap_g_std,
892 .vidioc_s_dv_timings = bcap_s_dv_timings,
893 .vidioc_g_dv_timings = bcap_g_dv_timings,
894 .vidioc_query_dv_timings = bcap_query_dv_timings,
895 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
896 .vidioc_reqbufs = bcap_reqbufs,
897 .vidioc_querybuf = bcap_querybuf,
898 .vidioc_qbuf = bcap_qbuf,
899 .vidioc_dqbuf = bcap_dqbuf,
900 .vidioc_streamon = bcap_streamon,
901 .vidioc_streamoff = bcap_streamoff,
902 .vidioc_g_parm = bcap_g_parm,
903 .vidioc_s_parm = bcap_s_parm,
904 .vidioc_log_status = bcap_log_status,
907 static struct v4l2_file_operations bcap_fops = {
908 .owner = THIS_MODULE,
910 .release = bcap_release,
911 .unlocked_ioctl = video_ioctl2,
914 .get_unmapped_area = bcap_get_unmapped_area,
919 static int bcap_probe(struct platform_device *pdev)
921 struct bcap_device *bcap_dev;
922 struct video_device *vfd;
923 struct i2c_adapter *i2c_adap;
924 struct bfin_capture_config *config;
926 struct bcap_route *route;
929 config = pdev->dev.platform_data;
930 if (!config || !config->num_inputs) {
931 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
935 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
937 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
941 bcap_dev->cfg = config;
943 bcap_dev->ppi = ppi_create_instance(config->ppi_info);
944 if (!bcap_dev->ppi) {
945 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
949 bcap_dev->ppi->priv = bcap_dev;
951 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
952 if (IS_ERR(bcap_dev->alloc_ctx)) {
953 ret = PTR_ERR(bcap_dev->alloc_ctx);
957 vfd = video_device_alloc();
960 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
961 goto err_cleanup_ctx;
964 /* initialize field of video device */
965 vfd->release = video_device_release;
966 vfd->fops = &bcap_fops;
967 vfd->ioctl_ops = &bcap_ioctl_ops;
969 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
970 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
971 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
972 bcap_dev->video_dev = vfd;
974 ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
976 v4l2_err(pdev->dev.driver,
977 "Unable to register v4l2 device\n");
978 goto err_release_vdev;
980 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
982 bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
983 ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
985 v4l2_err(&bcap_dev->v4l2_dev,
986 "Unable to init control handler\n");
990 spin_lock_init(&bcap_dev->lock);
991 /* initialize queue */
992 q = &bcap_dev->buffer_queue;
993 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
994 q->io_modes = VB2_MMAP;
995 q->drv_priv = bcap_dev;
996 q->buf_struct_size = sizeof(struct bcap_buffer);
997 q->ops = &bcap_video_qops;
998 q->mem_ops = &vb2_dma_contig_memops;
999 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1001 ret = vb2_queue_init(q);
1003 goto err_free_handler;
1005 mutex_init(&bcap_dev->mutex);
1006 init_completion(&bcap_dev->comp);
1008 /* init video dma queues */
1009 INIT_LIST_HEAD(&bcap_dev->dma_queue);
1011 vfd->lock = &bcap_dev->mutex;
1013 /* register video device */
1014 ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
1016 v4l2_err(&bcap_dev->v4l2_dev,
1017 "Unable to register video device\n");
1018 goto err_free_handler;
1020 video_set_drvdata(bcap_dev->video_dev, bcap_dev);
1021 v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
1022 video_device_node_name(vfd));
1024 /* load up the subdevice */
1025 i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
1027 v4l2_err(&bcap_dev->v4l2_dev,
1028 "Unable to find i2c adapter\n");
1030 goto err_unreg_vdev;
1033 bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
1035 &config->board_info,
1040 /* update tvnorms from the sub devices */
1041 for (i = 0; i < config->num_inputs; i++)
1042 vfd->tvnorms |= config->inputs[i].std;
1044 v4l2_err(&bcap_dev->v4l2_dev,
1045 "Unable to register sub device\n");
1047 goto err_unreg_vdev;
1050 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
1053 * explicitly set input, otherwise some boards
1054 * may not work at the state as we expected
1056 route = &config->routes[0];
1057 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
1058 route->input, route->output, 0);
1059 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
1060 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
1061 goto err_unreg_vdev;
1063 bcap_dev->cur_input = 0;
1064 /* if this route has specific config, update ppi control */
1065 if (route->ppi_control)
1066 config->ppi_control = route->ppi_control;
1068 /* now we can probe the default state */
1069 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
1071 ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
1073 v4l2_err(&bcap_dev->v4l2_dev,
1074 "Unable to get std\n");
1075 goto err_unreg_vdev;
1077 bcap_dev->std = std;
1079 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
1080 struct v4l2_dv_timings dv_timings;
1081 ret = v4l2_subdev_call(bcap_dev->sd, video,
1082 g_dv_timings, &dv_timings);
1084 v4l2_err(&bcap_dev->v4l2_dev,
1085 "Unable to get dv timings\n");
1086 goto err_unreg_vdev;
1088 bcap_dev->dv_timings = dv_timings;
1090 ret = bcap_init_sensor_formats(bcap_dev);
1092 v4l2_err(&bcap_dev->v4l2_dev,
1093 "Unable to create sensor formats table\n");
1094 goto err_unreg_vdev;
1098 video_unregister_device(bcap_dev->video_dev);
1099 bcap_dev->video_dev = NULL;
1101 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1103 v4l2_device_unregister(&bcap_dev->v4l2_dev);
1105 if (bcap_dev->video_dev)
1106 video_device_release(bcap_dev->video_dev);
1108 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1110 ppi_delete_instance(bcap_dev->ppi);
1116 static int bcap_remove(struct platform_device *pdev)
1118 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1119 struct bcap_device *bcap_dev = container_of(v4l2_dev,
1120 struct bcap_device, v4l2_dev);
1122 bcap_free_sensor_formats(bcap_dev);
1123 video_unregister_device(bcap_dev->video_dev);
1124 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1125 v4l2_device_unregister(v4l2_dev);
1126 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1127 ppi_delete_instance(bcap_dev->ppi);
1132 static struct platform_driver bcap_driver = {
1134 .name = CAPTURE_DRV_NAME,
1135 .owner = THIS_MODULE,
1137 .probe = bcap_probe,
1138 .remove = bcap_remove,
1140 module_platform_driver(bcap_driver);
1142 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1143 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1144 MODULE_LICENSE("GPL v2");