[media] bfin_capture: fix compiler warning
[pandora-kernel.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         enum v4l2_mbus_pixelcode 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   = V4L2_MBUS_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   = V4L2_MBUS_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   = V4L2_MBUS_FMT_UYVY8_1X16,
134                 .bpp         = 16,
135                 .dlen        = 16,
136         },
137         {
138                 .desc        = "RGB 565",
139                 .pixelformat = V4L2_PIX_FMT_RGB565,
140                 .mbus_code   = V4L2_MBUS_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   = V4L2_MBUS_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         enum v4l2_mbus_pixelcode 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_init(struct vb2_buffer *vb)
306 {
307         struct bcap_buffer *buf = to_bcap_vb(vb);
308
309         INIT_LIST_HEAD(&buf->list);
310         return 0;
311 }
312
313 static int bcap_buffer_prepare(struct vb2_buffer *vb)
314 {
315         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
316         struct bcap_buffer *buf = to_bcap_vb(vb);
317         unsigned long size;
318
319         size = bcap_dev->fmt.sizeimage;
320         if (vb2_plane_size(vb, 0) < size) {
321                 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
322                                 vb2_plane_size(vb, 0), size);
323                 return -EINVAL;
324         }
325         vb2_set_plane_payload(&buf->vb, 0, size);
326
327         return 0;
328 }
329
330 static void bcap_buffer_queue(struct vb2_buffer *vb)
331 {
332         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
333         struct bcap_buffer *buf = to_bcap_vb(vb);
334         unsigned long flags;
335
336         spin_lock_irqsave(&bcap_dev->lock, flags);
337         list_add_tail(&buf->list, &bcap_dev->dma_queue);
338         spin_unlock_irqrestore(&bcap_dev->lock, flags);
339 }
340
341 static void bcap_buffer_cleanup(struct vb2_buffer *vb)
342 {
343         struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
344         struct bcap_buffer *buf = to_bcap_vb(vb);
345         unsigned long flags;
346
347         spin_lock_irqsave(&bcap_dev->lock, flags);
348         list_del_init(&buf->list);
349         spin_unlock_irqrestore(&bcap_dev->lock, flags);
350 }
351
352 static void bcap_lock(struct vb2_queue *vq)
353 {
354         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
355         mutex_lock(&bcap_dev->mutex);
356 }
357
358 static void bcap_unlock(struct vb2_queue *vq)
359 {
360         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
361         mutex_unlock(&bcap_dev->mutex);
362 }
363
364 static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
365 {
366         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
367         struct ppi_if *ppi = bcap_dev->ppi;
368         struct ppi_params params;
369         int ret;
370
371         /* enable streamon on the sub device */
372         ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
373         if (ret && (ret != -ENOIOCTLCMD)) {
374                 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
375                 return ret;
376         }
377
378         /* set ppi params */
379         params.width = bcap_dev->fmt.width;
380         params.height = bcap_dev->fmt.height;
381         params.bpp = bcap_dev->bpp;
382         params.dlen = bcap_dev->dlen;
383         params.ppi_control = bcap_dev->cfg->ppi_control;
384         params.int_mask = bcap_dev->cfg->int_mask;
385         if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
386                         & V4L2_IN_CAP_DV_TIMINGS) {
387                 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
388
389                 params.hdelay = bt->hsync + bt->hbackporch;
390                 params.vdelay = bt->vsync + bt->vbackporch;
391                 params.line = bt->hfrontporch + bt->hsync
392                                 + bt->hbackporch + bt->width;
393                 params.frame = bt->vfrontporch + bt->vsync
394                                 + bt->vbackporch + bt->height;
395                 if (bt->interlaced)
396                         params.frame += bt->il_vfrontporch + bt->il_vsync
397                                         + bt->il_vbackporch;
398         } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
399                         & V4L2_IN_CAP_STD) {
400                 params.hdelay = 0;
401                 params.vdelay = 0;
402                 if (bcap_dev->std & V4L2_STD_525_60) {
403                         params.line = 858;
404                         params.frame = 525;
405                 } else {
406                         params.line = 864;
407                         params.frame = 625;
408                 }
409         } else {
410                 params.hdelay = 0;
411                 params.vdelay = 0;
412                 params.line = params.width + bcap_dev->cfg->blank_pixels;
413                 params.frame = params.height;
414         }
415         ret = ppi->ops->set_params(ppi, &params);
416         if (ret < 0) {
417                 v4l2_err(&bcap_dev->v4l2_dev,
418                                 "Error in setting ppi params\n");
419                 return ret;
420         }
421
422         /* attach ppi DMA irq handler */
423         ret = ppi->ops->attach_irq(ppi, bcap_isr);
424         if (ret < 0) {
425                 v4l2_err(&bcap_dev->v4l2_dev,
426                                 "Error in attaching interrupt handler\n");
427                 return ret;
428         }
429
430         INIT_COMPLETION(bcap_dev->comp);
431         bcap_dev->stop = false;
432         return 0;
433 }
434
435 static int bcap_stop_streaming(struct vb2_queue *vq)
436 {
437         struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
438         struct ppi_if *ppi = bcap_dev->ppi;
439         int ret;
440
441         if (!vb2_is_streaming(vq))
442                 return 0;
443
444         bcap_dev->stop = true;
445         wait_for_completion(&bcap_dev->comp);
446         ppi->ops->stop(ppi);
447         ppi->ops->detach_irq(ppi);
448         ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
449         if (ret && (ret != -ENOIOCTLCMD))
450                 v4l2_err(&bcap_dev->v4l2_dev,
451                                 "stream off failed in subdev\n");
452
453         /* release all active buffers */
454         while (!list_empty(&bcap_dev->dma_queue)) {
455                 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
456                                                 struct bcap_buffer, list);
457                 list_del(&bcap_dev->cur_frm->list);
458                 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
459         }
460         return 0;
461 }
462
463 static struct vb2_ops bcap_video_qops = {
464         .queue_setup            = bcap_queue_setup,
465         .buf_init               = bcap_buffer_init,
466         .buf_prepare            = bcap_buffer_prepare,
467         .buf_cleanup            = bcap_buffer_cleanup,
468         .buf_queue              = bcap_buffer_queue,
469         .wait_prepare           = bcap_unlock,
470         .wait_finish            = bcap_lock,
471         .start_streaming        = bcap_start_streaming,
472         .stop_streaming         = bcap_stop_streaming,
473 };
474
475 static int bcap_reqbufs(struct file *file, void *priv,
476                         struct v4l2_requestbuffers *req_buf)
477 {
478         struct bcap_device *bcap_dev = video_drvdata(file);
479         struct vb2_queue *vq = &bcap_dev->buffer_queue;
480         struct v4l2_fh *fh = file->private_data;
481         struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
482
483         if (vb2_is_busy(vq))
484                 return -EBUSY;
485
486         bcap_fh->io_allowed = true;
487
488         return vb2_reqbufs(vq, req_buf);
489 }
490
491 static int bcap_querybuf(struct file *file, void *priv,
492                                 struct v4l2_buffer *buf)
493 {
494         struct bcap_device *bcap_dev = video_drvdata(file);
495
496         return vb2_querybuf(&bcap_dev->buffer_queue, buf);
497 }
498
499 static int bcap_qbuf(struct file *file, void *priv,
500                         struct v4l2_buffer *buf)
501 {
502         struct bcap_device *bcap_dev = video_drvdata(file);
503         struct v4l2_fh *fh = file->private_data;
504         struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
505
506         if (!bcap_fh->io_allowed)
507                 return -EBUSY;
508
509         return vb2_qbuf(&bcap_dev->buffer_queue, buf);
510 }
511
512 static int bcap_dqbuf(struct file *file, void *priv,
513                         struct v4l2_buffer *buf)
514 {
515         struct bcap_device *bcap_dev = video_drvdata(file);
516         struct v4l2_fh *fh = file->private_data;
517         struct bcap_fh *bcap_fh = container_of(fh, struct bcap_fh, fh);
518
519         if (!bcap_fh->io_allowed)
520                 return -EBUSY;
521
522         return vb2_dqbuf(&bcap_dev->buffer_queue,
523                                 buf, file->f_flags & O_NONBLOCK);
524 }
525
526 static irqreturn_t bcap_isr(int irq, void *dev_id)
527 {
528         struct ppi_if *ppi = dev_id;
529         struct bcap_device *bcap_dev = ppi->priv;
530         struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
531         dma_addr_t addr;
532
533         spin_lock(&bcap_dev->lock);
534
535         if (!list_empty(&bcap_dev->dma_queue)) {
536                 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
537                 if (ppi->err) {
538                         vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
539                         ppi->err = false;
540                 } else {
541                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
542                 }
543                 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
544                                 struct bcap_buffer, list);
545                 list_del(&bcap_dev->cur_frm->list);
546         } else {
547                 /* clear error flag, we will get a new frame */
548                 if (ppi->err)
549                         ppi->err = false;
550         }
551
552         ppi->ops->stop(ppi);
553
554         if (bcap_dev->stop) {
555                 complete(&bcap_dev->comp);
556         } else {
557                 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
558                 ppi->ops->update_addr(ppi, (unsigned long)addr);
559                 ppi->ops->start(ppi);
560         }
561
562         spin_unlock(&bcap_dev->lock);
563
564         return IRQ_HANDLED;
565 }
566
567 static int bcap_streamon(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         struct ppi_if *ppi = bcap_dev->ppi;
573         dma_addr_t addr;
574         int ret;
575
576         if (!fh->io_allowed)
577                 return -EBUSY;
578
579         /* call streamon to start streaming in videobuf */
580         ret = vb2_streamon(&bcap_dev->buffer_queue, buf_type);
581         if (ret)
582                 return ret;
583
584         /* if dma queue is empty, return error */
585         if (list_empty(&bcap_dev->dma_queue)) {
586                 v4l2_err(&bcap_dev->v4l2_dev, "dma queue is empty\n");
587                 ret = -EINVAL;
588                 goto err;
589         }
590
591         /* get the next frame from the dma queue */
592         bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
593                                         struct bcap_buffer, list);
594         /* remove buffer from the dma queue */
595         list_del(&bcap_dev->cur_frm->list);
596         addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
597         /* update DMA address */
598         ppi->ops->update_addr(ppi, (unsigned long)addr);
599         /* enable ppi */
600         ppi->ops->start(ppi);
601
602         return 0;
603 err:
604         vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
605         return ret;
606 }
607
608 static int bcap_streamoff(struct file *file, void *priv,
609                                 enum v4l2_buf_type buf_type)
610 {
611         struct bcap_device *bcap_dev = video_drvdata(file);
612         struct bcap_fh *fh = file->private_data;
613
614         if (!fh->io_allowed)
615                 return -EBUSY;
616
617         return vb2_streamoff(&bcap_dev->buffer_queue, buf_type);
618 }
619
620 static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
621 {
622         struct bcap_device *bcap_dev = video_drvdata(file);
623
624         return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
625 }
626
627 static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
628 {
629         struct bcap_device *bcap_dev = video_drvdata(file);
630
631         *std = bcap_dev->std;
632         return 0;
633 }
634
635 static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
636 {
637         struct bcap_device *bcap_dev = video_drvdata(file);
638         int ret;
639
640         if (vb2_is_busy(&bcap_dev->buffer_queue))
641                 return -EBUSY;
642
643         ret = v4l2_subdev_call(bcap_dev->sd, core, s_std, std);
644         if (ret < 0)
645                 return ret;
646
647         bcap_dev->std = std;
648         return 0;
649 }
650
651 static int bcap_enum_dv_timings(struct file *file, void *priv,
652                                 struct v4l2_enum_dv_timings *timings)
653 {
654         struct bcap_device *bcap_dev = video_drvdata(file);
655
656         return v4l2_subdev_call(bcap_dev->sd, video,
657                         enum_dv_timings, timings);
658 }
659
660 static int bcap_query_dv_timings(struct file *file, void *priv,
661                                 struct v4l2_dv_timings *timings)
662 {
663         struct bcap_device *bcap_dev = video_drvdata(file);
664
665         return v4l2_subdev_call(bcap_dev->sd, video,
666                                 query_dv_timings, timings);
667 }
668
669 static int bcap_g_dv_timings(struct file *file, void *priv,
670                                 struct v4l2_dv_timings *timings)
671 {
672         struct bcap_device *bcap_dev = video_drvdata(file);
673
674         *timings = bcap_dev->dv_timings;
675         return 0;
676 }
677
678 static int bcap_s_dv_timings(struct file *file, void *priv,
679                                 struct v4l2_dv_timings *timings)
680 {
681         struct bcap_device *bcap_dev = video_drvdata(file);
682         int ret;
683         if (vb2_is_busy(&bcap_dev->buffer_queue))
684                 return -EBUSY;
685
686         ret = v4l2_subdev_call(bcap_dev->sd, video, s_dv_timings, timings);
687         if (ret < 0)
688                 return ret;
689
690         bcap_dev->dv_timings = *timings;
691         return 0;
692 }
693
694 static int bcap_enum_input(struct file *file, void *priv,
695                                 struct v4l2_input *input)
696 {
697         struct bcap_device *bcap_dev = video_drvdata(file);
698         struct bfin_capture_config *config = bcap_dev->cfg;
699         int ret;
700         u32 status;
701
702         if (input->index >= config->num_inputs)
703                 return -EINVAL;
704
705         *input = config->inputs[input->index];
706         /* get input status */
707         ret = v4l2_subdev_call(bcap_dev->sd, video, g_input_status, &status);
708         if (!ret)
709                 input->status = status;
710         return 0;
711 }
712
713 static int bcap_g_input(struct file *file, void *priv, unsigned int *index)
714 {
715         struct bcap_device *bcap_dev = video_drvdata(file);
716
717         *index = bcap_dev->cur_input;
718         return 0;
719 }
720
721 static int bcap_s_input(struct file *file, void *priv, unsigned int index)
722 {
723         struct bcap_device *bcap_dev = video_drvdata(file);
724         struct bfin_capture_config *config = bcap_dev->cfg;
725         struct bcap_route *route;
726         int ret;
727
728         if (vb2_is_busy(&bcap_dev->buffer_queue))
729                 return -EBUSY;
730
731         if (index >= config->num_inputs)
732                 return -EINVAL;
733
734         route = &config->routes[index];
735         ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
736                                 route->input, route->output, 0);
737         if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
738                 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
739                 return ret;
740         }
741         bcap_dev->cur_input = index;
742         /* if this route has specific config, update ppi control */
743         if (route->ppi_control)
744                 config->ppi_control = route->ppi_control;
745         return 0;
746 }
747
748 static int bcap_try_format(struct bcap_device *bcap,
749                                 struct v4l2_pix_format *pixfmt,
750                                 struct bcap_format *bcap_fmt)
751 {
752         struct bcap_format *sf = bcap->sensor_formats;
753         struct bcap_format *fmt = NULL;
754         struct v4l2_mbus_framefmt mbus_fmt;
755         int ret, i;
756
757         for (i = 0; i < bcap->num_sensor_formats; i++) {
758                 fmt = &sf[i];
759                 if (pixfmt->pixelformat == fmt->pixelformat)
760                         break;
761         }
762         if (i == bcap->num_sensor_formats)
763                 fmt = &sf[0];
764
765         v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
766         ret = v4l2_subdev_call(bcap->sd, video,
767                                 try_mbus_fmt, &mbus_fmt);
768         if (ret < 0)
769                 return ret;
770         v4l2_fill_pix_format(pixfmt, &mbus_fmt);
771         if (bcap_fmt) {
772                 for (i = 0; i < bcap->num_sensor_formats; i++) {
773                         fmt = &sf[i];
774                         if (mbus_fmt.code == fmt->mbus_code)
775                                 break;
776                 }
777                 *bcap_fmt = *fmt;
778         }
779         pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
780         pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
781         return 0;
782 }
783
784 static int bcap_enum_fmt_vid_cap(struct file *file, void  *priv,
785                                         struct v4l2_fmtdesc *fmt)
786 {
787         struct bcap_device *bcap_dev = video_drvdata(file);
788         struct bcap_format *sf = bcap_dev->sensor_formats;
789
790         if (fmt->index >= bcap_dev->num_sensor_formats)
791                 return -EINVAL;
792
793         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
794         strlcpy(fmt->description,
795                 sf[fmt->index].desc,
796                 sizeof(fmt->description));
797         fmt->pixelformat = sf[fmt->index].pixelformat;
798         return 0;
799 }
800
801 static int bcap_try_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_pix_format *pixfmt = &fmt->fmt.pix;
806
807         return bcap_try_format(bcap_dev, pixfmt, NULL);
808 }
809
810 static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
811                                 struct v4l2_format *fmt)
812 {
813         struct bcap_device *bcap_dev = video_drvdata(file);
814
815         fmt->fmt.pix = bcap_dev->fmt;
816         return 0;
817 }
818
819 static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
820                                 struct v4l2_format *fmt)
821 {
822         struct bcap_device *bcap_dev = video_drvdata(file);
823         struct v4l2_mbus_framefmt mbus_fmt;
824         struct bcap_format bcap_fmt;
825         struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
826         int ret;
827
828         if (vb2_is_busy(&bcap_dev->buffer_queue))
829                 return -EBUSY;
830
831         /* see if format works */
832         ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
833         if (ret < 0)
834                 return ret;
835
836         v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
837         ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
838         if (ret < 0)
839                 return ret;
840         bcap_dev->fmt = *pixfmt;
841         bcap_dev->bpp = bcap_fmt.bpp;
842         bcap_dev->dlen = bcap_fmt.dlen;
843         return 0;
844 }
845
846 static int bcap_querycap(struct file *file, void  *priv,
847                                 struct v4l2_capability *cap)
848 {
849         struct bcap_device *bcap_dev = video_drvdata(file);
850
851         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
852         strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
853         strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
854         strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
855         return 0;
856 }
857
858 static int bcap_g_parm(struct file *file, void *fh,
859                                 struct v4l2_streamparm *a)
860 {
861         struct bcap_device *bcap_dev = video_drvdata(file);
862
863         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
864                 return -EINVAL;
865         return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
866 }
867
868 static int bcap_s_parm(struct file *file, void *fh,
869                                 struct v4l2_streamparm *a)
870 {
871         struct bcap_device *bcap_dev = video_drvdata(file);
872
873         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
874                 return -EINVAL;
875         return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
876 }
877
878 static int bcap_log_status(struct file *file, void *priv)
879 {
880         struct bcap_device *bcap_dev = video_drvdata(file);
881         /* status for sub devices */
882         v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
883         return 0;
884 }
885
886 static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
887         .vidioc_querycap         = bcap_querycap,
888         .vidioc_g_fmt_vid_cap    = bcap_g_fmt_vid_cap,
889         .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
890         .vidioc_s_fmt_vid_cap    = bcap_s_fmt_vid_cap,
891         .vidioc_try_fmt_vid_cap  = bcap_try_fmt_vid_cap,
892         .vidioc_enum_input       = bcap_enum_input,
893         .vidioc_g_input          = bcap_g_input,
894         .vidioc_s_input          = bcap_s_input,
895         .vidioc_querystd         = bcap_querystd,
896         .vidioc_s_std            = bcap_s_std,
897         .vidioc_g_std            = bcap_g_std,
898         .vidioc_s_dv_timings     = bcap_s_dv_timings,
899         .vidioc_g_dv_timings     = bcap_g_dv_timings,
900         .vidioc_query_dv_timings = bcap_query_dv_timings,
901         .vidioc_enum_dv_timings  = bcap_enum_dv_timings,
902         .vidioc_reqbufs          = bcap_reqbufs,
903         .vidioc_querybuf         = bcap_querybuf,
904         .vidioc_qbuf             = bcap_qbuf,
905         .vidioc_dqbuf            = bcap_dqbuf,
906         .vidioc_streamon         = bcap_streamon,
907         .vidioc_streamoff        = bcap_streamoff,
908         .vidioc_g_parm           = bcap_g_parm,
909         .vidioc_s_parm           = bcap_s_parm,
910         .vidioc_log_status       = bcap_log_status,
911 };
912
913 static struct v4l2_file_operations bcap_fops = {
914         .owner = THIS_MODULE,
915         .open = bcap_open,
916         .release = bcap_release,
917         .unlocked_ioctl = video_ioctl2,
918         .mmap = bcap_mmap,
919 #ifndef CONFIG_MMU
920         .get_unmapped_area = bcap_get_unmapped_area,
921 #endif
922         .poll = bcap_poll
923 };
924
925 static int bcap_probe(struct platform_device *pdev)
926 {
927         struct bcap_device *bcap_dev;
928         struct video_device *vfd;
929         struct i2c_adapter *i2c_adap;
930         struct bfin_capture_config *config;
931         struct vb2_queue *q;
932         struct bcap_route *route;
933         int ret;
934
935         config = pdev->dev.platform_data;
936         if (!config || !config->num_inputs) {
937                 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
938                 return -ENODEV;
939         }
940
941         bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
942         if (!bcap_dev) {
943                 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
944                 return -ENOMEM;
945         }
946
947         bcap_dev->cfg = config;
948
949         bcap_dev->ppi = ppi_create_instance(config->ppi_info);
950         if (!bcap_dev->ppi) {
951                 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
952                 ret = -ENODEV;
953                 goto err_free_dev;
954         }
955         bcap_dev->ppi->priv = bcap_dev;
956
957         bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
958         if (IS_ERR(bcap_dev->alloc_ctx)) {
959                 ret = PTR_ERR(bcap_dev->alloc_ctx);
960                 goto err_free_ppi;
961         }
962
963         vfd = video_device_alloc();
964         if (!vfd) {
965                 ret = -ENOMEM;
966                 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
967                 goto err_cleanup_ctx;
968         }
969
970         /* initialize field of video device */
971         vfd->release            = video_device_release;
972         vfd->fops               = &bcap_fops;
973         vfd->ioctl_ops          = &bcap_ioctl_ops;
974         vfd->tvnorms            = 0;
975         vfd->v4l2_dev           = &bcap_dev->v4l2_dev;
976         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
977         strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
978         bcap_dev->video_dev     = vfd;
979
980         ret = v4l2_device_register(&pdev->dev, &bcap_dev->v4l2_dev);
981         if (ret) {
982                 v4l2_err(pdev->dev.driver,
983                                 "Unable to register v4l2 device\n");
984                 goto err_release_vdev;
985         }
986         v4l2_info(&bcap_dev->v4l2_dev, "v4l2 device registered\n");
987
988         bcap_dev->v4l2_dev.ctrl_handler = &bcap_dev->ctrl_handler;
989         ret = v4l2_ctrl_handler_init(&bcap_dev->ctrl_handler, 0);
990         if (ret) {
991                 v4l2_err(&bcap_dev->v4l2_dev,
992                                 "Unable to init control handler\n");
993                 goto err_unreg_v4l2;
994         }
995
996         spin_lock_init(&bcap_dev->lock);
997         /* initialize queue */
998         q = &bcap_dev->buffer_queue;
999         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1000         q->io_modes = VB2_MMAP;
1001         q->drv_priv = bcap_dev;
1002         q->buf_struct_size = sizeof(struct bcap_buffer);
1003         q->ops = &bcap_video_qops;
1004         q->mem_ops = &vb2_dma_contig_memops;
1005         q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1006
1007         ret = vb2_queue_init(q);
1008         if (ret)
1009                 goto err_free_handler;
1010
1011         mutex_init(&bcap_dev->mutex);
1012         init_completion(&bcap_dev->comp);
1013
1014         /* init video dma queues */
1015         INIT_LIST_HEAD(&bcap_dev->dma_queue);
1016
1017         vfd->lock = &bcap_dev->mutex;
1018
1019         /* register video device */
1020         ret = video_register_device(bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
1021         if (ret) {
1022                 v4l2_err(&bcap_dev->v4l2_dev,
1023                                 "Unable to register video device\n");
1024                 goto err_free_handler;
1025         }
1026         video_set_drvdata(bcap_dev->video_dev, bcap_dev);
1027         v4l2_info(&bcap_dev->v4l2_dev, "video device registered as: %s\n",
1028                         video_device_node_name(vfd));
1029
1030         /* load up the subdevice */
1031         i2c_adap = i2c_get_adapter(config->i2c_adapter_id);
1032         if (!i2c_adap) {
1033                 v4l2_err(&bcap_dev->v4l2_dev,
1034                                 "Unable to find i2c adapter\n");
1035                 ret = -ENODEV;
1036                 goto err_unreg_vdev;
1037
1038         }
1039         bcap_dev->sd = v4l2_i2c_new_subdev_board(&bcap_dev->v4l2_dev,
1040                                                  i2c_adap,
1041                                                  &config->board_info,
1042                                                  NULL);
1043         if (bcap_dev->sd) {
1044                 int i;
1045
1046                 /* update tvnorms from the sub devices */
1047                 for (i = 0; i < config->num_inputs; i++)
1048                         vfd->tvnorms |= config->inputs[i].std;
1049         } else {
1050                 v4l2_err(&bcap_dev->v4l2_dev,
1051                                 "Unable to register sub device\n");
1052                 ret = -ENODEV;
1053                 goto err_unreg_vdev;
1054         }
1055
1056         v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
1057
1058         /*
1059          * explicitly set input, otherwise some boards
1060          * may not work at the state as we expected
1061          */
1062         route = &config->routes[0];
1063         ret = v4l2_subdev_call(bcap_dev->sd, video, s_routing,
1064                                 route->input, route->output, 0);
1065         if ((ret < 0) && (ret != -ENOIOCTLCMD)) {
1066                 v4l2_err(&bcap_dev->v4l2_dev, "Failed to set input\n");
1067                 goto err_unreg_vdev;
1068         }
1069         bcap_dev->cur_input = 0;
1070         /* if this route has specific config, update ppi control */
1071         if (route->ppi_control)
1072                 config->ppi_control = route->ppi_control;
1073
1074         /* now we can probe the default state */
1075         if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
1076                 v4l2_std_id std;
1077                 ret = v4l2_subdev_call(bcap_dev->sd, core, g_std, &std);
1078                 if (ret) {
1079                         v4l2_err(&bcap_dev->v4l2_dev,
1080                                         "Unable to get std\n");
1081                         goto err_unreg_vdev;
1082                 }
1083                 bcap_dev->std = std;
1084         }
1085         if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
1086                 struct v4l2_dv_timings dv_timings;
1087                 ret = v4l2_subdev_call(bcap_dev->sd, video,
1088                                 g_dv_timings, &dv_timings);
1089                 if (ret) {
1090                         v4l2_err(&bcap_dev->v4l2_dev,
1091                                         "Unable to get dv timings\n");
1092                         goto err_unreg_vdev;
1093                 }
1094                 bcap_dev->dv_timings = dv_timings;
1095         }
1096         ret = bcap_init_sensor_formats(bcap_dev);
1097         if (ret) {
1098                 v4l2_err(&bcap_dev->v4l2_dev,
1099                                 "Unable to create sensor formats table\n");
1100                 goto err_unreg_vdev;
1101         }
1102         return 0;
1103 err_unreg_vdev:
1104         video_unregister_device(bcap_dev->video_dev);
1105         bcap_dev->video_dev = NULL;
1106 err_free_handler:
1107         v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1108 err_unreg_v4l2:
1109         v4l2_device_unregister(&bcap_dev->v4l2_dev);
1110 err_release_vdev:
1111         if (bcap_dev->video_dev)
1112                 video_device_release(bcap_dev->video_dev);
1113 err_cleanup_ctx:
1114         vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1115 err_free_ppi:
1116         ppi_delete_instance(bcap_dev->ppi);
1117 err_free_dev:
1118         kfree(bcap_dev);
1119         return ret;
1120 }
1121
1122 static int bcap_remove(struct platform_device *pdev)
1123 {
1124         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1125         struct bcap_device *bcap_dev = container_of(v4l2_dev,
1126                                                 struct bcap_device, v4l2_dev);
1127
1128         bcap_free_sensor_formats(bcap_dev);
1129         video_unregister_device(bcap_dev->video_dev);
1130         v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
1131         v4l2_device_unregister(v4l2_dev);
1132         vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
1133         ppi_delete_instance(bcap_dev->ppi);
1134         kfree(bcap_dev);
1135         return 0;
1136 }
1137
1138 static struct platform_driver bcap_driver = {
1139         .driver = {
1140                 .name  = CAPTURE_DRV_NAME,
1141                 .owner = THIS_MODULE,
1142         },
1143         .probe = bcap_probe,
1144         .remove = bcap_remove,
1145 };
1146 module_platform_driver(bcap_driver);
1147
1148 MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1149 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1150 MODULE_LICENSE("GPL v2");