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