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