]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/blackfin/bfin_capture.c
[media] media: blackfin: bfin_capture: add support for vidioc_create_bufs
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / blackfin / bfin_capture.c
CommitLineData
63b1a90d
SJ
1/*
2 * Analog Devices video capture driver
3 *
4 * Copyright (c) 2011 Analog Devices Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/completion.h>
21#include <linux/delay.h>
22#include <linux/errno.h>
23#include <linux/fs.h>
24#include <linux/i2c.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/slab.h>
32#include <linux/time.h>
33#include <linux/types.h>
34
63b1a90d
SJ
35#include <media/v4l2-common.h>
36#include <media/v4l2-ctrls.h>
37#include <media/v4l2-device.h>
38#include <media/v4l2-ioctl.h>
39#include <media/videobuf2-dma-contig.h>
40
41#include <asm/dma.h>
42
43#include <media/blackfin/bfin_capture.h>
44#include <media/blackfin/ppi.h>
45
46#define CAPTURE_DRV_NAME "bfin_capture"
63b1a90d
SJ
47
48struct bcap_format {
49 char *desc;
50 u32 pixelformat;
27ffaeb0 51 u32 mbus_code;
63b1a90d 52 int bpp; /* bits per pixel */
45b82596 53 int dlen; /* data length for ppi in bits */
63b1a90d
SJ
54};
55
56struct bcap_buffer {
57 struct vb2_buffer vb;
58 struct list_head list;
59};
60
61struct bcap_device {
62 /* capture device instance */
63 struct v4l2_device v4l2_dev;
64 /* v4l2 control handler */
65 struct v4l2_ctrl_handler ctrl_handler;
66 /* device node data */
67 struct video_device *video_dev;
68 /* sub device instance */
69 struct v4l2_subdev *sd;
70 /* capture config */
71 struct bfin_capture_config *cfg;
72 /* ppi interface */
73 struct ppi_if *ppi;
74 /* current input */
75 unsigned int cur_input;
76 /* current selected standard */
77 v4l2_std_id std;
45b82596
SJ
78 /* current selected dv_timings */
79 struct v4l2_dv_timings dv_timings;
63b1a90d
SJ
80 /* used to store pixel format */
81 struct v4l2_pix_format fmt;
82 /* bits per pixel*/
83 int bpp;
45b82596
SJ
84 /* data length for ppi in bits */
85 int dlen;
63b1a90d
SJ
86 /* used to store sensor supported format */
87 struct bcap_format *sensor_formats;
88 /* number of sensor formats array */
89 int num_sensor_formats;
90 /* pointing to current video buffer */
91 struct bcap_buffer *cur_frm;
63b1a90d
SJ
92 /* buffer queue used in videobuf2 */
93 struct vb2_queue buffer_queue;
94 /* allocator-specific contexts for each plane */
95 struct vb2_alloc_ctx *alloc_ctx;
96 /* queue of filled frames */
97 struct list_head dma_queue;
98 /* used in videobuf2 callback */
99 spinlock_t lock;
100 /* used to access capture device */
101 struct mutex mutex;
102 /* used to wait ppi to complete one transfer */
103 struct completion comp;
104 /* prepare to stop */
105 bool stop;
106};
107
63b1a90d
SJ
108static const struct bcap_format bcap_formats[] = {
109 {
110 .desc = "YCbCr 4:2:2 Interleaved UYVY",
111 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 112 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
63b1a90d 113 .bpp = 16,
45b82596 114 .dlen = 8,
63b1a90d
SJ
115 },
116 {
117 .desc = "YCbCr 4:2:2 Interleaved YUYV",
118 .pixelformat = V4L2_PIX_FMT_YUYV,
27ffaeb0 119 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
63b1a90d 120 .bpp = 16,
45b82596
SJ
121 .dlen = 8,
122 },
123 {
124 .desc = "YCbCr 4:2:2 Interleaved UYVY",
125 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 126 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
45b82596
SJ
127 .bpp = 16,
128 .dlen = 16,
63b1a90d
SJ
129 },
130 {
131 .desc = "RGB 565",
132 .pixelformat = V4L2_PIX_FMT_RGB565,
27ffaeb0 133 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
63b1a90d 134 .bpp = 16,
45b82596 135 .dlen = 8,
63b1a90d
SJ
136 },
137 {
138 .desc = "RGB 444",
139 .pixelformat = V4L2_PIX_FMT_RGB444,
27ffaeb0 140 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
63b1a90d 141 .bpp = 16,
45b82596 142 .dlen = 8,
63b1a90d
SJ
143 },
144
145};
146#define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
147
148static irqreturn_t bcap_isr(int irq, void *dev_id);
149
150static struct bcap_buffer *to_bcap_vb(struct vb2_buffer *vb)
151{
152 return container_of(vb, struct bcap_buffer, vb);
153}
154
155static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
156{
27ffaeb0 157 u32 code;
63b1a90d
SJ
158 struct bcap_format *sf;
159 unsigned int num_formats = 0;
160 int i, j;
161
162 while (!v4l2_subdev_call(bcap_dev->sd, video,
163 enum_mbus_fmt, num_formats, &code))
164 num_formats++;
165 if (!num_formats)
166 return -ENXIO;
167
168 sf = kzalloc(num_formats * sizeof(*sf), GFP_KERNEL);
169 if (!sf)
170 return -ENOMEM;
171
172 for (i = 0; i < num_formats; i++) {
173 v4l2_subdev_call(bcap_dev->sd, video,
174 enum_mbus_fmt, i, &code);
175 for (j = 0; j < BCAP_MAX_FMTS; j++)
176 if (code == bcap_formats[j].mbus_code)
177 break;
178 if (j == BCAP_MAX_FMTS) {
179 /* we don't allow this sensor working with our bridge */
180 kfree(sf);
181 return -EINVAL;
182 }
183 sf[i] = bcap_formats[j];
184 }
185 bcap_dev->sensor_formats = sf;
186 bcap_dev->num_sensor_formats = num_formats;
187 return 0;
188}
189
190static void bcap_free_sensor_formats(struct bcap_device *bcap_dev)
191{
192 bcap_dev->num_sensor_formats = 0;
193 kfree(bcap_dev->sensor_formats);
194 bcap_dev->sensor_formats = NULL;
195}
196
63b1a90d
SJ
197#ifndef CONFIG_MMU
198static unsigned long bcap_get_unmapped_area(struct file *file,
199 unsigned long addr,
200 unsigned long len,
201 unsigned long pgoff,
202 unsigned long flags)
203{
204 struct bcap_device *bcap_dev = video_drvdata(file);
205
206 return vb2_get_unmapped_area(&bcap_dev->buffer_queue,
207 addr,
208 len,
209 pgoff,
210 flags);
211}
212#endif
213
63b1a90d
SJ
214static int bcap_queue_setup(struct vb2_queue *vq,
215 const struct v4l2_format *fmt,
216 unsigned int *nbuffers, unsigned int *nplanes,
217 unsigned int sizes[], void *alloc_ctxs[])
218{
219 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
220
f3140022
LP
221 if (fmt && fmt->fmt.pix.sizeimage < bcap_dev->fmt.sizeimage)
222 return -EINVAL;
223
224 if (vq->num_buffers + *nbuffers < 2)
225 *nbuffers = 2;
63b1a90d
SJ
226
227 *nplanes = 1;
f3140022 228 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
63b1a90d
SJ
229 alloc_ctxs[0] = bcap_dev->alloc_ctx;
230
231 return 0;
232}
233
63b1a90d
SJ
234static int bcap_buffer_prepare(struct vb2_buffer *vb)
235{
236 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
cd3c38ef 237 unsigned long size = bcap_dev->fmt.sizeimage;
63b1a90d 238
63b1a90d
SJ
239 if (vb2_plane_size(vb, 0) < size) {
240 v4l2_err(&bcap_dev->v4l2_dev, "buffer too small (%lu < %lu)\n",
241 vb2_plane_size(vb, 0), size);
242 return -EINVAL;
243 }
cd3c38ef
LP
244 vb2_set_plane_payload(vb, 0, size);
245
246 vb->v4l2_buf.field = bcap_dev->fmt.field;
63b1a90d
SJ
247
248 return 0;
249}
250
251static void bcap_buffer_queue(struct vb2_buffer *vb)
252{
253 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
254 struct bcap_buffer *buf = to_bcap_vb(vb);
255 unsigned long flags;
256
257 spin_lock_irqsave(&bcap_dev->lock, flags);
258 list_add_tail(&buf->list, &bcap_dev->dma_queue);
259 spin_unlock_irqrestore(&bcap_dev->lock, flags);
260}
261
262static void bcap_buffer_cleanup(struct vb2_buffer *vb)
263{
264 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
265 struct bcap_buffer *buf = to_bcap_vb(vb);
266 unsigned long flags;
267
268 spin_lock_irqsave(&bcap_dev->lock, flags);
269 list_del_init(&buf->list);
270 spin_unlock_irqrestore(&bcap_dev->lock, flags);
271}
272
63b1a90d
SJ
273static int bcap_start_streaming(struct vb2_queue *vq, unsigned int count)
274{
275 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
276 struct ppi_if *ppi = bcap_dev->ppi;
6692871a 277 struct bcap_buffer *buf, *tmp;
63b1a90d 278 struct ppi_params params;
d61233bf 279 dma_addr_t addr;
63b1a90d
SJ
280 int ret;
281
282 /* enable streamon on the sub device */
283 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 1);
284 if (ret && (ret != -ENOIOCTLCMD)) {
285 v4l2_err(&bcap_dev->v4l2_dev, "stream on failed in subdev\n");
6692871a 286 goto err;
63b1a90d
SJ
287 }
288
289 /* set ppi params */
290 params.width = bcap_dev->fmt.width;
291 params.height = bcap_dev->fmt.height;
292 params.bpp = bcap_dev->bpp;
45b82596 293 params.dlen = bcap_dev->dlen;
63b1a90d
SJ
294 params.ppi_control = bcap_dev->cfg->ppi_control;
295 params.int_mask = bcap_dev->cfg->int_mask;
45b82596 296 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
a8451ed2 297 & V4L2_IN_CAP_DV_TIMINGS) {
45b82596
SJ
298 struct v4l2_bt_timings *bt = &bcap_dev->dv_timings.bt;
299
300 params.hdelay = bt->hsync + bt->hbackporch;
301 params.vdelay = bt->vsync + bt->vbackporch;
eacf8f9a
HV
302 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
303 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
45b82596
SJ
304 } else if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
305 & V4L2_IN_CAP_STD) {
306 params.hdelay = 0;
307 params.vdelay = 0;
308 if (bcap_dev->std & V4L2_STD_525_60) {
309 params.line = 858;
310 params.frame = 525;
311 } else {
312 params.line = 864;
313 params.frame = 625;
314 }
315 } else {
316 params.hdelay = 0;
317 params.vdelay = 0;
318 params.line = params.width + bcap_dev->cfg->blank_pixels;
319 params.frame = params.height;
320 }
63b1a90d
SJ
321 ret = ppi->ops->set_params(ppi, &params);
322 if (ret < 0) {
323 v4l2_err(&bcap_dev->v4l2_dev,
324 "Error in setting ppi params\n");
6692871a 325 goto err;
63b1a90d
SJ
326 }
327
328 /* attach ppi DMA irq handler */
329 ret = ppi->ops->attach_irq(ppi, bcap_isr);
330 if (ret < 0) {
331 v4l2_err(&bcap_dev->v4l2_dev,
332 "Error in attaching interrupt handler\n");
6692871a 333 goto err;
63b1a90d
SJ
334 }
335
16735d02 336 reinit_completion(&bcap_dev->comp);
63b1a90d 337 bcap_dev->stop = false;
6692871a 338
d61233bf
LP
339 /* get the next frame from the dma queue */
340 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
341 struct bcap_buffer, list);
342 /* remove buffer from the dma queue */
343 list_del_init(&bcap_dev->cur_frm->list);
344 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
345 /* update DMA address */
346 ppi->ops->update_addr(ppi, (unsigned long)addr);
347 /* enable ppi */
348 ppi->ops->start(ppi);
349
63b1a90d 350 return 0;
6692871a
LP
351
352err:
353 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
354 list_del(&buf->list);
355 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
356 }
357
358 return ret;
63b1a90d
SJ
359}
360
e37559b2 361static void bcap_stop_streaming(struct vb2_queue *vq)
63b1a90d
SJ
362{
363 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
364 struct ppi_if *ppi = bcap_dev->ppi;
365 int ret;
366
63b1a90d
SJ
367 bcap_dev->stop = true;
368 wait_for_completion(&bcap_dev->comp);
369 ppi->ops->stop(ppi);
370 ppi->ops->detach_irq(ppi);
371 ret = v4l2_subdev_call(bcap_dev->sd, video, s_stream, 0);
372 if (ret && (ret != -ENOIOCTLCMD))
373 v4l2_err(&bcap_dev->v4l2_dev,
374 "stream off failed in subdev\n");
375
376 /* release all active buffers */
2bf34b2f
LP
377 if (bcap_dev->cur_frm)
378 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
379
63b1a90d 380 while (!list_empty(&bcap_dev->dma_queue)) {
d78a4882 381 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
63b1a90d 382 struct bcap_buffer, list);
7ba3c21c 383 list_del_init(&bcap_dev->cur_frm->list);
d78a4882 384 vb2_buffer_done(&bcap_dev->cur_frm->vb, VB2_BUF_STATE_ERROR);
63b1a90d 385 }
63b1a90d
SJ
386}
387
388static struct vb2_ops bcap_video_qops = {
389 .queue_setup = bcap_queue_setup,
63b1a90d
SJ
390 .buf_prepare = bcap_buffer_prepare,
391 .buf_cleanup = bcap_buffer_cleanup,
392 .buf_queue = bcap_buffer_queue,
4d655ca4
PL
393 .wait_prepare = vb2_ops_wait_prepare,
394 .wait_finish = vb2_ops_wait_finish,
63b1a90d
SJ
395 .start_streaming = bcap_start_streaming,
396 .stop_streaming = bcap_stop_streaming,
397};
398
63b1a90d
SJ
399static irqreturn_t bcap_isr(int irq, void *dev_id)
400{
401 struct ppi_if *ppi = dev_id;
402 struct bcap_device *bcap_dev = ppi->priv;
63b1a90d
SJ
403 struct vb2_buffer *vb = &bcap_dev->cur_frm->vb;
404 dma_addr_t addr;
405
406 spin_lock(&bcap_dev->lock);
407
d78a4882 408 if (!list_empty(&bcap_dev->dma_queue)) {
8e6057b5 409 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
d78a4882
SJ
410 if (ppi->err) {
411 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
412 ppi->err = false;
413 } else {
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);
7ba3c21c 418 list_del_init(&bcap_dev->cur_frm->list);
d78a4882
SJ
419 } else {
420 /* clear error flag, we will get a new frame */
421 if (ppi->err)
422 ppi->err = false;
63b1a90d
SJ
423 }
424
425 ppi->ops->stop(ppi);
426
427 if (bcap_dev->stop) {
428 complete(&bcap_dev->comp);
429 } else {
d78a4882
SJ
430 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb, 0);
431 ppi->ops->update_addr(ppi, (unsigned long)addr);
63b1a90d
SJ
432 ppi->ops->start(ppi);
433 }
434
435 spin_unlock(&bcap_dev->lock);
436
437 return IRQ_HANDLED;
438}
439
63b1a90d
SJ
440static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
441{
442 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716
LP
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;
63b1a90d
SJ
448
449 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
450}
451
452static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
453{
454 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716
LP
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;
63b1a90d
SJ
460
461 *std = bcap_dev->std;
462 return 0;
463}
464
314527ac 465static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
63b1a90d
SJ
466{
467 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716 468 struct v4l2_input input;
63b1a90d
SJ
469 int ret;
470
fe00b716
LP
471 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
472 if (!(input.capabilities & V4L2_IN_CAP_STD))
473 return -ENODATA;
474
63b1a90d
SJ
475 if (vb2_is_busy(&bcap_dev->buffer_queue))
476 return -EBUSY;
477
8774bed9 478 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
63b1a90d
SJ
479 if (ret < 0)
480 return ret;
481
314527ac 482 bcap_dev->std = std;
63b1a90d
SJ
483 return 0;
484}
485
ead156c4
SJ
486static 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);
116a6488
LP
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;
ead156c4 495
f1f8e434
LP
496 timings->pad = 0;
497
498 return v4l2_subdev_call(bcap_dev->sd, pad,
ead156c4
SJ
499 enum_dv_timings, timings);
500}
501
502static int bcap_query_dv_timings(struct file *file, void *priv,
45b82596
SJ
503 struct v4l2_dv_timings *timings)
504{
505 struct bcap_device *bcap_dev = video_drvdata(file);
116a6488
LP
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;
45b82596 511
ead156c4
SJ
512 return v4l2_subdev_call(bcap_dev->sd, video,
513 query_dv_timings, timings);
514}
45b82596 515
ead156c4
SJ
516static 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);
116a6488
LP
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;
ead156c4
SJ
525
526 *timings = bcap_dev->dv_timings;
45b82596
SJ
527 return 0;
528}
529
530static 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);
116a6488 534 struct v4l2_input input;
45b82596 535 int ret;
116a6488
LP
536
537 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
538 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
539 return -ENODATA;
540
45b82596
SJ
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
63b1a90d
SJ
552static 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
571static 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
579static 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;
45b82596
SJ
600 /* if this route has specific config, update ppi control */
601 if (route->ppi_control)
602 config->ppi_control = route->ppi_control;
63b1a90d
SJ
603 return 0;
604}
605
606static int bcap_try_format(struct bcap_device *bcap,
607 struct v4l2_pix_format *pixfmt,
45b82596 608 struct bcap_format *bcap_fmt)
63b1a90d
SJ
609{
610 struct bcap_format *sf = bcap->sensor_formats;
611 struct bcap_format *fmt = NULL;
612 struct v4l2_mbus_framefmt mbus_fmt;
613 int ret, i;
614
615 for (i = 0; i < bcap->num_sensor_formats; i++) {
616 fmt = &sf[i];
617 if (pixfmt->pixelformat == fmt->pixelformat)
618 break;
619 }
620 if (i == bcap->num_sensor_formats)
621 fmt = &sf[0];
622
63b1a90d
SJ
623 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, fmt->mbus_code);
624 ret = v4l2_subdev_call(bcap->sd, video,
625 try_mbus_fmt, &mbus_fmt);
626 if (ret < 0)
627 return ret;
628 v4l2_fill_pix_format(pixfmt, &mbus_fmt);
45b82596
SJ
629 if (bcap_fmt) {
630 for (i = 0; i < bcap->num_sensor_formats; i++) {
631 fmt = &sf[i];
632 if (mbus_fmt.code == fmt->mbus_code)
633 break;
634 }
635 *bcap_fmt = *fmt;
636 }
63b1a90d
SJ
637 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
638 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
639 return 0;
640}
641
642static int bcap_enum_fmt_vid_cap(struct file *file, void *priv,
643 struct v4l2_fmtdesc *fmt)
644{
645 struct bcap_device *bcap_dev = video_drvdata(file);
646 struct bcap_format *sf = bcap_dev->sensor_formats;
647
648 if (fmt->index >= bcap_dev->num_sensor_formats)
649 return -EINVAL;
650
651 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
652 strlcpy(fmt->description,
653 sf[fmt->index].desc,
654 sizeof(fmt->description));
655 fmt->pixelformat = sf[fmt->index].pixelformat;
656 return 0;
657}
658
659static int bcap_try_fmt_vid_cap(struct file *file, void *priv,
660 struct v4l2_format *fmt)
661{
662 struct bcap_device *bcap_dev = video_drvdata(file);
663 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
664
45b82596 665 return bcap_try_format(bcap_dev, pixfmt, NULL);
63b1a90d
SJ
666}
667
668static int bcap_g_fmt_vid_cap(struct file *file, void *priv,
669 struct v4l2_format *fmt)
670{
671 struct bcap_device *bcap_dev = video_drvdata(file);
672
673 fmt->fmt.pix = bcap_dev->fmt;
674 return 0;
675}
676
677static int bcap_s_fmt_vid_cap(struct file *file, void *priv,
678 struct v4l2_format *fmt)
679{
680 struct bcap_device *bcap_dev = video_drvdata(file);
681 struct v4l2_mbus_framefmt mbus_fmt;
45b82596 682 struct bcap_format bcap_fmt;
63b1a90d 683 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
45b82596 684 int ret;
63b1a90d
SJ
685
686 if (vb2_is_busy(&bcap_dev->buffer_queue))
687 return -EBUSY;
688
689 /* see if format works */
45b82596 690 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
63b1a90d
SJ
691 if (ret < 0)
692 return ret;
693
45b82596 694 v4l2_fill_mbus_format(&mbus_fmt, pixfmt, bcap_fmt.mbus_code);
63b1a90d
SJ
695 ret = v4l2_subdev_call(bcap_dev->sd, video, s_mbus_fmt, &mbus_fmt);
696 if (ret < 0)
697 return ret;
698 bcap_dev->fmt = *pixfmt;
45b82596
SJ
699 bcap_dev->bpp = bcap_fmt.bpp;
700 bcap_dev->dlen = bcap_fmt.dlen;
63b1a90d
SJ
701 return 0;
702}
703
704static int bcap_querycap(struct file *file, void *priv,
705 struct v4l2_capability *cap)
706{
707 struct bcap_device *bcap_dev = video_drvdata(file);
708
a020c747
HV
709 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
710 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
63b1a90d
SJ
711 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
712 strlcpy(cap->bus_info, "Blackfin Platform", sizeof(cap->bus_info));
713 strlcpy(cap->card, bcap_dev->cfg->card_name, sizeof(cap->card));
714 return 0;
715}
716
717static int bcap_g_parm(struct file *file, void *fh,
718 struct v4l2_streamparm *a)
719{
720 struct bcap_device *bcap_dev = video_drvdata(file);
721
722 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
723 return -EINVAL;
724 return v4l2_subdev_call(bcap_dev->sd, video, g_parm, a);
725}
726
727static int bcap_s_parm(struct file *file, void *fh,
728 struct v4l2_streamparm *a)
729{
730 struct bcap_device *bcap_dev = video_drvdata(file);
731
732 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
733 return -EINVAL;
734 return v4l2_subdev_call(bcap_dev->sd, video, s_parm, a);
735}
736
63b1a90d
SJ
737static int bcap_log_status(struct file *file, void *priv)
738{
739 struct bcap_device *bcap_dev = video_drvdata(file);
740 /* status for sub devices */
741 v4l2_device_call_all(&bcap_dev->v4l2_dev, 0, core, log_status);
742 return 0;
743}
744
745static const struct v4l2_ioctl_ops bcap_ioctl_ops = {
746 .vidioc_querycap = bcap_querycap,
747 .vidioc_g_fmt_vid_cap = bcap_g_fmt_vid_cap,
748 .vidioc_enum_fmt_vid_cap = bcap_enum_fmt_vid_cap,
749 .vidioc_s_fmt_vid_cap = bcap_s_fmt_vid_cap,
750 .vidioc_try_fmt_vid_cap = bcap_try_fmt_vid_cap,
751 .vidioc_enum_input = bcap_enum_input,
752 .vidioc_g_input = bcap_g_input,
753 .vidioc_s_input = bcap_s_input,
754 .vidioc_querystd = bcap_querystd,
755 .vidioc_s_std = bcap_s_std,
756 .vidioc_g_std = bcap_g_std,
45b82596
SJ
757 .vidioc_s_dv_timings = bcap_s_dv_timings,
758 .vidioc_g_dv_timings = bcap_g_dv_timings,
ead156c4
SJ
759 .vidioc_query_dv_timings = bcap_query_dv_timings,
760 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
d61233bf 761 .vidioc_reqbufs = vb2_ioctl_reqbufs,
8df229bd 762 .vidioc_create_bufs = vb2_ioctl_create_bufs,
d61233bf
LP
763 .vidioc_querybuf = vb2_ioctl_querybuf,
764 .vidioc_qbuf = vb2_ioctl_qbuf,
765 .vidioc_dqbuf = vb2_ioctl_dqbuf,
766 .vidioc_streamon = vb2_ioctl_streamon,
767 .vidioc_streamoff = vb2_ioctl_streamoff,
63b1a90d
SJ
768 .vidioc_g_parm = bcap_g_parm,
769 .vidioc_s_parm = bcap_s_parm,
63b1a90d
SJ
770 .vidioc_log_status = bcap_log_status,
771};
772
773static struct v4l2_file_operations bcap_fops = {
774 .owner = THIS_MODULE,
f0e91c65
LP
775 .open = v4l2_fh_open,
776 .release = vb2_fop_release,
63b1a90d 777 .unlocked_ioctl = video_ioctl2,
8fd05b7e 778 .mmap = vb2_fop_mmap,
63b1a90d
SJ
779#ifndef CONFIG_MMU
780 .get_unmapped_area = bcap_get_unmapped_area,
781#endif
8fd05b7e 782 .poll = vb2_fop_poll
63b1a90d
SJ
783};
784
4c62e976 785static int bcap_probe(struct platform_device *pdev)
63b1a90d
SJ
786{
787 struct bcap_device *bcap_dev;
788 struct video_device *vfd;
789 struct i2c_adapter *i2c_adap;
790 struct bfin_capture_config *config;
791 struct vb2_queue *q;
45b82596 792 struct bcap_route *route;
63b1a90d
SJ
793 int ret;
794
795 config = pdev->dev.platform_data;
d9fdbeff 796 if (!config || !config->num_inputs) {
63b1a90d
SJ
797 v4l2_err(pdev->dev.driver, "Unable to get board config\n");
798 return -ENODEV;
799 }
800
801 bcap_dev = kzalloc(sizeof(*bcap_dev), GFP_KERNEL);
802 if (!bcap_dev) {
803 v4l2_err(pdev->dev.driver, "Unable to alloc bcap_dev\n");
804 return -ENOMEM;
805 }
806
807 bcap_dev->cfg = config;
808
f7d0e6d6 809 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
63b1a90d
SJ
810 if (!bcap_dev->ppi) {
811 v4l2_err(pdev->dev.driver, "Unable to create ppi\n");
812 ret = -ENODEV;
813 goto err_free_dev;
814 }
815 bcap_dev->ppi->priv = bcap_dev;
816
817 bcap_dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
818 if (IS_ERR(bcap_dev->alloc_ctx)) {
819 ret = PTR_ERR(bcap_dev->alloc_ctx);
820 goto err_free_ppi;
821 }
822
823 vfd = video_device_alloc();
824 if (!vfd) {
825 ret = -ENOMEM;
826 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
827 goto err_cleanup_ctx;
828 }
829
830 /* initialize field of video device */
831 vfd->release = video_device_release;
832 vfd->fops = &bcap_fops;
833 vfd->ioctl_ops = &bcap_ioctl_ops;
834 vfd->tvnorms = 0;
835 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
63b1a90d
SJ
836 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
837 bcap_dev->video_dev = vfd;
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_release_vdev;
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;
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;
ade48681 864 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
4d655ca4 865 q->lock = &bcap_dev->mutex;
5f65ca81 866 q->min_buffers_needed = 1;
63b1a90d 867
20420f92
HV
868 ret = vb2_queue_init(q);
869 if (ret)
870 goto err_free_handler;
63b1a90d
SJ
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;
8fd05b7e 879 vfd->queue = q;
63b1a90d
SJ
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");
150a1363 897 ret = -ENODEV;
63b1a90d
SJ
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;
45b82596 907
63b1a90d
SJ
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");
d9fdbeff 914 ret = -ENODEV;
63b1a90d
SJ
915 goto err_unreg_vdev;
916 }
917
918 v4l2_info(&bcap_dev->v4l2_dev, "v4l2 sub device registered\n");
919
45b82596
SJ
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
63b1a90d 936 /* now we can probe the default state */
45b82596 937 if (config->inputs[0].capabilities & V4L2_IN_CAP_STD) {
63b1a90d 938 v4l2_std_id std;
8774bed9 939 ret = v4l2_subdev_call(bcap_dev->sd, video, g_std, &std);
63b1a90d
SJ
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 }
a8451ed2 947 if (config->inputs[0].capabilities & V4L2_IN_CAP_DV_TIMINGS) {
45b82596
SJ
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 }
63b1a90d
SJ
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;
965err_unreg_vdev:
966 video_unregister_device(bcap_dev->video_dev);
967 bcap_dev->video_dev = NULL;
968err_free_handler:
969 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
970err_unreg_v4l2:
971 v4l2_device_unregister(&bcap_dev->v4l2_dev);
972err_release_vdev:
973 if (bcap_dev->video_dev)
974 video_device_release(bcap_dev->video_dev);
975err_cleanup_ctx:
976 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
977err_free_ppi:
978 ppi_delete_instance(bcap_dev->ppi);
979err_free_dev:
980 kfree(bcap_dev);
981 return ret;
982}
983
4c62e976 984static int bcap_remove(struct platform_device *pdev)
63b1a90d
SJ
985{
986 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
987 struct bcap_device *bcap_dev = container_of(v4l2_dev,
988 struct bcap_device, v4l2_dev);
989
990 bcap_free_sensor_formats(bcap_dev);
991 video_unregister_device(bcap_dev->video_dev);
992 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
993 v4l2_device_unregister(v4l2_dev);
994 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
995 ppi_delete_instance(bcap_dev->ppi);
996 kfree(bcap_dev);
997 return 0;
998}
999
1000static struct platform_driver bcap_driver = {
1001 .driver = {
1002 .name = CAPTURE_DRV_NAME,
63b1a90d
SJ
1003 },
1004 .probe = bcap_probe,
4c62e976 1005 .remove = bcap_remove,
63b1a90d 1006};
7f2b3a7d 1007module_platform_driver(bcap_driver);
63b1a90d
SJ
1008
1009MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1010MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1011MODULE_LICENSE("GPL v2");