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