sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / platform / blackfin / bfin_capture.c
blob2e6edc09b58fb0bb755a0bb75cfcc45b44ad84e8
1 /*
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>
23 #include <linux/fs.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/mm.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>
41 #include <asm/dma.h>
43 #include <media/blackfin/bfin_capture.h>
44 #include <media/blackfin/ppi.h>
46 #define CAPTURE_DRV_NAME "bfin_capture"
48 struct bcap_format {
49 char *desc;
50 u32 pixelformat;
51 u32 mbus_code;
52 int bpp; /* bits per pixel */
53 int dlen; /* data length for ppi in bits */
56 struct bcap_buffer {
57 struct vb2_v4l2_buffer vb;
58 struct list_head list;
61 struct bcap_device {
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;
70 /* capture config */
71 struct bfin_capture_config *cfg;
72 /* ppi interface */
73 struct ppi_if *ppi;
74 /* current input */
75 unsigned int cur_input;
76 /* current selected standard */
77 v4l2_std_id std;
78 /* current selected dv_timings */
79 struct v4l2_dv_timings dv_timings;
80 /* used to store pixel format */
81 struct v4l2_pix_format fmt;
82 /* bits per pixel*/
83 int bpp;
84 /* data length for ppi in bits */
85 int dlen;
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 /* queue of filled frames */
95 struct list_head dma_queue;
96 /* used in videobuf2 callback */
97 spinlock_t lock;
98 /* used to access capture device */
99 struct mutex mutex;
100 /* used to wait ppi to complete one transfer */
101 struct completion comp;
102 /* prepare to stop */
103 bool stop;
104 /* vb2 buffer sequence counter */
105 unsigned sequence;
108 static const struct bcap_format bcap_formats[] = {
110 .desc = "YCbCr 4:2:2 Interleaved UYVY",
111 .pixelformat = V4L2_PIX_FMT_UYVY,
112 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
113 .bpp = 16,
114 .dlen = 8,
117 .desc = "YCbCr 4:2:2 Interleaved YUYV",
118 .pixelformat = V4L2_PIX_FMT_YUYV,
119 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
120 .bpp = 16,
121 .dlen = 8,
124 .desc = "YCbCr 4:2:2 Interleaved UYVY",
125 .pixelformat = V4L2_PIX_FMT_UYVY,
126 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
127 .bpp = 16,
128 .dlen = 16,
131 .desc = "RGB 565",
132 .pixelformat = V4L2_PIX_FMT_RGB565,
133 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
134 .bpp = 16,
135 .dlen = 8,
138 .desc = "RGB 444",
139 .pixelformat = V4L2_PIX_FMT_RGB444,
140 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
141 .bpp = 16,
142 .dlen = 8,
146 #define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
148 static irqreturn_t bcap_isr(int irq, void *dev_id);
150 static struct bcap_buffer *to_bcap_vb(struct vb2_v4l2_buffer *vb)
152 return container_of(vb, struct bcap_buffer, vb);
155 static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
157 struct v4l2_subdev_mbus_code_enum code = {
158 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
160 struct bcap_format *sf;
161 unsigned int num_formats = 0;
162 int i, j;
164 while (!v4l2_subdev_call(bcap_dev->sd, pad,
165 enum_mbus_code, NULL, &code)) {
166 num_formats++;
167 code.index++;
169 if (!num_formats)
170 return -ENXIO;
172 sf = kcalloc(num_formats, sizeof(*sf), GFP_KERNEL);
173 if (!sf)
174 return -ENOMEM;
176 for (i = 0; i < num_formats; i++) {
177 code.index = i;
178 v4l2_subdev_call(bcap_dev->sd, pad,
179 enum_mbus_code, NULL, &code);
180 for (j = 0; j < BCAP_MAX_FMTS; j++)
181 if (code.code == bcap_formats[j].mbus_code)
182 break;
183 if (j == BCAP_MAX_FMTS) {
184 /* we don't allow this sensor working with our bridge */
185 kfree(sf);
186 return -EINVAL;
188 sf[i] = bcap_formats[j];
190 bcap_dev->sensor_formats = sf;
191 bcap_dev->num_sensor_formats = num_formats;
192 return 0;
195 static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
197 bcap_dev->num_sensor_formats = 0;
198 kfree(bcap_dev->sensor_formats);
199 bcap_dev->sensor_formats = NULL;
202 static int bcap_queue_setup(struct vb2_queue *vq,
203 unsigned int *nbuffers, unsigned int *nplanes,
204 unsigned int sizes[], struct device *alloc_devs[])
206 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
208 if (vq->num_buffers + *nbuffers < 2)
209 *nbuffers = 2;
211 if (*nplanes)
212 return sizes[0] < bcap_dev->fmt.sizeimage ? -EINVAL : 0;
214 *nplanes = 1;
215 sizes[0] = bcap_dev->fmt.sizeimage;
217 return 0;
220 static int bcap_buffer_prepare(struct vb2_buffer *vb)
222 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
223 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
224 unsigned long size = bcap_dev->fmt.sizeimage;
226 if (vb2_plane_size(vb, 0) < size) {
227 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
228 vb2_plane_size(vb, 0), size);
229 return -EINVAL;
231 vb2_set_plane_payload(vb, 0, size);
233 vbuf->field = bcap_dev->fmt.field;
235 return 0;
238 static void bcap_buffer_queue(struct vb2_buffer *vb)
240 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
241 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
242 struct bcap_buffer *buf = to_bcap_vb(vbuf);
243 unsigned long flags;
245 spin_lock_irqsave(&bcap_dev->lock, flags);
246 list_add_tail(&buf->list, &bcap_dev->dma_queue);
247 spin_unlock_irqrestore(&bcap_dev->lock, flags);
250 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
252 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
253 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
254 struct bcap_buffer *buf = to_bcap_vb(vbuf);
255 unsigned long flags;
257 spin_lock_irqsave(&bcap_dev->lock, flags);
258 list_del_init(&buf->list);
259 spin_unlock_irqrestore(&bcap_dev->lock, flags);
262 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
264 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
265 struct ppi_if *ppi = bcap_dev->ppi;
266 struct bcap_buffer *buf, *tmp;
267 struct ppi_params params;
268 dma_addr_t addr;
269 int ret;
271 /* enable streamon on the sub device */
272 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
273 if (ret && (ret != -ENOIOCTLCMD)) {
274 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
275 goto err;
278 /* set ppi params */
279 params.width = bcap_dev->fmt.width;
280 params.height = bcap_dev->fmt.height;
281 params.bpp = bcap_dev->bpp;
282 params.dlen = bcap_dev->dlen;
283 params.ppi_control = bcap_dev->cfg->ppi_control;
284 params.int_mask = bcap_dev->cfg->int_mask;
285 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
286 & V4L2_IN_CAP_DV_TIMINGS) {
287 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
289 params.hdelay = bt->hsync + bt->hbackporch;
290 params.vdelay = bt->vsync + bt->vbackporch;
291 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
292 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
293 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
294 & V4L2_IN_CAP_STD) {
295 params.hdelay = 0;
296 params.vdelay = 0;
297 if (bcap_dev->std & V4L2_STD_525_60) {
298 params.line = 858;
299 params.frame = 525;
300 } else {
301 params.line = 864;
302 params.frame = 625;
304 } else {
305 params.hdelay = 0;
306 params.vdelay = 0;
307 params.line = params.width + bcap_dev->cfg->blank_pixels;
308 params.frame = params.height;
310 ret = ppi->ops->set_params(ppi, &params);
311 if (ret < 0) {
312 v4l2_err(&bcap_dev->v4l2_dev,
313 "Error in setting ppi params\n");
314 goto err;
317 /* attach ppi DMA irq handler */
318 ret = ppi->ops->attach_irq(ppi, bcap_isr);
319 if (ret < 0) {
320 v4l2_err(&bcap_dev->v4l2_dev,
321 "Error in attaching interrupt handler\n");
322 goto err;
325 bcap_dev->sequence = 0;
327 reinit_completion(&bcap_dev->comp);
328 bcap_dev->stop = false;
330 /* get the next frame from the dma queue */
331 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
332 struct bcap_buffer, list);
333 /* remove buffer from the dma queue */
334 list_del_init(&bcap_dev->cur_frm->list);
335 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb.vb2_buf,
337 /* update DMA address */
338 ppi->ops->update_addr(ppi, (unsigned long)addr);
339 /* enable ppi */
340 ppi->ops->start(ppi);
342 return 0;
344 err:
345 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
346 list_del(&buf->list);
347 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
350 return ret;
353 static void bcap_stop_streaming(struct vb2_queue *vq)
355 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
356 struct ppi_if *ppi = bcap_dev->ppi;
357 int ret;
359 bcap_dev->stop = true;
360 wait_for_completion(&bcap_dev->comp);
361 ppi->ops->stop(ppi);
362 ppi->ops->detach_irq(ppi);
363 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
364 if (ret && (ret != -ENOIOCTLCMD))
365 v4l2_err(&bcap_dev->v4l2_dev,
366 "stream off failed in subdev\n");
368 /* release all active buffers */
369 if (bcap_dev->cur_frm)
370 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
371 VB2_BUF_STATE_ERROR);
373 while (!list_empty(&bcap_dev->dma_queue)) {
374 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
375 struct bcap_buffer, list);
376 list_del_init(&bcap_dev->cur_frm->list);
377 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
378 VB2_BUF_STATE_ERROR);
382 static struct vb2_ops bcap_video_qops = {
383 .queue_setup = bcap_queue_setup,
384 .buf_prepare = bcap_buffer_prepare,
385 .buf_cleanup = bcap_buffer_cleanup,
386 .buf_queue = bcap_buffer_queue,
387 .wait_prepare = vb2_ops_wait_prepare,
388 .wait_finish = vb2_ops_wait_finish,
389 .start_streaming = bcap_start_streaming,
390 .stop_streaming = bcap_stop_streaming,
393 static irqreturn_t bcap_isr(int irq, void *dev_id)
395 struct ppi_if *ppi = dev_id;
396 struct bcap_device *bcap_dev = ppi->priv;
397 struct vb2_v4l2_buffer *vbuf = &bcap_dev->cur_frm->vb;
398 struct vb2_buffer *vb = &vbuf->vb2_buf;
399 dma_addr_t addr;
401 spin_lock(&bcap_dev->lock);
403 if (!list_empty(&bcap_dev->dma_queue)) {
404 vb->timestamp = ktime_get_ns();
405 if (ppi->err) {
406 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
407 ppi->err = false;
408 } else {
409 vbuf->sequence = bcap_dev->sequence++;
410 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
412 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
413 struct bcap_buffer, list);
414 list_del_init(&bcap_dev->cur_frm->list);
415 } else {
416 /* clear error flag, we will get a new frame */
417 if (ppi->err)
418 ppi->err = false;
421 ppi->ops->stop(ppi);
423 if (bcap_dev->stop) {
424 complete(&bcap_dev->comp);
425 } else {
426 addr = vb2_dma_contig_plane_dma_addr(
427 &bcap_dev->cur_frm->vb.vb2_buf, 0);
428 ppi->ops->update_addr(ppi, (unsigned long)addr);
429 ppi->ops->start(ppi);
432 spin_unlock(&bcap_dev->lock);
434 return IRQ_HANDLED;
437 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
439 struct bcap_device *bcap_dev = video_drvdata(file);
440 struct v4l2_input input;
442 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
443 if (!(input.capabilities & V4L2_IN_CAP_STD))
444 return -ENODATA;
446 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
449 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
451 struct bcap_device *bcap_dev = video_drvdata(file);
452 struct v4l2_input input;
454 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
455 if (!(input.capabilities & V4L2_IN_CAP_STD))
456 return -ENODATA;
458 *std = bcap_dev->std;
459 return 0;
462 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
464 struct bcap_device *bcap_dev = video_drvdata(file);
465 struct v4l2_input input;
466 int ret;
468 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
469 if (!(input.capabilities & V4L2_IN_CAP_STD))
470 return -ENODATA;
472 if (vb2_is_busy(&bcap_dev->buffer_queue))
473 return -EBUSY;
475 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
476 if (ret < 0)
477 return ret;
479 bcap_dev->std = std;
480 return 0;
483 static int bcap_enum_dv_timings(struct file *file, void *priv,
484 struct v4l2_enum_dv_timings *timings)
486 struct bcap_device *bcap_dev = video_drvdata(file);
487 struct v4l2_input input;
489 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
490 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
491 return -ENODATA;
493 timings->pad = 0;
495 return v4l2_subdev_call(bcap_dev->sd, pad,
496 enum_dv_timings, timings);
499 static int bcap_query_dv_timings(struct file *file, void *priv,
500 struct v4l2_dv_timings *timings)
502 struct bcap_device *bcap_dev = video_drvdata(file);
503 struct v4l2_input input;
505 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
506 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
507 return -ENODATA;
509 return v4l2_subdev_call(bcap_dev->sd, video,
510 query_dv_timings, timings);
513 static int bcap_g_dv_timings(struct file *file, void *priv,
514 struct v4l2_dv_timings *timings)
516 struct bcap_device *bcap_dev = video_drvdata(file);
517 struct v4l2_input input;
519 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
520 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
521 return -ENODATA;
523 *timings = bcap_dev->dv_timings;
524 return 0;
527 static int bcap_s_dv_timings(struct file *file, void *priv,
528 struct v4l2_dv_timings *timings)
530 struct bcap_device *bcap_dev = video_drvdata(file);
531 struct v4l2_input input;
532 int ret;
534 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
535 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
536 return -ENODATA;
538 if (vb2_is_busy(&bcap_dev->buffer_queue))
539 return -EBUSY;
541 ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
542 if (ret < 0)
543 return ret;
545 bcap_dev->dv_timings = *timings;
546 return 0;
549 static int bcap_enum_input(struct file *file, void *priv,
550 struct v4l2_input *input)
552 struct bcap_device *bcap_dev = video_drvdata(file);
553 struct bfin_capture_config *config = bcap_dev->cfg;
554 int ret;
555 u32 status;
557 if (input->index >= config->num_inputs)
558 return -EINVAL;
560 *input = config->inputs[input->index];
561 /* get input status */
562 ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
563 if (!ret)
564 input->status = status;
565 return 0;
568 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
570 struct bcap_device *bcap_dev = video_drvdata(file);
572 *index = bcap_dev->cur_input;
573 return 0;
576 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
578 struct bcap_device *bcap_dev = video_drvdata(file);
579 struct bfin_capture_config *config = bcap_dev->cfg;
580 struct bcap_route *route;
581 int ret;
583 if (vb2_is_busy(&bcap_dev->buffer_queue))
584 return -EBUSY;
586 if (index >= config->num_inputs)
587 return -EINVAL;
589 route = &config->routes[index];
590 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
591 route->input, route->output, 0);
592 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
593 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
594 return ret;
596 bcap_dev->cur_input = index;
597 /* if this route has specific config, update ppi control */
598 if (route->ppi_control)
599 config->ppi_control = route->ppi_control;
600 return 0;
603 static int bcap_try_format(struct bcap_device *bcap,
604 struct v4l2_pix_format *pixfmt,
605 struct bcap_format *bcap_fmt)
607 struct bcap_format *sf = bcap->sensor_formats;
608 struct bcap_format *fmt = NULL;
609 struct v4l2_subdev_pad_config pad_cfg;
610 struct v4l2_subdev_format format = {
611 .which = V4L2_SUBDEV_FORMAT_TRY,
613 int ret, i;
615 for (i = 0; i < bcap->num_sensor_formats; i++) {
616 fmt = &sf[i];
617 if (pixfmt->pixelformat == fmt->pixelformat)
618 break;
620 if (i == bcap->num_sensor_formats)
621 fmt = &sf[0];
623 v4l2_fill_mbus_format(&format.format, pixfmt, fmt->mbus_code);
624 ret = v4l2_subdev_call(bcap->sd, pad, set_fmt, &pad_cfg,
625 &format);
626 if (ret < 0)
627 return ret;
628 v4l2_fill_pix_format(pixfmt, &format.format);
629 if (bcap_fmt) {
630 for (i = 0; i < bcap->num_sensor_formats; i++) {
631 fmt = &sf[i];
632 if (format.format.code == fmt->mbus_code)
633 break;
635 *bcap_fmt = *fmt;
637 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
638 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
639 return 0;
642 static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
643 struct v4l2_fmtdesc *fmt)
645 struct bcap_device *bcap_dev = video_drvdata(file);
646 struct bcap_format *sf = bcap_dev->sensor_formats;
648 if (fmt->index >= bcap_dev->num_sensor_formats)
649 return -EINVAL;
651 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
652 strlcpy(fmt->description,
653 sf[fmt->index].desc,
654 sizeof(fmt->description));
655 fmt->pixelformat = sf[fmt->index].pixelformat;
656 return 0;
659 static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
660 struct v4l2_format *fmt)
662 struct bcap_device *bcap_dev = video_drvdata(file);
663 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
665 return bcap_try_format(bcap_dev, pixfmt, NULL);
668 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
669 struct v4l2_format *fmt)
671 struct bcap_device *bcap_dev = video_drvdata(file);
673 fmt->fmt.pix = bcap_dev->fmt;
674 return 0;
677 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
678 struct v4l2_format *fmt)
680 struct bcap_device *bcap_dev = video_drvdata(file);
681 struct v4l2_subdev_format format = {
682 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
684 struct bcap_format bcap_fmt;
685 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
686 int ret;
688 if (vb2_is_busy(&bcap_dev->buffer_queue))
689 return -EBUSY;
691 /* see if format works */
692 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
693 if (ret < 0)
694 return ret;
696 v4l2_fill_mbus_format(&format.format, pixfmt, bcap_fmt.mbus_code);
697 ret = v4l2_subdev_call(bcap_dev->sd, pad, set_fmt, NULL, &format);
698 if (ret < 0)
699 return ret;
700 bcap_dev->fmt = *pixfmt;
701 bcap_dev->bpp = bcap_fmt.bpp;
702 bcap_dev->dlen = bcap_fmt.dlen;
703 return 0;
706 static int bcap_querycap(struct file *file, void *priv,
707 struct v4l2_capability *cap)
709 struct bcap_device *bcap_dev = video_drvdata(file);
711 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
712 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
713 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
714 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
715 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
716 return 0;
719 static int bcap_g_parm(struct file *file, void *fh,
720 struct v4l2_streamparm *a)
722 struct bcap_device *bcap_dev = video_drvdata(file);
724 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
725 return -EINVAL;
726 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
729 static int bcap_s_parm(struct file *file, void *fh,
730 struct v4l2_streamparm *a)
732 struct bcap_device *bcap_dev = video_drvdata(file);
734 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
735 return -EINVAL;
736 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
739 static int bcap_log_status(struct file *file, void *priv)
741 struct bcap_device *bcap_dev = video_drvdata(file);
742 /* status for sub devices */
743 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
744 return 0;
747 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
748 .vidioc_querycap = bcap_querycap,
749 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
750 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
751 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
752 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
753 .vidioc_enum_input = bcap_enum_input,
754 .vidioc_g_input = bcap_g_input,
755 .vidioc_s_input = bcap_s_input,
756 .vidioc_querystd = bcap_querystd,
757 .vidioc_s_std = bcap_s_std,
758 .vidioc_g_std = bcap_g_std,
759 .vidioc_s_dv_timings = bcap_s_dv_timings,
760 .vidioc_g_dv_timings = bcap_g_dv_timings,
761 .vidioc_query_dv_timings = bcap_query_dv_timings,
762 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
763 .vidioc_reqbufs = vb2_ioctl_reqbufs,
764 .vidioc_create_bufs = vb2_ioctl_create_bufs,
765 .vidioc_querybuf = vb2_ioctl_querybuf,
766 .vidioc_qbuf = vb2_ioctl_qbuf,
767 .vidioc_dqbuf = vb2_ioctl_dqbuf,
768 .vidioc_expbuf = vb2_ioctl_expbuf,
769 .vidioc_streamon = vb2_ioctl_streamon,
770 .vidioc_streamoff = vb2_ioctl_streamoff,
771 .vidioc_g_parm = bcap_g_parm,
772 .vidioc_s_parm = bcap_s_parm,
773 .vidioc_log_status = bcap_log_status,
776 static struct v4l2_file_operations bcap_fops = {
777 .owner = THIS_MODULE,
778 .open = v4l2_fh_open,
779 .release = vb2_fop_release,
780 .unlocked_ioctl = video_ioctl2,
781 .mmap = vb2_fop_mmap,
782 #ifndef CONFIG_MMU
783 .get_unmapped_area = vb2_fop_get_unmapped_area,
784 #endif
785 .poll = vb2_fop_poll
788 static int bcap_probe(struct platform_device *pdev)
790 struct bcap_device *bcap_dev;
791 struct video_device *vfd;
792 struct i2c_adapter *i2c_adap;
793 struct bfin_capture_config *config;
794 struct vb2_queue *q;
795 struct bcap_route *route;
796 int ret;
798 config = pdev->dev.platform_data;
799 if (!config || !config->num_inputs) {
800 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
801 return -ENODEV;
804 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
805 if (!bcap_dev)
806 return -ENOMEM;
808 bcap_dev->cfg = config;
810 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
811 if (!bcap_dev->ppi) {
812 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
813 ret = -ENODEV;
814 goto err_free_dev;
816 bcap_dev->ppi->priv = bcap_dev;
818 vfd = &bcap_dev->video_dev;
819 /* initialize field of video device */
820 vfd->release = video_device_release_empty;
821 vfd->fops = &bcap_fops;
822 vfd->ioctl_ops = &bcap_ioctl_ops;
823 vfd->tvnorms = 0;
824 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
825 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
827 ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
828 if (ret) {
829 v4l2_err(pdev->dev.driver,
830 "Unable to register v4l2 device\n");
831 goto err_free_ppi;
833 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
835 bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
836 ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
837 if (ret) {
838 v4l2_err(&bcap_dev->v4l2_dev,
839 "Unable to init control handler\n");
840 goto err_unreg_v4l2;
843 spin_lock_init(&bcap_dev->lock);
844 /* initialize queue */
845 q = &bcap_dev->buffer_queue;
846 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
847 q->io_modes = VB2_MMAP | VB2_DMABUF;
848 q->drv_priv = bcap_dev;
849 q->buf_struct_size = sizeof(struct bcap_buffer);
850 q->ops = &bcap_video_qops;
851 q->mem_ops = &vb2_dma_contig_memops;
852 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
853 q->lock = &bcap_dev->mutex;
854 q->min_buffers_needed = 1;
855 q->dev = &pdev->dev;
857 ret = vb2_queue_init(q);
858 if (ret)
859 goto err_free_handler;
861 mutex_init(&bcap_dev->mutex);
862 init_completion(&bcap_dev->comp);
864 /* init video dma queues */
865 INIT_LIST_HEAD(&bcap_dev->dma_queue);
867 vfd->lock = &bcap_dev->mutex;
868 vfd->queue = q;
870 /* register video device */
871 ret = video_register_device(&bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
872 if (ret) {
873 v4l2_err(&bcap_dev->v4l2_dev,
874 "Unable to register video device\n");
875 goto err_free_handler;
877 video_set_drvdata(&bcap_dev->video_dev, bcap_dev);
878 v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
879 video_device_node_name(vfd));
881 /* load up the subdevice */
882 i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
883 if (!i2c_adap) {
884 v4l2_err(&bcap_dev->v4l2_dev,
885 "Unable to find i2c adapter\n");
886 ret = -ENODEV;
887 goto err_unreg_vdev;
890 bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
891 i2c_adap,
892 &config->board_info,
893 NULL);
894 if (bcap_dev->sd) {
895 int i;
897 /* update tvnorms from the sub devices */
898 for (i = 0; i < config->num_inputs; i++)
899 vfd->tvnorms |= config->inputs[i].std;
900 } else {
901 v4l2_err(&bcap_dev->v4l2_dev,
902 "Unable to register sub device\n");
903 ret = -ENODEV;
904 goto err_unreg_vdev;
907 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
910 * explicitly set input, otherwise some boards
911 * may not work at the state as we expected
913 route = &config->routes[0];
914 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
915 route->input, route->output, 0);
916 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
917 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
918 goto err_unreg_vdev;
920 bcap_dev->cur_input = 0;
921 /* if this route has specific config, update ppi control */
922 if (route->ppi_control)
923 config->ppi_control = route->ppi_control;
925 /* now we can probe the default state */
926 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
927 v4l2_std_id std;
928 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
929 if (ret) {
930 v4l2_err(&bcap_dev->v4l2_dev,
931 "Unable to get std\n");
932 goto err_unreg_vdev;
934 bcap_dev->std = std;
936 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
937 struct v4l2_dv_timings dv_timings;
938 ret = v4l2_subdev_call(bcap_dev->sd, video,
939 g_dv_timings, &dv_timings);
940 if (ret) {
941 v4l2_err(&bcap_dev->v4l2_dev,
942 "Unable to get dv timings\n");
943 goto err_unreg_vdev;
945 bcap_dev->dv_timings = dv_timings;
947 ret = bcap_init_sensor_formats(bcap_dev);
948 if (ret) {
949 v4l2_err(&bcap_dev->v4l2_dev,
950 "Unable to create sensor formats table\n");
951 goto err_unreg_vdev;
953 return 0;
954 err_unreg_vdev:
955 video_unregister_device(&bcap_dev->video_dev);
956 err_free_handler:
957 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
958 err_unreg_v4l2:
959 v4l2_device_unregister(&bcap_dev->v4l2_dev);
960 err_free_ppi:
961 ppi_delete_instance(bcap_dev->ppi);
962 err_free_dev:
963 kfree(bcap_dev);
964 return ret;
967 static int bcap_remove(struct platform_device *pdev)
969 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
970 struct bcap_device *bcap_dev = container_of(v4l2_dev,
971 struct bcap_device, v4l2_dev);
973 bcap_free_sensor_formats(bcap_dev);
974 video_unregister_device(&bcap_dev->video_dev);
975 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
976 v4l2_device_unregister(v4l2_dev);
977 ppi_delete_instance(bcap_dev->ppi);
978 kfree(bcap_dev);
979 return 0;
982 static struct platform_driver bcap_driver = {
983 .driver = {
984 .name = CAPTURE_DRV_NAME,
986 .probe = bcap_probe,
987 .remove = bcap_remove,
989 module_platform_driver(bcap_driver);
991 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
992 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
993 MODULE_LICENSE("GPL v2");