]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/platform/davinci/vpfe_capture.c
[media] DaVinci-VPFE-Capture: Use kmalloc_array() in vpfe_probe()
[mirror_ubuntu-artful-kernel.git] / drivers / media / platform / 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
47c0b565 128static const struct vpfe_standard vpfe_standards[] = {
7da8a6cb
MK
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.
da298c6d 373 * It then checks if sub device supports get_fmt and then override the
7da8a6cb
MK
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,
314527ac 379 v4l2_std_id std_id)
7da8a6cb
MK
380{
381 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
da298c6d
HV
382 struct v4l2_subdev_format fmt = {
383 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
384 };
385 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
d3ca7759 386 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
7da8a6cb
MK
387 int i, ret = 0;
388
389 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
314527ac 390 if (vpfe_standards[i].std_id & std_id) {
7da8a6cb
MK
391 vpfe_dev->std_info.active_pixels =
392 vpfe_standards[i].width;
393 vpfe_dev->std_info.active_lines =
394 vpfe_standards[i].height;
395 vpfe_dev->std_info.frame_format =
396 vpfe_standards[i].frame_format;
397 vpfe_dev->std_index = i;
398 break;
399 }
400 }
401
402 if (i == ARRAY_SIZE(vpfe_standards)) {
403 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
404 return -EINVAL;
405 }
406
407 vpfe_dev->crop.top = 0;
408 vpfe_dev->crop.left = 0;
409 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
410 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
d3ca7759
HV
411 pix->width = vpfe_dev->crop.width;
412 pix->height = vpfe_dev->crop.height;
7da8a6cb
MK
413
414 /* first field and frame format based on standard frame format */
415 if (vpfe_dev->std_info.frame_format) {
d3ca7759 416 pix->field = V4L2_FIELD_INTERLACED;
7da8a6cb 417 /* assume V4L2_PIX_FMT_UYVY as default */
d3ca7759 418 pix->pixelformat = V4L2_PIX_FMT_UYVY;
da298c6d 419 v4l2_fill_mbus_format(mbus_fmt, pix,
27ffaeb0 420 MEDIA_BUS_FMT_YUYV10_2X10);
7da8a6cb 421 } else {
d3ca7759 422 pix->field = V4L2_FIELD_NONE;
7da8a6cb 423 /* assume V4L2_PIX_FMT_SBGGR8 */
d3ca7759 424 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
da298c6d 425 v4l2_fill_mbus_format(mbus_fmt, pix,
27ffaeb0 426 MEDIA_BUS_FMT_SBGGR8_1X8);
7da8a6cb
MK
427 }
428
da298c6d 429 /* if sub device supports get_fmt, override the defaults */
7da8a6cb 430 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
da298c6d 431 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
7da8a6cb
MK
432
433 if (ret && ret != -ENOIOCTLCMD) {
434 v4l2_err(&vpfe_dev->v4l2_dev,
da298c6d 435 "error in getting get_fmt from sub device\n");
7da8a6cb
MK
436 return ret;
437 }
da298c6d 438 v4l2_fill_pix_format(pix, mbus_fmt);
d3ca7759
HV
439 pix->bytesperline = pix->width * 2;
440 pix->sizeimage = pix->bytesperline * pix->height;
7da8a6cb
MK
441
442 /* Sets the values in CCDC */
443 ret = vpfe_config_ccdc_image_format(vpfe_dev);
444 if (ret)
445 return ret;
446
447 /* Update the values of sizeimage and bytesperline */
692f6375
DC
448 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
449 pix->sizeimage = pix->bytesperline * pix->height;
450
451 return 0;
7da8a6cb
MK
452}
453
454static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
455{
456 int ret = 0;
457
458 /* set first input of current subdevice as the current input */
459 vpfe_dev->current_input = 0;
460
461 /* set default standard */
462 vpfe_dev->std_index = 0;
463
464 /* Configure the default format information */
465 ret = vpfe_config_image_format(vpfe_dev,
314527ac 466 vpfe_standards[vpfe_dev->std_index].std_id);
7da8a6cb
MK
467 if (ret)
468 return ret;
469
470 /* now open the ccdc device to initialize it */
471 mutex_lock(&ccdc_lock);
472 if (NULL == ccdc_dev) {
473 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
474 ret = -ENODEV;
475 goto unlock;
476 }
477
478 if (!try_module_get(ccdc_dev->owner)) {
479 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
480 ret = -ENODEV;
481 goto unlock;
482 }
483 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
484 if (!ret)
485 vpfe_dev->initialized = 1;
085b54a2
VH
486
487 /* Clear all VPFE/CCDC interrupts */
488 if (vpfe_dev->cfg->clr_intr)
489 vpfe_dev->cfg->clr_intr(-1);
490
7da8a6cb
MK
491unlock:
492 mutex_unlock(&ccdc_lock);
493 return ret;
494}
495
496/*
497 * vpfe_open : It creates object of file handle structure and
498 * stores it in private_data member of filepointer
499 */
500static int vpfe_open(struct file *file)
501{
502 struct vpfe_device *vpfe_dev = video_drvdata(file);
3bdaa382 503 struct video_device *vdev = video_devdata(file);
7da8a6cb
MK
504 struct vpfe_fh *fh;
505
506 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
507
508 if (!vpfe_dev->cfg->num_subdevs) {
509 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
510 return -ENODEV;
511 }
512
513 /* Allocate memory for the file handle object */
514 fh = kmalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
515 if (NULL == fh) {
516 v4l2_err(&vpfe_dev->v4l2_dev,
517 "unable to allocate memory for file handle object\n");
518 return -ENOMEM;
519 }
520 /* store pointer to fh in private_data member of file */
521 file->private_data = fh;
522 fh->vpfe_dev = vpfe_dev;
3bdaa382 523 v4l2_fh_init(&fh->fh, vdev);
7da8a6cb
MK
524 mutex_lock(&vpfe_dev->lock);
525 /* If decoder is not initialized. initialize it */
526 if (!vpfe_dev->initialized) {
527 if (vpfe_initialize_device(vpfe_dev)) {
528 mutex_unlock(&vpfe_dev->lock);
529 return -ENODEV;
530 }
531 }
532 /* Increment device usrs counter */
533 vpfe_dev->usrs++;
534 /* Set io_allowed member to false */
535 fh->io_allowed = 0;
3bdaa382 536 v4l2_fh_add(&fh->fh);
7da8a6cb
MK
537 mutex_unlock(&vpfe_dev->lock);
538 return 0;
539}
540
541static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
542{
543 unsigned long addr;
544
545 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
546 struct videobuf_buffer, queue);
547 list_del(&vpfe_dev->next_frm->queue);
548 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
549 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
844cc0dc
VH
550
551 ccdc_dev->hw_ops.setfbaddr(addr);
552}
553
554static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
555{
556 unsigned long addr;
557
558 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
559 addr += vpfe_dev->field_off;
7da8a6cb
MK
560 ccdc_dev->hw_ops.setfbaddr(addr);
561}
562
563static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
564{
8e6057b5 565 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
7da8a6cb
MK
566 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
567 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
568 wake_up_interruptible(&vpfe_dev->cur_frm->done);
569 vpfe_dev->cur_frm = vpfe_dev->next_frm;
570}
571
572/* ISR for VINT0*/
573static irqreturn_t vpfe_isr(int irq, void *dev_id)
574{
575 struct vpfe_device *vpfe_dev = dev_id;
576 enum v4l2_field field;
7da8a6cb
MK
577 int fid;
578
579 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
580 field = vpfe_dev->fmt.fmt.pix.field;
581
582 /* if streaming not started, don't do anything */
583 if (!vpfe_dev->started)
085b54a2 584 goto clear_intr;
7da8a6cb
MK
585
586 /* only for 6446 this will be applicable */
587 if (NULL != ccdc_dev->hw_ops.reset)
588 ccdc_dev->hw_ops.reset();
589
590 if (field == V4L2_FIELD_NONE) {
591 /* handle progressive frame capture */
592 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
593 "frame format is progressive...\n");
594 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
595 vpfe_process_buffer_complete(vpfe_dev);
085b54a2 596 goto clear_intr;
7da8a6cb
MK
597 }
598
599 /* interlaced or TB capture check which field we are in hardware */
600 fid = ccdc_dev->hw_ops.getfid();
601
602 /* switch the software maintained field id */
603 vpfe_dev->field_id ^= 1;
604 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
605 fid, vpfe_dev->field_id);
606 if (fid == vpfe_dev->field_id) {
607 /* we are in-sync here,continue */
608 if (fid == 0) {
609 /*
610 * One frame is just being captured. If the next frame
611 * is available, release the current frame and move on
612 */
613 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
614 vpfe_process_buffer_complete(vpfe_dev);
615 /*
616 * based on whether the two fields are stored
617 * interleavely or separately in memory, reconfigure
618 * the CCDC memory address
619 */
620 if (field == V4L2_FIELD_SEQ_TB) {
844cc0dc 621 vpfe_schedule_bottom_field(vpfe_dev);
7da8a6cb 622 }
085b54a2 623 goto clear_intr;
7da8a6cb
MK
624 }
625 /*
626 * if one field is just being captured configure
627 * the next frame get the next frame from the empty
628 * queue if no frame is available hold on to the
629 * current buffer
630 */
631 spin_lock(&vpfe_dev->dma_queue_lock);
632 if (!list_empty(&vpfe_dev->dma_queue) &&
633 vpfe_dev->cur_frm == vpfe_dev->next_frm)
634 vpfe_schedule_next_buffer(vpfe_dev);
635 spin_unlock(&vpfe_dev->dma_queue_lock);
636 } else if (fid == 0) {
637 /*
638 * out of sync. Recover from any hardware out-of-sync.
639 * May loose one frame
640 */
641 vpfe_dev->field_id = fid;
642 }
085b54a2
VH
643clear_intr:
644 if (vpfe_dev->cfg->clr_intr)
645 vpfe_dev->cfg->clr_intr(irq);
646
7da8a6cb
MK
647 return IRQ_HANDLED;
648}
649
650/* vdint1_isr - isr handler for VINT1 interrupt */
651static irqreturn_t vdint1_isr(int irq, void *dev_id)
652{
653 struct vpfe_device *vpfe_dev = dev_id;
654
655 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
656
657 /* if streaming not started, don't do anything */
085b54a2
VH
658 if (!vpfe_dev->started) {
659 if (vpfe_dev->cfg->clr_intr)
660 vpfe_dev->cfg->clr_intr(irq);
7da8a6cb 661 return IRQ_HANDLED;
085b54a2 662 }
7da8a6cb
MK
663
664 spin_lock(&vpfe_dev->dma_queue_lock);
665 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
666 !list_empty(&vpfe_dev->dma_queue) &&
667 vpfe_dev->cur_frm == vpfe_dev->next_frm)
668 vpfe_schedule_next_buffer(vpfe_dev);
669 spin_unlock(&vpfe_dev->dma_queue_lock);
085b54a2
VH
670
671 if (vpfe_dev->cfg->clr_intr)
672 vpfe_dev->cfg->clr_intr(irq);
673
7da8a6cb
MK
674 return IRQ_HANDLED;
675}
676
677static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
678{
679 enum ccdc_frmfmt frame_format;
680
681 frame_format = ccdc_dev->hw_ops.get_frame_format();
682 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1ead696b 683 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
7da8a6cb
MK
684}
685
686static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
687{
688 enum ccdc_frmfmt frame_format;
689
690 frame_format = ccdc_dev->hw_ops.get_frame_format();
691 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
692 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
d8c279a0 693 0, "vpfe_capture1",
7da8a6cb
MK
694 vpfe_dev);
695 }
696 return 0;
697}
698
699/* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
700static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
701{
702 vpfe_dev->started = 0;
703 ccdc_dev->hw_ops.enable(0);
704 if (ccdc_dev->hw_ops.enable_out_to_sdram)
705 ccdc_dev->hw_ops.enable_out_to_sdram(0);
706}
707
708/*
709 * vpfe_release : This function deletes buffer queue, frees the
710 * buffers and the vpfe file handle
711 */
712static int vpfe_release(struct file *file)
713{
714 struct vpfe_device *vpfe_dev = video_drvdata(file);
715 struct vpfe_fh *fh = file->private_data;
716 struct vpfe_subdev_info *sdinfo;
717 int ret;
718
719 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
720
721 /* Get the device lock */
722 mutex_lock(&vpfe_dev->lock);
723 /* if this instance is doing IO */
724 if (fh->io_allowed) {
725 if (vpfe_dev->started) {
726 sdinfo = vpfe_dev->current_subdev;
727 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
728 sdinfo->grp_id,
729 video, s_stream, 0);
730 if (ret && (ret != -ENOIOCTLCMD))
731 v4l2_err(&vpfe_dev->v4l2_dev,
732 "stream off failed in subdev\n");
733 vpfe_stop_ccdc_capture(vpfe_dev);
734 vpfe_detach_irq(vpfe_dev);
735 videobuf_streamoff(&vpfe_dev->buffer_queue);
736 }
737 vpfe_dev->io_usrs = 0;
738 vpfe_dev->numbuffers = config_params.numbuffers;
c1d1e40b
LP
739 videobuf_stop(&vpfe_dev->buffer_queue);
740 videobuf_mmap_free(&vpfe_dev->buffer_queue);
7da8a6cb
MK
741 }
742
743 /* Decrement device usrs counter */
744 vpfe_dev->usrs--;
3bdaa382
LP
745 v4l2_fh_del(&fh->fh);
746 v4l2_fh_exit(&fh->fh);
7da8a6cb
MK
747 /* If this is the last file handle */
748 if (!vpfe_dev->usrs) {
749 vpfe_dev->initialized = 0;
750 if (ccdc_dev->hw_ops.close)
751 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
752 module_put(ccdc_dev->owner);
753 }
754 mutex_unlock(&vpfe_dev->lock);
755 file->private_data = NULL;
756 /* Free memory allocated to file handle object */
757 kfree(fh);
758 return 0;
759}
760
761/*
762 * vpfe_mmap : It is used to map kernel space buffers
763 * into user spaces
764 */
765static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
766{
767 /* Get the device object and file handle object */
768 struct vpfe_device *vpfe_dev = video_drvdata(file);
769
770 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
771
772 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
773}
774
775/*
776 * vpfe_poll: It is used for select/poll system call
777 */
778static unsigned int vpfe_poll(struct file *file, poll_table *wait)
779{
780 struct vpfe_device *vpfe_dev = video_drvdata(file);
781
782 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
783
784 if (vpfe_dev->started)
785 return videobuf_poll_stream(file,
786 &vpfe_dev->buffer_queue, wait);
787 return 0;
788}
789
790/* vpfe capture driver file operations */
791static const struct v4l2_file_operations vpfe_fops = {
792 .owner = THIS_MODULE,
793 .open = vpfe_open,
794 .release = vpfe_release,
795 .unlocked_ioctl = video_ioctl2,
796 .mmap = vpfe_mmap,
797 .poll = vpfe_poll
798};
799
800/*
801 * vpfe_check_format()
802 * This function adjust the input pixel format as per hardware
803 * capabilities and update the same in pixfmt.
804 * Following algorithm used :-
805 *
806 * If given pixformat is not in the vpfe list of pix formats or not
807 * supported by the hardware, current value of pixformat in the device
808 * is used
809 * If given field is not supported, then current field is used. If field
810 * is different from current, then it is matched with that from sub device.
811 * Minimum height is 2 lines for interlaced or tb field and 1 line for
812 * progressive. Maximum height is clamped to active active lines of scan
813 * Minimum width is 32 bytes in memory and width is clamped to active
814 * pixels of scan.
815 * bytesperline is a multiple of 32.
816 */
817static const struct vpfe_pixel_format *
818 vpfe_check_format(struct vpfe_device *vpfe_dev,
819 struct v4l2_pix_format *pixfmt)
820{
821 u32 min_height = 1, min_width = 32, max_width, max_height;
822 const struct vpfe_pixel_format *vpfe_pix_fmt;
823 u32 pix;
824 int temp, found;
825
826 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
827 if (NULL == vpfe_pix_fmt) {
828 /*
829 * use current pixel format in the vpfe device. We
830 * will find this pix format in the table
831 */
832 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
833 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
834 }
835
836 /* check if hw supports it */
837 temp = 0;
838 found = 0;
839 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
840 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
841 found = 1;
842 break;
843 }
844 temp++;
845 }
846
847 if (!found) {
848 /* use current pixel format */
849 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
850 /*
851 * Since this is currently used in the vpfe device, we
852 * will find this pix format in the table
853 */
854 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
855 }
856
857 /* check what field format is supported */
858 if (pixfmt->field == V4L2_FIELD_ANY) {
859 /* if field is any, use current value as default */
860 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
861 }
862
863 /*
864 * if field is not same as current field in the vpfe device
865 * try matching the field with the sub device field
866 */
867 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
868 /*
869 * If field value is not in the supported fields, use current
870 * field used in the device as default
871 */
872 switch (pixfmt->field) {
873 case V4L2_FIELD_INTERLACED:
874 case V4L2_FIELD_SEQ_TB:
875 /* if sub device is supporting progressive, use that */
876 if (!vpfe_dev->std_info.frame_format)
877 pixfmt->field = V4L2_FIELD_NONE;
878 break;
879 case V4L2_FIELD_NONE:
880 if (vpfe_dev->std_info.frame_format)
881 pixfmt->field = V4L2_FIELD_INTERLACED;
882 break;
883
884 default:
885 /* use current field as default */
886 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
887 break;
888 }
889 }
890
891 /* Now adjust image resolutions supported */
892 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
893 pixfmt->field == V4L2_FIELD_SEQ_TB)
894 min_height = 2;
895
896 max_width = vpfe_dev->std_info.active_pixels;
897 max_height = vpfe_dev->std_info.active_lines;
898 min_width /= vpfe_pix_fmt->bpp;
899
900 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
901 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
902
903 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
904 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
905
906 /* If interlaced, adjust height to be a multiple of 2 */
907 if (pixfmt->field == V4L2_FIELD_INTERLACED)
908 pixfmt->height &= (~1);
909 /*
910 * recalculate bytesperline and sizeimage since width
911 * and height might have changed
912 */
913 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
914 & ~31);
915 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
916 pixfmt->sizeimage =
917 pixfmt->bytesperline * pixfmt->height +
918 ((pixfmt->bytesperline * pixfmt->height) >> 1);
919 else
920 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
921
ded026e0 922 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
7da8a6cb
MK
923 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
924 pixfmt->bytesperline, pixfmt->sizeimage);
925 return vpfe_pix_fmt;
926}
927
928static int vpfe_querycap(struct file *file, void *priv,
929 struct v4l2_capability *cap)
930{
931 struct vpfe_device *vpfe_dev = video_drvdata(file);
932
933 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
934
8c17e5e3
HV
935 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
936 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
7da8a6cb
MK
937 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
938 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
939 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
940 return 0;
941}
942
943static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
944 struct v4l2_format *fmt)
945{
946 struct vpfe_device *vpfe_dev = video_drvdata(file);
7da8a6cb
MK
947
948 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
949 /* Fill in the information about format */
950 *fmt = vpfe_dev->fmt;
b80cefb4 951 return 0;
7da8a6cb
MK
952}
953
954static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
955 struct v4l2_fmtdesc *fmt)
956{
957 struct vpfe_device *vpfe_dev = video_drvdata(file);
958 const struct vpfe_pixel_format *pix_fmt;
959 int temp_index;
960 u32 pix;
961
962 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
963
964 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
965 return -EINVAL;
966
967 /* Fill in the information about format */
968 pix_fmt = vpfe_lookup_pix_format(pix);
969 if (NULL != pix_fmt) {
970 temp_index = fmt->index;
971 *fmt = pix_fmt->fmtdesc;
972 fmt->index = temp_index;
973 return 0;
974 }
975 return -EINVAL;
976}
977
978static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
979 struct v4l2_format *fmt)
980{
981 struct vpfe_device *vpfe_dev = video_drvdata(file);
982 const struct vpfe_pixel_format *pix_fmts;
983 int ret = 0;
984
985 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
986
987 /* If streaming is started, return error */
988 if (vpfe_dev->started) {
989 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
990 return -EBUSY;
991 }
992
993 /* Check for valid frame format */
994 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
995
996 if (NULL == pix_fmts)
997 return -EINVAL;
998
999 /* store the pixel format in the device object */
1000 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1001 if (ret)
1002 return ret;
1003
1004 /* First detach any IRQ if currently attached */
1005 vpfe_detach_irq(vpfe_dev);
1006 vpfe_dev->fmt = *fmt;
1007 /* set image capture parameters in the ccdc */
1008 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1009 mutex_unlock(&vpfe_dev->lock);
1010 return ret;
1011}
1012
1013static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1014 struct v4l2_format *f)
1015{
1016 struct vpfe_device *vpfe_dev = video_drvdata(file);
1017 const struct vpfe_pixel_format *pix_fmts;
1018
1019 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1020
1021 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
1022 if (NULL == pix_fmts)
1023 return -EINVAL;
1024 return 0;
1025}
1026
1027/*
1028 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1029 * given app input index
1030 */
1031static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1032 int *subdev_index,
1033 int *subdev_input_index,
1034 int app_input_index)
1035{
1036 struct vpfe_config *cfg = vpfe_dev->cfg;
1037 struct vpfe_subdev_info *sdinfo;
1038 int i, j = 0;
1039
1040 for (i = 0; i < cfg->num_subdevs; i++) {
1041 sdinfo = &cfg->sub_devs[i];
1042 if (app_input_index < (j + sdinfo->num_inputs)) {
1043 *subdev_index = i;
1044 *subdev_input_index = app_input_index - j;
1045 return 0;
1046 }
1047 j += sdinfo->num_inputs;
1048 }
1049 return -EINVAL;
1050}
1051
1052/*
1053 * vpfe_get_app_input - Get app input index for a given subdev input index
1054 * driver stores the input index of the current sub device and translate it
1055 * when application request the current input
1056 */
1057static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1058 int *app_input_index)
1059{
1060 struct vpfe_config *cfg = vpfe_dev->cfg;
1061 struct vpfe_subdev_info *sdinfo;
1062 int i, j = 0;
1063
1064 for (i = 0; i < cfg->num_subdevs; i++) {
1065 sdinfo = &cfg->sub_devs[i];
1066 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1067 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1068 return -1;
1069 *app_input_index = j + vpfe_dev->current_input;
1070 return 0;
1071 }
1072 j += sdinfo->num_inputs;
1073 }
1074 return -EINVAL;
1075}
1076
1077static int vpfe_enum_input(struct file *file, void *priv,
1078 struct v4l2_input *inp)
1079{
1080 struct vpfe_device *vpfe_dev = video_drvdata(file);
1081 struct vpfe_subdev_info *sdinfo;
1082 int subdev, index ;
1083
1084 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1085
1086 if (vpfe_get_subdev_input_index(vpfe_dev,
1087 &subdev,
1088 &index,
1089 inp->index) < 0) {
ded026e0 1090 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
7da8a6cb
MK
1091 return -EINVAL;
1092 }
1093 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1094 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1095 return 0;
1096}
1097
1098static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1099{
1100 struct vpfe_device *vpfe_dev = video_drvdata(file);
1101
1102 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1103
1104 return vpfe_get_app_input_index(vpfe_dev, index);
1105}
1106
1107
1108static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1109{
1110 struct vpfe_device *vpfe_dev = video_drvdata(file);
6f55dbae 1111 struct v4l2_subdev *sd;
7da8a6cb
MK
1112 struct vpfe_subdev_info *sdinfo;
1113 int subdev_index, inp_index;
1114 struct vpfe_route *route;
1115 u32 input = 0, output = 0;
1116 int ret = -EINVAL;
1117
1118 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1119
1120 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1121 if (ret)
1122 return ret;
1123
1124 /*
1125 * If streaming is started return device busy
1126 * error
1127 */
1128 if (vpfe_dev->started) {
1129 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1130 ret = -EBUSY;
1131 goto unlock_out;
1132 }
9a888ba2
PST
1133 ret = vpfe_get_subdev_input_index(vpfe_dev,
1134 &subdev_index,
1135 &inp_index,
1136 index);
1137 if (ret < 0) {
7da8a6cb
MK
1138 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1139 goto unlock_out;
1140 }
1141
1142 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
6f55dbae 1143 sd = vpfe_dev->sd[subdev_index];
7da8a6cb
MK
1144 route = &sdinfo->routes[inp_index];
1145 if (route && sdinfo->can_route) {
1146 input = route->input;
1147 output = route->output;
1148 }
1149
6f55dbae
HV
1150 if (sd)
1151 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
7da8a6cb
MK
1152
1153 if (ret) {
1154 v4l2_err(&vpfe_dev->v4l2_dev,
1155 "vpfe_doioctl:error in setting input in decoder\n");
1156 ret = -EINVAL;
1157 goto unlock_out;
1158 }
1159 vpfe_dev->current_subdev = sdinfo;
6f55dbae
HV
1160 if (sd)
1161 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
7da8a6cb
MK
1162 vpfe_dev->current_input = index;
1163 vpfe_dev->std_index = 0;
1164
1165 /* set the bus/interface parameter for the sub device in ccdc */
1166 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1167 if (ret)
1168 goto unlock_out;
1169
1170 /* set the default image parameters in the device */
1171 ret = vpfe_config_image_format(vpfe_dev,
314527ac 1172 vpfe_standards[vpfe_dev->std_index].std_id);
7da8a6cb
MK
1173unlock_out:
1174 mutex_unlock(&vpfe_dev->lock);
1175 return ret;
1176}
1177
1178static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1179{
1180 struct vpfe_device *vpfe_dev = video_drvdata(file);
1181 struct vpfe_subdev_info *sdinfo;
1182 int ret = 0;
1183
1184 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1185
1186 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1187 sdinfo = vpfe_dev->current_subdev;
1188 if (ret)
1189 return ret;
1190 /* Call querystd function of decoder device */
1191 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1192 video, querystd, std_id);
1193 mutex_unlock(&vpfe_dev->lock);
1194 return ret;
1195}
1196
314527ac 1197static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
7da8a6cb
MK
1198{
1199 struct vpfe_device *vpfe_dev = video_drvdata(file);
1200 struct vpfe_subdev_info *sdinfo;
1201 int ret = 0;
1202
1203 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1204
1205 /* Call decoder driver function to set the standard */
1206 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1207 if (ret)
1208 return ret;
1209
1210 sdinfo = vpfe_dev->current_subdev;
1211 /* If streaming is started, return device busy error */
1212 if (vpfe_dev->started) {
1213 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1214 ret = -EBUSY;
1215 goto unlock_out;
1216 }
1217
1218 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
8774bed9 1219 video, s_std, std_id);
7da8a6cb
MK
1220 if (ret < 0) {
1221 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1222 goto unlock_out;
1223 }
1224 ret = vpfe_config_image_format(vpfe_dev, std_id);
1225
1226unlock_out:
1227 mutex_unlock(&vpfe_dev->lock);
1228 return ret;
1229}
1230
1231static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1232{
1233 struct vpfe_device *vpfe_dev = video_drvdata(file);
1234
1235 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1236
1237 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1238 return 0;
1239}
1240/*
1241 * Videobuf operations
1242 */
1243static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1244 unsigned int *count,
1245 unsigned int *size)
1246{
1247 struct vpfe_fh *fh = vq->priv_data;
1248 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1249
1250 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
844cc0dc
VH
1251 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1252 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1253 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1254 *size = config_params.device_bufsize;
7da8a6cb
MK
1255
1256 if (*count < config_params.min_numbuffers)
1257 *count = config_params.min_numbuffers;
1258 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1259 "count=%d, size=%d\n", *count, *size);
1260 return 0;
1261}
1262
1263static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1264 struct videobuf_buffer *vb,
1265 enum v4l2_field field)
1266{
1267 struct vpfe_fh *fh = vq->priv_data;
1268 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
844cc0dc
VH
1269 unsigned long addr;
1270 int ret;
7da8a6cb
MK
1271
1272 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1273
1274 /* If buffer is not initialized, initialize it */
1275 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1276 vb->width = vpfe_dev->fmt.fmt.pix.width;
1277 vb->height = vpfe_dev->fmt.fmt.pix.height;
1278 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1279 vb->field = field;
844cc0dc 1280
b1dc614a 1281 ret = videobuf_iolock(vq, vb, NULL);
844cc0dc
VH
1282 if (ret < 0)
1283 return ret;
1284
1285 addr = videobuf_to_dma_contig(vb);
1286 /* Make sure user addresses are aligned to 32 bytes */
1287 if (!ALIGN(addr, 32))
1288 return -EINVAL;
1289
1290 vb->state = VIDEOBUF_PREPARED;
7da8a6cb 1291 }
7da8a6cb
MK
1292 return 0;
1293}
1294
1295static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1296 struct videobuf_buffer *vb)
1297{
1298 /* Get the file handle object and device object */
1299 struct vpfe_fh *fh = vq->priv_data;
1300 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1301 unsigned long flags;
1302
1303 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1304
1305 /* add the buffer to the DMA queue */
1306 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1307 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1308 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1309
1310 /* Change state of the buffer */
1311 vb->state = VIDEOBUF_QUEUED;
1312}
1313
1314static void vpfe_videobuf_release(struct videobuf_queue *vq,
1315 struct videobuf_buffer *vb)
1316{
1317 struct vpfe_fh *fh = vq->priv_data;
1318 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1319 unsigned long flags;
1320
1321 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1322
1323 /*
1324 * We need to flush the buffer from the dma queue since
1325 * they are de-allocated
1326 */
1327 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1328 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1329 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1330 videobuf_dma_contig_free(vq, vb);
1331 vb->state = VIDEOBUF_NEEDS_INIT;
1332}
1333
1334static struct videobuf_queue_ops vpfe_videobuf_qops = {
1335 .buf_setup = vpfe_videobuf_setup,
1336 .buf_prepare = vpfe_videobuf_prepare,
1337 .buf_queue = vpfe_videobuf_queue,
1338 .buf_release = vpfe_videobuf_release,
1339};
1340
1341/*
1342 * vpfe_reqbufs. currently support REQBUF only once opening
1343 * the device.
1344 */
1345static int vpfe_reqbufs(struct file *file, void *priv,
1346 struct v4l2_requestbuffers *req_buf)
1347{
1348 struct vpfe_device *vpfe_dev = video_drvdata(file);
1349 struct vpfe_fh *fh = file->private_data;
1350 int ret = 0;
1351
1352 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1353
1354 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1355 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1356 return -EINVAL;
1357 }
1358
7da8a6cb
MK
1359 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1360 if (ret)
1361 return ret;
1362
1363 if (vpfe_dev->io_usrs != 0) {
1364 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1365 ret = -EBUSY;
1366 goto unlock_out;
1367 }
1368
1369 vpfe_dev->memory = req_buf->memory;
1370 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1371 &vpfe_videobuf_qops,
204e6ea9 1372 vpfe_dev->pdev,
7da8a6cb
MK
1373 &vpfe_dev->irqlock,
1374 req_buf->type,
1375 vpfe_dev->fmt.fmt.pix.field,
1376 sizeof(struct videobuf_buffer),
e3cfd447 1377 fh, NULL);
7da8a6cb
MK
1378
1379 fh->io_allowed = 1;
1380 vpfe_dev->io_usrs = 1;
1381 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1382 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1383unlock_out:
1384 mutex_unlock(&vpfe_dev->lock);
1385 return ret;
1386}
1387
1388static int vpfe_querybuf(struct file *file, void *priv,
1389 struct v4l2_buffer *buf)
1390{
1391 struct vpfe_device *vpfe_dev = video_drvdata(file);
1392
1393 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1394
1395 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1396 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1397 return -EINVAL;
1398 }
1399
1400 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1401 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1402 return -EINVAL;
1403 }
1404 /* Call videobuf_querybuf to get information */
1405 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1406}
1407
1408static int vpfe_qbuf(struct file *file, void *priv,
1409 struct v4l2_buffer *p)
1410{
1411 struct vpfe_device *vpfe_dev = video_drvdata(file);
1412 struct vpfe_fh *fh = file->private_data;
1413
1414 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1415
1416 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1417 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1418 return -EINVAL;
1419 }
1420
1421 /*
1422 * If this file handle is not allowed to do IO,
1423 * return error
1424 */
1425 if (!fh->io_allowed) {
1426 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1427 return -EACCES;
1428 }
1429 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1430}
1431
1432static int vpfe_dqbuf(struct file *file, void *priv,
1433 struct v4l2_buffer *buf)
1434{
1435 struct vpfe_device *vpfe_dev = video_drvdata(file);
1436
1437 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1438
1439 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1440 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1441 return -EINVAL;
1442 }
1443 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1444 buf, file->f_flags & O_NONBLOCK);
1445}
1446
1447/*
1448 * vpfe_calculate_offsets : This function calculates buffers offset
1449 * for top and bottom field
1450 */
1451static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1452{
1453 struct v4l2_rect image_win;
1454
1455 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1456
1457 ccdc_dev->hw_ops.get_image_window(&image_win);
1458 vpfe_dev->field_off = image_win.height * image_win.width;
1459}
1460
1461/* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1462static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1463{
1464 ccdc_dev->hw_ops.enable(1);
1465 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1466 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1467 vpfe_dev->started = 1;
1468}
1469
1470/*
1471 * vpfe_streamon. Assume the DMA queue is not empty.
1472 * application is expected to call QBUF before calling
1473 * this ioctl. If not, driver returns error
1474 */
1475static int vpfe_streamon(struct file *file, void *priv,
1476 enum v4l2_buf_type buf_type)
1477{
1478 struct vpfe_device *vpfe_dev = video_drvdata(file);
1479 struct vpfe_fh *fh = file->private_data;
1480 struct vpfe_subdev_info *sdinfo;
1481 unsigned long addr;
1482 int ret = 0;
1483
1484 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1485
1486 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1487 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1488 return -EINVAL;
1489 }
1490
1491 /* If file handle is not allowed IO, return error */
1492 if (!fh->io_allowed) {
1493 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1494 return -EACCES;
1495 }
1496
1497 sdinfo = vpfe_dev->current_subdev;
1498 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1499 video, s_stream, 1);
1500
1501 if (ret && (ret != -ENOIOCTLCMD)) {
1502 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1503 return -EINVAL;
1504 }
1505
1506 /* If buffer queue is empty, return error */
1507 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1508 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1509 return -EIO;
1510 }
1511
1512 /* Call videobuf_streamon to start streaming * in videobuf */
1513 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1514 if (ret)
1515 return ret;
1516
1517
1518 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1519 if (ret)
1520 goto streamoff;
1521 /* Get the next frame from the buffer queue */
1522 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1523 struct videobuf_buffer, queue);
1524 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1525 /* Remove buffer from the buffer queue */
1526 list_del(&vpfe_dev->cur_frm->queue);
1527 /* Mark state of the current frame to active */
1528 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1529 /* Initialize field_id and started member */
1530 vpfe_dev->field_id = 0;
1531 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1532
1533 /* Calculate field offset */
1534 vpfe_calculate_offsets(vpfe_dev);
1535
1536 if (vpfe_attach_irq(vpfe_dev) < 0) {
1537 v4l2_err(&vpfe_dev->v4l2_dev,
1538 "Error in attaching interrupt handle\n");
1539 ret = -EFAULT;
1540 goto unlock_out;
1541 }
1542 if (ccdc_dev->hw_ops.configure() < 0) {
1543 v4l2_err(&vpfe_dev->v4l2_dev,
1544 "Error in configuring ccdc\n");
1545 ret = -EINVAL;
1546 goto unlock_out;
1547 }
1548 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1549 vpfe_start_ccdc_capture(vpfe_dev);
1550 mutex_unlock(&vpfe_dev->lock);
1551 return ret;
1552unlock_out:
1553 mutex_unlock(&vpfe_dev->lock);
1554streamoff:
1555 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1556 return ret;
1557}
1558
1559static int vpfe_streamoff(struct file *file, void *priv,
1560 enum v4l2_buf_type buf_type)
1561{
1562 struct vpfe_device *vpfe_dev = video_drvdata(file);
1563 struct vpfe_fh *fh = file->private_data;
1564 struct vpfe_subdev_info *sdinfo;
1565 int ret = 0;
1566
1567 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1568
1569 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1570 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1571 return -EINVAL;
1572 }
1573
1574 /* If io is allowed for this file handle, return error */
1575 if (!fh->io_allowed) {
1576 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1577 return -EACCES;
1578 }
1579
1580 /* If streaming is not started, return error */
1581 if (!vpfe_dev->started) {
1582 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1583 return -EINVAL;
1584 }
1585
1586 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1587 if (ret)
1588 return ret;
1589
1590 vpfe_stop_ccdc_capture(vpfe_dev);
1591 vpfe_detach_irq(vpfe_dev);
1592
1593 sdinfo = vpfe_dev->current_subdev;
1594 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1595 video, s_stream, 0);
1596
1597 if (ret && (ret != -ENOIOCTLCMD))
1598 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1599 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1600 mutex_unlock(&vpfe_dev->lock);
1601 return ret;
1602}
1603
1604static int vpfe_cropcap(struct file *file, void *priv,
1605 struct v4l2_cropcap *crop)
1606{
1607 struct vpfe_device *vpfe_dev = video_drvdata(file);
1608
1609 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1610
ffc2a6bb 1611 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
7da8a6cb 1612 return -EINVAL;
ffc2a6bb
HV
1613 /* If std_index is invalid, then just return (== 1:1 aspect) */
1614 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1615 return 0;
7da8a6cb 1616
7da8a6cb
MK
1617 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1618 return 0;
1619}
1620
ffc2a6bb
HV
1621static int vpfe_g_selection(struct file *file, void *priv,
1622 struct v4l2_selection *sel)
7da8a6cb
MK
1623{
1624 struct vpfe_device *vpfe_dev = video_drvdata(file);
1625
ffc2a6bb 1626 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
7da8a6cb 1627
ffc2a6bb
HV
1628 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1629 return -EINVAL;
1630
1631 switch (sel->target) {
1632 case V4L2_SEL_TGT_CROP:
1633 sel->r = vpfe_dev->crop;
1634 break;
1635 case V4L2_SEL_TGT_CROP_DEFAULT:
1636 case V4L2_SEL_TGT_CROP_BOUNDS:
1637 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1638 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1639 break;
1640 default:
1641 return -EINVAL;
1642 }
7da8a6cb
MK
1643 return 0;
1644}
1645
ffc2a6bb
HV
1646static int vpfe_s_selection(struct file *file, void *priv,
1647 struct v4l2_selection *sel)
7da8a6cb
MK
1648{
1649 struct vpfe_device *vpfe_dev = video_drvdata(file);
ffc2a6bb 1650 struct v4l2_rect rect = sel->r;
7da8a6cb
MK
1651 int ret = 0;
1652
ffc2a6bb
HV
1653 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1654
1655 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1656 sel->target != V4L2_SEL_TGT_CROP)
1657 return -EINVAL;
7da8a6cb
MK
1658
1659 if (vpfe_dev->started) {
1660 /* make sure streaming is not started */
1661 v4l2_err(&vpfe_dev->v4l2_dev,
1662 "Cannot change crop when streaming is ON\n");
1663 return -EBUSY;
1664 }
1665
1666 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1667 if (ret)
1668 return ret;
1669
8b6faacd 1670 if (rect.top < 0 || rect.left < 0) {
7da8a6cb
MK
1671 v4l2_err(&vpfe_dev->v4l2_dev,
1672 "doesn't support negative values for top & left\n");
1673 ret = -EINVAL;
1674 goto unlock_out;
1675 }
1676
25985edc 1677 /* adjust the width to 16 pixel boundary */
8b6faacd 1678 rect.width = ((rect.width + 15) & ~0xf);
7da8a6cb
MK
1679
1680 /* make sure parameters are valid */
8b6faacd 1681 if ((rect.left + rect.width >
7da8a6cb 1682 vpfe_dev->std_info.active_pixels) ||
8b6faacd 1683 (rect.top + rect.height >
7da8a6cb 1684 vpfe_dev->std_info.active_lines)) {
ffc2a6bb 1685 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
7da8a6cb
MK
1686 ret = -EINVAL;
1687 goto unlock_out;
1688 }
8b6faacd
LP
1689 ccdc_dev->hw_ops.set_image_window(&rect);
1690 vpfe_dev->fmt.fmt.pix.width = rect.width;
1691 vpfe_dev->fmt.fmt.pix.height = rect.height;
7da8a6cb
MK
1692 vpfe_dev->fmt.fmt.pix.bytesperline =
1693 ccdc_dev->hw_ops.get_line_length();
1694 vpfe_dev->fmt.fmt.pix.sizeimage =
1695 vpfe_dev->fmt.fmt.pix.bytesperline *
1696 vpfe_dev->fmt.fmt.pix.height;
8b6faacd 1697 vpfe_dev->crop = rect;
ffc2a6bb 1698 sel->r = rect;
7da8a6cb
MK
1699unlock_out:
1700 mutex_unlock(&vpfe_dev->lock);
1701 return ret;
1702}
1703
1704
1705static long vpfe_param_handler(struct file *file, void *priv,
6d43be77 1706 bool valid_prio, unsigned int cmd, void *param)
7da8a6cb
MK
1707{
1708 struct vpfe_device *vpfe_dev = video_drvdata(file);
1709 int ret = 0;
1710
6a4f0623 1711 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
7da8a6cb
MK
1712
1713 if (vpfe_dev->started) {
1714 /* only allowed if streaming is not started */
6a4f0623
MK
1715 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1716 "device already started\n");
7da8a6cb
MK
1717 return -EBUSY;
1718 }
1719
1720 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1721 if (ret)
1722 return ret;
1723
1724 switch (cmd) {
1725 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1726 v4l2_warn(&vpfe_dev->v4l2_dev,
1727 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
6a4f0623
MK
1728 if (ccdc_dev->hw_ops.set_params) {
1729 ret = ccdc_dev->hw_ops.set_params(param);
1730 if (ret) {
1731 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1732 "Error setting parameters in CCDC\n");
1733 goto unlock_out;
1734 }
9a888ba2
PST
1735 ret = vpfe_get_ccdc_image_format(vpfe_dev,
1736 &vpfe_dev->fmt);
1737 if (ret < 0) {
6a4f0623
MK
1738 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1739 "Invalid image format at CCDC\n");
1740 goto unlock_out;
1741 }
1742 } else {
1743 ret = -EINVAL;
1744 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1745 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
7da8a6cb
MK
1746 }
1747 break;
1748 default:
d1c754a9 1749 ret = -ENOTTY;
7da8a6cb
MK
1750 }
1751unlock_out:
1752 mutex_unlock(&vpfe_dev->lock);
1753 return ret;
1754}
1755
1756
1757/* vpfe capture ioctl operations */
1758static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1759 .vidioc_querycap = vpfe_querycap,
1760 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1761 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1762 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1763 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1764 .vidioc_enum_input = vpfe_enum_input,
1765 .vidioc_g_input = vpfe_g_input,
1766 .vidioc_s_input = vpfe_s_input,
1767 .vidioc_querystd = vpfe_querystd,
1768 .vidioc_s_std = vpfe_s_std,
1769 .vidioc_g_std = vpfe_g_std,
1770 .vidioc_reqbufs = vpfe_reqbufs,
1771 .vidioc_querybuf = vpfe_querybuf,
1772 .vidioc_qbuf = vpfe_qbuf,
1773 .vidioc_dqbuf = vpfe_dqbuf,
1774 .vidioc_streamon = vpfe_streamon,
1775 .vidioc_streamoff = vpfe_streamoff,
1776 .vidioc_cropcap = vpfe_cropcap,
ffc2a6bb
HV
1777 .vidioc_g_selection = vpfe_g_selection,
1778 .vidioc_s_selection = vpfe_s_selection,
7da8a6cb
MK
1779 .vidioc_default = vpfe_param_handler,
1780};
1781
1782static struct vpfe_device *vpfe_initialize(void)
1783{
1784 struct vpfe_device *vpfe_dev;
1785
1786 /* Default number of buffers should be 3 */
1787 if ((numbuffers > 0) &&
1788 (numbuffers < config_params.min_numbuffers))
1789 numbuffers = config_params.min_numbuffers;
1790
1791 /*
1792 * Set buffer size to min buffers size if invalid buffer size is
1793 * given
1794 */
1795 if (bufsize < config_params.min_bufsize)
1796 bufsize = config_params.min_bufsize;
1797
1798 config_params.numbuffers = numbuffers;
1799
1800 if (numbuffers)
1801 config_params.device_bufsize = bufsize;
1802
1803 /* Allocate memory for device objects */
1804 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1805
1806 return vpfe_dev;
1807}
1808
7da8a6cb
MK
1809/*
1810 * vpfe_probe : This function creates device entries by register
1811 * itself to the V4L2 driver and initializes fields of each
1812 * device objects
1813 */
4c62e976 1814static int vpfe_probe(struct platform_device *pdev)
7da8a6cb
MK
1815{
1816 struct vpfe_subdev_info *sdinfo;
1817 struct vpfe_config *vpfe_cfg;
1818 struct resource *res1;
1819 struct vpfe_device *vpfe_dev;
1820 struct i2c_adapter *i2c_adap;
1821 struct video_device *vfd;
1822 int ret = -ENOMEM, i, j;
1823 int num_subdevs = 0;
1824
1825 /* Get the pointer to the device object */
1826 vpfe_dev = vpfe_initialize();
1827
1828 if (!vpfe_dev) {
1829 v4l2_err(pdev->dev.driver,
1830 "Failed to allocate memory for vpfe_dev\n");
1831 return ret;
1832 }
1833
1834 vpfe_dev->pdev = &pdev->dev;
1835
1836 if (NULL == pdev->dev.platform_data) {
1837 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
51444ea3 1838 ret = -ENODEV;
7da8a6cb
MK
1839 goto probe_free_dev_mem;
1840 }
1841
1842 vpfe_cfg = pdev->dev.platform_data;
1843 vpfe_dev->cfg = vpfe_cfg;
1844 if (NULL == vpfe_cfg->ccdc ||
1845 NULL == vpfe_cfg->card_name ||
1846 NULL == vpfe_cfg->sub_devs) {
1847 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1848 ret = -ENOENT;
1849 goto probe_free_dev_mem;
1850 }
1851
7da8a6cb
MK
1852 /* Allocate memory for ccdc configuration */
1853 ccdc_cfg = kmalloc(sizeof(struct ccdc_config), GFP_KERNEL);
1854 if (NULL == ccdc_cfg) {
1855 v4l2_err(pdev->dev.driver,
1856 "Memory allocation failed for ccdc_cfg\n");
39e219d9 1857 goto probe_free_dev_mem;
7da8a6cb
MK
1858 }
1859
2b080c5d
DC
1860 mutex_lock(&ccdc_lock);
1861
7da8a6cb
MK
1862 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1863 /* Get VINT0 irq resource */
1864 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1865 if (!res1) {
1866 v4l2_err(pdev->dev.driver,
1867 "Unable to get interrupt for VINT0\n");
51444ea3
MK
1868 ret = -ENODEV;
1869 goto probe_free_ccdc_cfg_mem;
7da8a6cb
MK
1870 }
1871 vpfe_dev->ccdc_irq0 = res1->start;
1872
1873 /* Get VINT1 irq resource */
51444ea3 1874 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
7da8a6cb
MK
1875 if (!res1) {
1876 v4l2_err(pdev->dev.driver,
1877 "Unable to get interrupt for VINT1\n");
51444ea3
MK
1878 ret = -ENODEV;
1879 goto probe_free_ccdc_cfg_mem;
7da8a6cb
MK
1880 }
1881 vpfe_dev->ccdc_irq1 = res1->start;
1882
d8c279a0 1883 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
7da8a6cb
MK
1884 "vpfe_capture0", vpfe_dev);
1885
1886 if (0 != ret) {
1887 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
51444ea3 1888 goto probe_free_ccdc_cfg_mem;
7da8a6cb
MK
1889 }
1890
03c278f0 1891 vfd = &vpfe_dev->video_dev;
7da8a6cb 1892 /* Initialize field of video device */
03c278f0 1893 vfd->release = video_device_release_empty;
7da8a6cb
MK
1894 vfd->fops = &vpfe_fops;
1895 vfd->ioctl_ops = &vpfe_ioctl_ops;
7da8a6cb 1896 vfd->tvnorms = 0;
7da8a6cb
MK
1897 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1898 snprintf(vfd->name, sizeof(vfd->name),
1899 "%s_V%d.%d.%d",
1900 CAPTURE_DRV_NAME,
1901 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1902 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1903 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
7da8a6cb
MK
1904
1905 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1906 if (ret) {
1907 v4l2_err(pdev->dev.driver,
1908 "Unable to register v4l2 device.\n");
03c278f0 1909 goto probe_out_release_irq;
7da8a6cb
MK
1910 }
1911 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1912 spin_lock_init(&vpfe_dev->irqlock);
1913 spin_lock_init(&vpfe_dev->dma_queue_lock);
1914 mutex_init(&vpfe_dev->lock);
1915
1916 /* Initialize field of the device objects */
1917 vpfe_dev->numbuffers = config_params.numbuffers;
1918
7da8a6cb
MK
1919 /* register video device */
1920 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1921 "trying to register vpfe device.\n");
1922 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
212bdba3 1923 "video_dev=%p\n", &vpfe_dev->video_dev);
7da8a6cb 1924 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
03c278f0 1925 ret = video_register_device(&vpfe_dev->video_dev,
7da8a6cb
MK
1926 VFL_TYPE_GRABBER, -1);
1927
1928 if (ret) {
1929 v4l2_err(pdev->dev.driver,
1930 "Unable to register video device.\n");
1931 goto probe_out_v4l2_unregister;
1932 }
1933
1934 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1935 /* set the driver data in platform device */
1936 platform_set_drvdata(pdev, vpfe_dev);
1937 /* set driver private data */
03c278f0 1938 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
14cbaafe 1939 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
7da8a6cb 1940 num_subdevs = vpfe_cfg->num_subdevs;
ed011a23
ME
1941 vpfe_dev->sd = kmalloc_array(num_subdevs,
1942 sizeof(*vpfe_dev->sd),
1943 GFP_KERNEL);
7da8a6cb
MK
1944 if (NULL == vpfe_dev->sd) {
1945 v4l2_err(&vpfe_dev->v4l2_dev,
1946 "unable to allocate memory for subdevice pointers\n");
1947 ret = -ENOMEM;
1948 goto probe_out_video_unregister;
1949 }
1950
1951 for (i = 0; i < num_subdevs; i++) {
1952 struct v4l2_input *inps;
1953
1954 sdinfo = &vpfe_cfg->sub_devs[i];
1955
1956 /* Load up the subdevice */
1957 vpfe_dev->sd[i] =
1958 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1959 i2c_adap,
7da8a6cb
MK
1960 &sdinfo->board_info,
1961 NULL);
1962 if (vpfe_dev->sd[i]) {
1963 v4l2_info(&vpfe_dev->v4l2_dev,
1964 "v4l2 sub device %s registered\n",
1965 sdinfo->name);
1966 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1967 /* update tvnorms from the sub devices */
1968 for (j = 0; j < sdinfo->num_inputs; j++) {
1969 inps = &sdinfo->inputs[j];
1970 vfd->tvnorms |= inps->std;
1971 }
1972 } else {
1973 v4l2_info(&vpfe_dev->v4l2_dev,
1974 "v4l2 sub device %s register fails\n",
1975 sdinfo->name);
1976 goto probe_sd_out;
1977 }
1978 }
1979
1980 /* set first sub device as current one */
1981 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
6f55dbae 1982 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
7da8a6cb
MK
1983
1984 /* We have at least one sub device to work with */
1985 mutex_unlock(&ccdc_lock);
1986 return 0;
1987
1988probe_sd_out:
1989 kfree(vpfe_dev->sd);
1990probe_out_video_unregister:
03c278f0 1991 video_unregister_device(&vpfe_dev->video_dev);
7da8a6cb
MK
1992probe_out_v4l2_unregister:
1993 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
7da8a6cb
MK
1994probe_out_release_irq:
1995 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
51444ea3 1996probe_free_ccdc_cfg_mem:
7da8a6cb 1997 kfree(ccdc_cfg);
ab51bec1 1998 mutex_unlock(&ccdc_lock);
7da8a6cb
MK
1999probe_free_dev_mem:
2000 kfree(vpfe_dev);
2001 return ret;
2002}
2003
2004/*
2005 * vpfe_remove : It un-register device from V4L2 driver
2006 */
4c62e976 2007static int vpfe_remove(struct platform_device *pdev)
7da8a6cb
MK
2008{
2009 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
7da8a6cb
MK
2010
2011 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2012
2013 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2014 kfree(vpfe_dev->sd);
2015 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
03c278f0 2016 video_unregister_device(&vpfe_dev->video_dev);
7da8a6cb
MK
2017 kfree(vpfe_dev);
2018 kfree(ccdc_cfg);
2019 return 0;
2020}
2021
aa2dc903 2022static int vpfe_suspend(struct device *dev)
7da8a6cb 2023{
aa2dc903 2024 return 0;
7da8a6cb
MK
2025}
2026
aa2dc903 2027static int vpfe_resume(struct device *dev)
7da8a6cb 2028{
aa2dc903 2029 return 0;
7da8a6cb
MK
2030}
2031
47145210 2032static const struct dev_pm_ops vpfe_dev_pm_ops = {
7da8a6cb
MK
2033 .suspend = vpfe_suspend,
2034 .resume = vpfe_resume,
2035};
2036
a1b3a6ce 2037static struct platform_driver vpfe_driver = {
7da8a6cb
MK
2038 .driver = {
2039 .name = CAPTURE_DRV_NAME,
7da8a6cb
MK
2040 .pm = &vpfe_dev_pm_ops,
2041 },
2042 .probe = vpfe_probe,
4c62e976 2043 .remove = vpfe_remove,
7da8a6cb
MK
2044};
2045
1d6629b1 2046module_platform_driver(vpfe_driver);