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