• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2008-2009 Texas Instruments Inc
4  *
5  * Driver name : VPFE Capture driver
6  *    VPFE Capture driver allows applications to capture and stream video
7  *    frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
8  *    TVP5146 or  Raw Bayer RGB image data from an image sensor
9  *    such as Microns' MT9T001, MT9T031 etc.
10  *
11  *    These SoCs have, in common, a Video Processing Subsystem (VPSS) that
12  *    consists of a Video Processing Front End (VPFE) for capturing
13  *    video/raw image data and Video Processing Back End (VPBE) for displaying
14  *    YUV data through an in-built analog encoder or Digital LCD port. This
15  *    driver is for capture through VPFE. A typical EVM using these SoCs have
16  *    following high level configuration.
17  *
18  *    decoder(TVP5146/		YUV/
19  *	     MT9T001)   -->  Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
20  *				data input              |      |
21  *							V      |
22  *						      SDRAM    |
23  *							       V
24  *							   Image Processor
25  *							       |
26  *							       V
27  *							     SDRAM
28  *    The data flow happens from a decoder connected to the VPFE over a
29  *    YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
30  *    and to the input of VPFE through an optional MUX (if more inputs are
31  *    to be interfaced on the EVM). The input data is first passed through
32  *    CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
33  *    does very little or no processing on YUV data and does pre-process Raw
34  *    Bayer RGB data through modules such as Defect Pixel Correction (DFC)
35  *    Color Space Conversion (CSC), data gain/offset etc. After this, data
36  *    can be written to SDRAM or can be connected to the image processing
37  *    block such as IPIPE (on DM355 only).
38  *
39  *    Features supported
40  *		- MMAP IO
41  *		- Capture using TVP5146 over BT.656
42  *		- support for interfacing decoders using sub device model
43  *		- Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
44  *		  data capture to SDRAM.
45  *    TODO list
46  *		- Support multiple REQBUF after open
47  *		- Support for de-allocating buffers through REQBUF
48  *		- Support for Raw Bayer RGB capture
49  *		- Support for chaining Image Processor
50  *		- Support for static allocation of buffers
51  *		- Support for USERPTR IO
52  *		- Support for STREAMON before QBUF
53  *		- Support for control ioctls
54  */
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/init.h>
58 #include <linux/platform_device.h>
59 #include <linux/interrupt.h>
60 #include <media/v4l2-common.h>
61 #include <linux/io.h>
62 #include <media/davinci/vpfe_capture.h>
63 #include "ccdc_hw_device.h"
64 
65 static int debug;
66 static u32 numbuffers = 3;
67 static u32 bufsize = (720 * 576 * 2);
68 
69 module_param(numbuffers, uint, S_IRUGO);
70 module_param(bufsize, uint, S_IRUGO);
71 module_param(debug, int, 0644);
72 
73 MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
74 MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
75 MODULE_PARM_DESC(debug, "Debug level 0-1");
76 
77 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
78 MODULE_LICENSE("GPL");
79 MODULE_AUTHOR("Texas Instruments");
80 
81 /* standard information */
82 struct vpfe_standard {
83 	v4l2_std_id std_id;
84 	unsigned int width;
85 	unsigned int height;
86 	struct v4l2_fract pixelaspect;
87 	/* 0 - progressive, 1 - interlaced */
88 	int frame_format;
89 };
90 
91 /* ccdc configuration */
92 struct ccdc_config {
93 	/* This make sure vpfe is probed and ready to go */
94 	int vpfe_probed;
95 	/* name of ccdc device */
96 	char name[32];
97 };
98 
99 /* data structures */
100 static struct vpfe_config_params config_params = {
101 	.min_numbuffers = 3,
102 	.numbuffers = 3,
103 	.min_bufsize = 720 * 480 * 2,
104 	.device_bufsize = 720 * 576 * 2,
105 };
106 
107 /* ccdc device registered */
108 static const struct ccdc_hw_device *ccdc_dev;
109 /* lock for accessing ccdc information */
110 static DEFINE_MUTEX(ccdc_lock);
111 /* ccdc configuration */
112 static struct ccdc_config *ccdc_cfg;
113 
114 static const struct vpfe_standard vpfe_standards[] = {
115 	{V4L2_STD_525_60, 720, 480, {11, 10}, 1},
116 	{V4L2_STD_625_50, 720, 576, {54, 59}, 1},
117 };
118 
119 /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
120 static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
121 	{
122 		.pixelformat = V4L2_PIX_FMT_SBGGR8,
123 		.bpp = 1,
124 	},
125 	{
126 		.pixelformat = V4L2_PIX_FMT_SBGGR16,
127 		.bpp = 2,
128 	},
129 	{
130 		.pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
131 		.bpp = 1,
132 	},
133 	{
134 		.pixelformat = V4L2_PIX_FMT_UYVY,
135 		.bpp = 2,
136 	},
137 	{
138 		.pixelformat = V4L2_PIX_FMT_YUYV,
139 		.bpp = 2,
140 	},
141 	{
142 		.pixelformat = V4L2_PIX_FMT_NV12,
143 		.bpp = 1,
144 	},
145 };
146 
147 /*
148  * vpfe_lookup_pix_format()
149  * lookup an entry in the vpfe pix format table based on pix_format
150  */
vpfe_lookup_pix_format(u32 pix_format)151 static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
152 {
153 	int i;
154 
155 	for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
156 		if (pix_format == vpfe_pix_fmts[i].pixelformat)
157 			return &vpfe_pix_fmts[i];
158 	}
159 	return NULL;
160 }
161 
162 /*
163  * vpfe_register_ccdc_device. CCDC module calls this to
164  * register with vpfe capture
165  */
vpfe_register_ccdc_device(const struct ccdc_hw_device * dev)166 int vpfe_register_ccdc_device(const struct ccdc_hw_device *dev)
167 {
168 	int ret = 0;
169 	printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
170 
171 	BUG_ON(!dev->hw_ops.open);
172 	BUG_ON(!dev->hw_ops.enable);
173 	BUG_ON(!dev->hw_ops.set_hw_if_params);
174 	BUG_ON(!dev->hw_ops.configure);
175 	BUG_ON(!dev->hw_ops.set_buftype);
176 	BUG_ON(!dev->hw_ops.get_buftype);
177 	BUG_ON(!dev->hw_ops.enum_pix);
178 	BUG_ON(!dev->hw_ops.set_frame_format);
179 	BUG_ON(!dev->hw_ops.get_frame_format);
180 	BUG_ON(!dev->hw_ops.get_pixel_format);
181 	BUG_ON(!dev->hw_ops.set_pixel_format);
182 	BUG_ON(!dev->hw_ops.set_image_window);
183 	BUG_ON(!dev->hw_ops.get_image_window);
184 	BUG_ON(!dev->hw_ops.get_line_length);
185 	BUG_ON(!dev->hw_ops.getfid);
186 
187 	mutex_lock(&ccdc_lock);
188 	if (!ccdc_cfg) {
189 		/*
190 		 * TODO. Will this ever happen? if so, we need to fix it.
191 		 * Proabably we need to add the request to a linked list and
192 		 * walk through it during vpfe probe
193 		 */
194 		printk(KERN_ERR "vpfe capture not initialized\n");
195 		ret = -EFAULT;
196 		goto unlock;
197 	}
198 
199 	if (strcmp(dev->name, ccdc_cfg->name)) {
200 		/* ignore this ccdc */
201 		ret = -EINVAL;
202 		goto unlock;
203 	}
204 
205 	if (ccdc_dev) {
206 		printk(KERN_ERR "ccdc already registered\n");
207 		ret = -EINVAL;
208 		goto unlock;
209 	}
210 
211 	ccdc_dev = dev;
212 unlock:
213 	mutex_unlock(&ccdc_lock);
214 	return ret;
215 }
216 EXPORT_SYMBOL(vpfe_register_ccdc_device);
217 
218 /*
219  * vpfe_unregister_ccdc_device. CCDC module calls this to
220  * unregister with vpfe capture
221  */
vpfe_unregister_ccdc_device(const struct ccdc_hw_device * dev)222 void vpfe_unregister_ccdc_device(const struct ccdc_hw_device *dev)
223 {
224 	if (!dev) {
225 		printk(KERN_ERR "invalid ccdc device ptr\n");
226 		return;
227 	}
228 
229 	printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
230 		dev->name);
231 
232 	if (strcmp(dev->name, ccdc_cfg->name)) {
233 		/* ignore this ccdc */
234 		return;
235 	}
236 
237 	mutex_lock(&ccdc_lock);
238 	ccdc_dev = NULL;
239 	mutex_unlock(&ccdc_lock);
240 }
241 EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
242 
243 /*
244  * vpfe_config_ccdc_image_format()
245  * For a pix format, configure ccdc to setup the capture
246  */
vpfe_config_ccdc_image_format(struct vpfe_device * vpfe_dev)247 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
248 {
249 	enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
250 	int ret = 0;
251 
252 	if (ccdc_dev->hw_ops.set_pixel_format(
253 			vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
254 		v4l2_err(&vpfe_dev->v4l2_dev,
255 			"couldn't set pix format in ccdc\n");
256 		return -EINVAL;
257 	}
258 	/* configure the image window */
259 	ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
260 
261 	switch (vpfe_dev->fmt.fmt.pix.field) {
262 	case V4L2_FIELD_INTERLACED:
263 		/* do nothing, since it is default */
264 		ret = ccdc_dev->hw_ops.set_buftype(
265 				CCDC_BUFTYPE_FLD_INTERLEAVED);
266 		break;
267 	case V4L2_FIELD_NONE:
268 		frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
269 		/* buffer type only applicable for interlaced scan */
270 		break;
271 	case V4L2_FIELD_SEQ_TB:
272 		ret = ccdc_dev->hw_ops.set_buftype(
273 				CCDC_BUFTYPE_FLD_SEPARATED);
274 		break;
275 	default:
276 		return -EINVAL;
277 	}
278 
279 	/* set the frame format */
280 	if (!ret)
281 		ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
282 	return ret;
283 }
284 /*
285  * vpfe_config_image_format()
286  * For a given standard, this functions sets up the default
287  * pix format & crop values in the vpfe device and ccdc.  It first
288  * starts with defaults based values from the standard table.
289  * It then checks if sub device supports get_fmt and then override the
290  * values based on that.Sets crop values to match with scan resolution
291  * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
292  * values in ccdc
293  */
vpfe_config_image_format(struct vpfe_device * vpfe_dev,v4l2_std_id std_id)294 static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
295 				    v4l2_std_id std_id)
296 {
297 	struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
298 	struct v4l2_subdev_format fmt = {
299 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
300 	};
301 	struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
302 	struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
303 	int i, ret;
304 
305 	for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
306 		if (vpfe_standards[i].std_id & std_id) {
307 			vpfe_dev->std_info.active_pixels =
308 					vpfe_standards[i].width;
309 			vpfe_dev->std_info.active_lines =
310 					vpfe_standards[i].height;
311 			vpfe_dev->std_info.frame_format =
312 					vpfe_standards[i].frame_format;
313 			vpfe_dev->std_index = i;
314 			break;
315 		}
316 	}
317 
318 	if (i ==  ARRAY_SIZE(vpfe_standards)) {
319 		v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
320 		return -EINVAL;
321 	}
322 
323 	vpfe_dev->crop.top = 0;
324 	vpfe_dev->crop.left = 0;
325 	vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
326 	vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
327 	pix->width = vpfe_dev->crop.width;
328 	pix->height = vpfe_dev->crop.height;
329 
330 	/* first field and frame format based on standard frame format */
331 	if (vpfe_dev->std_info.frame_format) {
332 		pix->field = V4L2_FIELD_INTERLACED;
333 		/* assume V4L2_PIX_FMT_UYVY as default */
334 		pix->pixelformat = V4L2_PIX_FMT_UYVY;
335 		v4l2_fill_mbus_format(mbus_fmt, pix,
336 				MEDIA_BUS_FMT_YUYV10_2X10);
337 	} else {
338 		pix->field = V4L2_FIELD_NONE;
339 		/* assume V4L2_PIX_FMT_SBGGR8 */
340 		pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
341 		v4l2_fill_mbus_format(mbus_fmt, pix,
342 				MEDIA_BUS_FMT_SBGGR8_1X8);
343 	}
344 
345 	/* if sub device supports get_fmt, override the defaults */
346 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
347 			sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
348 
349 	if (ret && ret != -ENOIOCTLCMD) {
350 		v4l2_err(&vpfe_dev->v4l2_dev,
351 			"error in getting get_fmt from sub device\n");
352 		return ret;
353 	}
354 	v4l2_fill_pix_format(pix, mbus_fmt);
355 	pix->bytesperline = pix->width * 2;
356 	pix->sizeimage = pix->bytesperline * pix->height;
357 
358 	/* Sets the values in CCDC */
359 	ret = vpfe_config_ccdc_image_format(vpfe_dev);
360 	if (ret)
361 		return ret;
362 
363 	/* Update the values of sizeimage and bytesperline */
364 	pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
365 	pix->sizeimage = pix->bytesperline * pix->height;
366 
367 	return 0;
368 }
369 
vpfe_initialize_device(struct vpfe_device * vpfe_dev)370 static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
371 {
372 	int ret;
373 
374 	/* set first input of current subdevice as the current input */
375 	vpfe_dev->current_input = 0;
376 
377 	/* set default standard */
378 	vpfe_dev->std_index = 0;
379 
380 	/* Configure the default format information */
381 	ret = vpfe_config_image_format(vpfe_dev,
382 				vpfe_standards[vpfe_dev->std_index].std_id);
383 	if (ret)
384 		return ret;
385 
386 	/* now open the ccdc device to initialize it */
387 	mutex_lock(&ccdc_lock);
388 	if (!ccdc_dev) {
389 		v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
390 		ret = -ENODEV;
391 		goto unlock;
392 	}
393 
394 	if (!try_module_get(ccdc_dev->owner)) {
395 		v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
396 		ret = -ENODEV;
397 		goto unlock;
398 	}
399 	ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
400 	if (!ret)
401 		vpfe_dev->initialized = 1;
402 
403 	/* Clear all VPFE/CCDC interrupts */
404 	if (vpfe_dev->cfg->clr_intr)
405 		vpfe_dev->cfg->clr_intr(-1);
406 
407 unlock:
408 	mutex_unlock(&ccdc_lock);
409 	return ret;
410 }
411 
412 /*
413  * vpfe_open : It creates object of file handle structure and
414  * stores it in private_data  member of filepointer
415  */
vpfe_open(struct file * file)416 static int vpfe_open(struct file *file)
417 {
418 	struct vpfe_device *vpfe_dev = video_drvdata(file);
419 	struct video_device *vdev = video_devdata(file);
420 	struct vpfe_fh *fh;
421 
422 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
423 
424 	if (!vpfe_dev->cfg->num_subdevs) {
425 		v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
426 		return -ENODEV;
427 	}
428 
429 	/* Allocate memory for the file handle object */
430 	fh = kmalloc(sizeof(*fh), GFP_KERNEL);
431 	if (!fh)
432 		return -ENOMEM;
433 
434 	/* store pointer to fh in private_data member of file */
435 	file->private_data = fh;
436 	fh->vpfe_dev = vpfe_dev;
437 	v4l2_fh_init(&fh->fh, vdev);
438 	mutex_lock(&vpfe_dev->lock);
439 	/* If decoder is not initialized. initialize it */
440 	if (!vpfe_dev->initialized) {
441 		if (vpfe_initialize_device(vpfe_dev)) {
442 			mutex_unlock(&vpfe_dev->lock);
443 			v4l2_fh_exit(&fh->fh);
444 			kfree(fh);
445 			return -ENODEV;
446 		}
447 	}
448 	/* Increment device usrs counter */
449 	vpfe_dev->usrs++;
450 	/* Set io_allowed member to false */
451 	fh->io_allowed = 0;
452 	v4l2_fh_add(&fh->fh);
453 	mutex_unlock(&vpfe_dev->lock);
454 	return 0;
455 }
456 
vpfe_schedule_next_buffer(struct vpfe_device * vpfe_dev)457 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
458 {
459 	unsigned long addr;
460 
461 	vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
462 					struct videobuf_buffer, queue);
463 	list_del(&vpfe_dev->next_frm->queue);
464 	vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
465 	addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
466 
467 	ccdc_dev->hw_ops.setfbaddr(addr);
468 }
469 
vpfe_schedule_bottom_field(struct vpfe_device * vpfe_dev)470 static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
471 {
472 	unsigned long addr;
473 
474 	addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
475 	addr += vpfe_dev->field_off;
476 	ccdc_dev->hw_ops.setfbaddr(addr);
477 }
478 
vpfe_process_buffer_complete(struct vpfe_device * vpfe_dev)479 static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
480 {
481 	vpfe_dev->cur_frm->ts = ktime_get_ns();
482 	vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
483 	vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
484 	wake_up_interruptible(&vpfe_dev->cur_frm->done);
485 	vpfe_dev->cur_frm = vpfe_dev->next_frm;
486 }
487 
488 /* ISR for VINT0*/
vpfe_isr(int irq,void * dev_id)489 static irqreturn_t vpfe_isr(int irq, void *dev_id)
490 {
491 	struct vpfe_device *vpfe_dev = dev_id;
492 	enum v4l2_field field;
493 	int fid;
494 
495 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
496 	field = vpfe_dev->fmt.fmt.pix.field;
497 
498 	/* if streaming not started, don't do anything */
499 	if (!vpfe_dev->started)
500 		goto clear_intr;
501 
502 	/* only for 6446 this will be applicable */
503 	if (ccdc_dev->hw_ops.reset)
504 		ccdc_dev->hw_ops.reset();
505 
506 	if (field == V4L2_FIELD_NONE) {
507 		/* handle progressive frame capture */
508 		v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
509 			"frame format is progressive...\n");
510 		if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
511 			vpfe_process_buffer_complete(vpfe_dev);
512 		goto clear_intr;
513 	}
514 
515 	/* interlaced or TB capture check which field we are in hardware */
516 	fid = ccdc_dev->hw_ops.getfid();
517 
518 	/* switch the software maintained field id */
519 	vpfe_dev->field_id ^= 1;
520 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
521 		fid, vpfe_dev->field_id);
522 	if (fid == vpfe_dev->field_id) {
523 		/* we are in-sync here,continue */
524 		if (fid == 0) {
525 			/*
526 			 * One frame is just being captured. If the next frame
527 			 * is available, release the current frame and move on
528 			 */
529 			if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
530 				vpfe_process_buffer_complete(vpfe_dev);
531 			/*
532 			 * based on whether the two fields are stored
533 			 * interleavely or separately in memory, reconfigure
534 			 * the CCDC memory address
535 			 */
536 			if (field == V4L2_FIELD_SEQ_TB)
537 				vpfe_schedule_bottom_field(vpfe_dev);
538 			goto clear_intr;
539 		}
540 		/*
541 		 * if one field is just being captured configure
542 		 * the next frame get the next frame from the empty
543 		 * queue if no frame is available hold on to the
544 		 * current buffer
545 		 */
546 		spin_lock(&vpfe_dev->dma_queue_lock);
547 		if (!list_empty(&vpfe_dev->dma_queue) &&
548 		    vpfe_dev->cur_frm == vpfe_dev->next_frm)
549 			vpfe_schedule_next_buffer(vpfe_dev);
550 		spin_unlock(&vpfe_dev->dma_queue_lock);
551 	} else if (fid == 0) {
552 		/*
553 		 * out of sync. Recover from any hardware out-of-sync.
554 		 * May loose one frame
555 		 */
556 		vpfe_dev->field_id = fid;
557 	}
558 clear_intr:
559 	if (vpfe_dev->cfg->clr_intr)
560 		vpfe_dev->cfg->clr_intr(irq);
561 
562 	return IRQ_HANDLED;
563 }
564 
565 /* vdint1_isr - isr handler for VINT1 interrupt */
vdint1_isr(int irq,void * dev_id)566 static irqreturn_t vdint1_isr(int irq, void *dev_id)
567 {
568 	struct vpfe_device *vpfe_dev = dev_id;
569 
570 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
571 
572 	/* if streaming not started, don't do anything */
573 	if (!vpfe_dev->started) {
574 		if (vpfe_dev->cfg->clr_intr)
575 			vpfe_dev->cfg->clr_intr(irq);
576 		return IRQ_HANDLED;
577 	}
578 
579 	spin_lock(&vpfe_dev->dma_queue_lock);
580 	if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
581 	    !list_empty(&vpfe_dev->dma_queue) &&
582 	    vpfe_dev->cur_frm == vpfe_dev->next_frm)
583 		vpfe_schedule_next_buffer(vpfe_dev);
584 	spin_unlock(&vpfe_dev->dma_queue_lock);
585 
586 	if (vpfe_dev->cfg->clr_intr)
587 		vpfe_dev->cfg->clr_intr(irq);
588 
589 	return IRQ_HANDLED;
590 }
591 
vpfe_detach_irq(struct vpfe_device * vpfe_dev)592 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
593 {
594 	enum ccdc_frmfmt frame_format;
595 
596 	frame_format = ccdc_dev->hw_ops.get_frame_format();
597 	if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
598 		free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
599 }
600 
vpfe_attach_irq(struct vpfe_device * vpfe_dev)601 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
602 {
603 	enum ccdc_frmfmt frame_format;
604 
605 	frame_format = ccdc_dev->hw_ops.get_frame_format();
606 	if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
607 		return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
608 				    0, "vpfe_capture1",
609 				    vpfe_dev);
610 	}
611 	return 0;
612 }
613 
614 /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
vpfe_stop_ccdc_capture(struct vpfe_device * vpfe_dev)615 static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
616 {
617 	vpfe_dev->started = 0;
618 	ccdc_dev->hw_ops.enable(0);
619 	if (ccdc_dev->hw_ops.enable_out_to_sdram)
620 		ccdc_dev->hw_ops.enable_out_to_sdram(0);
621 }
622 
623 /*
624  * vpfe_release : This function deletes buffer queue, frees the
625  * buffers and the vpfe file  handle
626  */
vpfe_release(struct file * file)627 static int vpfe_release(struct file *file)
628 {
629 	struct vpfe_device *vpfe_dev = video_drvdata(file);
630 	struct vpfe_fh *fh = file->private_data;
631 	struct vpfe_subdev_info *sdinfo;
632 	int ret;
633 
634 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
635 
636 	/* Get the device lock */
637 	mutex_lock(&vpfe_dev->lock);
638 	/* if this instance is doing IO */
639 	if (fh->io_allowed) {
640 		if (vpfe_dev->started) {
641 			sdinfo = vpfe_dev->current_subdev;
642 			ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
643 							 sdinfo->grp_id,
644 							 video, s_stream, 0);
645 			if (ret && (ret != -ENOIOCTLCMD))
646 				v4l2_err(&vpfe_dev->v4l2_dev,
647 				"stream off failed in subdev\n");
648 			vpfe_stop_ccdc_capture(vpfe_dev);
649 			vpfe_detach_irq(vpfe_dev);
650 			videobuf_streamoff(&vpfe_dev->buffer_queue);
651 		}
652 		vpfe_dev->io_usrs = 0;
653 		vpfe_dev->numbuffers = config_params.numbuffers;
654 		videobuf_stop(&vpfe_dev->buffer_queue);
655 		videobuf_mmap_free(&vpfe_dev->buffer_queue);
656 	}
657 
658 	/* Decrement device usrs counter */
659 	vpfe_dev->usrs--;
660 	v4l2_fh_del(&fh->fh);
661 	v4l2_fh_exit(&fh->fh);
662 	/* If this is the last file handle */
663 	if (!vpfe_dev->usrs) {
664 		vpfe_dev->initialized = 0;
665 		if (ccdc_dev->hw_ops.close)
666 			ccdc_dev->hw_ops.close(vpfe_dev->pdev);
667 		module_put(ccdc_dev->owner);
668 	}
669 	mutex_unlock(&vpfe_dev->lock);
670 	file->private_data = NULL;
671 	/* Free memory allocated to file handle object */
672 	kfree(fh);
673 	return 0;
674 }
675 
676 /*
677  * vpfe_mmap : It is used to map kernel space buffers
678  * into user spaces
679  */
vpfe_mmap(struct file * file,struct vm_area_struct * vma)680 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
681 {
682 	/* Get the device object and file handle object */
683 	struct vpfe_device *vpfe_dev = video_drvdata(file);
684 
685 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
686 
687 	return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
688 }
689 
690 /*
691  * vpfe_poll: It is used for select/poll system call
692  */
vpfe_poll(struct file * file,poll_table * wait)693 static __poll_t vpfe_poll(struct file *file, poll_table *wait)
694 {
695 	struct vpfe_device *vpfe_dev = video_drvdata(file);
696 
697 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
698 
699 	if (vpfe_dev->started)
700 		return videobuf_poll_stream(file,
701 					    &vpfe_dev->buffer_queue, wait);
702 	return 0;
703 }
704 
705 /* vpfe capture driver file operations */
706 static const struct v4l2_file_operations vpfe_fops = {
707 	.owner = THIS_MODULE,
708 	.open = vpfe_open,
709 	.release = vpfe_release,
710 	.unlocked_ioctl = video_ioctl2,
711 	.mmap = vpfe_mmap,
712 	.poll = vpfe_poll
713 };
714 
715 /*
716  * vpfe_check_format()
717  * This function adjust the input pixel format as per hardware
718  * capabilities and update the same in pixfmt.
719  * Following algorithm used :-
720  *
721  *	If given pixformat is not in the vpfe list of pix formats or not
722  *	supported by the hardware, current value of pixformat in the device
723  *	is used
724  *	If given field is not supported, then current field is used. If field
725  *	is different from current, then it is matched with that from sub device.
726  *	Minimum height is 2 lines for interlaced or tb field and 1 line for
727  *	progressive. Maximum height is clamped to active active lines of scan
728  *	Minimum width is 32 bytes in memory and width is clamped to active
729  *	pixels of scan.
730  *	bytesperline is a multiple of 32.
731  */
732 static const struct vpfe_pixel_format *
vpfe_check_format(struct vpfe_device * vpfe_dev,struct v4l2_pix_format * pixfmt)733 	vpfe_check_format(struct vpfe_device *vpfe_dev,
734 			  struct v4l2_pix_format *pixfmt)
735 {
736 	u32 min_height = 1, min_width = 32, max_width, max_height;
737 	const struct vpfe_pixel_format *vpfe_pix_fmt;
738 	u32 pix;
739 	int temp, found;
740 
741 	vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
742 	if (!vpfe_pix_fmt) {
743 		/*
744 		 * use current pixel format in the vpfe device. We
745 		 * will find this pix format in the table
746 		 */
747 		pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
748 		vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
749 	}
750 
751 	/* check if hw supports it */
752 	temp = 0;
753 	found = 0;
754 	while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
755 		if (vpfe_pix_fmt->pixelformat == pix) {
756 			found = 1;
757 			break;
758 		}
759 		temp++;
760 	}
761 
762 	if (!found) {
763 		/* use current pixel format */
764 		pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
765 		/*
766 		 * Since this is currently used in the vpfe device, we
767 		 * will find this pix format in the table
768 		 */
769 		vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
770 	}
771 
772 	/* check what field format is supported */
773 	if (pixfmt->field == V4L2_FIELD_ANY) {
774 		/* if field is any, use current value as default */
775 		pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
776 	}
777 
778 	/*
779 	 * if field is not same as current field in the vpfe device
780 	 * try matching the field with the sub device field
781 	 */
782 	if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
783 		/*
784 		 * If field value is not in the supported fields, use current
785 		 * field used in the device as default
786 		 */
787 		switch (pixfmt->field) {
788 		case V4L2_FIELD_INTERLACED:
789 		case V4L2_FIELD_SEQ_TB:
790 			/* if sub device is supporting progressive, use that */
791 			if (!vpfe_dev->std_info.frame_format)
792 				pixfmt->field = V4L2_FIELD_NONE;
793 			break;
794 		case V4L2_FIELD_NONE:
795 			if (vpfe_dev->std_info.frame_format)
796 				pixfmt->field = V4L2_FIELD_INTERLACED;
797 			break;
798 
799 		default:
800 			/* use current field as default */
801 			pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
802 			break;
803 		}
804 	}
805 
806 	/* Now adjust image resolutions supported */
807 	if (pixfmt->field == V4L2_FIELD_INTERLACED ||
808 	    pixfmt->field == V4L2_FIELD_SEQ_TB)
809 		min_height = 2;
810 
811 	max_width = vpfe_dev->std_info.active_pixels;
812 	max_height = vpfe_dev->std_info.active_lines;
813 	min_width /= vpfe_pix_fmt->bpp;
814 
815 	v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
816 		  pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
817 
818 	pixfmt->width = clamp((pixfmt->width), min_width, max_width);
819 	pixfmt->height = clamp((pixfmt->height), min_height, max_height);
820 
821 	/* If interlaced, adjust height to be a multiple of 2 */
822 	if (pixfmt->field == V4L2_FIELD_INTERLACED)
823 		pixfmt->height &= (~1);
824 	/*
825 	 * recalculate bytesperline and sizeimage since width
826 	 * and height might have changed
827 	 */
828 	pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
829 				& ~31);
830 	if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
831 		pixfmt->sizeimage =
832 			pixfmt->bytesperline * pixfmt->height +
833 			((pixfmt->bytesperline * pixfmt->height) >> 1);
834 	else
835 		pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
836 
837 	v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
838 		 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
839 		 pixfmt->bytesperline, pixfmt->sizeimage);
840 	return vpfe_pix_fmt;
841 }
842 
vpfe_querycap(struct file * file,void * priv,struct v4l2_capability * cap)843 static int vpfe_querycap(struct file *file, void  *priv,
844 			       struct v4l2_capability *cap)
845 {
846 	struct vpfe_device *vpfe_dev = video_drvdata(file);
847 
848 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
849 
850 	strscpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
851 	strscpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
852 	strscpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
853 	return 0;
854 }
855 
vpfe_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * fmt)856 static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
857 				struct v4l2_format *fmt)
858 {
859 	struct vpfe_device *vpfe_dev = video_drvdata(file);
860 
861 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
862 	/* Fill in the information about format */
863 	*fmt = vpfe_dev->fmt;
864 	return 0;
865 }
866 
vpfe_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * fmt)867 static int vpfe_enum_fmt_vid_cap(struct file *file, void  *priv,
868 				   struct v4l2_fmtdesc *fmt)
869 {
870 	struct vpfe_device *vpfe_dev = video_drvdata(file);
871 	const struct vpfe_pixel_format *pix_fmt;
872 	u32 pix;
873 
874 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
875 
876 	if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
877 		return -EINVAL;
878 
879 	/* Fill in the information about format */
880 	pix_fmt = vpfe_lookup_pix_format(pix);
881 	if (pix_fmt) {
882 		fmt->pixelformat = fmt->pixelformat;
883 		return 0;
884 	}
885 	return -EINVAL;
886 }
887 
vpfe_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * fmt)888 static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
889 				struct v4l2_format *fmt)
890 {
891 	struct vpfe_device *vpfe_dev = video_drvdata(file);
892 	const struct vpfe_pixel_format *pix_fmts;
893 	int ret;
894 
895 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
896 
897 	/* If streaming is started, return error */
898 	if (vpfe_dev->started) {
899 		v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
900 		return -EBUSY;
901 	}
902 
903 	/* Check for valid frame format */
904 	pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
905 	if (!pix_fmts)
906 		return -EINVAL;
907 
908 	/* store the pixel format in the device  object */
909 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
910 	if (ret)
911 		return ret;
912 
913 	/* First detach any IRQ if currently attached */
914 	vpfe_detach_irq(vpfe_dev);
915 	vpfe_dev->fmt = *fmt;
916 	/* set image capture parameters in the ccdc */
917 	ret = vpfe_config_ccdc_image_format(vpfe_dev);
918 	mutex_unlock(&vpfe_dev->lock);
919 	return ret;
920 }
921 
vpfe_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)922 static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
923 				  struct v4l2_format *f)
924 {
925 	struct vpfe_device *vpfe_dev = video_drvdata(file);
926 	const struct vpfe_pixel_format *pix_fmts;
927 
928 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
929 
930 	pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
931 	if (!pix_fmts)
932 		return -EINVAL;
933 	return 0;
934 }
935 
936 /*
937  * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
938  * given app input index
939  */
vpfe_get_subdev_input_index(struct vpfe_device * vpfe_dev,int * subdev_index,int * subdev_input_index,int app_input_index)940 static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
941 					int *subdev_index,
942 					int *subdev_input_index,
943 					int app_input_index)
944 {
945 	struct vpfe_config *cfg = vpfe_dev->cfg;
946 	struct vpfe_subdev_info *sdinfo;
947 	int i, j = 0;
948 
949 	for (i = 0; i < cfg->num_subdevs; i++) {
950 		sdinfo = &cfg->sub_devs[i];
951 		if (app_input_index < (j + sdinfo->num_inputs)) {
952 			*subdev_index = i;
953 			*subdev_input_index = app_input_index - j;
954 			return 0;
955 		}
956 		j += sdinfo->num_inputs;
957 	}
958 	return -EINVAL;
959 }
960 
961 /*
962  * vpfe_get_app_input - Get app input index for a given subdev input index
963  * driver stores the input index of the current sub device and translate it
964  * when application request the current input
965  */
vpfe_get_app_input_index(struct vpfe_device * vpfe_dev,int * app_input_index)966 static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
967 				    int *app_input_index)
968 {
969 	struct vpfe_config *cfg = vpfe_dev->cfg;
970 	struct vpfe_subdev_info *sdinfo;
971 	int i, j = 0;
972 
973 	for (i = 0; i < cfg->num_subdevs; i++) {
974 		sdinfo = &cfg->sub_devs[i];
975 		if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
976 			if (vpfe_dev->current_input >= sdinfo->num_inputs)
977 				return -1;
978 			*app_input_index = j + vpfe_dev->current_input;
979 			return 0;
980 		}
981 		j += sdinfo->num_inputs;
982 	}
983 	return -EINVAL;
984 }
985 
vpfe_enum_input(struct file * file,void * priv,struct v4l2_input * inp)986 static int vpfe_enum_input(struct file *file, void *priv,
987 				 struct v4l2_input *inp)
988 {
989 	struct vpfe_device *vpfe_dev = video_drvdata(file);
990 	struct vpfe_subdev_info *sdinfo;
991 	int subdev, index ;
992 
993 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
994 
995 	if (vpfe_get_subdev_input_index(vpfe_dev,
996 					&subdev,
997 					&index,
998 					inp->index) < 0) {
999 		v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
1000 		return -EINVAL;
1001 	}
1002 	sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1003 	*inp = sdinfo->inputs[index];
1004 	return 0;
1005 }
1006 
vpfe_g_input(struct file * file,void * priv,unsigned int * index)1007 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1008 {
1009 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1010 
1011 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1012 
1013 	return vpfe_get_app_input_index(vpfe_dev, index);
1014 }
1015 
1016 
vpfe_s_input(struct file * file,void * priv,unsigned int index)1017 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1018 {
1019 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1020 	struct v4l2_subdev *sd;
1021 	struct vpfe_subdev_info *sdinfo;
1022 	int subdev_index, inp_index;
1023 	struct vpfe_route *route;
1024 	u32 input, output;
1025 	int ret;
1026 
1027 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1028 
1029 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1030 	if (ret)
1031 		return ret;
1032 
1033 	/*
1034 	 * If streaming is started return device busy
1035 	 * error
1036 	 */
1037 	if (vpfe_dev->started) {
1038 		v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1039 		ret = -EBUSY;
1040 		goto unlock_out;
1041 	}
1042 	ret = vpfe_get_subdev_input_index(vpfe_dev,
1043 					  &subdev_index,
1044 					  &inp_index,
1045 					  index);
1046 	if (ret < 0) {
1047 		v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1048 		goto unlock_out;
1049 	}
1050 
1051 	sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1052 	sd = vpfe_dev->sd[subdev_index];
1053 	route = &sdinfo->routes[inp_index];
1054 	if (route && sdinfo->can_route) {
1055 		input = route->input;
1056 		output = route->output;
1057 	} else {
1058 		input = 0;
1059 		output = 0;
1060 	}
1061 
1062 	if (sd)
1063 		ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1064 
1065 	if (ret) {
1066 		v4l2_err(&vpfe_dev->v4l2_dev,
1067 			"vpfe_doioctl:error in setting input in decoder\n");
1068 		ret = -EINVAL;
1069 		goto unlock_out;
1070 	}
1071 	vpfe_dev->current_subdev = sdinfo;
1072 	if (sd)
1073 		vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
1074 	vpfe_dev->current_input = index;
1075 	vpfe_dev->std_index = 0;
1076 
1077 	/* set the bus/interface parameter for the sub device in ccdc */
1078 	ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1079 	if (ret)
1080 		goto unlock_out;
1081 
1082 	/* set the default image parameters in the device */
1083 	ret = vpfe_config_image_format(vpfe_dev,
1084 				vpfe_standards[vpfe_dev->std_index].std_id);
1085 unlock_out:
1086 	mutex_unlock(&vpfe_dev->lock);
1087 	return ret;
1088 }
1089 
vpfe_querystd(struct file * file,void * priv,v4l2_std_id * std_id)1090 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1091 {
1092 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1093 	struct vpfe_subdev_info *sdinfo;
1094 	int ret;
1095 
1096 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1097 
1098 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1099 	sdinfo = vpfe_dev->current_subdev;
1100 	if (ret)
1101 		return ret;
1102 	/* Call querystd function of decoder device */
1103 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1104 					 video, querystd, std_id);
1105 	mutex_unlock(&vpfe_dev->lock);
1106 	return ret;
1107 }
1108 
vpfe_s_std(struct file * file,void * priv,v4l2_std_id std_id)1109 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1110 {
1111 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1112 	struct vpfe_subdev_info *sdinfo;
1113 	int ret;
1114 
1115 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1116 
1117 	/* Call decoder driver function to set the standard */
1118 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1119 	if (ret)
1120 		return ret;
1121 
1122 	sdinfo = vpfe_dev->current_subdev;
1123 	/* If streaming is started, return device busy error */
1124 	if (vpfe_dev->started) {
1125 		v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1126 		ret = -EBUSY;
1127 		goto unlock_out;
1128 	}
1129 
1130 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1131 					 video, s_std, std_id);
1132 	if (ret < 0) {
1133 		v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1134 		goto unlock_out;
1135 	}
1136 	ret = vpfe_config_image_format(vpfe_dev, std_id);
1137 
1138 unlock_out:
1139 	mutex_unlock(&vpfe_dev->lock);
1140 	return ret;
1141 }
1142 
vpfe_g_std(struct file * file,void * priv,v4l2_std_id * std_id)1143 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1144 {
1145 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1146 
1147 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1148 
1149 	*std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1150 	return 0;
1151 }
1152 /*
1153  *  Videobuf operations
1154  */
vpfe_videobuf_setup(struct videobuf_queue * vq,unsigned int * count,unsigned int * size)1155 static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1156 				unsigned int *count,
1157 				unsigned int *size)
1158 {
1159 	struct vpfe_fh *fh = vq->priv_data;
1160 	struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1161 
1162 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1163 	*size = vpfe_dev->fmt.fmt.pix.sizeimage;
1164 	if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1165 		vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1166 		*size = config_params.device_bufsize;
1167 
1168 	if (*count < config_params.min_numbuffers)
1169 		*count = config_params.min_numbuffers;
1170 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1171 		"count=%d, size=%d\n", *count, *size);
1172 	return 0;
1173 }
1174 
vpfe_videobuf_prepare(struct videobuf_queue * vq,struct videobuf_buffer * vb,enum v4l2_field field)1175 static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1176 				struct videobuf_buffer *vb,
1177 				enum v4l2_field field)
1178 {
1179 	struct vpfe_fh *fh = vq->priv_data;
1180 	struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1181 	unsigned long addr;
1182 	int ret;
1183 
1184 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1185 
1186 	/* If buffer is not initialized, initialize it */
1187 	if (VIDEOBUF_NEEDS_INIT == vb->state) {
1188 		vb->width = vpfe_dev->fmt.fmt.pix.width;
1189 		vb->height = vpfe_dev->fmt.fmt.pix.height;
1190 		vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1191 		vb->field = field;
1192 
1193 		ret = videobuf_iolock(vq, vb, NULL);
1194 		if (ret < 0)
1195 			return ret;
1196 
1197 		addr = videobuf_to_dma_contig(vb);
1198 		/* Make sure user addresses are aligned to 32 bytes */
1199 		if (!ALIGN(addr, 32))
1200 			return -EINVAL;
1201 
1202 		vb->state = VIDEOBUF_PREPARED;
1203 	}
1204 	return 0;
1205 }
1206 
vpfe_videobuf_queue(struct videobuf_queue * vq,struct videobuf_buffer * vb)1207 static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1208 				struct videobuf_buffer *vb)
1209 {
1210 	/* Get the file handle object and device object */
1211 	struct vpfe_fh *fh = vq->priv_data;
1212 	struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1213 	unsigned long flags;
1214 
1215 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1216 
1217 	/* add the buffer to the DMA queue */
1218 	spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1219 	list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1220 	spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1221 
1222 	/* Change state of the buffer */
1223 	vb->state = VIDEOBUF_QUEUED;
1224 }
1225 
vpfe_videobuf_release(struct videobuf_queue * vq,struct videobuf_buffer * vb)1226 static void vpfe_videobuf_release(struct videobuf_queue *vq,
1227 				  struct videobuf_buffer *vb)
1228 {
1229 	struct vpfe_fh *fh = vq->priv_data;
1230 	struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1231 	unsigned long flags;
1232 
1233 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1234 
1235 	/*
1236 	 * We need to flush the buffer from the dma queue since
1237 	 * they are de-allocated
1238 	 */
1239 	spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1240 	INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1241 	spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1242 	videobuf_dma_contig_free(vq, vb);
1243 	vb->state = VIDEOBUF_NEEDS_INIT;
1244 }
1245 
1246 static const struct videobuf_queue_ops vpfe_videobuf_qops = {
1247 	.buf_setup      = vpfe_videobuf_setup,
1248 	.buf_prepare    = vpfe_videobuf_prepare,
1249 	.buf_queue      = vpfe_videobuf_queue,
1250 	.buf_release    = vpfe_videobuf_release,
1251 };
1252 
1253 /*
1254  * vpfe_reqbufs. currently support REQBUF only once opening
1255  * the device.
1256  */
vpfe_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * req_buf)1257 static int vpfe_reqbufs(struct file *file, void *priv,
1258 			struct v4l2_requestbuffers *req_buf)
1259 {
1260 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1261 	struct vpfe_fh *fh = file->private_data;
1262 	int ret;
1263 
1264 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1265 
1266 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1267 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1268 		return -EINVAL;
1269 	}
1270 
1271 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1272 	if (ret)
1273 		return ret;
1274 
1275 	if (vpfe_dev->io_usrs != 0) {
1276 		v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1277 		ret = -EBUSY;
1278 		goto unlock_out;
1279 	}
1280 
1281 	vpfe_dev->memory = req_buf->memory;
1282 	videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1283 				&vpfe_videobuf_qops,
1284 				vpfe_dev->pdev,
1285 				&vpfe_dev->irqlock,
1286 				req_buf->type,
1287 				vpfe_dev->fmt.fmt.pix.field,
1288 				sizeof(struct videobuf_buffer),
1289 				fh, NULL);
1290 
1291 	fh->io_allowed = 1;
1292 	vpfe_dev->io_usrs = 1;
1293 	INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1294 	ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1295 unlock_out:
1296 	mutex_unlock(&vpfe_dev->lock);
1297 	return ret;
1298 }
1299 
vpfe_querybuf(struct file * file,void * priv,struct v4l2_buffer * buf)1300 static int vpfe_querybuf(struct file *file, void *priv,
1301 			 struct v4l2_buffer *buf)
1302 {
1303 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1304 
1305 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1306 
1307 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1308 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1309 		return  -EINVAL;
1310 	}
1311 
1312 	if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1313 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1314 		return -EINVAL;
1315 	}
1316 	/* Call videobuf_querybuf to get information */
1317 	return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1318 }
1319 
vpfe_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)1320 static int vpfe_qbuf(struct file *file, void *priv,
1321 		     struct v4l2_buffer *p)
1322 {
1323 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1324 	struct vpfe_fh *fh = file->private_data;
1325 
1326 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1327 
1328 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1329 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1330 		return -EINVAL;
1331 	}
1332 
1333 	/*
1334 	 * If this file handle is not allowed to do IO,
1335 	 * return error
1336 	 */
1337 	if (!fh->io_allowed) {
1338 		v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1339 		return -EACCES;
1340 	}
1341 	return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1342 }
1343 
vpfe_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)1344 static int vpfe_dqbuf(struct file *file, void *priv,
1345 		      struct v4l2_buffer *buf)
1346 {
1347 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1348 
1349 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1350 
1351 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1352 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1353 		return -EINVAL;
1354 	}
1355 	return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1356 				      buf, file->f_flags & O_NONBLOCK);
1357 }
1358 
1359 /*
1360  * vpfe_calculate_offsets : This function calculates buffers offset
1361  * for top and bottom field
1362  */
vpfe_calculate_offsets(struct vpfe_device * vpfe_dev)1363 static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1364 {
1365 	struct v4l2_rect image_win;
1366 
1367 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1368 
1369 	ccdc_dev->hw_ops.get_image_window(&image_win);
1370 	vpfe_dev->field_off = image_win.height * image_win.width;
1371 }
1372 
1373 /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
vpfe_start_ccdc_capture(struct vpfe_device * vpfe_dev)1374 static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1375 {
1376 	ccdc_dev->hw_ops.enable(1);
1377 	if (ccdc_dev->hw_ops.enable_out_to_sdram)
1378 		ccdc_dev->hw_ops.enable_out_to_sdram(1);
1379 	vpfe_dev->started = 1;
1380 }
1381 
1382 /*
1383  * vpfe_streamon. Assume the DMA queue is not empty.
1384  * application is expected to call QBUF before calling
1385  * this ioctl. If not, driver returns error
1386  */
vpfe_streamon(struct file * file,void * priv,enum v4l2_buf_type buf_type)1387 static int vpfe_streamon(struct file *file, void *priv,
1388 			 enum v4l2_buf_type buf_type)
1389 {
1390 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1391 	struct vpfe_fh *fh = file->private_data;
1392 	struct vpfe_subdev_info *sdinfo;
1393 	unsigned long addr;
1394 	int ret;
1395 
1396 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1397 
1398 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1399 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1400 		return -EINVAL;
1401 	}
1402 
1403 	/* If file handle is not allowed IO, return error */
1404 	if (!fh->io_allowed) {
1405 		v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1406 		return -EACCES;
1407 	}
1408 
1409 	sdinfo = vpfe_dev->current_subdev;
1410 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1411 					video, s_stream, 1);
1412 
1413 	if (ret && (ret != -ENOIOCTLCMD)) {
1414 		v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1415 		return -EINVAL;
1416 	}
1417 
1418 	/* If buffer queue is empty, return error */
1419 	if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1420 		v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1421 		return -EIO;
1422 	}
1423 
1424 	/* Call videobuf_streamon to start streaming * in videobuf */
1425 	ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1426 	if (ret)
1427 		return ret;
1428 
1429 
1430 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1431 	if (ret)
1432 		goto streamoff;
1433 	/* Get the next frame from the buffer queue */
1434 	vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1435 					struct videobuf_buffer, queue);
1436 	vpfe_dev->cur_frm = vpfe_dev->next_frm;
1437 	/* Remove buffer from the buffer queue */
1438 	list_del(&vpfe_dev->cur_frm->queue);
1439 	/* Mark state of the current frame to active */
1440 	vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1441 	/* Initialize field_id and started member */
1442 	vpfe_dev->field_id = 0;
1443 	addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1444 
1445 	/* Calculate field offset */
1446 	vpfe_calculate_offsets(vpfe_dev);
1447 
1448 	if (vpfe_attach_irq(vpfe_dev) < 0) {
1449 		v4l2_err(&vpfe_dev->v4l2_dev,
1450 			 "Error in attaching interrupt handle\n");
1451 		ret = -EFAULT;
1452 		goto unlock_out;
1453 	}
1454 	if (ccdc_dev->hw_ops.configure() < 0) {
1455 		v4l2_err(&vpfe_dev->v4l2_dev,
1456 			 "Error in configuring ccdc\n");
1457 		ret = -EINVAL;
1458 		goto unlock_out;
1459 	}
1460 	ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1461 	vpfe_start_ccdc_capture(vpfe_dev);
1462 	mutex_unlock(&vpfe_dev->lock);
1463 	return ret;
1464 unlock_out:
1465 	mutex_unlock(&vpfe_dev->lock);
1466 streamoff:
1467 	videobuf_streamoff(&vpfe_dev->buffer_queue);
1468 	return ret;
1469 }
1470 
vpfe_streamoff(struct file * file,void * priv,enum v4l2_buf_type buf_type)1471 static int vpfe_streamoff(struct file *file, void *priv,
1472 			  enum v4l2_buf_type buf_type)
1473 {
1474 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1475 	struct vpfe_fh *fh = file->private_data;
1476 	struct vpfe_subdev_info *sdinfo;
1477 	int ret;
1478 
1479 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1480 
1481 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1482 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1483 		return -EINVAL;
1484 	}
1485 
1486 	/* If io is allowed for this file handle, return error */
1487 	if (!fh->io_allowed) {
1488 		v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1489 		return -EACCES;
1490 	}
1491 
1492 	/* If streaming is not started, return error */
1493 	if (!vpfe_dev->started) {
1494 		v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1495 		return -EINVAL;
1496 	}
1497 
1498 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1499 	if (ret)
1500 		return ret;
1501 
1502 	vpfe_stop_ccdc_capture(vpfe_dev);
1503 	vpfe_detach_irq(vpfe_dev);
1504 
1505 	sdinfo = vpfe_dev->current_subdev;
1506 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1507 					video, s_stream, 0);
1508 
1509 	if (ret && (ret != -ENOIOCTLCMD))
1510 		v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1511 	ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1512 	mutex_unlock(&vpfe_dev->lock);
1513 	return ret;
1514 }
1515 
vpfe_g_pixelaspect(struct file * file,void * priv,int type,struct v4l2_fract * f)1516 static int vpfe_g_pixelaspect(struct file *file, void *priv,
1517 			      int type, struct v4l2_fract *f)
1518 {
1519 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1520 
1521 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_pixelaspect\n");
1522 
1523 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1524 		return -EINVAL;
1525 	/* If std_index is invalid, then just return (== 1:1 aspect) */
1526 	if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1527 		return 0;
1528 
1529 	*f = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1530 	return 0;
1531 }
1532 
vpfe_g_selection(struct file * file,void * priv,struct v4l2_selection * sel)1533 static int vpfe_g_selection(struct file *file, void *priv,
1534 			    struct v4l2_selection *sel)
1535 {
1536 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1537 
1538 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
1539 
1540 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1541 		return -EINVAL;
1542 
1543 	switch (sel->target) {
1544 	case V4L2_SEL_TGT_CROP:
1545 		sel->r = vpfe_dev->crop;
1546 		break;
1547 	case V4L2_SEL_TGT_CROP_DEFAULT:
1548 	case V4L2_SEL_TGT_CROP_BOUNDS:
1549 		sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1550 		sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1551 		break;
1552 	default:
1553 		return -EINVAL;
1554 	}
1555 	return 0;
1556 }
1557 
vpfe_s_selection(struct file * file,void * priv,struct v4l2_selection * sel)1558 static int vpfe_s_selection(struct file *file, void *priv,
1559 			    struct v4l2_selection *sel)
1560 {
1561 	struct vpfe_device *vpfe_dev = video_drvdata(file);
1562 	struct v4l2_rect rect = sel->r;
1563 	int ret;
1564 
1565 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1566 
1567 	if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1568 	    sel->target != V4L2_SEL_TGT_CROP)
1569 		return -EINVAL;
1570 
1571 	if (vpfe_dev->started) {
1572 		/* make sure streaming is not started */
1573 		v4l2_err(&vpfe_dev->v4l2_dev,
1574 			"Cannot change crop when streaming is ON\n");
1575 		return -EBUSY;
1576 	}
1577 
1578 	ret = mutex_lock_interruptible(&vpfe_dev->lock);
1579 	if (ret)
1580 		return ret;
1581 
1582 	if (rect.top < 0 || rect.left < 0) {
1583 		v4l2_err(&vpfe_dev->v4l2_dev,
1584 			"doesn't support negative values for top & left\n");
1585 		ret = -EINVAL;
1586 		goto unlock_out;
1587 	}
1588 
1589 	/* adjust the width to 16 pixel boundary */
1590 	rect.width = ((rect.width + 15) & ~0xf);
1591 
1592 	/* make sure parameters are valid */
1593 	if ((rect.left + rect.width >
1594 		vpfe_dev->std_info.active_pixels) ||
1595 	    (rect.top + rect.height >
1596 		vpfe_dev->std_info.active_lines)) {
1597 		v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
1598 		ret = -EINVAL;
1599 		goto unlock_out;
1600 	}
1601 	ccdc_dev->hw_ops.set_image_window(&rect);
1602 	vpfe_dev->fmt.fmt.pix.width = rect.width;
1603 	vpfe_dev->fmt.fmt.pix.height = rect.height;
1604 	vpfe_dev->fmt.fmt.pix.bytesperline =
1605 		ccdc_dev->hw_ops.get_line_length();
1606 	vpfe_dev->fmt.fmt.pix.sizeimage =
1607 		vpfe_dev->fmt.fmt.pix.bytesperline *
1608 		vpfe_dev->fmt.fmt.pix.height;
1609 	vpfe_dev->crop = rect;
1610 	sel->r = rect;
1611 unlock_out:
1612 	mutex_unlock(&vpfe_dev->lock);
1613 	return ret;
1614 }
1615 
1616 /* vpfe capture ioctl operations */
1617 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1618 	.vidioc_querycap	 = vpfe_querycap,
1619 	.vidioc_g_fmt_vid_cap    = vpfe_g_fmt_vid_cap,
1620 	.vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1621 	.vidioc_s_fmt_vid_cap    = vpfe_s_fmt_vid_cap,
1622 	.vidioc_try_fmt_vid_cap  = vpfe_try_fmt_vid_cap,
1623 	.vidioc_enum_input	 = vpfe_enum_input,
1624 	.vidioc_g_input		 = vpfe_g_input,
1625 	.vidioc_s_input		 = vpfe_s_input,
1626 	.vidioc_querystd	 = vpfe_querystd,
1627 	.vidioc_s_std		 = vpfe_s_std,
1628 	.vidioc_g_std		 = vpfe_g_std,
1629 	.vidioc_reqbufs		 = vpfe_reqbufs,
1630 	.vidioc_querybuf	 = vpfe_querybuf,
1631 	.vidioc_qbuf		 = vpfe_qbuf,
1632 	.vidioc_dqbuf		 = vpfe_dqbuf,
1633 	.vidioc_streamon	 = vpfe_streamon,
1634 	.vidioc_streamoff	 = vpfe_streamoff,
1635 	.vidioc_g_pixelaspect	 = vpfe_g_pixelaspect,
1636 	.vidioc_g_selection	 = vpfe_g_selection,
1637 	.vidioc_s_selection	 = vpfe_s_selection,
1638 };
1639 
vpfe_initialize(void)1640 static struct vpfe_device *vpfe_initialize(void)
1641 {
1642 	struct vpfe_device *vpfe_dev;
1643 
1644 	/* Default number of buffers should be 3 */
1645 	if ((numbuffers > 0) &&
1646 	    (numbuffers < config_params.min_numbuffers))
1647 		numbuffers = config_params.min_numbuffers;
1648 
1649 	/*
1650 	 * Set buffer size to min buffers size if invalid buffer size is
1651 	 * given
1652 	 */
1653 	if (bufsize < config_params.min_bufsize)
1654 		bufsize = config_params.min_bufsize;
1655 
1656 	config_params.numbuffers = numbuffers;
1657 
1658 	if (numbuffers)
1659 		config_params.device_bufsize = bufsize;
1660 
1661 	/* Allocate memory for device objects */
1662 	vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1663 
1664 	return vpfe_dev;
1665 }
1666 
1667 /*
1668  * vpfe_probe : This function creates device entries by register
1669  * itself to the V4L2 driver and initializes fields of each
1670  * device objects
1671  */
vpfe_probe(struct platform_device * pdev)1672 static int vpfe_probe(struct platform_device *pdev)
1673 {
1674 	struct vpfe_subdev_info *sdinfo;
1675 	struct vpfe_config *vpfe_cfg;
1676 	struct resource *res1;
1677 	struct vpfe_device *vpfe_dev;
1678 	struct i2c_adapter *i2c_adap;
1679 	struct video_device *vfd;
1680 	int ret, i, j;
1681 	int num_subdevs = 0;
1682 
1683 	/* Get the pointer to the device object */
1684 	vpfe_dev = vpfe_initialize();
1685 
1686 	if (!vpfe_dev) {
1687 		v4l2_err(pdev->dev.driver,
1688 			"Failed to allocate memory for vpfe_dev\n");
1689 		return -ENOMEM;
1690 	}
1691 
1692 	vpfe_dev->pdev = &pdev->dev;
1693 
1694 	if (!pdev->dev.platform_data) {
1695 		v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
1696 		ret = -ENODEV;
1697 		goto probe_free_dev_mem;
1698 	}
1699 
1700 	vpfe_cfg = pdev->dev.platform_data;
1701 	vpfe_dev->cfg = vpfe_cfg;
1702 	if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
1703 		v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1704 		ret = -ENOENT;
1705 		goto probe_free_dev_mem;
1706 	}
1707 
1708 	/* Allocate memory for ccdc configuration */
1709 	ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
1710 	if (!ccdc_cfg) {
1711 		ret = -ENOMEM;
1712 		goto probe_free_dev_mem;
1713 	}
1714 
1715 	mutex_lock(&ccdc_lock);
1716 
1717 	strscpy(ccdc_cfg->name, vpfe_cfg->ccdc, sizeof(ccdc_cfg->name));
1718 	/* Get VINT0 irq resource */
1719 	res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1720 	if (!res1) {
1721 		v4l2_err(pdev->dev.driver,
1722 			 "Unable to get interrupt for VINT0\n");
1723 		ret = -ENODEV;
1724 		goto probe_free_ccdc_cfg_mem;
1725 	}
1726 	vpfe_dev->ccdc_irq0 = res1->start;
1727 
1728 	/* Get VINT1 irq resource */
1729 	res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1730 	if (!res1) {
1731 		v4l2_err(pdev->dev.driver,
1732 			 "Unable to get interrupt for VINT1\n");
1733 		ret = -ENODEV;
1734 		goto probe_free_ccdc_cfg_mem;
1735 	}
1736 	vpfe_dev->ccdc_irq1 = res1->start;
1737 
1738 	ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
1739 			  "vpfe_capture0", vpfe_dev);
1740 
1741 	if (0 != ret) {
1742 		v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
1743 		goto probe_free_ccdc_cfg_mem;
1744 	}
1745 
1746 	vfd = &vpfe_dev->video_dev;
1747 	/* Initialize field of video device */
1748 	vfd->release		= video_device_release_empty;
1749 	vfd->fops		= &vpfe_fops;
1750 	vfd->ioctl_ops		= &vpfe_ioctl_ops;
1751 	vfd->tvnorms		= 0;
1752 	vfd->v4l2_dev		= &vpfe_dev->v4l2_dev;
1753 	vfd->device_caps	= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1754 	snprintf(vfd->name, sizeof(vfd->name),
1755 		 "%s_V%d.%d.%d",
1756 		 CAPTURE_DRV_NAME,
1757 		 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1758 		 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1759 		 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1760 
1761 	ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1762 	if (ret) {
1763 		v4l2_err(pdev->dev.driver,
1764 			"Unable to register v4l2 device.\n");
1765 		goto probe_out_release_irq;
1766 	}
1767 	v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1768 	spin_lock_init(&vpfe_dev->irqlock);
1769 	spin_lock_init(&vpfe_dev->dma_queue_lock);
1770 	mutex_init(&vpfe_dev->lock);
1771 
1772 	/* Initialize field of the device objects */
1773 	vpfe_dev->numbuffers = config_params.numbuffers;
1774 
1775 	/* register video device */
1776 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1777 		"trying to register vpfe device.\n");
1778 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1779 		"video_dev=%p\n", &vpfe_dev->video_dev);
1780 	vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1781 	ret = video_register_device(&vpfe_dev->video_dev,
1782 				    VFL_TYPE_GRABBER, -1);
1783 
1784 	if (ret) {
1785 		v4l2_err(pdev->dev.driver,
1786 			"Unable to register video device.\n");
1787 		goto probe_out_v4l2_unregister;
1788 	}
1789 
1790 	v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1791 	/* set the driver data in platform device */
1792 	platform_set_drvdata(pdev, vpfe_dev);
1793 	/* set driver private data */
1794 	video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
1795 	i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
1796 	num_subdevs = vpfe_cfg->num_subdevs;
1797 	vpfe_dev->sd = kmalloc_array(num_subdevs,
1798 				     sizeof(*vpfe_dev->sd),
1799 				     GFP_KERNEL);
1800 	if (!vpfe_dev->sd) {
1801 		ret = -ENOMEM;
1802 		goto probe_out_video_unregister;
1803 	}
1804 
1805 	for (i = 0; i < num_subdevs; i++) {
1806 		struct v4l2_input *inps;
1807 
1808 		sdinfo = &vpfe_cfg->sub_devs[i];
1809 
1810 		/* Load up the subdevice */
1811 		vpfe_dev->sd[i] =
1812 			v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1813 						  i2c_adap,
1814 						  &sdinfo->board_info,
1815 						  NULL);
1816 		if (vpfe_dev->sd[i]) {
1817 			v4l2_info(&vpfe_dev->v4l2_dev,
1818 				  "v4l2 sub device %s registered\n",
1819 				  sdinfo->name);
1820 			vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1821 			/* update tvnorms from the sub devices */
1822 			for (j = 0; j < sdinfo->num_inputs; j++) {
1823 				inps = &sdinfo->inputs[j];
1824 				vfd->tvnorms |= inps->std;
1825 			}
1826 		} else {
1827 			v4l2_info(&vpfe_dev->v4l2_dev,
1828 				  "v4l2 sub device %s register fails\n",
1829 				  sdinfo->name);
1830 			ret = -ENXIO;
1831 			goto probe_sd_out;
1832 		}
1833 	}
1834 
1835 	/* set first sub device as current one */
1836 	vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1837 	vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
1838 
1839 	/* We have at least one sub device to work with */
1840 	mutex_unlock(&ccdc_lock);
1841 	return 0;
1842 
1843 probe_sd_out:
1844 	kfree(vpfe_dev->sd);
1845 probe_out_video_unregister:
1846 	video_unregister_device(&vpfe_dev->video_dev);
1847 probe_out_v4l2_unregister:
1848 	v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1849 probe_out_release_irq:
1850 	free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1851 probe_free_ccdc_cfg_mem:
1852 	kfree(ccdc_cfg);
1853 	mutex_unlock(&ccdc_lock);
1854 probe_free_dev_mem:
1855 	kfree(vpfe_dev);
1856 	return ret;
1857 }
1858 
1859 /*
1860  * vpfe_remove : It un-register device from V4L2 driver
1861  */
vpfe_remove(struct platform_device * pdev)1862 static int vpfe_remove(struct platform_device *pdev)
1863 {
1864 	struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
1865 
1866 	v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1867 
1868 	free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
1869 	kfree(vpfe_dev->sd);
1870 	v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1871 	video_unregister_device(&vpfe_dev->video_dev);
1872 	kfree(vpfe_dev);
1873 	kfree(ccdc_cfg);
1874 	return 0;
1875 }
1876 
vpfe_suspend(struct device * dev)1877 static int vpfe_suspend(struct device *dev)
1878 {
1879 	return 0;
1880 }
1881 
vpfe_resume(struct device * dev)1882 static int vpfe_resume(struct device *dev)
1883 {
1884 	return 0;
1885 }
1886 
1887 static const struct dev_pm_ops vpfe_dev_pm_ops = {
1888 	.suspend = vpfe_suspend,
1889 	.resume = vpfe_resume,
1890 };
1891 
1892 static struct platform_driver vpfe_driver = {
1893 	.driver = {
1894 		.name = CAPTURE_DRV_NAME,
1895 		.pm = &vpfe_dev_pm_ops,
1896 	},
1897 	.probe = vpfe_probe,
1898 	.remove = vpfe_remove,
1899 };
1900 
1901 module_platform_driver(vpfe_driver);
1902