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