]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/blackfin/bfin_capture.c
[media] media: blackfin: bfin_capture: use vb2_fop_mmap/poll
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / blackfin / bfin_capture.c
CommitLineData
63b1a90d
SJ
1/*
2 * Analog Devices video capture driver
3 *
4 * Copyright (c) 2011 Analog Devices Inc.
5 *
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.
9 *
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.
14 *
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.
18 */
19
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>
34
63b1a90d
SJ
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>
40
41#include <asm/dma.h>
42
43#include <media/blackfin/bfin_capture.h>
44#include <media/blackfin/ppi.h>
45
46#define CAPTURE_DRV_NAME "bfin_capture"
63b1a90d
SJ
47
48struct bcap_format {
49 char *desc;
50 u32 pixelformat;
27ffaeb0 51 u32 mbus_code;
63b1a90d 52 int bpp; /* bits per pixel */
45b82596 53 int dlen; /* data length for ppi in bits */
63b1a90d
SJ
54};
55
56struct bcap_buffer {
57 struct vb2_buffer vb;
58 struct list_head list;
59};
60
61struct 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;
45b82596
SJ
78 /* current selected dv_timings */
79 struct v4l2_dv_timings dv_timings;
63b1a90d
SJ
80 /* used to store pixel format */
81 struct v4l2_pix_format fmt;
82 /* bits per pixel*/
83 int bpp;
45b82596
SJ
84 /* data length for ppi in bits */
85 int dlen;
63b1a90d
SJ
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;
63b1a90d
SJ
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 */
99 spinlock_t lock;
100 /* used to access capture device */
101 struct mutex mutex;
102 /* used to wait ppi to complete one transfer */
103 struct completion comp;
104 /* prepare to stop */
105 bool stop;
106};
107
108struct bcap_fh {
109 struct v4l2_fh fh;
110 /* indicates whether this file handle is doing IO */
111 bool io_allowed;
112};
113
114static const struct bcap_format bcap_formats[] = {
115 {
116 .desc = "YCbCr 4:2:2 Interleaved UYVY",
117 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 118 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
63b1a90d 119 .bpp = 16,
45b82596 120 .dlen = 8,
63b1a90d
SJ
121 },
122 {
123 .desc = "YCbCr 4:2:2 Interleaved YUYV",
124 .pixelformat = V4L2_PIX_FMT_YUYV,
27ffaeb0 125 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
63b1a90d 126 .bpp = 16,
45b82596
SJ
127 .dlen = 8,
128 },
129 {
130 .desc = "YCbCr 4:2:2 Interleaved UYVY",
131 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 132 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
45b82596
SJ
133 .bpp = 16,
134 .dlen = 16,
63b1a90d
SJ
135 },
136 {
137 .desc = "RGB 565",
138 .pixelformat = V4L2_PIX_FMT_RGB565,
27ffaeb0 139 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
63b1a90d 140 .bpp = 16,
45b82596 141 .dlen = 8,
63b1a90d
SJ
142 },
143 {
144 .desc = "RGB 444",
145 .pixelformat = V4L2_PIX_FMT_RGB444,
27ffaeb0 146 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
63b1a90d 147 .bpp = 16,
45b82596 148 .dlen = 8,
63b1a90d
SJ
149 },
150
151};
152#define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
153
154static irqreturn_t bcap_isr(int irq, void *dev_id);
155
156static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
157{
158 return container_of(vb, struct bcap_buffer, vb);
159}
160
161static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
162{
27ffaeb0 163 u32 code;
63b1a90d
SJ
164 struct bcap_format *sf;
165 unsigned int num_formats = 0;
166 int i, j;
167
168 while (!v4l2_subdev_call(bcap_dev->sd, video,
169 enum_mbus_fmt, num_formats, &code))
170 num_formats++;
171 if (!num_formats)
172 return -ENXIO;
173
174 sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
175 if (!sf)
176 return -ENOMEM;
177
178 for (i = 0; i < num_formats; i++) {
179 v4l2_subdev_call(bcap_dev->sd, video,
180 enum_mbus_fmt, i, &code);
181 for (j = 0; j < BCAP_MAX_FMTS; j++)
182 if (code == bcap_formats[j].mbus_code)
183 break;
184 if (j == BCAP_MAX_FMTS) {
185 /* we don't allow this sensor working with our bridge */
186 kfree(sf);
187 return -EINVAL;
188 }
189 sf[i] = bcap_formats[j];
190 }
191 bcap_dev->sensor_formats = sf;
192 bcap_dev->num_sensor_formats = num_formats;
193 return 0;
194}
195
196static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
197{
198 bcap_dev->num_sensor_formats = 0;
199 kfree(bcap_dev->sensor_formats);
200 bcap_dev->sensor_formats = NULL;
201}
202
203static int bcap_open(struct file *file)
204{
205 struct bcap_device *bcap_dev = video_drvdata(file);
206 struct video_device *vfd = bcap_dev->video_dev;
207 struct bcap_fh *bcap_fh;
208
209 if (!bcap_dev->sd) {
210 v4l2_err(&bcap_dev->v4l2_dev, "No sub device registered\n");
211 return -ENODEV;
212 }
213
214 bcap_fh = kzalloc(sizeof(*bcap_fh), GFP_KERNEL);
215 if (!bcap_fh) {
216 v4l2_err(&bcap_dev->v4l2_dev,
217 "unable to allocate memory for file handle object\n");
218 return -ENOMEM;
219 }
220
221 v4l2_fh_init(&bcap_fh->fh, vfd);
222
223 /* store pointer to v4l2_fh in private_data member of file */
224 file->private_data = &bcap_fh->fh;
225 v4l2_fh_add(&bcap_fh->fh);
226 bcap_fh->io_allowed = false;
227 return 0;
228}
229
230static int bcap_release(struct file *file)
231{
232 struct bcap_device *bcap_dev = video_drvdata(file);
233 struct v4l2_fh *fh = file->private_data;
234 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
235
236 /* if this instance is doing IO */
237 if (bcap_fh->io_allowed)
238 vb2_queue_release(&bcap_dev->buffer_queue);
239
240 file->private_data = NULL;
241 v4l2_fh_del(&bcap_fh->fh);
242 v4l2_fh_exit(&bcap_fh->fh);
243 kfree(bcap_fh);
244 return 0;
245}
246
63b1a90d
SJ
247#ifndef CONFIG_MMU
248static unsigned long bcap_get_unmapped_area(struct file *file,
249 unsigned long addr,
250 unsigned long len,
251 unsigned long pgoff,
252 unsigned long flags)
253{
254 struct bcap_device *bcap_dev = video_drvdata(file);
255
256 return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
257 addr,
258 len,
259 pgoff,
260 flags);
261}
262#endif
263
63b1a90d
SJ
264static int bcap_queue_setup(struct vb2_queue *vq,
265 const struct v4l2_format *fmt,
266 unsigned int *nbuffers, unsigned int *nplanes,
267 unsigned int sizes[], void *alloc_ctxs[])
268{
269 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
270
f3140022
LP
271 if (fmt && fmt->fmt.pix.sizeimage < bcap_dev->fmt.sizeimage)
272 return -EINVAL;
273
274 if (vq->num_buffers + *nbuffers < 2)
275 *nbuffers = 2;
63b1a90d
SJ
276
277 *nplanes = 1;
f3140022 278 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
63b1a90d
SJ
279 alloc_ctxs[0] = bcap_dev->alloc_ctx;
280
281 return 0;
282}
283
63b1a90d
SJ
284static int bcap_buffer_prepare(struct vb2_buffer *vb)
285{
286 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
cd3c38ef 287 unsigned long size = bcap_dev->fmt.sizeimage;
63b1a90d 288
63b1a90d
SJ
289 if (vb2_plane_size(vb, 0) < size) {
290 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
291 vb2_plane_size(vb, 0), size);
292 return -EINVAL;
293 }
cd3c38ef
LP
294 vb2_set_plane_payload(vb, 0, size);
295
296 vb->v4l2_buf.field = bcap_dev->fmt.field;
63b1a90d
SJ
297
298 return 0;
299}
300
301static void bcap_buffer_queue(struct vb2_buffer *vb)
302{
303 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
304 struct bcap_buffer *buf = to_bcap_vb(vb);
305 unsigned long flags;
306
307 spin_lock_irqsave(&bcap_dev->lock, flags);
308 list_add_tail(&buf->list, &bcap_dev->dma_queue);
309 spin_unlock_irqrestore(&bcap_dev->lock, flags);
310}
311
312static void bcap_buffer_cleanup(struct vb2_buffer *vb)
313{
314 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
315 struct bcap_buffer *buf = to_bcap_vb(vb);
316 unsigned long flags;
317
318 spin_lock_irqsave(&bcap_dev->lock, flags);
319 list_del_init(&buf->list);
320 spin_unlock_irqrestore(&bcap_dev->lock, flags);
321}
322
63b1a90d
SJ
323static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
324{
325 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
326 struct ppi_if *ppi = bcap_dev->ppi;
6692871a 327 struct bcap_buffer *buf, *tmp;
63b1a90d
SJ
328 struct ppi_params params;
329 int ret;
330
331 /* enable streamon on the sub device */
332 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
333 if (ret && (ret != -ENOIOCTLCMD)) {
334 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
6692871a 335 goto err;
63b1a90d
SJ
336 }
337
338 /* set ppi params */
339 params.width = bcap_dev->fmt.width;
340 params.height = bcap_dev->fmt.height;
341 params.bpp = bcap_dev->bpp;
45b82596 342 params.dlen = bcap_dev->dlen;
63b1a90d
SJ
343 params.ppi_control = bcap_dev->cfg->ppi_control;
344 params.int_mask = bcap_dev->cfg->int_mask;
45b82596 345 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
a8451ed2 346 & V4L2_IN_CAP_DV_TIMINGS) {
45b82596
SJ
347 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
348
349 params.hdelay = bt->hsync + bt->hbackporch;
350 params.vdelay = bt->vsync + bt->vbackporch;
eacf8f9a
HV
351 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
352 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
45b82596
SJ
353 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
354 & V4L2_IN_CAP_STD) {
355 params.hdelay = 0;
356 params.vdelay = 0;
357 if (bcap_dev->std & V4L2_STD_525_60) {
358 params.line = 858;
359 params.frame = 525;
360 } else {
361 params.line = 864;
362 params.frame = 625;
363 }
364 } else {
365 params.hdelay = 0;
366 params.vdelay = 0;
367 params.line = params.width + bcap_dev->cfg->blank_pixels;
368 params.frame = params.height;
369 }
63b1a90d
SJ
370 ret = ppi->ops->set_params(ppi, &params);
371 if (ret < 0) {
372 v4l2_err(&bcap_dev->v4l2_dev,
373 "Error in setting ppi params\n");
6692871a 374 goto err;
63b1a90d
SJ
375 }
376
377 /* attach ppi DMA irq handler */
378 ret = ppi->ops->attach_irq(ppi, bcap_isr);
379 if (ret < 0) {
380 v4l2_err(&bcap_dev->v4l2_dev,
381 "Error in attaching interrupt handler\n");
6692871a 382 goto err;
63b1a90d
SJ
383 }
384
16735d02 385 reinit_completion(&bcap_dev->comp);
63b1a90d 386 bcap_dev->stop = false;
6692871a 387
63b1a90d 388 return 0;
6692871a
LP
389
390err:
391 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
392 list_del(&buf->list);
393 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
394 }
395
396 return ret;
63b1a90d
SJ
397}
398
e37559b2 399static void bcap_stop_streaming(struct vb2_queue *vq)
63b1a90d
SJ
400{
401 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
402 struct ppi_if *ppi = bcap_dev->ppi;
403 int ret;
404
63b1a90d
SJ
405 bcap_dev->stop = true;
406 wait_for_completion(&bcap_dev->comp);
407 ppi->ops->stop(ppi);
408 ppi->ops->detach_irq(ppi);
409 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
410 if (ret && (ret != -ENOIOCTLCMD))
411 v4l2_err(&bcap_dev->v4l2_dev,
412 "stream off failed in subdev\n");
413
414 /* release all active buffers */
415 while (!list_empty(&bcap_dev->dma_queue)) {
d78a4882 416 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
63b1a90d 417 struct bcap_buffer, list);
7ba3c21c 418 list_del_init(&bcap_dev->cur_frm->list);
d78a4882 419 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
63b1a90d 420 }
63b1a90d
SJ
421}
422
423static struct vb2_ops bcap_video_qops = {
424 .queue_setup = bcap_queue_setup,
63b1a90d
SJ
425 .buf_prepare = bcap_buffer_prepare,
426 .buf_cleanup = bcap_buffer_cleanup,
427 .buf_queue = bcap_buffer_queue,
4d655ca4
PL
428 .wait_prepare = vb2_ops_wait_prepare,
429 .wait_finish = vb2_ops_wait_finish,
63b1a90d
SJ
430 .start_streaming = bcap_start_streaming,
431 .stop_streaming = bcap_stop_streaming,
432};
433
434static int bcap_reqbufs(struct file *file, void *priv,
435 struct v4l2_requestbuffers *req_buf)
436{
437 struct bcap_device *bcap_dev = video_drvdata(file);
438 struct vb2_queue *vq = &bcap_dev->buffer_queue;
439 struct v4l2_fh *fh = file->private_data;
440 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
441
442 if (vb2_is_busy(vq))
443 return -EBUSY;
444
445 bcap_fh->io_allowed = true;
446
447 return vb2_reqbufs(vq, req_buf);
448}
449
450static int bcap_querybuf(struct file *file, void *priv,
451 struct v4l2_buffer *buf)
452{
453 struct bcap_device *bcap_dev = video_drvdata(file);
454
455 return vb2_querybuf(&bcap_dev->buffer_queue, buf);
456}
457
458static int bcap_qbuf(struct file *file, void *priv,
459 struct v4l2_buffer *buf)
460{
461 struct bcap_device *bcap_dev = video_drvdata(file);
462 struct v4l2_fh *fh = file->private_data;
463 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
464
465 if (!bcap_fh->io_allowed)
466 return -EBUSY;
467
468 return vb2_qbuf(&bcap_dev->buffer_queue, buf);
469}
470
471static int bcap_dqbuf(struct file *file, void *priv,
472 struct v4l2_buffer *buf)
473{
474 struct bcap_device *bcap_dev = video_drvdata(file);
475 struct v4l2_fh *fh = file->private_data;
476 struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
477
478 if (!bcap_fh->io_allowed)
479 return -EBUSY;
480
481 return vb2_dqbuf(&bcap_dev->buffer_queue,
482 buf, file->f_flags & O_NONBLOCK);
483}
484
485static irqreturn_t bcap_isr(int irq, void *dev_id)
486{
487 struct ppi_if *ppi = dev_id;
488 struct bcap_device *bcap_dev = ppi->priv;
63b1a90d
SJ
489 struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
490 dma_addr_t addr;
491
492 spin_lock(&bcap_dev->lock);
493
d78a4882 494 if (!list_empty(&bcap_dev->dma_queue)) {
8e6057b5 495 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
d78a4882
SJ
496 if (ppi->err) {
497 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
498 ppi->err = false;
499 } else {
500 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
501 }
502 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
503 struct bcap_buffer, list);
7ba3c21c 504 list_del_init(&bcap_dev->cur_frm->list);
d78a4882
SJ
505 } else {
506 /* clear error flag, we will get a new frame */
507 if (ppi->err)
508 ppi->err = false;
63b1a90d
SJ
509 }
510
511 ppi->ops->stop(ppi);
512
513 if (bcap_dev->stop) {
514 complete(&bcap_dev->comp);
515 } else {
d78a4882
SJ
516 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
517 ppi->ops->update_addr(ppi, (unsigned long)addr);
63b1a90d
SJ
518 ppi->ops->start(ppi);
519 }
520
521 spin_unlock(&bcap_dev->lock);
522
523 return IRQ_HANDLED;
524}
525
526static int bcap_streamon(struct file *file, void *priv,
527 enum v4l2_buf_type buf_type)
528{
529 struct bcap_device *bcap_dev = video_drvdata(file);
530 struct bcap_fh *fh = file->private_data;
531 struct ppi_if *ppi = bcap_dev->ppi;
532 dma_addr_t addr;
533 int ret;
534
535 if (!fh->io_allowed)
536 return -EBUSY;
537
538 /* call streamon to start streaming in videobuf */
539 ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
540 if (ret)
541 return ret;
542
543 /* if dma queue is empty, return error */
544 if (list_empty(&bcap_dev->dma_queue)) {
545 v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
546 ret = -EINVAL;
547 goto err;
548 }
549
550 /* get the next frame from the dma queue */
d78a4882 551 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
63b1a90d 552 struct bcap_buffer, list);
63b1a90d 553 /* remove buffer from the dma queue */
7ba3c21c 554 list_del_init(&bcap_dev->cur_frm->list);
63b1a90d
SJ
555 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
556 /* update DMA address */
557 ppi->ops->update_addr(ppi, (unsigned long)addr);
558 /* enable ppi */
559 ppi->ops->start(ppi);
560
561 return 0;
562err:
563 vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
564 return ret;
565}
566
567static int bcap_streamoff(struct file *file, void *priv,
568 enum v4l2_buf_type buf_type)
569{
570 struct bcap_device *bcap_dev = video_drvdata(file);
571 struct bcap_fh *fh = file->private_data;
572
573 if (!fh->io_allowed)
574 return -EBUSY;
575
576 return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
577}
578
579static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
580{
581 struct bcap_device *bcap_dev = video_drvdata(file);
582
583 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
584}
585
586static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
587{
588 struct bcap_device *bcap_dev = video_drvdata(file);
589
590 *std = bcap_dev->std;
591 return 0;
592}
593
314527ac 594static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
63b1a90d
SJ
595{
596 struct bcap_device *bcap_dev = video_drvdata(file);
597 int ret;
598
599 if (vb2_is_busy(&bcap_dev->buffer_queue))
600 return -EBUSY;
601
8774bed9 602 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
63b1a90d
SJ
603 if (ret < 0)
604 return ret;
605
314527ac 606 bcap_dev->std = std;
63b1a90d
SJ
607 return 0;
608}
609
ead156c4
SJ
610static int bcap_enum_dv_timings(struct file *file, void *priv,
611 struct v4l2_enum_dv_timings *timings)
612{
613 struct bcap_device *bcap_dev = video_drvdata(file);
614
f1f8e434
LP
615 timings->pad = 0;
616
617 return v4l2_subdev_call(bcap_dev->sd, pad,
ead156c4
SJ
618 enum_dv_timings, timings);
619}
620
621static int bcap_query_dv_timings(struct file *file, void *priv,
45b82596
SJ
622 struct v4l2_dv_timings *timings)
623{
624 struct bcap_device *bcap_dev = video_drvdata(file);
45b82596 625
ead156c4
SJ
626 return v4l2_subdev_call(bcap_dev->sd, video,
627 query_dv_timings, timings);
628}
45b82596 629
ead156c4
SJ
630static int bcap_g_dv_timings(struct file *file, void *priv,
631 struct v4l2_dv_timings *timings)
632{
633 struct bcap_device *bcap_dev = video_drvdata(file);
634
635 *timings = bcap_dev->dv_timings;
45b82596
SJ
636 return 0;
637}
638
639static int bcap_s_dv_timings(struct file *file, void *priv,
640 struct v4l2_dv_timings *timings)
641{
642 struct bcap_device *bcap_dev = video_drvdata(file);
643 int ret;
644 if (vb2_is_busy(&bcap_dev->buffer_queue))
645 return -EBUSY;
646
647 ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
648 if (ret < 0)
649 return ret;
650
651 bcap_dev->dv_timings = *timings;
652 return 0;
653}
654
63b1a90d
SJ
655static int bcap_enum_input(struct file *file, void *priv,
656 struct v4l2_input *input)
657{
658 struct bcap_device *bcap_dev = video_drvdata(file);
659 struct bfin_capture_config *config = bcap_dev->cfg;
660 int ret;
661 u32 status;
662
663 if (input->index >= config->num_inputs)
664 return -EINVAL;
665
666 *input = config->inputs[input->index];
667 /* get input status */
668 ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
669 if (!ret)
670 input->status = status;
671 return 0;
672}
673
674static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
675{
676 struct bcap_device *bcap_dev = video_drvdata(file);
677
678 *index = bcap_dev->cur_input;
679 return 0;
680}
681
682static int bcap_s_input(struct file *file, void *priv, unsigned int index)
683{
684 struct bcap_device *bcap_dev = video_drvdata(file);
685 struct bfin_capture_config *config = bcap_dev->cfg;
686 struct bcap_route *route;
687 int ret;
688
689 if (vb2_is_busy(&bcap_dev->buffer_queue))
690 return -EBUSY;
691
692 if (index >= config->num_inputs)
693 return -EINVAL;
694
695 route = &config->routes[index];
696 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
697 route->input, route->output, 0);
698 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
699 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
700 return ret;
701 }
702 bcap_dev->cur_input = index;
45b82596
SJ
703 /* if this route has specific config, update ppi control */
704 if (route->ppi_control)
705 config->ppi_control = route->ppi_control;
63b1a90d
SJ
706 return 0;
707}
708
709static int bcap_try_format(struct bcap_device *bcap,
710 struct v4l2_pix_format *pixfmt,
45b82596 711 struct bcap_format *bcap_fmt)
63b1a90d
SJ
712{
713 struct bcap_format *sf = bcap->sensor_formats;
714 struct bcap_format *fmt = NULL;
715 struct v4l2_mbus_framefmt mbus_fmt;
716 int ret, i;
717
718 for (i = 0; i < bcap->num_sensor_formats; i++) {
719 fmt = &sf[i];
720 if (pixfmt->pixelformat == fmt->pixelformat)
721 break;
722 }
723 if (i == bcap->num_sensor_formats)
724 fmt = &sf[0];
725
63b1a90d
SJ
726 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
727 ret = v4l2_subdev_call(bcap->sd, video,
728 try_mbus_fmt, &mbus_fmt);
729 if (ret < 0)
730 return ret;
731 v4l2_fill_pix_format(pixfmt, &mbus_fmt);
45b82596
SJ
732 if (bcap_fmt) {
733 for (i = 0; i < bcap->num_sensor_formats; i++) {
734 fmt = &sf[i];
735 if (mbus_fmt.code == fmt->mbus_code)
736 break;
737 }
738 *bcap_fmt = *fmt;
739 }
63b1a90d
SJ
740 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
741 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
742 return 0;
743}
744
745static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
746 struct v4l2_fmtdesc *fmt)
747{
748 struct bcap_device *bcap_dev = video_drvdata(file);
749 struct bcap_format *sf = bcap_dev->sensor_formats;
750
751 if (fmt->index >= bcap_dev->num_sensor_formats)
752 return -EINVAL;
753
754 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
755 strlcpy(fmt->description,
756 sf[fmt->index].desc,
757 sizeof(fmt->description));
758 fmt->pixelformat = sf[fmt->index].pixelformat;
759 return 0;
760}
761
762static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
763 struct v4l2_format *fmt)
764{
765 struct bcap_device *bcap_dev = video_drvdata(file);
766 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
767
45b82596 768 return bcap_try_format(bcap_dev, pixfmt, NULL);
63b1a90d
SJ
769}
770
771static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
772 struct v4l2_format *fmt)
773{
774 struct bcap_device *bcap_dev = video_drvdata(file);
775
776 fmt->fmt.pix = bcap_dev->fmt;
777 return 0;
778}
779
780static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
781 struct v4l2_format *fmt)
782{
783 struct bcap_device *bcap_dev = video_drvdata(file);
784 struct v4l2_mbus_framefmt mbus_fmt;
45b82596 785 struct bcap_format bcap_fmt;
63b1a90d 786 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
45b82596 787 int ret;
63b1a90d
SJ
788
789 if (vb2_is_busy(&bcap_dev->buffer_queue))
790 return -EBUSY;
791
792 /* see if format works */
45b82596 793 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
63b1a90d
SJ
794 if (ret < 0)
795 return ret;
796
45b82596 797 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
63b1a90d
SJ
798 ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
799 if (ret < 0)
800 return ret;
801 bcap_dev->fmt = *pixfmt;
45b82596
SJ
802 bcap_dev->bpp = bcap_fmt.bpp;
803 bcap_dev->dlen = bcap_fmt.dlen;
63b1a90d
SJ
804 return 0;
805}
806
807static int bcap_querycap(struct file *file, void *priv,
808 struct v4l2_capability *cap)
809{
810 struct bcap_device *bcap_dev = video_drvdata(file);
811
a020c747
HV
812 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
813 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
63b1a90d
SJ
814 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
815 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
816 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
817 return 0;
818}
819
820static int bcap_g_parm(struct file *file, void *fh,
821 struct v4l2_streamparm *a)
822{
823 struct bcap_device *bcap_dev = video_drvdata(file);
824
825 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
826 return -EINVAL;
827 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
828}
829
830static int bcap_s_parm(struct file *file, void *fh,
831 struct v4l2_streamparm *a)
832{
833 struct bcap_device *bcap_dev = video_drvdata(file);
834
835 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
836 return -EINVAL;
837 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
838}
839
63b1a90d
SJ
840static int bcap_log_status(struct file *file, void *priv)
841{
842 struct bcap_device *bcap_dev = video_drvdata(file);
843 /* status for sub devices */
844 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
845 return 0;
846}
847
848static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
849 .vidioc_querycap = bcap_querycap,
850 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
851 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
852 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
853 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
854 .vidioc_enum_input = bcap_enum_input,
855 .vidioc_g_input = bcap_g_input,
856 .vidioc_s_input = bcap_s_input,
857 .vidioc_querystd = bcap_querystd,
858 .vidioc_s_std = bcap_s_std,
859 .vidioc_g_std = bcap_g_std,
45b82596
SJ
860 .vidioc_s_dv_timings = bcap_s_dv_timings,
861 .vidioc_g_dv_timings = bcap_g_dv_timings,
ead156c4
SJ
862 .vidioc_query_dv_timings = bcap_query_dv_timings,
863 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
63b1a90d
SJ
864 .vidioc_reqbufs = bcap_reqbufs,
865 .vidioc_querybuf = bcap_querybuf,
866 .vidioc_qbuf = bcap_qbuf,
867 .vidioc_dqbuf = bcap_dqbuf,
868 .vidioc_streamon = bcap_streamon,
869 .vidioc_streamoff = bcap_streamoff,
870 .vidioc_g_parm = bcap_g_parm,
871 .vidioc_s_parm = bcap_s_parm,
63b1a90d
SJ
872 .vidioc_log_status = bcap_log_status,
873};
874
875static struct v4l2_file_operations bcap_fops = {
876 .owner = THIS_MODULE,
877 .open = bcap_open,
878 .release = bcap_release,
879 .unlocked_ioctl = video_ioctl2,
8fd05b7e 880 .mmap = vb2_fop_mmap,
63b1a90d
SJ
881#ifndef CONFIG_MMU
882 .get_unmapped_area = bcap_get_unmapped_area,
883#endif
8fd05b7e 884 .poll = vb2_fop_poll
63b1a90d
SJ
885};
886
4c62e976 887static int bcap_probe(struct platform_device *pdev)
63b1a90d
SJ
888{
889 struct bcap_device *bcap_dev;
890 struct video_device *vfd;
891 struct i2c_adapter *i2c_adap;
892 struct bfin_capture_config *config;
893 struct vb2_queue *q;
45b82596 894 struct bcap_route *route;
63b1a90d
SJ
895 int ret;
896
897 config = pdev->dev.platform_data;
d9fdbeff 898 if (!config || !config->num_inputs) {
63b1a90d
SJ
899 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
900 return -ENODEV;
901 }
902
903 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
904 if (!bcap_dev) {
905 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
906 return -ENOMEM;
907 }
908
909 bcap_dev->cfg = config;
910
f7d0e6d6 911 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
63b1a90d
SJ
912 if (!bcap_dev->ppi) {
913 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
914 ret = -ENODEV;
915 goto err_free_dev;
916 }
917 bcap_dev->ppi->priv = bcap_dev;
918
919 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
920 if (IS_ERR(bcap_dev->alloc_ctx)) {
921 ret = PTR_ERR(bcap_dev->alloc_ctx);
922 goto err_free_ppi;
923 }
924
925 vfd = video_device_alloc();
926 if (!vfd) {
927 ret = -ENOMEM;
928 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
929 goto err_cleanup_ctx;
930 }
931
932 /* initialize field of video device */
933 vfd->release = video_device_release;
934 vfd->fops = &bcap_fops;
935 vfd->ioctl_ops = &bcap_ioctl_ops;
936 vfd->tvnorms = 0;
937 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
63b1a90d
SJ
938 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
939 bcap_dev->video_dev = vfd;
940
941 ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
942 if (ret) {
943 v4l2_err(pdev->dev.driver,
944 "Unable to register v4l2 device\n");
945 goto err_release_vdev;
946 }
947 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
948
949 bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
950 ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
951 if (ret) {
952 v4l2_err(&bcap_dev->v4l2_dev,
953 "Unable to init control handler\n");
954 goto err_unreg_v4l2;
955 }
956
957 spin_lock_init(&bcap_dev->lock);
958 /* initialize queue */
959 q = &bcap_dev->buffer_queue;
960 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
961 q->io_modes = VB2_MMAP;
962 q->drv_priv = bcap_dev;
963 q->buf_struct_size = sizeof(struct bcap_buffer);
964 q->ops = &bcap_video_qops;
965 q->mem_ops = &vb2_dma_contig_memops;
ade48681 966 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
4d655ca4 967 q->lock = &bcap_dev->mutex;
5f65ca81 968 q->min_buffers_needed = 1;
63b1a90d 969
20420f92
HV
970 ret = vb2_queue_init(q);
971 if (ret)
972 goto err_free_handler;
63b1a90d
SJ
973
974 mutex_init(&bcap_dev->mutex);
975 init_completion(&bcap_dev->comp);
976
977 /* init video dma queues */
978 INIT_LIST_HEAD(&bcap_dev->dma_queue);
979
980 vfd->lock = &bcap_dev->mutex;
8fd05b7e 981 vfd->queue = q;
63b1a90d
SJ
982
983 /* register video device */
984 ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
985 if (ret) {
986 v4l2_err(&bcap_dev->v4l2_dev,
987 "Unable to register video device\n");
988 goto err_free_handler;
989 }
990 video_set_drvdata(bcap_dev->video_dev, bcap_dev);
991 v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
992 video_device_node_name(vfd));
993
994 /* load up the subdevice */
995 i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
996 if (!i2c_adap) {
997 v4l2_err(&bcap_dev->v4l2_dev,
998 "Unable to find i2c adapter\n");
150a1363 999 ret = -ENODEV;
63b1a90d
SJ
1000 goto err_unreg_vdev;
1001
1002 }
1003 bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
1004 i2c_adap,
1005 &config->board_info,
1006 NULL);
1007 if (bcap_dev->sd) {
1008 int i;
45b82596 1009
63b1a90d
SJ
1010 /* update tvnorms from the sub devices */
1011 for (i = 0; i < config->num_inputs; i++)
1012 vfd->tvnorms |= config->inputs[i].std;
1013 } else {
1014 v4l2_err(&bcap_dev->v4l2_dev,
1015 "Unable to register sub device\n");
d9fdbeff 1016 ret = -ENODEV;
63b1a90d
SJ
1017 goto err_unreg_vdev;
1018 }
1019
1020 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
1021
45b82596
SJ
1022 /*
1023 * explicitly set input, otherwise some boards
1024 * may not work at the state as we expected
1025 */
1026 route = &config->routes[0];
1027 ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
1028 route->input, route->output, 0);
1029 if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
1030 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
1031 goto err_unreg_vdev;
1032 }
1033 bcap_dev->cur_input = 0;
1034 /* if this route has specific config, update ppi control */
1035 if (route->ppi_control)
1036 config->ppi_control = route->ppi_control;
1037
63b1a90d 1038 /* now we can probe the default state */
45b82596 1039 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
63b1a90d 1040 v4l2_std_id std;
8774bed9 1041 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
63b1a90d
SJ
1042 if (ret) {
1043 v4l2_err(&bcap_dev->v4l2_dev,
1044 "Unable to get std\n");
1045 goto err_unreg_vdev;
1046 }
1047 bcap_dev->std = std;
1048 }
a8451ed2 1049 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
45b82596
SJ
1050 struct v4l2_dv_timings dv_timings;
1051 ret = v4l2_subdev_call(bcap_dev->sd, video,
1052 g_dv_timings, &dv_timings);
1053 if (ret) {
1054 v4l2_err(&bcap_dev->v4l2_dev,
1055 "Unable to get dv timings\n");
1056 goto err_unreg_vdev;
1057 }
1058 bcap_dev->dv_timings = dv_timings;
1059 }
63b1a90d
SJ
1060 ret = bcap_init_sensor_formats(bcap_dev);
1061 if (ret) {
1062 v4l2_err(&bcap_dev->v4l2_dev,
1063 "Unable to create sensor formats table\n");
1064 goto err_unreg_vdev;
1065 }
1066 return 0;
1067err_unreg_vdev:
1068 video_unregister_device(bcap_dev->video_dev);
1069 bcap_dev->video_dev = NULL;
1070err_free_handler:
1071 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1072err_unreg_v4l2:
1073 v4l2_device_unregister(&bcap_dev->v4l2_dev);
1074err_release_vdev:
1075 if (bcap_dev->video_dev)
1076 video_device_release(bcap_dev->video_dev);
1077err_cleanup_ctx:
1078 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1079err_free_ppi:
1080 ppi_delete_instance(bcap_dev->ppi);
1081err_free_dev:
1082 kfree(bcap_dev);
1083 return ret;
1084}
1085
4c62e976 1086static int bcap_remove(struct platform_device *pdev)
63b1a90d
SJ
1087{
1088 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1089 struct bcap_device *bcap_dev = container_of(v4l2_dev,
1090 struct bcap_device, v4l2_dev);
1091
1092 bcap_free_sensor_formats(bcap_dev);
1093 video_unregister_device(bcap_dev->video_dev);
1094 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1095 v4l2_device_unregister(v4l2_dev);
1096 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1097 ppi_delete_instance(bcap_dev->ppi);
1098 kfree(bcap_dev);
1099 return 0;
1100}
1101
1102static struct platform_driver bcap_driver = {
1103 .driver = {
1104 .name = CAPTURE_DRV_NAME,
63b1a90d
SJ
1105 },
1106 .probe = bcap_probe,
4c62e976 1107 .remove = bcap_remove,
63b1a90d 1108};
7f2b3a7d 1109module_platform_driver(bcap_driver);
63b1a90d
SJ
1110
1111MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1112MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1113MODULE_LICENSE("GPL v2");