]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/blackfin/bfin_capture.c
[media] media: videobuf2: Restructure vb2_buffer
[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 {
2d700715 57 struct vb2_v4l2_buffer vb;
63b1a90d
SJ
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 */
afb7a966 67 struct video_device video_dev;
63b1a90d
SJ
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;
9d490e52
LP
106 /* vb2 buffer sequence counter */
107 unsigned sequence;
63b1a90d
SJ
108};
109
63b1a90d
SJ
110static const struct bcap_format bcap_formats[] = {
111 {
112 .desc = "YCbCr 4:2:2 Interleaved UYVY",
113 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 114 .mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
63b1a90d 115 .bpp = 16,
45b82596 116 .dlen = 8,
63b1a90d
SJ
117 },
118 {
119 .desc = "YCbCr 4:2:2 Interleaved YUYV",
120 .pixelformat = V4L2_PIX_FMT_YUYV,
27ffaeb0 121 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
63b1a90d 122 .bpp = 16,
45b82596
SJ
123 .dlen = 8,
124 },
125 {
126 .desc = "YCbCr 4:2:2 Interleaved UYVY",
127 .pixelformat = V4L2_PIX_FMT_UYVY,
27ffaeb0 128 .mbus_code = MEDIA_BUS_FMT_UYVY8_1X16,
45b82596
SJ
129 .bpp = 16,
130 .dlen = 16,
63b1a90d
SJ
131 },
132 {
133 .desc = "RGB 565",
134 .pixelformat = V4L2_PIX_FMT_RGB565,
27ffaeb0 135 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
63b1a90d 136 .bpp = 16,
45b82596 137 .dlen = 8,
63b1a90d
SJ
138 },
139 {
140 .desc = "RGB 444",
141 .pixelformat = V4L2_PIX_FMT_RGB444,
27ffaeb0 142 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
63b1a90d 143 .bpp = 16,
45b82596 144 .dlen = 8,
63b1a90d
SJ
145 },
146
147};
148#define BCAP_MAX_FMTS ARRAY_SIZE(bcap_formats)
149
150static irqreturn_t bcap_isr(int irq, void *dev_id);
151
2d700715 152static struct bcap_buffer *to_bcap_vb(struct vb2_v4l2_buffer *vb)
63b1a90d
SJ
153{
154 return container_of(vb, struct bcap_buffer, vb);
155}
156
157static int bcap_init_sensor_formats(struct bcap_device *bcap_dev)
158{
ebcff5fc
HV
159 struct v4l2_subdev_mbus_code_enum code = {
160 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
161 };
63b1a90d
SJ
162 struct bcap_format *sf;
163 unsigned int num_formats = 0;
164 int i, j;
165
ebcff5fc
HV
166 while (!v4l2_subdev_call(bcap_dev->sd, pad,
167 enum_mbus_code, NULL, &code)) {
63b1a90d 168 num_formats++;
ebcff5fc
HV
169 code.index++;
170 }
63b1a90d
SJ
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++) {
ebcff5fc
HV
179 code.index = i;
180 v4l2_subdev_call(bcap_dev->sd, pad,
181 enum_mbus_code, NULL, &code);
63b1a90d 182 for (j = 0; j < BCAP_MAX_FMTS; j++)
ebcff5fc 183 if (code.code == bcap_formats[j].mbus_code)
63b1a90d
SJ
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
197static 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
63b1a90d
SJ
204static 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
f3140022
LP
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;
63b1a90d
SJ
216
217 *nplanes = 1;
f3140022 218 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : bcap_dev->fmt.sizeimage;
63b1a90d
SJ
219 alloc_ctxs[0] = bcap_dev->alloc_ctx;
220
221 return 0;
222}
223
63b1a90d
SJ
224static int bcap_buffer_prepare(struct vb2_buffer *vb)
225{
2d700715 226 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
63b1a90d 227 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
cd3c38ef 228 unsigned long size = bcap_dev->fmt.sizeimage;
63b1a90d 229
63b1a90d
SJ
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 }
cd3c38ef
LP
235 vb2_set_plane_payload(vb, 0, size);
236
2d700715 237 vbuf->field = bcap_dev->fmt.field;
63b1a90d
SJ
238
239 return 0;
240}
241
242static void bcap_buffer_queue(struct vb2_buffer *vb)
243{
2d700715 244 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
63b1a90d 245 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
2d700715 246 struct bcap_buffer *buf = to_bcap_vb(vbuf);
63b1a90d
SJ
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
254static void bcap_buffer_cleanup(struct vb2_buffer *vb)
255{
2d700715 256 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
63b1a90d 257 struct bcap_device *bcap_dev = vb2_get_drv_priv(vb->vb2_queue);
2d700715 258 struct bcap_buffer *buf = to_bcap_vb(vbuf);
63b1a90d
SJ
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
63b1a90d
SJ
266static 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;
6692871a 270 struct bcap_buffer *buf, *tmp;
63b1a90d 271 struct ppi_params params;
d61233bf 272 dma_addr_t addr;
63b1a90d
SJ
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");
6692871a 279 goto err;
63b1a90d
SJ
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;
45b82596 286 params.dlen = bcap_dev->dlen;
63b1a90d
SJ
287 params.ppi_control = bcap_dev->cfg->ppi_control;
288 params.int_mask = bcap_dev->cfg->int_mask;
45b82596 289 if (bcap_dev->cfg->inputs[bcap_dev->cur_input].capabilities
a8451ed2 290 & V4L2_IN_CAP_DV_TIMINGS) {
45b82596
SJ
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;
eacf8f9a
HV
295 params.line = V4L2_DV_BT_FRAME_WIDTH(bt);
296 params.frame = V4L2_DV_BT_FRAME_HEIGHT(bt);
45b82596
SJ
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 }
63b1a90d
SJ
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");
6692871a 318 goto err;
63b1a90d
SJ
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");
6692871a 326 goto err;
63b1a90d
SJ
327 }
328
9d490e52
LP
329 bcap_dev->sequence = 0;
330
16735d02 331 reinit_completion(&bcap_dev->comp);
63b1a90d 332 bcap_dev->stop = false;
6692871a 333
d61233bf
LP
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);
2d700715
JS
339 addr = vb2_dma_contig_plane_dma_addr(&bcap_dev->cur_frm->vb.vb2_buf,
340 0);
d61233bf
LP
341 /* update DMA address */
342 ppi->ops->update_addr(ppi, (unsigned long)addr);
343 /* enable ppi */
344 ppi->ops->start(ppi);
345
63b1a90d 346 return 0;
6692871a
LP
347
348err:
349 list_for_each_entry_safe(buf, tmp, &bcap_dev->dma_queue, list) {
350 list_del(&buf->list);
2d700715 351 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
6692871a
LP
352 }
353
354 return ret;
63b1a90d
SJ
355}
356
e37559b2 357static void bcap_stop_streaming(struct vb2_queue *vq)
63b1a90d
SJ
358{
359 struct bcap_device *bcap_dev = vb2_get_drv_priv(vq);
360 struct ppi_if *ppi = bcap_dev->ppi;
361 int ret;
362
63b1a90d
SJ
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 */
2bf34b2f 373 if (bcap_dev->cur_frm)
2d700715
JS
374 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
375 VB2_BUF_STATE_ERROR);
2bf34b2f 376
63b1a90d 377 while (!list_empty(&bcap_dev->dma_queue)) {
d78a4882 378 bcap_dev->cur_frm = list_entry(bcap_dev->dma_queue.next,
63b1a90d 379 struct bcap_buffer, list);
7ba3c21c 380 list_del_init(&bcap_dev->cur_frm->list);
2d700715
JS
381 vb2_buffer_done(&bcap_dev->cur_frm->vb.vb2_buf,
382 VB2_BUF_STATE_ERROR);
63b1a90d 383 }
63b1a90d
SJ
384}
385
386static struct vb2_ops bcap_video_qops = {
387 .queue_setup = bcap_queue_setup,
63b1a90d
SJ
388 .buf_prepare = bcap_buffer_prepare,
389 .buf_cleanup = bcap_buffer_cleanup,
390 .buf_queue = bcap_buffer_queue,
4d655ca4
PL
391 .wait_prepare = vb2_ops_wait_prepare,
392 .wait_finish = vb2_ops_wait_finish,
63b1a90d
SJ
393 .start_streaming = bcap_start_streaming,
394 .stop_streaming = bcap_stop_streaming,
395};
396
63b1a90d
SJ
397static 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;
2d700715
JS
401 struct vb2_v4l2_buffer *vbuf = &bcap_dev->cur_frm->vb;
402 struct vb2_buffer *vb = &vbuf->vb2_buf;
63b1a90d
SJ
403 dma_addr_t addr;
404
405 spin_lock(&bcap_dev->lock);
406
d78a4882 407 if (!list_empty(&bcap_dev->dma_queue)) {
2d700715 408 v4l2_get_timestamp(&vbuf->timestamp);
d78a4882
SJ
409 if (ppi->err) {
410 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
411 ppi->err = false;
412 } else {
2d700715 413 vbuf->sequence = bcap_dev->sequence++;
d78a4882
SJ
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 {
2d700715
JS
430 addr = vb2_dma_contig_plane_dma_addr(
431 &bcap_dev->cur_frm->vb.vb2_buf, 0);
d78a4882 432 ppi->ops->update_addr(ppi, (unsigned long)addr);
63b1a90d
SJ
433 ppi->ops->start(ppi);
434 }
435
436 spin_unlock(&bcap_dev->lock);
437
438 return IRQ_HANDLED;
439}
440
63b1a90d
SJ
441static int bcap_querystd(struct file *file, void *priv, v4l2_std_id *std)
442{
443 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716
LP
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;
63b1a90d
SJ
449
450 return v4l2_subdev_call(bcap_dev->sd, video, querystd, std);
451}
452
453static int bcap_g_std(struct file *file, void *priv, v4l2_std_id *std)
454{
455 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716
LP
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;
63b1a90d
SJ
461
462 *std = bcap_dev->std;
463 return 0;
464}
465
314527ac 466static int bcap_s_std(struct file *file, void *priv, v4l2_std_id std)
63b1a90d
SJ
467{
468 struct bcap_device *bcap_dev = video_drvdata(file);
fe00b716 469 struct v4l2_input input;
63b1a90d
SJ
470 int ret;
471
fe00b716
LP
472 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
473 if (!(input.capabilities & V4L2_IN_CAP_STD))
474 return -ENODATA;
475
63b1a90d
SJ
476 if (vb2_is_busy(&bcap_dev->buffer_queue))
477 return -EBUSY;
478
8774bed9 479 ret = v4l2_subdev_call(bcap_dev->sd, video, s_std, std);
63b1a90d
SJ
480 if (ret < 0)
481 return ret;
482
314527ac 483 bcap_dev->std = std;
63b1a90d
SJ
484 return 0;
485}
486
ead156c4
SJ
487static 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);
116a6488
LP
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;
ead156c4 496
f1f8e434
LP
497 timings->pad = 0;
498
499 return v4l2_subdev_call(bcap_dev->sd, pad,
ead156c4
SJ
500 enum_dv_timings, timings);
501}
502
503static int bcap_query_dv_timings(struct file *file, void *priv,
45b82596
SJ
504 struct v4l2_dv_timings *timings)
505{
506 struct bcap_device *bcap_dev = video_drvdata(file);
116a6488
LP
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;
45b82596 512
ead156c4
SJ
513 return v4l2_subdev_call(bcap_dev->sd, video,
514 query_dv_timings, timings);
515}
45b82596 516
ead156c4
SJ
517static 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);
116a6488
LP
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;
ead156c4
SJ
526
527 *timings = bcap_dev->dv_timings;
45b82596
SJ
528 return 0;
529}
530
531static 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);
116a6488 535 struct v4l2_input input;
45b82596 536 int ret;
116a6488
LP
537
538 input = bcap_dev->cfg->inputs[bcap_dev->cur_input];
539 if (!(input.capabilities & V4L2_IN_CAP_DV_TIMINGS))
540 return -ENODATA;
541
45b82596
SJ
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
63b1a90d
SJ
553static 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
572static 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
580static 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;
45b82596
SJ
601 /* if this route has specific config, update ppi control */
602 if (route->ppi_control)
603 config->ppi_control = route->ppi_control;
63b1a90d
SJ
604 return 0;
605}
606
607static int bcap_try_format(struct bcap_device *bcap,
608 struct v4l2_pix_format *pixfmt,
45b82596 609 struct bcap_format *bcap_fmt)
63b1a90d
SJ
610{
611 struct bcap_format *sf = bcap->sensor_formats;
612 struct bcap_format *fmt = NULL;
5eab4983
HV
613 struct v4l2_subdev_pad_config pad_cfg;
614 struct v4l2_subdev_format format = {
615 .which = V4L2_SUBDEV_FORMAT_TRY,
616 };
63b1a90d
SJ
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
5eab4983
HV
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);
63b1a90d
SJ
630 if (ret < 0)
631 return ret;
5eab4983 632 v4l2_fill_pix_format(pixfmt, &format.format);
45b82596
SJ
633 if (bcap_fmt) {
634 for (i = 0; i < bcap->num_sensor_formats; i++) {
635 fmt = &sf[i];
5eab4983 636 if (format.format.code == fmt->mbus_code)
45b82596
SJ
637 break;
638 }
639 *bcap_fmt = *fmt;
640 }
63b1a90d
SJ
641 pixfmt->bytesperline = pixfmt->width * fmt->bpp / 8;
642 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
643 return 0;
644}
645
646static 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
663static 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
45b82596 669 return bcap_try_format(bcap_dev, pixfmt, NULL);
63b1a90d
SJ
670}
671
672static 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
681static 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);
ebf984bb
HV
685 struct v4l2_subdev_format format = {
686 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
687 };
45b82596 688 struct bcap_format bcap_fmt;
63b1a90d 689 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
45b82596 690 int ret;
63b1a90d
SJ
691
692 if (vb2_is_busy(&bcap_dev->buffer_queue))
693 return -EBUSY;
694
695 /* see if format works */
45b82596 696 ret = bcap_try_format(bcap_dev, pixfmt, &bcap_fmt);
63b1a90d
SJ
697 if (ret < 0)
698 return ret;
699
ebf984bb
HV
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);
63b1a90d
SJ
702 if (ret < 0)
703 return ret;
704 bcap_dev->fmt = *pixfmt;
45b82596
SJ
705 bcap_dev->bpp = bcap_fmt.bpp;
706 bcap_dev->dlen = bcap_fmt.dlen;
63b1a90d
SJ
707 return 0;
708}
709
710static int bcap_querycap(struct file *file, void *priv,
711 struct v4l2_capability *cap)
712{
713 struct bcap_device *bcap_dev = video_drvdata(file);
714
a020c747
HV
715 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
716 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
63b1a90d
SJ
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
723static 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
733static 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
63b1a90d
SJ
743static 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
751static 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,
45b82596
SJ
763 .vidioc_s_dv_timings = bcap_s_dv_timings,
764 .vidioc_g_dv_timings = bcap_g_dv_timings,
ead156c4
SJ
765 .vidioc_query_dv_timings = bcap_query_dv_timings,
766 .vidioc_enum_dv_timings = bcap_enum_dv_timings,
d61233bf 767 .vidioc_reqbufs = vb2_ioctl_reqbufs,
8df229bd 768 .vidioc_create_bufs = vb2_ioctl_create_bufs,
d61233bf
LP
769 .vidioc_querybuf = vb2_ioctl_querybuf,
770 .vidioc_qbuf = vb2_ioctl_qbuf,
771 .vidioc_dqbuf = vb2_ioctl_dqbuf,
812447e5 772 .vidioc_expbuf = vb2_ioctl_expbuf,
d61233bf
LP
773 .vidioc_streamon = vb2_ioctl_streamon,
774 .vidioc_streamoff = vb2_ioctl_streamoff,
63b1a90d
SJ
775 .vidioc_g_parm = bcap_g_parm,
776 .vidioc_s_parm = bcap_s_parm,
63b1a90d
SJ
777 .vidioc_log_status = bcap_log_status,
778};
779
780static struct v4l2_file_operations bcap_fops = {
781 .owner = THIS_MODULE,
f0e91c65
LP
782 .open = v4l2_fh_open,
783 .release = vb2_fop_release,
63b1a90d 784 .unlocked_ioctl = video_ioctl2,
8fd05b7e 785 .mmap = vb2_fop_mmap,
63b1a90d 786#ifndef CONFIG_MMU
225ccaa3 787 .get_unmapped_area = vb2_fop_get_unmapped_area,
63b1a90d 788#endif
8fd05b7e 789 .poll = vb2_fop_poll
63b1a90d
SJ
790};
791
4c62e976 792static int bcap_probe(struct platform_device *pdev)
63b1a90d
SJ
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;
45b82596 799 struct bcap_route *route;
63b1a90d
SJ
800 int ret;
801
802 config = pdev->dev.platform_data;
d9fdbeff 803 if (!config || !config->num_inputs) {
63b1a90d
SJ
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
f7d0e6d6 816 bcap_dev->ppi = ppi_create_instance(pdev, config->ppi_info);
63b1a90d
SJ
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
afb7a966 830 vfd = &bcap_dev->video_dev;
63b1a90d 831 /* initialize field of video device */
afb7a966 832 vfd->release = video_device_release_empty;
63b1a90d
SJ
833 vfd->fops = &bcap_fops;
834 vfd->ioctl_ops = &bcap_ioctl_ops;
835 vfd->tvnorms = 0;
836 vfd->v4l2_dev = &bcap_dev->v4l2_dev;
63b1a90d 837 strncpy(vfd->name, CAPTURE_DRV_NAME, sizeof(vfd->name));
63b1a90d
SJ
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");
afb7a966 843 goto err_cleanup_ctx;
63b1a90d
SJ
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;
70753b5e 859 q->io_modes = VB2_MMAP | VB2_DMABUF;
63b1a90d
SJ
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 */
afb7a966 882 ret = video_register_device(&bcap_dev->video_dev, VFL_TYPE_GRABBER, -1);
63b1a90d
SJ
883 if (ret) {
884 v4l2_err(&bcap_dev->v4l2_dev,
885 "Unable to register video device\n");
886 goto err_free_handler;
887 }
afb7a966 888 video_set_drvdata(&bcap_dev->video_dev, bcap_dev);
63b1a90d
SJ
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:
afb7a966 966 video_unregister_device(&bcap_dev->video_dev);
63b1a90d
SJ
967err_free_handler:
968 v4l2_ctrl_handler_free(&bcap_dev->ctrl_handler);
969err_unreg_v4l2:
970 v4l2_device_unregister(&bcap_dev->v4l2_dev);
63b1a90d
SJ
971err_cleanup_ctx:
972 vb2_dma_contig_cleanup_ctx(bcap_dev->alloc_ctx);
973err_free_ppi:
974 ppi_delete_instance(bcap_dev->ppi);
975err_free_dev:
976 kfree(bcap_dev);
977 return ret;
978}
979
4c62e976 980static int bcap_remove(struct platform_device *pdev)
63b1a90d
SJ
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);
afb7a966 987 video_unregister_device(&bcap_dev->video_dev);
63b1a90d
SJ
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
996static struct platform_driver bcap_driver = {
997 .driver = {
998 .name = CAPTURE_DRV_NAME,
63b1a90d
SJ
999 },
1000 .probe = bcap_probe,
4c62e976 1001 .remove = bcap_remove,
63b1a90d 1002};
7f2b3a7d 1003module_platform_driver(bcap_driver);
63b1a90d
SJ
1004
1005MODULE_DESCRIPTION("Analog Devices blackfin video capture driver");
1006MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
1007MODULE_LICENSE("GPL v2");