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 */
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 /* allocator-specific contexts for each plane */
95 struct vb2_alloc_ctx *alloc_ctx;
96 /* queue of filled frames */
97 struct list_head dma_queue;
98 /* used in videobuf2 callback */
100 /* used to access capture device */
102 /* used to wait ppi to complete one transfer */
103 struct completion comp;
104 /* prepare to stop */
106 /* vb2 buffer sequence counter */
110 static const struct bcap_format bcap_formats[] = {
112 .desc = "YCbCr 4:2:2 Interleaved UYVY",
113 .pixelformat = V4L2_PIX_FMT_UYVY,
114 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
119 .desc = "YCbCr 4:2:2 Interleaved YUYV",
120 .pixelformat = V4L2_PIX_FMT_YUYV,
121 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
126 .desc = "YCbCr 4:2:2 Interleaved UYVY",
127 .pixelformat = V4L2_PIX_FMT_UYVY,
128 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
134 .pixelformat = V4L2_PIX_FMT_RGB565,
135 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
141 .pixelformat = V4L2_PIX_FMT_RGB444,
142 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
148 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
150 static irqreturn_t bcap_isr(int irq, void *dev_id);
152 static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
154 return container_of(vb, struct bcap_buffer, vb);
157 static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
160 struct bcap_format *sf;
161 unsigned int num_formats = 0;
164 while (!v4l2_subdev_call(bcap_dev->sd, video,
165 enum_mbus_fmt, num_formats, &code))
170 sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
174 for (i = 0; i < num_formats; i++) {
175 v4l2_subdev_call(bcap_dev->sd, video,
176 enum_mbus_fmt, i, &code);
177 for (j = 0; j < BCAP_MAX_FMTS; j++)
178 if (code == bcap_formats[j].mbus_code)
180 if (j == BCAP_MAX_FMTS) {
181 /* we don't allow this sensor working with our bridge */
185 sf[i] = bcap_formats[j];
187 bcap_dev->sensor_formats = sf;
188 bcap_dev->num_sensor_formats = num_formats;
192 static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
194 bcap_dev->num_sensor_formats = 0;
195 kfree(bcap_dev->sensor_formats);
196 bcap_dev->sensor_formats = NULL;
199 static int bcap_queue_setup(struct vb2_queue *vq,
200 const struct v4l2_format *fmt,
201 unsigned int *nbuffers, unsigned int *nplanes,
202 unsigned int sizes[], void *alloc_ctxs[])
204 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
206 if (fmt && fmt->fmt.pix.sizeimage < bcap_dev->fmt.sizeimage)
209 if (vq->num_buffers + *nbuffers < 2)
213 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
214 alloc_ctxs[0] = bcap_dev->alloc_ctx;
219 static int bcap_buffer_prepare(struct vb2_buffer *vb)
221 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
222 unsigned long size = bcap_dev->fmt.sizeimage;
224 if (vb2_plane_size(vb, 0) < size) {
225 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
226 vb2_plane_size(vb, 0), size);
229 vb2_set_plane_payload(vb, 0, size);
231 vb->v4l2_buf.field = bcap_dev->fmt.field;
236 static void bcap_buffer_queue(struct vb2_buffer *vb)
238 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
239 struct bcap_buffer *buf = to_bcap_vb(vb);
242 spin_lock_irqsave(&bcap_dev->lock, flags);
243 list_add_tail(&buf->list, &bcap_dev->dma_queue);
244 spin_unlock_irqrestore(&bcap_dev->lock, flags);
247 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
249 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
250 struct bcap_buffer *buf = to_bcap_vb(vb);
253 spin_lock_irqsave(&bcap_dev->lock, flags);
254 list_del_init(&buf->list);
255 spin_unlock_irqrestore(&bcap_dev->lock, flags);
258 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
260 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
261 struct ppi_if *ppi = bcap_dev->ppi;
262 struct bcap_buffer *buf, *tmp;
263 struct ppi_params params;
267 /* enable streamon on the sub device */
268 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
269 if (ret && (ret != -ENOIOCTLCMD)) {
270 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
275 params.width = bcap_dev->fmt.width;
276 params.height = bcap_dev->fmt.height;
277 params.bpp = bcap_dev->bpp;
278 params.dlen = bcap_dev->dlen;
279 params.ppi_control = bcap_dev->cfg->ppi_control;
280 params.int_mask = bcap_dev->cfg->int_mask;
281 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
282 & V4L2_IN_CAP_DV_TIMINGS) {
283 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
285 params.hdelay = bt->hsync + bt->hbackporch;
286 params.vdelay = bt->vsync + bt->vbackporch;
287 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
288 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
289 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
293 if (bcap_dev->std & V4L2_STD_525_60) {
303 params.line = params.width + bcap_dev->cfg->blank_pixels;
304 params.frame = params.height;
306 ret = ppi->ops->set_params(ppi, ¶ms);
308 v4l2_err(&bcap_dev->v4l2_dev,
309 "Error in setting ppi params\n");
313 /* attach ppi DMA irq handler */
314 ret = ppi->ops->attach_irq(ppi, bcap_isr);
316 v4l2_err(&bcap_dev->v4l2_dev,
317 "Error in attaching interrupt handler\n");
321 bcap_dev->sequence = 0;
323 reinit_completion(&bcap_dev->comp);
324 bcap_dev->stop = false;
326 /* get the next frame from the dma queue */
327 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
328 struct bcap_buffer, list);
329 /* remove buffer from the dma queue */
330 list_del_init(&bcap_dev->cur_frm->list);
331 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
332 /* update DMA address */
333 ppi->ops->update_addr(ppi, (unsigned long)addr);
335 ppi->ops->start(ppi);
340 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
341 list_del(&buf->list);
342 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
348 static void bcap_stop_streaming(struct vb2_queue *vq)
350 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
351 struct ppi_if *ppi = bcap_dev->ppi;
354 bcap_dev->stop = true;
355 wait_for_completion(&bcap_dev->comp);
357 ppi->ops->detach_irq(ppi);
358 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
359 if (ret && (ret != -ENOIOCTLCMD))
360 v4l2_err(&bcap_dev->v4l2_dev,
361 "stream off failed in subdev\n");
363 /* release all active buffers */
364 if (bcap_dev->cur_frm)
365 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
367 while (!list_empty(&bcap_dev->dma_queue)) {
368 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
369 struct bcap_buffer, list);
370 list_del_init(&bcap_dev->cur_frm->list);
371 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
375 static struct vb2_ops bcap_video_qops = {
376 .queue_setup = bcap_queue_setup,
377 .buf_prepare = bcap_buffer_prepare,
378 .buf_cleanup = bcap_buffer_cleanup,
379 .buf_queue = bcap_buffer_queue,
380 .wait_prepare = vb2_ops_wait_prepare,
381 .wait_finish = vb2_ops_wait_finish,
382 .start_streaming = bcap_start_streaming,
383 .stop_streaming = bcap_stop_streaming,
386 static irqreturn_t bcap_isr(int irq, void *dev_id)
388 struct ppi_if *ppi = dev_id;
389 struct bcap_device *bcap_dev = ppi->priv;
390 struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
393 spin_lock(&bcap_dev->lock);
395 if (!list_empty(&bcap_dev->dma_queue)) {
396 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
398 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
401 vb->v4l2_buf.sequence = bcap_dev->sequence++;
402 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
404 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
405 struct bcap_buffer, list);
406 list_del_init(&bcap_dev->cur_frm->list);
408 /* clear error flag, we will get a new frame */
415 if (bcap_dev->stop) {
416 complete(&bcap_dev->comp);
418 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
419 ppi->ops->update_addr(ppi, (unsigned long)addr);
420 ppi->ops->start(ppi);
423 spin_unlock(&bcap_dev->lock);
428 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
430 struct bcap_device *bcap_dev = video_drvdata(file);
431 struct v4l2_input input;
433 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
434 if (!(input.capabilities & V4L2_IN_CAP_STD))
437 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
440 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
442 struct bcap_device *bcap_dev = video_drvdata(file);
443 struct v4l2_input input;
445 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
446 if (!(input.capabilities & V4L2_IN_CAP_STD))
449 *std = bcap_dev->std;
453 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
455 struct bcap_device *bcap_dev = video_drvdata(file);
456 struct v4l2_input input;
459 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
460 if (!(input.capabilities & V4L2_IN_CAP_STD))
463 if (vb2_is_busy(&bcap_dev->buffer_queue))
466 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
474 static int bcap_enum_dv_timings(struct file *file, void *priv,
475 struct v4l2_enum_dv_timings *timings)
477 struct bcap_device *bcap_dev = video_drvdata(file);
478 struct v4l2_input input;
480 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
481 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
486 return v4l2_subdev_call(bcap_dev->sd, pad,
487 enum_dv_timings, timings);
490 static int bcap_query_dv_timings(struct file *file, void *priv,
491 struct v4l2_dv_timings *timings)
493 struct bcap_device *bcap_dev = video_drvdata(file);
494 struct v4l2_input input;
496 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
497 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
500 return v4l2_subdev_call(bcap_dev->sd, video,
501 query_dv_timings, timings);
504 static int bcap_g_dv_timings(struct file *file, void *priv,
505 struct v4l2_dv_timings *timings)
507 struct bcap_device *bcap_dev = video_drvdata(file);
508 struct v4l2_input input;
510 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
511 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
514 *timings = bcap_dev->dv_timings;
518 static int bcap_s_dv_timings(struct file *file, void *priv,
519 struct v4l2_dv_timings *timings)
521 struct bcap_device *bcap_dev = video_drvdata(file);
522 struct v4l2_input input;
525 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
526 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
529 if (vb2_is_busy(&bcap_dev->buffer_queue))
532 ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
536 bcap_dev->dv_timings = *timings;
540 static int bcap_enum_input(struct file *file, void *priv,
541 struct v4l2_input *input)
543 struct bcap_device *bcap_dev = video_drvdata(file);
544 struct bfin_capture_config *config = bcap_dev->cfg;
548 if (input->index >= config->num_inputs)
551 *input = config->inputs[input->index];
552 /* get input status */
553 ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
555 input->status = status;
559 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
561 struct bcap_device *bcap_dev = video_drvdata(file);
563 *index = bcap_dev->cur_input;
567 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
569 struct bcap_device *bcap_dev = video_drvdata(file);
570 struct bfin_capture_config *config = bcap_dev->cfg;
571 struct bcap_route *route;
574 if (vb2_is_busy(&bcap_dev->buffer_queue))
577 if (index >= config->num_inputs)
580 route = &config->routes[index];
581 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
582 route->input, route->output, 0);
583 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
584 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
587 bcap_dev->cur_input = index;
588 /* if this route has specific config, update ppi control */
589 if (route->ppi_control)
590 config->ppi_control = route->ppi_control;
594 static int bcap_try_format(struct bcap_device *bcap,
595 struct v4l2_pix_format *pixfmt,
596 struct bcap_format *bcap_fmt)
598 struct bcap_format *sf = bcap->sensor_formats;
599 struct bcap_format *fmt = NULL;
600 struct v4l2_mbus_framefmt mbus_fmt;
603 for (i = 0; i < bcap->num_sensor_formats; i++) {
605 if (pixfmt->pixelformat == fmt->pixelformat)
608 if (i == bcap->num_sensor_formats)
611 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
612 ret = v4l2_subdev_call(bcap->sd, video,
613 try_mbus_fmt, &mbus_fmt);
616 v4l2_fill_pix_format(pixfmt, &mbus_fmt);
618 for (i = 0; i < bcap->num_sensor_formats; i++) {
620 if (mbus_fmt.code == fmt->mbus_code)
625 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
626 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
630 static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
631 struct v4l2_fmtdesc *fmt)
633 struct bcap_device *bcap_dev = video_drvdata(file);
634 struct bcap_format *sf = bcap_dev->sensor_formats;
636 if (fmt->index >= bcap_dev->num_sensor_formats)
639 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
640 strlcpy(fmt->description,
642 sizeof(fmt->description));
643 fmt->pixelformat = sf[fmt->index].pixelformat;
647 static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
648 struct v4l2_format *fmt)
650 struct bcap_device *bcap_dev = video_drvdata(file);
651 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
653 return bcap_try_format(bcap_dev, pixfmt, NULL);
656 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *fmt)
659 struct bcap_device *bcap_dev = video_drvdata(file);
661 fmt->fmt.pix = bcap_dev->fmt;
665 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
666 struct v4l2_format *fmt)
668 struct bcap_device *bcap_dev = video_drvdata(file);
669 struct v4l2_mbus_framefmt mbus_fmt;
670 struct bcap_format bcap_fmt;
671 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
674 if (vb2_is_busy(&bcap_dev->buffer_queue))
677 /* see if format works */
678 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
682 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
683 ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
686 bcap_dev->fmt = *pixfmt;
687 bcap_dev->bpp = bcap_fmt.bpp;
688 bcap_dev->dlen = bcap_fmt.dlen;
692 static int bcap_querycap(struct file *file, void *priv,
693 struct v4l2_capability *cap)
695 struct bcap_device *bcap_dev = video_drvdata(file);
697 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
698 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
699 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
700 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
701 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
705 static int bcap_g_parm(struct file *file, void *fh,
706 struct v4l2_streamparm *a)
708 struct bcap_device *bcap_dev = video_drvdata(file);
710 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
712 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
715 static int bcap_s_parm(struct file *file, void *fh,
716 struct v4l2_streamparm *a)
718 struct bcap_device *bcap_dev = video_drvdata(file);
720 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
722 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
725 static int bcap_log_status(struct file *file, void *priv)
727 struct bcap_device *bcap_dev = video_drvdata(file);
728 /* status for sub devices */
729 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
733 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
734 .vidioc_querycap = bcap_querycap,
735 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
736 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
737 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
738 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
739 .vidioc_enum_input = bcap_enum_input,
740 .vidioc_g_input = bcap_g_input,
741 .vidioc_s_input = bcap_s_input,
742 .vidioc_querystd = bcap_querystd,
743 .vidioc_s_std = bcap_s_std,
744 .vidioc_g_std = bcap_g_std,
745 .vidioc_s_dv_timings = bcap_s_dv_timings,
746 .vidioc_g_dv_timings = bcap_g_dv_timings,
747 .vidioc_query_dv_timings = bcap_query_dv_timings,
748 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
749 .vidioc_reqbufs = vb2_ioctl_reqbufs,
750 .vidioc_create_bufs = vb2_ioctl_create_bufs,
751 .vidioc_querybuf = vb2_ioctl_querybuf,
752 .vidioc_qbuf = vb2_ioctl_qbuf,
753 .vidioc_dqbuf = vb2_ioctl_dqbuf,
754 .vidioc_expbuf = vb2_ioctl_expbuf,
755 .vidioc_streamon = vb2_ioctl_streamon,
756 .vidioc_streamoff = vb2_ioctl_streamoff,
757 .vidioc_g_parm = bcap_g_parm,
758 .vidioc_s_parm = bcap_s_parm,
759 .vidioc_log_status = bcap_log_status,
762 static struct v4l2_file_operations bcap_fops = {
763 .owner = THIS_MODULE,
764 .open = v4l2_fh_open,
765 .release = vb2_fop_release,
766 .unlocked_ioctl = video_ioctl2,
767 .mmap = vb2_fop_mmap,
769 .get_unmapped_area = vb2_fop_get_unmapped_area,
774 static int bcap_probe(struct platform_device *pdev)
776 struct bcap_device *bcap_dev;
777 struct video_device *vfd;
778 struct i2c_adapter *i2c_adap;
779 struct bfin_capture_config *config;
781 struct bcap_route *route;
784 config = pdev->dev.platform_data;
785 if (!config || !config->num_inputs) {
786 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
790 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
792 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
796 bcap_dev->cfg = config;
798 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
799 if (!bcap_dev->ppi) {
800 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
804 bcap_dev->ppi->priv = bcap_dev;
806 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
807 if (IS_ERR(bcap_dev->alloc_ctx)) {
808 ret = PTR_ERR(bcap_dev->alloc_ctx);
812 vfd = &bcap_dev->video_dev;
813 /* initialize field of video device */
814 vfd->release = video_device_release_empty;
815 vfd->fops = &bcap_fops;
816 vfd->ioctl_ops = &bcap_ioctl_ops;
818 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
819 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
821 ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
823 v4l2_err(pdev->dev.driver,
824 "Unable to register v4l2 device\n");
825 goto err_cleanup_ctx;
827 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
829 bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
830 ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
832 v4l2_err(&bcap_dev->v4l2_dev,
833 "Unable to init control handler\n");
837 spin_lock_init(&bcap_dev->lock);
838 /* initialize queue */
839 q = &bcap_dev->buffer_queue;
840 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
841 q->io_modes = VB2_MMAP | VB2_DMABUF;
842 q->drv_priv = bcap_dev;
843 q->buf_struct_size = sizeof(struct bcap_buffer);
844 q->ops = &bcap_video_qops;
845 q->mem_ops = &vb2_dma_contig_memops;
846 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
847 q->lock = &bcap_dev->mutex;
848 q->min_buffers_needed = 1;
850 ret = vb2_queue_init(q);
852 goto err_free_handler;
854 mutex_init(&bcap_dev->mutex);
855 init_completion(&bcap_dev->comp);
857 /* init video dma queues */
858 INIT_LIST_HEAD(&bcap_dev->dma_queue);
860 vfd->lock = &bcap_dev->mutex;
863 /* register video device */
864 ret = video_register_device(&bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
866 v4l2_err(&bcap_dev->v4l2_dev,
867 "Unable to register video device\n");
868 goto err_free_handler;
870 video_set_drvdata(&bcap_dev->video_dev, bcap_dev);
871 v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
872 video_device_node_name(vfd));
874 /* load up the subdevice */
875 i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
877 v4l2_err(&bcap_dev->v4l2_dev,
878 "Unable to find i2c adapter\n");
883 bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
890 /* update tvnorms from the sub devices */
891 for (i = 0; i < config->num_inputs; i++)
892 vfd->tvnorms |= config->inputs[i].std;
894 v4l2_err(&bcap_dev->v4l2_dev,
895 "Unable to register sub device\n");
900 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
903 * explicitly set input, otherwise some boards
904 * may not work at the state as we expected
906 route = &config->routes[0];
907 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
908 route->input, route->output, 0);
909 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
910 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
913 bcap_dev->cur_input = 0;
914 /* if this route has specific config, update ppi control */
915 if (route->ppi_control)
916 config->ppi_control = route->ppi_control;
918 /* now we can probe the default state */
919 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
921 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
923 v4l2_err(&bcap_dev->v4l2_dev,
924 "Unable to get std\n");
929 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
930 struct v4l2_dv_timings dv_timings;
931 ret = v4l2_subdev_call(bcap_dev->sd, video,
932 g_dv_timings, &dv_timings);
934 v4l2_err(&bcap_dev->v4l2_dev,
935 "Unable to get dv timings\n");
938 bcap_dev->dv_timings = dv_timings;
940 ret = bcap_init_sensor_formats(bcap_dev);
942 v4l2_err(&bcap_dev->v4l2_dev,
943 "Unable to create sensor formats table\n");
948 video_unregister_device(&bcap_dev->video_dev);
950 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
952 v4l2_device_unregister(&bcap_dev->v4l2_dev);
954 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
956 ppi_delete_instance(bcap_dev->ppi);
962 static int bcap_remove(struct platform_device *pdev)
964 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
965 struct bcap_device *bcap_dev = container_of(v4l2_dev,
966 struct bcap_device, v4l2_dev);
968 bcap_free_sensor_formats(bcap_dev);
969 video_unregister_device(&bcap_dev->video_dev);
970 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
971 v4l2_device_unregister(v4l2_dev);
972 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
973 ppi_delete_instance(bcap_dev->ppi);
978 static struct platform_driver bcap_driver = {
980 .name = CAPTURE_DRV_NAME,
983 .remove = bcap_remove,
985 module_platform_driver(bcap_driver);
987 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
988 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
989 MODULE_LICENSE("GPL v2");