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