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