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