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