blob: 885cd54499cf6952abbae826e7526ca5ac186673 [file] [log] [blame]
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001/*
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>
70#include <linux/init.h>
71#include <linux/platform_device.h>
72#include <linux/interrupt.h>
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -030073#include <media/v4l2-common.h>
74#include <linux/io.h>
75#include <media/davinci/vpfe_capture.h>
76#include "ccdc_hw_device.h"
77
78static int debug;
79static u32 numbuffers = 3;
80static u32 bufsize = (720 * 576 * 2);
81
82module_param(numbuffers, uint, S_IRUGO);
83module_param(bufsize, uint, S_IRUGO);
84module_param(debug, int, 0644);
85
86MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
87MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
88MODULE_PARM_DESC(debug, "Debug level 0-1");
89
90MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
91MODULE_LICENSE("GPL");
92MODULE_AUTHOR("Texas Instruments");
93
94/* standard information */
95struct vpfe_standard {
96 v4l2_std_id std_id;
97 unsigned int width;
98 unsigned int height;
99 struct v4l2_fract pixelaspect;
100 /* 0 - progressive, 1 - interlaced */
101 int frame_format;
102};
103
104/* ccdc configuration */
105struct ccdc_config {
106 /* This make sure vpfe is probed and ready to go */
107 int vpfe_probed;
108 /* name of ccdc device */
109 char name[32];
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300110};
111
112/* data structures */
113static struct vpfe_config_params config_params = {
114 .min_numbuffers = 3,
115 .numbuffers = 3,
116 .min_bufsize = 720 * 480 * 2,
117 .device_bufsize = 720 * 576 * 2,
118};
119
120/* ccdc device registered */
121static struct ccdc_hw_device *ccdc_dev;
122/* lock for accessing ccdc information */
123static DEFINE_MUTEX(ccdc_lock);
124/* ccdc configuration */
125static struct ccdc_config *ccdc_cfg;
126
127const struct vpfe_standard vpfe_standards[] = {
128 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
129 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
130};
131
132/* Used when raw Bayer image from ccdc is directly captured to SDRAM */
133static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
134 {
135 .fmtdesc = {
136 .index = 0,
137 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
138 .description = "Bayer GrRBGb 8bit A-Law compr.",
139 .pixelformat = V4L2_PIX_FMT_SBGGR8,
140 },
141 .bpp = 1,
142 },
143 {
144 .fmtdesc = {
145 .index = 1,
146 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
147 .description = "Bayer GrRBGb - 16bit",
148 .pixelformat = V4L2_PIX_FMT_SBGGR16,
149 },
150 .bpp = 2,
151 },
152 {
153 .fmtdesc = {
154 .index = 2,
155 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
156 .description = "Bayer GrRBGb 8bit DPCM compr.",
157 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
158 },
159 .bpp = 1,
160 },
161 {
162 .fmtdesc = {
163 .index = 3,
164 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
165 .description = "YCbCr 4:2:2 Interleaved UYVY",
166 .pixelformat = V4L2_PIX_FMT_UYVY,
167 },
168 .bpp = 2,
169 },
170 {
171 .fmtdesc = {
172 .index = 4,
173 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
174 .description = "YCbCr 4:2:2 Interleaved YUYV",
175 .pixelformat = V4L2_PIX_FMT_YUYV,
176 },
177 .bpp = 2,
178 },
179 {
180 .fmtdesc = {
181 .index = 5,
182 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
183 .description = "Y/CbCr 4:2:0 - Semi planar",
184 .pixelformat = V4L2_PIX_FMT_NV12,
185 },
186 .bpp = 1,
187 },
188};
189
190/*
191 * vpfe_lookup_pix_format()
192 * lookup an entry in the vpfe pix format table based on pix_format
193 */
194static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
195{
196 int i;
197
198 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
199 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
200 return &vpfe_pix_fmts[i];
201 }
202 return NULL;
203}
204
205/*
206 * vpfe_register_ccdc_device. CCDC module calls this to
207 * register with vpfe capture
208 */
209int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
210{
211 int ret = 0;
212 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
213
214 BUG_ON(!dev->hw_ops.open);
215 BUG_ON(!dev->hw_ops.enable);
216 BUG_ON(!dev->hw_ops.set_hw_if_params);
217 BUG_ON(!dev->hw_ops.configure);
218 BUG_ON(!dev->hw_ops.set_buftype);
219 BUG_ON(!dev->hw_ops.get_buftype);
220 BUG_ON(!dev->hw_ops.enum_pix);
221 BUG_ON(!dev->hw_ops.set_frame_format);
222 BUG_ON(!dev->hw_ops.get_frame_format);
223 BUG_ON(!dev->hw_ops.get_pixel_format);
224 BUG_ON(!dev->hw_ops.set_pixel_format);
225 BUG_ON(!dev->hw_ops.set_params);
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);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300229 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");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300239 ret = -EFAULT;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300240 goto unlock;
241 }
242
243 if (strcmp(dev->name, ccdc_cfg->name)) {
244 /* ignore this ccdc */
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300245 ret = -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300246 goto unlock;
247 }
248
249 if (ccdc_dev) {
250 printk(KERN_ERR "ccdc already registered\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300251 ret = -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300252 goto unlock;
253 }
254
255 ccdc_dev = dev;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300256unlock:
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;
478unlock:
479 mutex_unlock(&ccdc_lock);
480 return ret;
481}
482
483/*
484 * vpfe_open : It creates object of file handle structure and
485 * stores it in private_data member of filepointer
486 */
487static int vpfe_open(struct file *file)
488{
489 struct vpfe_device *vpfe_dev = video_drvdata(file);
490 struct vpfe_fh *fh;
491
492 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
493
494 if (!vpfe_dev->cfg->num_subdevs) {
495 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
496 return -ENODEV;
497 }
498
499 /* Allocate memory for the file handle object */
500 fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
501 if (NULL == fh) {
502 v4l2_err(&vpfe_dev->v4l2_dev,
503 "unable to allocate memory for file handle object\n");
504 return -ENOMEM;
505 }
506 /* store pointer to fh in private_data member of file */
507 file->private_data = fh;
508 fh->vpfe_dev = vpfe_dev;
509 mutex_lock(&vpfe_dev->lock);
510 /* If decoder is not initialized. initialize it */
511 if (!vpfe_dev->initialized) {
512 if (vpfe_initialize_device(vpfe_dev)) {
513 mutex_unlock(&vpfe_dev->lock);
514 return -ENODEV;
515 }
516 }
517 /* Increment device usrs counter */
518 vpfe_dev->usrs++;
519 /* Set io_allowed member to false */
520 fh->io_allowed = 0;
521 /* Initialize priority of this instance to default priority */
522 fh->prio = V4L2_PRIORITY_UNSET;
523 v4l2_prio_open(&vpfe_dev->prio, &fh->prio);
524 mutex_unlock(&vpfe_dev->lock);
525 return 0;
526}
527
528static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
529{
530 unsigned long addr;
531
532 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
533 struct videobuf_buffer, queue);
534 list_del(&vpfe_dev->next_frm->queue);
535 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
536 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
537 ccdc_dev->hw_ops.setfbaddr(addr);
538}
539
540static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
541{
542 struct timeval timevalue;
543
544 do_gettimeofday(&timevalue);
545 vpfe_dev->cur_frm->ts = timevalue;
546 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
547 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
548 wake_up_interruptible(&vpfe_dev->cur_frm->done);
549 vpfe_dev->cur_frm = vpfe_dev->next_frm;
550}
551
552/* ISR for VINT0*/
553static irqreturn_t vpfe_isr(int irq, void *dev_id)
554{
555 struct vpfe_device *vpfe_dev = dev_id;
556 enum v4l2_field field;
557 unsigned long addr;
558 int fid;
559
560 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
561 field = vpfe_dev->fmt.fmt.pix.field;
562
563 /* if streaming not started, don't do anything */
564 if (!vpfe_dev->started)
565 return IRQ_HANDLED;
566
567 /* only for 6446 this will be applicable */
568 if (NULL != ccdc_dev->hw_ops.reset)
569 ccdc_dev->hw_ops.reset();
570
571 if (field == V4L2_FIELD_NONE) {
572 /* handle progressive frame capture */
573 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
574 "frame format is progressive...\n");
575 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
576 vpfe_process_buffer_complete(vpfe_dev);
577 return IRQ_HANDLED;
578 }
579
580 /* interlaced or TB capture check which field we are in hardware */
581 fid = ccdc_dev->hw_ops.getfid();
582
583 /* switch the software maintained field id */
584 vpfe_dev->field_id ^= 1;
585 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
586 fid, vpfe_dev->field_id);
587 if (fid == vpfe_dev->field_id) {
588 /* we are in-sync here,continue */
589 if (fid == 0) {
590 /*
591 * One frame is just being captured. If the next frame
592 * is available, release the current frame and move on
593 */
594 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
595 vpfe_process_buffer_complete(vpfe_dev);
596 /*
597 * based on whether the two fields are stored
598 * interleavely or separately in memory, reconfigure
599 * the CCDC memory address
600 */
601 if (field == V4L2_FIELD_SEQ_TB) {
602 addr =
603 videobuf_to_dma_contig(vpfe_dev->cur_frm);
604 addr += vpfe_dev->field_off;
605 ccdc_dev->hw_ops.setfbaddr(addr);
606 }
607 return IRQ_HANDLED;
608 }
609 /*
610 * if one field is just being captured configure
611 * the next frame get the next frame from the empty
612 * queue if no frame is available hold on to the
613 * current buffer
614 */
615 spin_lock(&vpfe_dev->dma_queue_lock);
616 if (!list_empty(&vpfe_dev->dma_queue) &&
617 vpfe_dev->cur_frm == vpfe_dev->next_frm)
618 vpfe_schedule_next_buffer(vpfe_dev);
619 spin_unlock(&vpfe_dev->dma_queue_lock);
620 } else if (fid == 0) {
621 /*
622 * out of sync. Recover from any hardware out-of-sync.
623 * May loose one frame
624 */
625 vpfe_dev->field_id = fid;
626 }
627 return IRQ_HANDLED;
628}
629
630/* vdint1_isr - isr handler for VINT1 interrupt */
631static irqreturn_t vdint1_isr(int irq, void *dev_id)
632{
633 struct vpfe_device *vpfe_dev = dev_id;
634
635 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
636
637 /* if streaming not started, don't do anything */
638 if (!vpfe_dev->started)
639 return IRQ_HANDLED;
640
641 spin_lock(&vpfe_dev->dma_queue_lock);
642 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
643 !list_empty(&vpfe_dev->dma_queue) &&
644 vpfe_dev->cur_frm == vpfe_dev->next_frm)
645 vpfe_schedule_next_buffer(vpfe_dev);
646 spin_unlock(&vpfe_dev->dma_queue_lock);
647 return IRQ_HANDLED;
648}
649
650static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
651{
652 enum ccdc_frmfmt frame_format;
653
654 frame_format = ccdc_dev->hw_ops.get_frame_format();
655 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
Vaibhav Hiremath1ead6962009-11-09 09:15:07 -0300656 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300657}
658
659static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
660{
661 enum ccdc_frmfmt frame_format;
662
663 frame_format = ccdc_dev->hw_ops.get_frame_format();
664 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
665 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
666 IRQF_DISABLED, "vpfe_capture1",
667 vpfe_dev);
668 }
669 return 0;
670}
671
672/* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
673static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
674{
675 vpfe_dev->started = 0;
676 ccdc_dev->hw_ops.enable(0);
677 if (ccdc_dev->hw_ops.enable_out_to_sdram)
678 ccdc_dev->hw_ops.enable_out_to_sdram(0);
679}
680
681/*
682 * vpfe_release : This function deletes buffer queue, frees the
683 * buffers and the vpfe file handle
684 */
685static int vpfe_release(struct file *file)
686{
687 struct vpfe_device *vpfe_dev = video_drvdata(file);
688 struct vpfe_fh *fh = file->private_data;
689 struct vpfe_subdev_info *sdinfo;
690 int ret;
691
692 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
693
694 /* Get the device lock */
695 mutex_lock(&vpfe_dev->lock);
696 /* if this instance is doing IO */
697 if (fh->io_allowed) {
698 if (vpfe_dev->started) {
699 sdinfo = vpfe_dev->current_subdev;
700 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
701 sdinfo->grp_id,
702 video, s_stream, 0);
703 if (ret && (ret != -ENOIOCTLCMD))
704 v4l2_err(&vpfe_dev->v4l2_dev,
705 "stream off failed in subdev\n");
706 vpfe_stop_ccdc_capture(vpfe_dev);
707 vpfe_detach_irq(vpfe_dev);
708 videobuf_streamoff(&vpfe_dev->buffer_queue);
709 }
710 vpfe_dev->io_usrs = 0;
711 vpfe_dev->numbuffers = config_params.numbuffers;
712 }
713
714 /* Decrement device usrs counter */
715 vpfe_dev->usrs--;
716 /* Close the priority */
717 v4l2_prio_close(&vpfe_dev->prio, &fh->prio);
718 /* If this is the last file handle */
719 if (!vpfe_dev->usrs) {
720 vpfe_dev->initialized = 0;
721 if (ccdc_dev->hw_ops.close)
722 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
723 module_put(ccdc_dev->owner);
724 }
725 mutex_unlock(&vpfe_dev->lock);
726 file->private_data = NULL;
727 /* Free memory allocated to file handle object */
728 kfree(fh);
729 return 0;
730}
731
732/*
733 * vpfe_mmap : It is used to map kernel space buffers
734 * into user spaces
735 */
736static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
737{
738 /* Get the device object and file handle object */
739 struct vpfe_device *vpfe_dev = video_drvdata(file);
740
741 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
742
743 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
744}
745
746/*
747 * vpfe_poll: It is used for select/poll system call
748 */
749static unsigned int vpfe_poll(struct file *file, poll_table *wait)
750{
751 struct vpfe_device *vpfe_dev = video_drvdata(file);
752
753 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
754
755 if (vpfe_dev->started)
756 return videobuf_poll_stream(file,
757 &vpfe_dev->buffer_queue, wait);
758 return 0;
759}
760
761/* vpfe capture driver file operations */
762static const struct v4l2_file_operations vpfe_fops = {
763 .owner = THIS_MODULE,
764 .open = vpfe_open,
765 .release = vpfe_release,
766 .unlocked_ioctl = video_ioctl2,
767 .mmap = vpfe_mmap,
768 .poll = vpfe_poll
769};
770
771/*
772 * vpfe_check_format()
773 * This function adjust the input pixel format as per hardware
774 * capabilities and update the same in pixfmt.
775 * Following algorithm used :-
776 *
777 * If given pixformat is not in the vpfe list of pix formats or not
778 * supported by the hardware, current value of pixformat in the device
779 * is used
780 * If given field is not supported, then current field is used. If field
781 * is different from current, then it is matched with that from sub device.
782 * Minimum height is 2 lines for interlaced or tb field and 1 line for
783 * progressive. Maximum height is clamped to active active lines of scan
784 * Minimum width is 32 bytes in memory and width is clamped to active
785 * pixels of scan.
786 * bytesperline is a multiple of 32.
787 */
788static const struct vpfe_pixel_format *
789 vpfe_check_format(struct vpfe_device *vpfe_dev,
790 struct v4l2_pix_format *pixfmt)
791{
792 u32 min_height = 1, min_width = 32, max_width, max_height;
793 const struct vpfe_pixel_format *vpfe_pix_fmt;
794 u32 pix;
795 int temp, found;
796
797 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
798 if (NULL == vpfe_pix_fmt) {
799 /*
800 * use current pixel format in the vpfe device. We
801 * will find this pix format in the table
802 */
803 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
804 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
805 }
806
807 /* check if hw supports it */
808 temp = 0;
809 found = 0;
810 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
811 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
812 found = 1;
813 break;
814 }
815 temp++;
816 }
817
818 if (!found) {
819 /* use current pixel format */
820 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
821 /*
822 * Since this is currently used in the vpfe device, we
823 * will find this pix format in the table
824 */
825 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
826 }
827
828 /* check what field format is supported */
829 if (pixfmt->field == V4L2_FIELD_ANY) {
830 /* if field is any, use current value as default */
831 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
832 }
833
834 /*
835 * if field is not same as current field in the vpfe device
836 * try matching the field with the sub device field
837 */
838 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
839 /*
840 * If field value is not in the supported fields, use current
841 * field used in the device as default
842 */
843 switch (pixfmt->field) {
844 case V4L2_FIELD_INTERLACED:
845 case V4L2_FIELD_SEQ_TB:
846 /* if sub device is supporting progressive, use that */
847 if (!vpfe_dev->std_info.frame_format)
848 pixfmt->field = V4L2_FIELD_NONE;
849 break;
850 case V4L2_FIELD_NONE:
851 if (vpfe_dev->std_info.frame_format)
852 pixfmt->field = V4L2_FIELD_INTERLACED;
853 break;
854
855 default:
856 /* use current field as default */
857 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
858 break;
859 }
860 }
861
862 /* Now adjust image resolutions supported */
863 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
864 pixfmt->field == V4L2_FIELD_SEQ_TB)
865 min_height = 2;
866
867 max_width = vpfe_dev->std_info.active_pixels;
868 max_height = vpfe_dev->std_info.active_lines;
869 min_width /= vpfe_pix_fmt->bpp;
870
871 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
872 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
873
874 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
875 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
876
877 /* If interlaced, adjust height to be a multiple of 2 */
878 if (pixfmt->field == V4L2_FIELD_INTERLACED)
879 pixfmt->height &= (~1);
880 /*
881 * recalculate bytesperline and sizeimage since width
882 * and height might have changed
883 */
884 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
885 & ~31);
886 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
887 pixfmt->sizeimage =
888 pixfmt->bytesperline * pixfmt->height +
889 ((pixfmt->bytesperline * pixfmt->height) >> 1);
890 else
891 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
892
893 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height ="
894 " %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
895 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
896 pixfmt->bytesperline, pixfmt->sizeimage);
897 return vpfe_pix_fmt;
898}
899
900static int vpfe_querycap(struct file *file, void *priv,
901 struct v4l2_capability *cap)
902{
903 struct vpfe_device *vpfe_dev = video_drvdata(file);
904
905 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
906
907 cap->version = VPFE_CAPTURE_VERSION_CODE;
908 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
909 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
910 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
911 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
912 return 0;
913}
914
915static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
916 struct v4l2_format *fmt)
917{
918 struct vpfe_device *vpfe_dev = video_drvdata(file);
919 int ret = 0;
920
921 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
922 /* Fill in the information about format */
923 *fmt = vpfe_dev->fmt;
924 return ret;
925}
926
927static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
928 struct v4l2_fmtdesc *fmt)
929{
930 struct vpfe_device *vpfe_dev = video_drvdata(file);
931 const struct vpfe_pixel_format *pix_fmt;
932 int temp_index;
933 u32 pix;
934
935 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
936
937 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
938 return -EINVAL;
939
940 /* Fill in the information about format */
941 pix_fmt = vpfe_lookup_pix_format(pix);
942 if (NULL != pix_fmt) {
943 temp_index = fmt->index;
944 *fmt = pix_fmt->fmtdesc;
945 fmt->index = temp_index;
946 return 0;
947 }
948 return -EINVAL;
949}
950
951static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
952 struct v4l2_format *fmt)
953{
954 struct vpfe_device *vpfe_dev = video_drvdata(file);
955 const struct vpfe_pixel_format *pix_fmts;
956 int ret = 0;
957
958 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
959
960 /* If streaming is started, return error */
961 if (vpfe_dev->started) {
962 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
963 return -EBUSY;
964 }
965
966 /* Check for valid frame format */
967 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
968
969 if (NULL == pix_fmts)
970 return -EINVAL;
971
972 /* store the pixel format in the device object */
973 ret = mutex_lock_interruptible(&vpfe_dev->lock);
974 if (ret)
975 return ret;
976
977 /* First detach any IRQ if currently attached */
978 vpfe_detach_irq(vpfe_dev);
979 vpfe_dev->fmt = *fmt;
980 /* set image capture parameters in the ccdc */
981 ret = vpfe_config_ccdc_image_format(vpfe_dev);
982 mutex_unlock(&vpfe_dev->lock);
983 return ret;
984}
985
986static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
987 struct v4l2_format *f)
988{
989 struct vpfe_device *vpfe_dev = video_drvdata(file);
990 const struct vpfe_pixel_format *pix_fmts;
991
992 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
993
994 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
995 if (NULL == pix_fmts)
996 return -EINVAL;
997 return 0;
998}
999
1000/*
1001 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1002 * given app input index
1003 */
1004static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1005 int *subdev_index,
1006 int *subdev_input_index,
1007 int app_input_index)
1008{
1009 struct vpfe_config *cfg = vpfe_dev->cfg;
1010 struct vpfe_subdev_info *sdinfo;
1011 int i, j = 0;
1012
1013 for (i = 0; i < cfg->num_subdevs; i++) {
1014 sdinfo = &cfg->sub_devs[i];
1015 if (app_input_index < (j + sdinfo->num_inputs)) {
1016 *subdev_index = i;
1017 *subdev_input_index = app_input_index - j;
1018 return 0;
1019 }
1020 j += sdinfo->num_inputs;
1021 }
1022 return -EINVAL;
1023}
1024
1025/*
1026 * vpfe_get_app_input - Get app input index for a given subdev input index
1027 * driver stores the input index of the current sub device and translate it
1028 * when application request the current input
1029 */
1030static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1031 int *app_input_index)
1032{
1033 struct vpfe_config *cfg = vpfe_dev->cfg;
1034 struct vpfe_subdev_info *sdinfo;
1035 int i, j = 0;
1036
1037 for (i = 0; i < cfg->num_subdevs; i++) {
1038 sdinfo = &cfg->sub_devs[i];
1039 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1040 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1041 return -1;
1042 *app_input_index = j + vpfe_dev->current_input;
1043 return 0;
1044 }
1045 j += sdinfo->num_inputs;
1046 }
1047 return -EINVAL;
1048}
1049
1050static int vpfe_enum_input(struct file *file, void *priv,
1051 struct v4l2_input *inp)
1052{
1053 struct vpfe_device *vpfe_dev = video_drvdata(file);
1054 struct vpfe_subdev_info *sdinfo;
1055 int subdev, index ;
1056
1057 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1058
1059 if (vpfe_get_subdev_input_index(vpfe_dev,
1060 &subdev,
1061 &index,
1062 inp->index) < 0) {
1063 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found"
1064 " for the subdev\n");
1065 return -EINVAL;
1066 }
1067 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1068 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1069 return 0;
1070}
1071
1072static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1073{
1074 struct vpfe_device *vpfe_dev = video_drvdata(file);
1075
1076 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1077
1078 return vpfe_get_app_input_index(vpfe_dev, index);
1079}
1080
1081
1082static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1083{
1084 struct vpfe_device *vpfe_dev = video_drvdata(file);
1085 struct vpfe_subdev_info *sdinfo;
1086 int subdev_index, inp_index;
1087 struct vpfe_route *route;
1088 u32 input = 0, output = 0;
1089 int ret = -EINVAL;
1090
1091 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1092
1093 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1094 if (ret)
1095 return ret;
1096
1097 /*
1098 * If streaming is started return device busy
1099 * error
1100 */
1101 if (vpfe_dev->started) {
1102 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1103 ret = -EBUSY;
1104 goto unlock_out;
1105 }
1106
1107 if (vpfe_get_subdev_input_index(vpfe_dev,
1108 &subdev_index,
1109 &inp_index,
1110 index) < 0) {
1111 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1112 goto unlock_out;
1113 }
1114
1115 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
1116 route = &sdinfo->routes[inp_index];
1117 if (route && sdinfo->can_route) {
1118 input = route->input;
1119 output = route->output;
1120 }
1121
1122 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1123 video, s_routing, input, output, 0);
1124
1125 if (ret) {
1126 v4l2_err(&vpfe_dev->v4l2_dev,
1127 "vpfe_doioctl:error in setting input in decoder\n");
1128 ret = -EINVAL;
1129 goto unlock_out;
1130 }
1131 vpfe_dev->current_subdev = sdinfo;
1132 vpfe_dev->current_input = index;
1133 vpfe_dev->std_index = 0;
1134
1135 /* set the bus/interface parameter for the sub device in ccdc */
1136 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1137 if (ret)
1138 goto unlock_out;
1139
1140 /* set the default image parameters in the device */
1141 ret = vpfe_config_image_format(vpfe_dev,
1142 &vpfe_standards[vpfe_dev->std_index].std_id);
1143unlock_out:
1144 mutex_unlock(&vpfe_dev->lock);
1145 return ret;
1146}
1147
1148static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1149{
1150 struct vpfe_device *vpfe_dev = video_drvdata(file);
1151 struct vpfe_subdev_info *sdinfo;
1152 int ret = 0;
1153
1154 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1155
1156 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1157 sdinfo = vpfe_dev->current_subdev;
1158 if (ret)
1159 return ret;
1160 /* Call querystd function of decoder device */
1161 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1162 video, querystd, std_id);
1163 mutex_unlock(&vpfe_dev->lock);
1164 return ret;
1165}
1166
1167static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1168{
1169 struct vpfe_device *vpfe_dev = video_drvdata(file);
1170 struct vpfe_subdev_info *sdinfo;
1171 int ret = 0;
1172
1173 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1174
1175 /* Call decoder driver function to set the standard */
1176 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1177 if (ret)
1178 return ret;
1179
1180 sdinfo = vpfe_dev->current_subdev;
1181 /* If streaming is started, return device busy error */
1182 if (vpfe_dev->started) {
1183 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1184 ret = -EBUSY;
1185 goto unlock_out;
1186 }
1187
1188 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1189 core, s_std, *std_id);
1190 if (ret < 0) {
1191 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1192 goto unlock_out;
1193 }
1194 ret = vpfe_config_image_format(vpfe_dev, std_id);
1195
1196unlock_out:
1197 mutex_unlock(&vpfe_dev->lock);
1198 return ret;
1199}
1200
1201static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1202{
1203 struct vpfe_device *vpfe_dev = video_drvdata(file);
1204
1205 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1206
1207 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1208 return 0;
1209}
1210/*
1211 * Videobuf operations
1212 */
1213static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1214 unsigned int *count,
1215 unsigned int *size)
1216{
1217 struct vpfe_fh *fh = vq->priv_data;
1218 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1219
1220 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
1221 *size = config_params.device_bufsize;
1222
1223 if (*count < config_params.min_numbuffers)
1224 *count = config_params.min_numbuffers;
1225 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1226 "count=%d, size=%d\n", *count, *size);
1227 return 0;
1228}
1229
1230static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1231 struct videobuf_buffer *vb,
1232 enum v4l2_field field)
1233{
1234 struct vpfe_fh *fh = vq->priv_data;
1235 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1236
1237 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1238
1239 /* If buffer is not initialized, initialize it */
1240 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1241 vb->width = vpfe_dev->fmt.fmt.pix.width;
1242 vb->height = vpfe_dev->fmt.fmt.pix.height;
1243 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1244 vb->field = field;
1245 }
1246 vb->state = VIDEOBUF_PREPARED;
1247 return 0;
1248}
1249
1250static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1251 struct videobuf_buffer *vb)
1252{
1253 /* Get the file handle object and device object */
1254 struct vpfe_fh *fh = vq->priv_data;
1255 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1256 unsigned long flags;
1257
1258 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1259
1260 /* add the buffer to the DMA queue */
1261 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1262 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1263 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1264
1265 /* Change state of the buffer */
1266 vb->state = VIDEOBUF_QUEUED;
1267}
1268
1269static void vpfe_videobuf_release(struct videobuf_queue *vq,
1270 struct videobuf_buffer *vb)
1271{
1272 struct vpfe_fh *fh = vq->priv_data;
1273 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1274 unsigned long flags;
1275
1276 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1277
1278 /*
1279 * We need to flush the buffer from the dma queue since
1280 * they are de-allocated
1281 */
1282 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1283 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1284 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1285 videobuf_dma_contig_free(vq, vb);
1286 vb->state = VIDEOBUF_NEEDS_INIT;
1287}
1288
1289static struct videobuf_queue_ops vpfe_videobuf_qops = {
1290 .buf_setup = vpfe_videobuf_setup,
1291 .buf_prepare = vpfe_videobuf_prepare,
1292 .buf_queue = vpfe_videobuf_queue,
1293 .buf_release = vpfe_videobuf_release,
1294};
1295
1296/*
1297 * vpfe_reqbufs. currently support REQBUF only once opening
1298 * the device.
1299 */
1300static int vpfe_reqbufs(struct file *file, void *priv,
1301 struct v4l2_requestbuffers *req_buf)
1302{
1303 struct vpfe_device *vpfe_dev = video_drvdata(file);
1304 struct vpfe_fh *fh = file->private_data;
1305 int ret = 0;
1306
1307 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1308
1309 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1310 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1311 return -EINVAL;
1312 }
1313
1314 if (V4L2_MEMORY_USERPTR == req_buf->memory) {
1315 /* we don't support user ptr IO */
1316 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs:"
1317 " USERPTR IO not supported\n");
1318 return -EINVAL;
1319 }
1320
1321 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1322 if (ret)
1323 return ret;
1324
1325 if (vpfe_dev->io_usrs != 0) {
1326 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1327 ret = -EBUSY;
1328 goto unlock_out;
1329 }
1330
1331 vpfe_dev->memory = req_buf->memory;
1332 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1333 &vpfe_videobuf_qops,
Vaibhav Hiremath204e6ea2009-11-09 09:07:36 -03001334 vpfe_dev->pdev,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001335 &vpfe_dev->irqlock,
1336 req_buf->type,
1337 vpfe_dev->fmt.fmt.pix.field,
1338 sizeof(struct videobuf_buffer),
1339 fh);
1340
1341 fh->io_allowed = 1;
1342 vpfe_dev->io_usrs = 1;
1343 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1344 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1345unlock_out:
1346 mutex_unlock(&vpfe_dev->lock);
1347 return ret;
1348}
1349
1350static int vpfe_querybuf(struct file *file, void *priv,
1351 struct v4l2_buffer *buf)
1352{
1353 struct vpfe_device *vpfe_dev = video_drvdata(file);
1354
1355 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1356
1357 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1358 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1359 return -EINVAL;
1360 }
1361
1362 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1363 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1364 return -EINVAL;
1365 }
1366 /* Call videobuf_querybuf to get information */
1367 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1368}
1369
1370static int vpfe_qbuf(struct file *file, void *priv,
1371 struct v4l2_buffer *p)
1372{
1373 struct vpfe_device *vpfe_dev = video_drvdata(file);
1374 struct vpfe_fh *fh = file->private_data;
1375
1376 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1377
1378 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1379 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1380 return -EINVAL;
1381 }
1382
1383 /*
1384 * If this file handle is not allowed to do IO,
1385 * return error
1386 */
1387 if (!fh->io_allowed) {
1388 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1389 return -EACCES;
1390 }
1391 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1392}
1393
1394static int vpfe_dqbuf(struct file *file, void *priv,
1395 struct v4l2_buffer *buf)
1396{
1397 struct vpfe_device *vpfe_dev = video_drvdata(file);
1398
1399 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1400
1401 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1402 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1403 return -EINVAL;
1404 }
1405 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1406 buf, file->f_flags & O_NONBLOCK);
1407}
1408
Vaibhav Hiremathd73bfc52009-11-10 13:12:25 -03001409static int vpfe_queryctrl(struct file *file, void *priv,
1410 struct v4l2_queryctrl *qctrl)
1411{
1412 struct vpfe_device *vpfe_dev = video_drvdata(file);
1413 struct vpfe_subdev_info *sdinfo;
1414
1415 sdinfo = vpfe_dev->current_subdev;
1416
1417 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1418 core, queryctrl, qctrl);
1419
1420}
1421
1422static int vpfe_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1423{
1424 struct vpfe_device *vpfe_dev = video_drvdata(file);
1425 struct vpfe_subdev_info *sdinfo;
1426
1427 sdinfo = vpfe_dev->current_subdev;
1428
1429 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1430 core, g_ctrl, ctrl);
1431}
1432
1433static int vpfe_s_ctrl(struct file *file, void *priv, struct v4l2_control *ctrl)
1434{
1435 struct vpfe_device *vpfe_dev = video_drvdata(file);
1436 struct vpfe_subdev_info *sdinfo;
1437
1438 sdinfo = vpfe_dev->current_subdev;
1439
1440 return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1441 core, s_ctrl, ctrl);
1442}
1443
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001444/*
1445 * vpfe_calculate_offsets : This function calculates buffers offset
1446 * for top and bottom field
1447 */
1448static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1449{
1450 struct v4l2_rect image_win;
1451
1452 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1453
1454 ccdc_dev->hw_ops.get_image_window(&image_win);
1455 vpfe_dev->field_off = image_win.height * image_win.width;
1456}
1457
1458/* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1459static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1460{
1461 ccdc_dev->hw_ops.enable(1);
1462 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1463 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1464 vpfe_dev->started = 1;
1465}
1466
1467/*
1468 * vpfe_streamon. Assume the DMA queue is not empty.
1469 * application is expected to call QBUF before calling
1470 * this ioctl. If not, driver returns error
1471 */
1472static int vpfe_streamon(struct file *file, void *priv,
1473 enum v4l2_buf_type buf_type)
1474{
1475 struct vpfe_device *vpfe_dev = video_drvdata(file);
1476 struct vpfe_fh *fh = file->private_data;
1477 struct vpfe_subdev_info *sdinfo;
1478 unsigned long addr;
1479 int ret = 0;
1480
1481 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1482
1483 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1484 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1485 return -EINVAL;
1486 }
1487
1488 /* If file handle is not allowed IO, return error */
1489 if (!fh->io_allowed) {
1490 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1491 return -EACCES;
1492 }
1493
1494 sdinfo = vpfe_dev->current_subdev;
1495 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1496 video, s_stream, 1);
1497
1498 if (ret && (ret != -ENOIOCTLCMD)) {
1499 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1500 return -EINVAL;
1501 }
1502
1503 /* If buffer queue is empty, return error */
1504 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1505 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1506 return -EIO;
1507 }
1508
1509 /* Call videobuf_streamon to start streaming * in videobuf */
1510 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1511 if (ret)
1512 return ret;
1513
1514
1515 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1516 if (ret)
1517 goto streamoff;
1518 /* Get the next frame from the buffer queue */
1519 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1520 struct videobuf_buffer, queue);
1521 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1522 /* Remove buffer from the buffer queue */
1523 list_del(&vpfe_dev->cur_frm->queue);
1524 /* Mark state of the current frame to active */
1525 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1526 /* Initialize field_id and started member */
1527 vpfe_dev->field_id = 0;
1528 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1529
1530 /* Calculate field offset */
1531 vpfe_calculate_offsets(vpfe_dev);
1532
1533 if (vpfe_attach_irq(vpfe_dev) < 0) {
1534 v4l2_err(&vpfe_dev->v4l2_dev,
1535 "Error in attaching interrupt handle\n");
1536 ret = -EFAULT;
1537 goto unlock_out;
1538 }
1539 if (ccdc_dev->hw_ops.configure() < 0) {
1540 v4l2_err(&vpfe_dev->v4l2_dev,
1541 "Error in configuring ccdc\n");
1542 ret = -EINVAL;
1543 goto unlock_out;
1544 }
1545 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1546 vpfe_start_ccdc_capture(vpfe_dev);
1547 mutex_unlock(&vpfe_dev->lock);
1548 return ret;
1549unlock_out:
1550 mutex_unlock(&vpfe_dev->lock);
1551streamoff:
1552 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1553 return ret;
1554}
1555
1556static int vpfe_streamoff(struct file *file, void *priv,
1557 enum v4l2_buf_type buf_type)
1558{
1559 struct vpfe_device *vpfe_dev = video_drvdata(file);
1560 struct vpfe_fh *fh = file->private_data;
1561 struct vpfe_subdev_info *sdinfo;
1562 int ret = 0;
1563
1564 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1565
1566 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1567 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1568 return -EINVAL;
1569 }
1570
1571 /* If io is allowed for this file handle, return error */
1572 if (!fh->io_allowed) {
1573 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1574 return -EACCES;
1575 }
1576
1577 /* If streaming is not started, return error */
1578 if (!vpfe_dev->started) {
1579 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1580 return -EINVAL;
1581 }
1582
1583 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1584 if (ret)
1585 return ret;
1586
1587 vpfe_stop_ccdc_capture(vpfe_dev);
1588 vpfe_detach_irq(vpfe_dev);
1589
1590 sdinfo = vpfe_dev->current_subdev;
1591 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1592 video, s_stream, 0);
1593
1594 if (ret && (ret != -ENOIOCTLCMD))
1595 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1596 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1597 mutex_unlock(&vpfe_dev->lock);
1598 return ret;
1599}
1600
1601static int vpfe_cropcap(struct file *file, void *priv,
1602 struct v4l2_cropcap *crop)
1603{
1604 struct vpfe_device *vpfe_dev = video_drvdata(file);
1605
1606 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1607
Roel Kluin0b66cf92009-11-04 14:16:25 -03001608 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001609 return -EINVAL;
1610
1611 memset(crop, 0, sizeof(struct v4l2_cropcap));
1612 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1613 crop->bounds.width = crop->defrect.width =
1614 vpfe_standards[vpfe_dev->std_index].width;
1615 crop->bounds.height = crop->defrect.height =
1616 vpfe_standards[vpfe_dev->std_index].height;
1617 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1618 return 0;
1619}
1620
1621static int vpfe_g_crop(struct file *file, void *priv,
1622 struct v4l2_crop *crop)
1623{
1624 struct vpfe_device *vpfe_dev = video_drvdata(file);
1625
1626 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_crop\n");
1627
1628 crop->c = vpfe_dev->crop;
1629 return 0;
1630}
1631
1632static int vpfe_s_crop(struct file *file, void *priv,
1633 struct v4l2_crop *crop)
1634{
1635 struct vpfe_device *vpfe_dev = video_drvdata(file);
1636 int ret = 0;
1637
1638 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_crop\n");
1639
1640 if (vpfe_dev->started) {
1641 /* make sure streaming is not started */
1642 v4l2_err(&vpfe_dev->v4l2_dev,
1643 "Cannot change crop when streaming is ON\n");
1644 return -EBUSY;
1645 }
1646
1647 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1648 if (ret)
1649 return ret;
1650
1651 if (crop->c.top < 0 || crop->c.left < 0) {
1652 v4l2_err(&vpfe_dev->v4l2_dev,
1653 "doesn't support negative values for top & left\n");
1654 ret = -EINVAL;
1655 goto unlock_out;
1656 }
1657
1658 /* adjust the width to 16 pixel boundry */
1659 crop->c.width = ((crop->c.width + 15) & ~0xf);
1660
1661 /* make sure parameters are valid */
1662 if ((crop->c.left + crop->c.width >
1663 vpfe_dev->std_info.active_pixels) ||
1664 (crop->c.top + crop->c.height >
1665 vpfe_dev->std_info.active_lines)) {
1666 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_CROP params\n");
1667 ret = -EINVAL;
1668 goto unlock_out;
1669 }
1670 ccdc_dev->hw_ops.set_image_window(&crop->c);
1671 vpfe_dev->fmt.fmt.pix.width = crop->c.width;
1672 vpfe_dev->fmt.fmt.pix.height = crop->c.height;
1673 vpfe_dev->fmt.fmt.pix.bytesperline =
1674 ccdc_dev->hw_ops.get_line_length();
1675 vpfe_dev->fmt.fmt.pix.sizeimage =
1676 vpfe_dev->fmt.fmt.pix.bytesperline *
1677 vpfe_dev->fmt.fmt.pix.height;
1678 vpfe_dev->crop = crop->c;
1679unlock_out:
1680 mutex_unlock(&vpfe_dev->lock);
1681 return ret;
1682}
1683
1684
1685static long vpfe_param_handler(struct file *file, void *priv,
1686 int cmd, void *param)
1687{
1688 struct vpfe_device *vpfe_dev = video_drvdata(file);
1689 int ret = 0;
1690
1691 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
1692
1693 if (vpfe_dev->started) {
1694 /* only allowed if streaming is not started */
1695 v4l2_err(&vpfe_dev->v4l2_dev, "device already started\n");
1696 return -EBUSY;
1697 }
1698
1699 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1700 if (ret)
1701 return ret;
1702
1703 switch (cmd) {
1704 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1705 v4l2_warn(&vpfe_dev->v4l2_dev,
1706 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
1707 ret = ccdc_dev->hw_ops.set_params(param);
1708 if (ret) {
1709 v4l2_err(&vpfe_dev->v4l2_dev,
1710 "Error in setting parameters in CCDC\n");
1711 goto unlock_out;
1712 }
1713 if (vpfe_get_ccdc_image_format(vpfe_dev, &vpfe_dev->fmt) < 0) {
1714 v4l2_err(&vpfe_dev->v4l2_dev,
1715 "Invalid image format at CCDC\n");
1716 goto unlock_out;
1717 }
1718 break;
1719 default:
1720 ret = -EINVAL;
1721 }
1722unlock_out:
1723 mutex_unlock(&vpfe_dev->lock);
1724 return ret;
1725}
1726
1727
1728/* vpfe capture ioctl operations */
1729static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1730 .vidioc_querycap = vpfe_querycap,
1731 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1732 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1733 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1734 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1735 .vidioc_enum_input = vpfe_enum_input,
1736 .vidioc_g_input = vpfe_g_input,
1737 .vidioc_s_input = vpfe_s_input,
1738 .vidioc_querystd = vpfe_querystd,
1739 .vidioc_s_std = vpfe_s_std,
1740 .vidioc_g_std = vpfe_g_std,
Vaibhav Hiremathd73bfc52009-11-10 13:12:25 -03001741 .vidioc_queryctrl = vpfe_queryctrl,
1742 .vidioc_g_ctrl = vpfe_g_ctrl,
1743 .vidioc_s_ctrl = vpfe_s_ctrl,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001744 .vidioc_reqbufs = vpfe_reqbufs,
1745 .vidioc_querybuf = vpfe_querybuf,
1746 .vidioc_qbuf = vpfe_qbuf,
1747 .vidioc_dqbuf = vpfe_dqbuf,
1748 .vidioc_streamon = vpfe_streamon,
1749 .vidioc_streamoff = vpfe_streamoff,
1750 .vidioc_cropcap = vpfe_cropcap,
1751 .vidioc_g_crop = vpfe_g_crop,
1752 .vidioc_s_crop = vpfe_s_crop,
1753 .vidioc_default = vpfe_param_handler,
1754};
1755
1756static struct vpfe_device *vpfe_initialize(void)
1757{
1758 struct vpfe_device *vpfe_dev;
1759
1760 /* Default number of buffers should be 3 */
1761 if ((numbuffers > 0) &&
1762 (numbuffers < config_params.min_numbuffers))
1763 numbuffers = config_params.min_numbuffers;
1764
1765 /*
1766 * Set buffer size to min buffers size if invalid buffer size is
1767 * given
1768 */
1769 if (bufsize < config_params.min_bufsize)
1770 bufsize = config_params.min_bufsize;
1771
1772 config_params.numbuffers = numbuffers;
1773
1774 if (numbuffers)
1775 config_params.device_bufsize = bufsize;
1776
1777 /* Allocate memory for device objects */
1778 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1779
1780 return vpfe_dev;
1781}
1782
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001783/*
1784 * vpfe_probe : This function creates device entries by register
1785 * itself to the V4L2 driver and initializes fields of each
1786 * device objects
1787 */
1788static __init int vpfe_probe(struct platform_device *pdev)
1789{
1790 struct vpfe_subdev_info *sdinfo;
1791 struct vpfe_config *vpfe_cfg;
1792 struct resource *res1;
1793 struct vpfe_device *vpfe_dev;
1794 struct i2c_adapter *i2c_adap;
1795 struct video_device *vfd;
1796 int ret = -ENOMEM, i, j;
1797 int num_subdevs = 0;
1798
1799 /* Get the pointer to the device object */
1800 vpfe_dev = vpfe_initialize();
1801
1802 if (!vpfe_dev) {
1803 v4l2_err(pdev->dev.driver,
1804 "Failed to allocate memory for vpfe_dev\n");
1805 return ret;
1806 }
1807
1808 vpfe_dev->pdev = &pdev->dev;
1809
1810 if (NULL == pdev->dev.platform_data) {
1811 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001812 ret = -ENODEV;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001813 goto probe_free_dev_mem;
1814 }
1815
1816 vpfe_cfg = pdev->dev.platform_data;
1817 vpfe_dev->cfg = vpfe_cfg;
1818 if (NULL == vpfe_cfg->ccdc ||
1819 NULL == vpfe_cfg->card_name ||
1820 NULL == vpfe_cfg->sub_devs) {
1821 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1822 ret = -ENOENT;
1823 goto probe_free_dev_mem;
1824 }
1825
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001826 mutex_lock(&ccdc_lock);
1827 /* Allocate memory for ccdc configuration */
1828 ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1829 if (NULL == ccdc_cfg) {
1830 v4l2_err(pdev->dev.driver,
1831 "Memory allocation failed for ccdc_cfg\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001832 goto probe_free_dev_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001833 }
1834
1835 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1836 /* Get VINT0 irq resource */
1837 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1838 if (!res1) {
1839 v4l2_err(pdev->dev.driver,
1840 "Unable to get interrupt for VINT0\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001841 ret = -ENODEV;
1842 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001843 }
1844 vpfe_dev->ccdc_irq0 = res1->start;
1845
1846 /* Get VINT1 irq resource */
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001847 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001848 if (!res1) {
1849 v4l2_err(pdev->dev.driver,
1850 "Unable to get interrupt for VINT1\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001851 ret = -ENODEV;
1852 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001853 }
1854 vpfe_dev->ccdc_irq1 = res1->start;
1855
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001856 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
1857 "vpfe_capture0", vpfe_dev);
1858
1859 if (0 != ret) {
1860 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001861 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001862 }
1863
1864 /* Allocate memory for video device */
1865 vfd = video_device_alloc();
1866 if (NULL == vfd) {
1867 ret = -ENOMEM;
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001868 v4l2_err(pdev->dev.driver, "Unable to alloc video device\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001869 goto probe_out_release_irq;
1870 }
1871
1872 /* Initialize field of video device */
1873 vfd->release = video_device_release;
1874 vfd->fops = &vpfe_fops;
1875 vfd->ioctl_ops = &vpfe_ioctl_ops;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001876 vfd->tvnorms = 0;
1877 vfd->current_norm = V4L2_STD_PAL;
1878 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1879 snprintf(vfd->name, sizeof(vfd->name),
1880 "%s_V%d.%d.%d",
1881 CAPTURE_DRV_NAME,
1882 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1883 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1884 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
1885 /* Set video_dev to the video device */
1886 vpfe_dev->video_dev = vfd;
1887
1888 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1889 if (ret) {
1890 v4l2_err(pdev->dev.driver,
1891 "Unable to register v4l2 device.\n");
1892 goto probe_out_video_release;
1893 }
1894 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1895 spin_lock_init(&vpfe_dev->irqlock);
1896 spin_lock_init(&vpfe_dev->dma_queue_lock);
1897 mutex_init(&vpfe_dev->lock);
1898
1899 /* Initialize field of the device objects */
1900 vpfe_dev->numbuffers = config_params.numbuffers;
1901
1902 /* Initialize prio member of device object */
1903 v4l2_prio_init(&vpfe_dev->prio);
1904 /* register video device */
1905 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1906 "trying to register vpfe device.\n");
1907 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1908 "video_dev=%x\n", (int)&vpfe_dev->video_dev);
1909 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1910 ret = video_register_device(vpfe_dev->video_dev,
1911 VFL_TYPE_GRABBER, -1);
1912
1913 if (ret) {
1914 v4l2_err(pdev->dev.driver,
1915 "Unable to register video device.\n");
1916 goto probe_out_v4l2_unregister;
1917 }
1918
1919 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1920 /* set the driver data in platform device */
1921 platform_set_drvdata(pdev, vpfe_dev);
1922 /* set driver private data */
1923 video_set_drvdata(vpfe_dev->video_dev, vpfe_dev);
Vaibhav Hiremath14cbaaf2009-11-09 09:14:16 -03001924 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001925 num_subdevs = vpfe_cfg->num_subdevs;
1926 vpfe_dev->sd = kmalloc(sizeof(struct v4l2_subdev *) * num_subdevs,
1927 GFP_KERNEL);
1928 if (NULL == vpfe_dev->sd) {
1929 v4l2_err(&vpfe_dev->v4l2_dev,
1930 "unable to allocate memory for subdevice pointers\n");
1931 ret = -ENOMEM;
1932 goto probe_out_video_unregister;
1933 }
1934
1935 for (i = 0; i < num_subdevs; i++) {
1936 struct v4l2_input *inps;
1937
1938 sdinfo = &vpfe_cfg->sub_devs[i];
1939
1940 /* Load up the subdevice */
1941 vpfe_dev->sd[i] =
1942 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1943 i2c_adap,
1944 sdinfo->name,
1945 &sdinfo->board_info,
1946 NULL);
1947 if (vpfe_dev->sd[i]) {
1948 v4l2_info(&vpfe_dev->v4l2_dev,
1949 "v4l2 sub device %s registered\n",
1950 sdinfo->name);
1951 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1952 /* update tvnorms from the sub devices */
1953 for (j = 0; j < sdinfo->num_inputs; j++) {
1954 inps = &sdinfo->inputs[j];
1955 vfd->tvnorms |= inps->std;
1956 }
1957 } else {
1958 v4l2_info(&vpfe_dev->v4l2_dev,
1959 "v4l2 sub device %s register fails\n",
1960 sdinfo->name);
1961 goto probe_sd_out;
1962 }
1963 }
1964
1965 /* set first sub device as current one */
1966 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
1967
1968 /* We have at least one sub device to work with */
1969 mutex_unlock(&ccdc_lock);
1970 return 0;
1971
1972probe_sd_out:
1973 kfree(vpfe_dev->sd);
1974probe_out_video_unregister:
1975 video_unregister_device(vpfe_dev->video_dev);
1976probe_out_v4l2_unregister:
1977 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
1978probe_out_video_release:
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001979 if (!video_is_registered(vpfe_dev->video_dev))
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001980 video_device_release(vpfe_dev->video_dev);
1981probe_out_release_irq:
1982 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001983probe_free_ccdc_cfg_mem:
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001984 mutex_unlock(&ccdc_lock);
1985 kfree(ccdc_cfg);
1986probe_free_dev_mem:
1987 kfree(vpfe_dev);
1988 return ret;
1989}
1990
1991/*
1992 * vpfe_remove : It un-register device from V4L2 driver
1993 */
Uwe Kleine-König9d893822009-12-10 17:01:13 -03001994static int __devexit vpfe_remove(struct platform_device *pdev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001995{
1996 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001997
1998 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
1999
2000 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2001 kfree(vpfe_dev->sd);
2002 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
2003 video_unregister_device(vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002004 kfree(vpfe_dev);
2005 kfree(ccdc_cfg);
2006 return 0;
2007}
2008
2009static int
2010vpfe_suspend(struct device *dev)
2011{
2012 /* add suspend code here later */
2013 return -1;
2014}
2015
2016static int
2017vpfe_resume(struct device *dev)
2018{
2019 /* add resume code here later */
2020 return -1;
2021}
2022
Alexey Dobriyan47145212009-12-14 18:00:08 -08002023static const struct dev_pm_ops vpfe_dev_pm_ops = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002024 .suspend = vpfe_suspend,
2025 .resume = vpfe_resume,
2026};
2027
2028static struct platform_driver vpfe_driver = {
2029 .driver = {
2030 .name = CAPTURE_DRV_NAME,
2031 .owner = THIS_MODULE,
2032 .pm = &vpfe_dev_pm_ops,
2033 },
2034 .probe = vpfe_probe,
2035 .remove = __devexit_p(vpfe_remove),
2036};
2037
2038static __init int vpfe_init(void)
2039{
2040 printk(KERN_NOTICE "vpfe_init\n");
2041 /* Register driver to the kernel */
2042 return platform_driver_register(&vpfe_driver);
2043}
2044
2045/*
2046 * vpfe_cleanup : This function un-registers device driver
2047 */
2048static void vpfe_cleanup(void)
2049{
2050 platform_driver_unregister(&vpfe_driver);
2051}
2052
2053module_init(vpfe_init);
2054module_exit(vpfe_cleanup);