2 * Analog Devices video capture driver
4 * Copyright (c) 2011 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/completion.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/types.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ctrls.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/videobuf2-dma-contig.h>
43 #include <media/blackfin/bfin_capture.h>
44 #include <media/blackfin/ppi.h>
46 #define CAPTURE_DRV_NAME "bfin_capture"
52 int bpp; /* bits per pixel */
53 int dlen; /* data length for ppi in bits */
57 struct vb2_v4l2_buffer vb;
58 struct list_head list;
62 /* capture device instance */
63 struct v4l2_device v4l2_dev;
64 /* v4l2 control handler */
65 struct v4l2_ctrl_handler ctrl_handler;
66 /* device node data */
67 struct video_device video_dev;
68 /* sub device instance */
69 struct v4l2_subdev *sd;
71 struct bfin_capture_config *cfg;
75 unsigned int cur_input;
76 /* current selected standard */
78 /* current selected dv_timings */
79 struct v4l2_dv_timings dv_timings;
80 /* used to store pixel format */
81 struct v4l2_pix_format fmt;
84 /* data length for ppi in bits */
86 /* used to store sensor supported format */
87 struct bcap_format *sensor_formats;
88 /* number of sensor formats array */
89 int num_sensor_formats;
90 /* pointing to current video buffer */
91 struct bcap_buffer *cur_frm;
92 /* buffer queue used in videobuf2 */
93 struct vb2_queue buffer_queue;
94 /* 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_v4l2_buffer *vb)
154 return container_of(vb, struct bcap_buffer, vb);
157 static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
159 struct v4l2_subdev_mbus_code_enum code = {
160 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
162 struct bcap_format *sf;
163 unsigned int num_formats = 0;
166 while (!v4l2_subdev_call(bcap_dev->sd, pad,
167 enum_mbus_code, NULL, &code)) {
174 sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
178 for (i = 0; i < num_formats; i++) {
180 v4l2_subdev_call(bcap_dev->sd, pad,
181 enum_mbus_code, NULL, &code);
182 for (j = 0; j < BCAP_MAX_FMTS; j++)
183 if (code.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_queue_setup(struct vb2_queue *vq,
206 unsigned int *nbuffers, unsigned int *nplanes,
207 unsigned int sizes[], void *alloc_ctxs[])
209 const struct v4l2_format *fmt = parg;
210 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
212 if (fmt && fmt->fmt.pix.sizeimage < bcap_dev->fmt.sizeimage)
215 if (vq->num_buffers + *nbuffers < 2)
219 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
220 alloc_ctxs[0] = bcap_dev->alloc_ctx;
225 static int bcap_buffer_prepare(struct vb2_buffer *vb)
227 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
228 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
229 unsigned long size = bcap_dev->fmt.sizeimage;
231 if (vb2_plane_size(vb, 0) < size) {
232 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
233 vb2_plane_size(vb, 0), size);
236 vb2_set_plane_payload(vb, 0, size);
238 vbuf->field = bcap_dev->fmt.field;
243 static void bcap_buffer_queue(struct vb2_buffer *vb)
245 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
246 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
247 struct bcap_buffer *buf = to_bcap_vb(vbuf);
250 spin_lock_irqsave(&bcap_dev->lock, flags);
251 list_add_tail(&buf->list, &bcap_dev->dma_queue);
252 spin_unlock_irqrestore(&bcap_dev->lock, flags);
255 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
257 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
258 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
259 struct bcap_buffer *buf = to_bcap_vb(vbuf);
262 spin_lock_irqsave(&bcap_dev->lock, flags);
263 list_del_init(&buf->list);
264 spin_unlock_irqrestore(&bcap_dev->lock, flags);
267 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
269 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
270 struct ppi_if *ppi = bcap_dev->ppi;
271 struct bcap_buffer *buf, *tmp;
272 struct ppi_params params;
276 /* enable streamon on the sub device */
277 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
278 if (ret && (ret != -ENOIOCTLCMD)) {
279 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
284 params.width = bcap_dev->fmt.width;
285 params.height = bcap_dev->fmt.height;
286 params.bpp = bcap_dev->bpp;
287 params.dlen = bcap_dev->dlen;
288 params.ppi_control = bcap_dev->cfg->ppi_control;
289 params.int_mask = bcap_dev->cfg->int_mask;
290 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
291 & V4L2_IN_CAP_DV_TIMINGS) {
292 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
294 params.hdelay = bt->hsync + bt->hbackporch;
295 params.vdelay = bt->vsync + bt->vbackporch;
296 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
297 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
298 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
302 if (bcap_dev->std & V4L2_STD_525_60) {
312 params.line = params.width + bcap_dev->cfg->blank_pixels;
313 params.frame = params.height;
315 ret = ppi->ops->set_params(ppi, ¶ms);
317 v4l2_err(&bcap_dev->v4l2_dev,
318 "Error in setting ppi params\n");
322 /* attach ppi DMA irq handler */
323 ret = ppi->ops->attach_irq(ppi, bcap_isr);
325 v4l2_err(&bcap_dev->v4l2_dev,
326 "Error in attaching interrupt handler\n");
330 bcap_dev->sequence = 0;
332 reinit_completion(&bcap_dev->comp);
333 bcap_dev->stop = false;
335 /* get the next frame from the dma queue */
336 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
337 struct bcap_buffer, list);
338 /* remove buffer from the dma queue */
339 list_del_init(&bcap_dev->cur_frm->list);
340 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb.vb2_buf,
342 /* update DMA address */
343 ppi->ops->update_addr(ppi, (unsigned long)addr);
345 ppi->ops->start(ppi);
350 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
351 list_del(&buf->list);
352 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
358 static void bcap_stop_streaming(struct vb2_queue *vq)
360 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
361 struct ppi_if *ppi = bcap_dev->ppi;
364 bcap_dev->stop = true;
365 wait_for_completion(&bcap_dev->comp);
367 ppi->ops->detach_irq(ppi);
368 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
369 if (ret && (ret != -ENOIOCTLCMD))
370 v4l2_err(&bcap_dev->v4l2_dev,
371 "stream off failed in subdev\n");
373 /* release all active buffers */
374 if (bcap_dev->cur_frm)
375 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
376 VB2_BUF_STATE_ERROR);
378 while (!list_empty(&bcap_dev->dma_queue)) {
379 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
380 struct bcap_buffer, list);
381 list_del_init(&bcap_dev->cur_frm->list);
382 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
383 VB2_BUF_STATE_ERROR);
387 static struct vb2_ops bcap_video_qops = {
388 .queue_setup = bcap_queue_setup,
389 .buf_prepare = bcap_buffer_prepare,
390 .buf_cleanup = bcap_buffer_cleanup,
391 .buf_queue = bcap_buffer_queue,
392 .wait_prepare = vb2_ops_wait_prepare,
393 .wait_finish = vb2_ops_wait_finish,
394 .start_streaming = bcap_start_streaming,
395 .stop_streaming = bcap_stop_streaming,
398 static irqreturn_t bcap_isr(int irq, void *dev_id)
400 struct ppi_if *ppi = dev_id;
401 struct bcap_device *bcap_dev = ppi->priv;
402 struct vb2_v4l2_buffer *vbuf = &bcap_dev->cur_frm->vb;
403 struct vb2_buffer *vb = &vbuf->vb2_buf;
406 spin_lock(&bcap_dev->lock);
408 if (!list_empty(&bcap_dev->dma_queue)) {
409 v4l2_get_timestamp(&vbuf->timestamp);
411 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
414 vbuf->sequence = bcap_dev->sequence++;
415 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
417 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
418 struct bcap_buffer, list);
419 list_del_init(&bcap_dev->cur_frm->list);
421 /* clear error flag, we will get a new frame */
428 if (bcap_dev->stop) {
429 complete(&bcap_dev->comp);
431 addr = vb2_dma_contig_plane_dma_addr(
432 &bcap_dev->cur_frm->vb.vb2_buf, 0);
433 ppi->ops->update_addr(ppi, (unsigned long)addr);
434 ppi->ops->start(ppi);
437 spin_unlock(&bcap_dev->lock);
442 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
444 struct bcap_device *bcap_dev = video_drvdata(file);
445 struct v4l2_input input;
447 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
448 if (!(input.capabilities & V4L2_IN_CAP_STD))
451 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
454 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
456 struct bcap_device *bcap_dev = video_drvdata(file);
457 struct v4l2_input input;
459 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
460 if (!(input.capabilities & V4L2_IN_CAP_STD))
463 *std = bcap_dev->std;
467 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
469 struct bcap_device *bcap_dev = video_drvdata(file);
470 struct v4l2_input input;
473 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
474 if (!(input.capabilities & V4L2_IN_CAP_STD))
477 if (vb2_is_busy(&bcap_dev->buffer_queue))
480 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
488 static int bcap_enum_dv_timings(struct file *file, void *priv,
489 struct v4l2_enum_dv_timings *timings)
491 struct bcap_device *bcap_dev = video_drvdata(file);
492 struct v4l2_input input;
494 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
495 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
500 return v4l2_subdev_call(bcap_dev->sd, pad,
501 enum_dv_timings, timings);
504 static int bcap_query_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 return v4l2_subdev_call(bcap_dev->sd, video,
515 query_dv_timings, timings);
518 static int bcap_g_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;
524 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
525 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
528 *timings = bcap_dev->dv_timings;
532 static int bcap_s_dv_timings(struct file *file, void *priv,
533 struct v4l2_dv_timings *timings)
535 struct bcap_device *bcap_dev = video_drvdata(file);
536 struct v4l2_input input;
539 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
540 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
543 if (vb2_is_busy(&bcap_dev->buffer_queue))
546 ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
550 bcap_dev->dv_timings = *timings;
554 static int bcap_enum_input(struct file *file, void *priv,
555 struct v4l2_input *input)
557 struct bcap_device *bcap_dev = video_drvdata(file);
558 struct bfin_capture_config *config = bcap_dev->cfg;
562 if (input->index >= config->num_inputs)
565 *input = config->inputs[input->index];
566 /* get input status */
567 ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
569 input->status = status;
573 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
575 struct bcap_device *bcap_dev = video_drvdata(file);
577 *index = bcap_dev->cur_input;
581 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
583 struct bcap_device *bcap_dev = video_drvdata(file);
584 struct bfin_capture_config *config = bcap_dev->cfg;
585 struct bcap_route *route;
588 if (vb2_is_busy(&bcap_dev->buffer_queue))
591 if (index >= config->num_inputs)
594 route = &config->routes[index];
595 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
596 route->input, route->output, 0);
597 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
598 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
601 bcap_dev->cur_input = index;
602 /* if this route has specific config, update ppi control */
603 if (route->ppi_control)
604 config->ppi_control = route->ppi_control;
608 static int bcap_try_format(struct bcap_device *bcap,
609 struct v4l2_pix_format *pixfmt,
610 struct bcap_format *bcap_fmt)
612 struct bcap_format *sf = bcap->sensor_formats;
613 struct bcap_format *fmt = NULL;
614 struct v4l2_subdev_pad_config pad_cfg;
615 struct v4l2_subdev_format format = {
616 .which = V4L2_SUBDEV_FORMAT_TRY,
620 for (i = 0; i < bcap->num_sensor_formats; i++) {
622 if (pixfmt->pixelformat == fmt->pixelformat)
625 if (i == bcap->num_sensor_formats)
628 v4l2_fill_mbus_format(&format.format, pixfmt, fmt->mbus_code);
629 ret = v4l2_subdev_call(bcap->sd, pad, set_fmt, &pad_cfg,
633 v4l2_fill_pix_format(pixfmt, &format.format);
635 for (i = 0; i < bcap->num_sensor_formats; i++) {
637 if (format.format.code == fmt->mbus_code)
642 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
643 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
647 static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
648 struct v4l2_fmtdesc *fmt)
650 struct bcap_device *bcap_dev = video_drvdata(file);
651 struct bcap_format *sf = bcap_dev->sensor_formats;
653 if (fmt->index >= bcap_dev->num_sensor_formats)
656 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
657 strlcpy(fmt->description,
659 sizeof(fmt->description));
660 fmt->pixelformat = sf[fmt->index].pixelformat;
664 static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
665 struct v4l2_format *fmt)
667 struct bcap_device *bcap_dev = video_drvdata(file);
668 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
670 return bcap_try_format(bcap_dev, pixfmt, NULL);
673 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
674 struct v4l2_format *fmt)
676 struct bcap_device *bcap_dev = video_drvdata(file);
678 fmt->fmt.pix = bcap_dev->fmt;
682 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
683 struct v4l2_format *fmt)
685 struct bcap_device *bcap_dev = video_drvdata(file);
686 struct v4l2_subdev_format format = {
687 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
689 struct bcap_format bcap_fmt;
690 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
693 if (vb2_is_busy(&bcap_dev->buffer_queue))
696 /* see if format works */
697 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
701 v4l2_fill_mbus_format(&format.format, pixfmt, bcap_fmt.mbus_code);
702 ret = v4l2_subdev_call(bcap_dev->sd, pad, set_fmt, NULL, &format);
705 bcap_dev->fmt = *pixfmt;
706 bcap_dev->bpp = bcap_fmt.bpp;
707 bcap_dev->dlen = bcap_fmt.dlen;
711 static int bcap_querycap(struct file *file, void *priv,
712 struct v4l2_capability *cap)
714 struct bcap_device *bcap_dev = video_drvdata(file);
716 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
717 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
718 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
719 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
720 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
724 static int bcap_g_parm(struct file *file, void *fh,
725 struct v4l2_streamparm *a)
727 struct bcap_device *bcap_dev = video_drvdata(file);
729 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
731 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
734 static int bcap_s_parm(struct file *file, void *fh,
735 struct v4l2_streamparm *a)
737 struct bcap_device *bcap_dev = video_drvdata(file);
739 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
741 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
744 static int bcap_log_status(struct file *file, void *priv)
746 struct bcap_device *bcap_dev = video_drvdata(file);
747 /* status for sub devices */
748 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
752 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
753 .vidioc_querycap = bcap_querycap,
754 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
755 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
756 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
757 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
758 .vidioc_enum_input = bcap_enum_input,
759 .vidioc_g_input = bcap_g_input,
760 .vidioc_s_input = bcap_s_input,
761 .vidioc_querystd = bcap_querystd,
762 .vidioc_s_std = bcap_s_std,
763 .vidioc_g_std = bcap_g_std,
764 .vidioc_s_dv_timings = bcap_s_dv_timings,
765 .vidioc_g_dv_timings = bcap_g_dv_timings,
766 .vidioc_query_dv_timings = bcap_query_dv_timings,
767 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
768 .vidioc_reqbufs = vb2_ioctl_reqbufs,
769 .vidioc_create_bufs = vb2_ioctl_create_bufs,
770 .vidioc_querybuf = vb2_ioctl_querybuf,
771 .vidioc_qbuf = vb2_ioctl_qbuf,
772 .vidioc_dqbuf = vb2_ioctl_dqbuf,
773 .vidioc_expbuf = vb2_ioctl_expbuf,
774 .vidioc_streamon = vb2_ioctl_streamon,
775 .vidioc_streamoff = vb2_ioctl_streamoff,
776 .vidioc_g_parm = bcap_g_parm,
777 .vidioc_s_parm = bcap_s_parm,
778 .vidioc_log_status = bcap_log_status,
781 static struct v4l2_file_operations bcap_fops = {
782 .owner = THIS_MODULE,
783 .open = v4l2_fh_open,
784 .release = vb2_fop_release,
785 .unlocked_ioctl = video_ioctl2,
786 .mmap = vb2_fop_mmap,
788 .get_unmapped_area = vb2_fop_get_unmapped_area,
793 static int bcap_probe(struct platform_device *pdev)
795 struct bcap_device *bcap_dev;
796 struct video_device *vfd;
797 struct i2c_adapter *i2c_adap;
798 struct bfin_capture_config *config;
800 struct bcap_route *route;
803 config = pdev->dev.platform_data;
804 if (!config || !config->num_inputs) {
805 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
809 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
811 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
815 bcap_dev->cfg = config;
817 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
818 if (!bcap_dev->ppi) {
819 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
823 bcap_dev->ppi->priv = bcap_dev;
825 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
826 if (IS_ERR(bcap_dev->alloc_ctx)) {
827 ret = PTR_ERR(bcap_dev->alloc_ctx);
831 vfd = &bcap_dev->video_dev;
832 /* initialize field of video device */
833 vfd->release = video_device_release_empty;
834 vfd->fops = &bcap_fops;
835 vfd->ioctl_ops = &bcap_ioctl_ops;
837 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
838 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
840 ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
842 v4l2_err(pdev->dev.driver,
843 "Unable to register v4l2 device\n");
844 goto err_cleanup_ctx;
846 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
848 bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
849 ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
851 v4l2_err(&bcap_dev->v4l2_dev,
852 "Unable to init control handler\n");
856 spin_lock_init(&bcap_dev->lock);
857 /* initialize queue */
858 q = &bcap_dev->buffer_queue;
859 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
860 q->io_modes = VB2_MMAP | VB2_DMABUF;
861 q->drv_priv = bcap_dev;
862 q->buf_struct_size = sizeof(struct bcap_buffer);
863 q->ops = &bcap_video_qops;
864 q->mem_ops = &vb2_dma_contig_memops;
865 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
866 q->lock = &bcap_dev->mutex;
867 q->min_buffers_needed = 1;
869 ret = vb2_queue_init(q);
871 goto err_free_handler;
873 mutex_init(&bcap_dev->mutex);
874 init_completion(&bcap_dev->comp);
876 /* init video dma queues */
877 INIT_LIST_HEAD(&bcap_dev->dma_queue);
879 vfd->lock = &bcap_dev->mutex;
882 /* register video device */
883 ret = video_register_device(&bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
885 v4l2_err(&bcap_dev->v4l2_dev,
886 "Unable to register video device\n");
887 goto err_free_handler;
889 video_set_drvdata(&bcap_dev->video_dev, bcap_dev);
890 v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
891 video_device_node_name(vfd));
893 /* load up the subdevice */
894 i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
896 v4l2_err(&bcap_dev->v4l2_dev,
897 "Unable to find i2c adapter\n");
902 bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
909 /* update tvnorms from the sub devices */
910 for (i = 0; i < config->num_inputs; i++)
911 vfd->tvnorms |= config->inputs[i].std;
913 v4l2_err(&bcap_dev->v4l2_dev,
914 "Unable to register sub device\n");
919 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
922 * explicitly set input, otherwise some boards
923 * may not work at the state as we expected
925 route = &config->routes[0];
926 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
927 route->input, route->output, 0);
928 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
929 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
932 bcap_dev->cur_input = 0;
933 /* if this route has specific config, update ppi control */
934 if (route->ppi_control)
935 config->ppi_control = route->ppi_control;
937 /* now we can probe the default state */
938 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
940 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
942 v4l2_err(&bcap_dev->v4l2_dev,
943 "Unable to get std\n");
948 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
949 struct v4l2_dv_timings dv_timings;
950 ret = v4l2_subdev_call(bcap_dev->sd, video,
951 g_dv_timings, &dv_timings);
953 v4l2_err(&bcap_dev->v4l2_dev,
954 "Unable to get dv timings\n");
957 bcap_dev->dv_timings = dv_timings;
959 ret = bcap_init_sensor_formats(bcap_dev);
961 v4l2_err(&bcap_dev->v4l2_dev,
962 "Unable to create sensor formats table\n");
967 video_unregister_device(&bcap_dev->video_dev);
969 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
971 v4l2_device_unregister(&bcap_dev->v4l2_dev);
973 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
975 ppi_delete_instance(bcap_dev->ppi);
981 static int bcap_remove(struct platform_device *pdev)
983 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
984 struct bcap_device *bcap_dev = container_of(v4l2_dev,
985 struct bcap_device, v4l2_dev);
987 bcap_free_sensor_formats(bcap_dev);
988 video_unregister_device(&bcap_dev->video_dev);
989 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
990 v4l2_device_unregister(v4l2_dev);
991 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
992 ppi_delete_instance(bcap_dev->ppi);
997 static struct platform_driver bcap_driver = {
999 .name = CAPTURE_DRV_NAME,
1001 .probe = bcap_probe,
1002 .remove = bcap_remove,
1004 module_platform_driver(bcap_driver);
1006 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1007 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1008 MODULE_LICENSE("GPL v2");