linux/drivers/media/video/davinci/vpif_display.c
<<
>>
Prefs
   1/*
   2 * vpif-display - VPIF display driver
   3 * Display driver for TI DaVinci VPIF
   4 *
   5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License as
   9 * published by the Free Software Foundation version 2.
  10 *
  11 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12 * kind, whether express or implied; without even the implied warranty
  13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 * GNU General Public License for more details.
  15 */
  16
  17#include <linux/kernel.h>
  18#include <linux/init.h>
  19#include <linux/module.h>
  20#include <linux/errno.h>
  21#include <linux/fs.h>
  22#include <linux/mm.h>
  23#include <linux/interrupt.h>
  24#include <linux/workqueue.h>
  25#include <linux/string.h>
  26#include <linux/videodev2.h>
  27#include <linux/wait.h>
  28#include <linux/time.h>
  29#include <linux/i2c.h>
  30#include <linux/platform_device.h>
  31#include <linux/io.h>
  32#include <linux/version.h>
  33
  34#include <asm/irq.h>
  35#include <asm/page.h>
  36
  37#include <media/adv7343.h>
  38#include <media/v4l2-device.h>
  39#include <media/v4l2-ioctl.h>
  40
  41#include <mach/dm646x.h>
  42
  43#include "vpif_display.h"
  44#include "vpif.h"
  45
  46MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  47MODULE_LICENSE("GPL");
  48
  49#define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  50
  51#define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  52#define vpif_dbg(level, debug, fmt, arg...)     \
  53                v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  54
  55static int debug = 1;
  56static u32 ch2_numbuffers = 3;
  57static u32 ch3_numbuffers = 3;
  58static u32 ch2_bufsize = 1920 * 1080 * 2;
  59static u32 ch3_bufsize = 720 * 576 * 2;
  60
  61module_param(debug, int, 0644);
  62module_param(ch2_numbuffers, uint, S_IRUGO);
  63module_param(ch3_numbuffers, uint, S_IRUGO);
  64module_param(ch2_bufsize, uint, S_IRUGO);
  65module_param(ch3_bufsize, uint, S_IRUGO);
  66
  67MODULE_PARM_DESC(debug, "Debug level 0-1");
  68MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  69MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  70MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  71MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  72
  73static struct vpif_config_params config_params = {
  74        .min_numbuffers         = 3,
  75        .numbuffers[0]          = 3,
  76        .numbuffers[1]          = 3,
  77        .min_bufsize[0]         = 720 * 480 * 2,
  78        .min_bufsize[1]         = 720 * 480 * 2,
  79        .channel_bufsize[0]     = 1920 * 1080 * 2,
  80        .channel_bufsize[1]     = 720 * 576 * 2,
  81};
  82
  83static struct vpif_device vpif_obj = { {NULL} };
  84static struct device *vpif_dev;
  85
  86static const struct vpif_channel_config_params ch_params[] = {
  87        {
  88                "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
  89                286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
  90        },
  91        {
  92                "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
  93                336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
  94        },
  95};
  96
  97/*
  98 * vpif_uservirt_to_phys: This function is used to convert user
  99 * space virtual address to physical address.
 100 */
 101static u32 vpif_uservirt_to_phys(u32 virtp)
 102{
 103        struct mm_struct *mm = current->mm;
 104        unsigned long physp = 0;
 105        struct vm_area_struct *vma;
 106
 107        vma = find_vma(mm, virtp);
 108
 109        /* For kernel direct-mapped memory, take the easy way */
 110        if (virtp >= PAGE_OFFSET) {
 111                physp = virt_to_phys((void *)virtp);
 112        } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
 113                /* this will catch, kernel-allocated, mmaped-to-usermode addr */
 114                physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
 115        } else {
 116                /* otherwise, use get_user_pages() for general userland pages */
 117                int res, nr_pages = 1;
 118                struct page *pages;
 119                down_read(&current->mm->mmap_sem);
 120
 121                res = get_user_pages(current, current->mm,
 122                                     virtp, nr_pages, 1, 0, &pages, NULL);
 123                up_read(&current->mm->mmap_sem);
 124
 125                if (res == nr_pages) {
 126                        physp = __pa(page_address(&pages[0]) +
 127                                                        (virtp & ~PAGE_MASK));
 128                } else {
 129                        vpif_err("get_user_pages failed\n");
 130                        return 0;
 131                }
 132        }
 133
 134        return physp;
 135}
 136
 137/*
 138 * buffer_prepare: This is the callback function called from videobuf_qbuf()
 139 * function the buffer is prepared and user space virtual address is converted
 140 * into physical address
 141 */
 142static int vpif_buffer_prepare(struct videobuf_queue *q,
 143                               struct videobuf_buffer *vb,
 144                               enum v4l2_field field)
 145{
 146        struct vpif_fh *fh = q->priv_data;
 147        struct common_obj *common;
 148        unsigned long addr;
 149
 150        common = &fh->channel->common[VPIF_VIDEO_INDEX];
 151        if (VIDEOBUF_NEEDS_INIT == vb->state) {
 152                vb->width       = common->width;
 153                vb->height      = common->height;
 154                vb->size        = vb->width * vb->height;
 155                vb->field       = field;
 156        }
 157        vb->state = VIDEOBUF_PREPARED;
 158
 159        /* if user pointer memory mechanism is used, get the physical
 160         * address of the buffer */
 161        if (V4L2_MEMORY_USERPTR == common->memory) {
 162                if (!vb->baddr) {
 163                        vpif_err("buffer_address is 0\n");
 164                        return -EINVAL;
 165                }
 166
 167                vb->boff = vpif_uservirt_to_phys(vb->baddr);
 168                if (!ISALIGNED(vb->boff))
 169                        goto buf_align_exit;
 170        }
 171
 172        addr = vb->boff;
 173        if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
 174                if (!ISALIGNED(addr + common->ytop_off) ||
 175                    !ISALIGNED(addr + common->ybtm_off) ||
 176                    !ISALIGNED(addr + common->ctop_off) ||
 177                    !ISALIGNED(addr + common->cbtm_off))
 178                        goto buf_align_exit;
 179        }
 180        return 0;
 181
 182buf_align_exit:
 183        vpif_err("buffer offset not aligned to 8 bytes\n");
 184        return -EINVAL;
 185}
 186
 187/*
 188 * vpif_buffer_setup: This function allocates memory for the buffers
 189 */
 190static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
 191                                unsigned int *size)
 192{
 193        struct vpif_fh *fh = q->priv_data;
 194        struct channel_obj *ch = fh->channel;
 195        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 196
 197        if (V4L2_MEMORY_MMAP != common->memory)
 198                return 0;
 199
 200        *size = config_params.channel_bufsize[ch->channel_id];
 201        if (*count < config_params.min_numbuffers)
 202                *count = config_params.min_numbuffers;
 203
 204        return 0;
 205}
 206
 207/*
 208 * vpif_buffer_queue: This function adds the buffer to DMA queue
 209 */
 210static void vpif_buffer_queue(struct videobuf_queue *q,
 211                              struct videobuf_buffer *vb)
 212{
 213        struct vpif_fh *fh = q->priv_data;
 214        struct common_obj *common;
 215
 216        common = &fh->channel->common[VPIF_VIDEO_INDEX];
 217
 218        /* add the buffer to the DMA queue */
 219        list_add_tail(&vb->queue, &common->dma_queue);
 220        vb->state = VIDEOBUF_QUEUED;
 221}
 222
 223/*
 224 * vpif_buffer_release: This function is called from the videobuf layer to
 225 * free memory allocated to the buffers
 226 */
 227static void vpif_buffer_release(struct videobuf_queue *q,
 228                                struct videobuf_buffer *vb)
 229{
 230        struct vpif_fh *fh = q->priv_data;
 231        struct channel_obj *ch = fh->channel;
 232        struct common_obj *common;
 233        unsigned int buf_size = 0;
 234
 235        common = &ch->common[VPIF_VIDEO_INDEX];
 236
 237        videobuf_dma_contig_free(q, vb);
 238        vb->state = VIDEOBUF_NEEDS_INIT;
 239
 240        if (V4L2_MEMORY_MMAP != common->memory)
 241                return;
 242
 243        buf_size = config_params.channel_bufsize[ch->channel_id];
 244}
 245
 246static struct videobuf_queue_ops video_qops = {
 247        .buf_setup      = vpif_buffer_setup,
 248        .buf_prepare    = vpif_buffer_prepare,
 249        .buf_queue      = vpif_buffer_queue,
 250        .buf_release    = vpif_buffer_release,
 251};
 252static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
 253
 254static void process_progressive_mode(struct common_obj *common)
 255{
 256        unsigned long addr = 0;
 257
 258        /* Get the next buffer from buffer queue */
 259        common->next_frm = list_entry(common->dma_queue.next,
 260                                struct videobuf_buffer, queue);
 261        /* Remove that buffer from the buffer queue */
 262        list_del(&common->next_frm->queue);
 263        /* Mark status of the buffer as active */
 264        common->next_frm->state = VIDEOBUF_ACTIVE;
 265
 266        /* Set top and bottom field addrs in VPIF registers */
 267        addr = videobuf_to_dma_contig(common->next_frm);
 268        common->set_addr(addr + common->ytop_off,
 269                                 addr + common->ybtm_off,
 270                                 addr + common->ctop_off,
 271                                 addr + common->cbtm_off);
 272}
 273
 274static void process_interlaced_mode(int fid, struct common_obj *common)
 275{
 276        /* device field id and local field id are in sync */
 277        /* If this is even field */
 278        if (0 == fid) {
 279                if (common->cur_frm == common->next_frm)
 280                        return;
 281
 282                /* one frame is displayed If next frame is
 283                 *  available, release cur_frm and move on */
 284                /* Copy frame display time */
 285                do_gettimeofday(&common->cur_frm->ts);
 286                /* Change status of the cur_frm */
 287                common->cur_frm->state = VIDEOBUF_DONE;
 288                /* unlock semaphore on cur_frm */
 289                wake_up_interruptible(&common->cur_frm->done);
 290                /* Make cur_frm pointing to next_frm */
 291                common->cur_frm = common->next_frm;
 292
 293        } else if (1 == fid) {  /* odd field */
 294                if (list_empty(&common->dma_queue)
 295                    || (common->cur_frm != common->next_frm)) {
 296                        return;
 297                }
 298                /* one field is displayed configure the next
 299                 * frame if it is available else hold on current
 300                 * frame */
 301                /* Get next from the buffer queue */
 302                process_progressive_mode(common);
 303
 304        }
 305}
 306
 307/*
 308 * vpif_channel_isr: It changes status of the displayed buffer, takes next
 309 * buffer from the queue and sets its address in VPIF registers
 310 */
 311static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
 312{
 313        struct vpif_device *dev = &vpif_obj;
 314        struct channel_obj *ch;
 315        struct common_obj *common;
 316        enum v4l2_field field;
 317        int fid = -1, i;
 318        int channel_id = 0;
 319
 320        channel_id = *(int *)(dev_id);
 321        ch = dev->dev[channel_id];
 322        field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
 323        for (i = 0; i < VPIF_NUMOBJECTS; i++) {
 324                common = &ch->common[i];
 325                /* If streaming is started in this channel */
 326                if (0 == common->started)
 327                        continue;
 328
 329                if (1 == ch->vpifparams.std_info.frm_fmt) {
 330                        if (list_empty(&common->dma_queue))
 331                                continue;
 332
 333                        /* Progressive mode */
 334                        if (!channel_first_int[i][channel_id]) {
 335                                /* Mark status of the cur_frm to
 336                                 * done and unlock semaphore on it */
 337                                do_gettimeofday(&common->cur_frm->ts);
 338                                common->cur_frm->state = VIDEOBUF_DONE;
 339                                wake_up_interruptible(&common->cur_frm->done);
 340                                /* Make cur_frm pointing to next_frm */
 341                                common->cur_frm = common->next_frm;
 342                        }
 343
 344                        channel_first_int[i][channel_id] = 0;
 345                        process_progressive_mode(common);
 346                } else {
 347                        /* Interlaced mode */
 348                        /* If it is first interrupt, ignore it */
 349
 350                        if (channel_first_int[i][channel_id]) {
 351                                channel_first_int[i][channel_id] = 0;
 352                                continue;
 353                        }
 354
 355                        if (0 == i) {
 356                                ch->field_id ^= 1;
 357                                /* Get field id from VPIF registers */
 358                                fid = vpif_channel_getfid(ch->channel_id + 2);
 359                                /* If fid does not match with stored field id */
 360                                if (fid != ch->field_id) {
 361                                        /* Make them in sync */
 362                                        if (0 == fid)
 363                                                ch->field_id = fid;
 364
 365                                        return IRQ_HANDLED;
 366                                }
 367                        }
 368                        process_interlaced_mode(fid, common);
 369                }
 370        }
 371
 372        return IRQ_HANDLED;
 373}
 374
 375static int vpif_get_std_info(struct channel_obj *ch)
 376{
 377        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 378        struct video_obj *vid_ch = &ch->video;
 379        struct vpif_params *vpifparams = &ch->vpifparams;
 380        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
 381        const struct vpif_channel_config_params *config;
 382
 383        int index;
 384
 385        std_info->stdid = vid_ch->stdid;
 386        if (!std_info)
 387                return -1;
 388
 389        for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
 390                config = &ch_params[index];
 391                if (config->stdid & std_info->stdid) {
 392                        memcpy(std_info, config, sizeof(*config));
 393                        break;
 394                }
 395        }
 396
 397        if (index == ARRAY_SIZE(ch_params))
 398                return -1;
 399
 400        common->fmt.fmt.pix.width = std_info->width;
 401        common->fmt.fmt.pix.height = std_info->height;
 402        vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
 403                        common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
 404
 405        /* Set height and width paramateres */
 406        ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
 407        ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
 408
 409        return 0;
 410}
 411
 412/*
 413 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
 414 * in the top and bottom field
 415 */
 416static void vpif_calculate_offsets(struct channel_obj *ch)
 417{
 418        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 419        struct vpif_params *vpifparams = &ch->vpifparams;
 420        enum v4l2_field field = common->fmt.fmt.pix.field;
 421        struct video_obj *vid_ch = &ch->video;
 422        unsigned int hpitch, vpitch, sizeimage;
 423
 424        if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
 425                if (ch->vpifparams.std_info.frm_fmt)
 426                        vid_ch->buf_field = V4L2_FIELD_NONE;
 427                else
 428                        vid_ch->buf_field = V4L2_FIELD_INTERLACED;
 429        } else {
 430                vid_ch->buf_field = common->fmt.fmt.pix.field;
 431        }
 432
 433        if (V4L2_MEMORY_USERPTR == common->memory)
 434                sizeimage = common->fmt.fmt.pix.sizeimage;
 435        else
 436                sizeimage = config_params.channel_bufsize[ch->channel_id];
 437
 438        hpitch = common->fmt.fmt.pix.bytesperline;
 439        vpitch = sizeimage / (hpitch * 2);
 440        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 441            (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
 442                common->ytop_off = 0;
 443                common->ybtm_off = hpitch;
 444                common->ctop_off = sizeimage / 2;
 445                common->cbtm_off = sizeimage / 2 + hpitch;
 446        } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
 447                common->ytop_off = 0;
 448                common->ybtm_off = sizeimage / 4;
 449                common->ctop_off = sizeimage / 2;
 450                common->cbtm_off = common->ctop_off + sizeimage / 4;
 451        } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
 452                common->ybtm_off = 0;
 453                common->ytop_off = sizeimage / 4;
 454                common->cbtm_off = sizeimage / 2;
 455                common->ctop_off = common->cbtm_off + sizeimage / 4;
 456        }
 457
 458        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 459            (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
 460                vpifparams->video_params.storage_mode = 1;
 461        } else {
 462                vpifparams->video_params.storage_mode = 0;
 463        }
 464
 465        if (ch->vpifparams.std_info.frm_fmt == 1) {
 466                vpifparams->video_params.hpitch =
 467                    common->fmt.fmt.pix.bytesperline;
 468        } else {
 469                if ((field == V4L2_FIELD_ANY) ||
 470                        (field == V4L2_FIELD_INTERLACED))
 471                        vpifparams->video_params.hpitch =
 472                            common->fmt.fmt.pix.bytesperline * 2;
 473                else
 474                        vpifparams->video_params.hpitch =
 475                            common->fmt.fmt.pix.bytesperline;
 476        }
 477
 478        ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
 479}
 480
 481static void vpif_config_format(struct channel_obj *ch)
 482{
 483        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 484
 485        common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
 486        if (config_params.numbuffers[ch->channel_id] == 0)
 487                common->memory = V4L2_MEMORY_USERPTR;
 488        else
 489                common->memory = V4L2_MEMORY_MMAP;
 490
 491        common->fmt.fmt.pix.sizeimage =
 492                        config_params.channel_bufsize[ch->channel_id];
 493        common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
 494        common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 495}
 496
 497static int vpif_check_format(struct channel_obj *ch,
 498                             struct v4l2_pix_format *pixfmt)
 499{
 500        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 501        enum v4l2_field field = pixfmt->field;
 502        u32 sizeimage, hpitch, vpitch;
 503
 504        if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
 505                goto invalid_fmt_exit;
 506
 507        if (!(VPIF_VALID_FIELD(field)))
 508                goto invalid_fmt_exit;
 509
 510        if (pixfmt->bytesperline <= 0)
 511                goto invalid_pitch_exit;
 512
 513        if (V4L2_MEMORY_USERPTR == common->memory)
 514                sizeimage = pixfmt->sizeimage;
 515        else
 516                sizeimage = config_params.channel_bufsize[ch->channel_id];
 517
 518        if (vpif_get_std_info(ch)) {
 519                vpif_err("Error getting the standard info\n");
 520                return -EINVAL;
 521        }
 522
 523        hpitch = pixfmt->bytesperline;
 524        vpitch = sizeimage / (hpitch * 2);
 525
 526        /* Check for valid value of pitch */
 527        if ((hpitch < ch->vpifparams.std_info.width) ||
 528            (vpitch < ch->vpifparams.std_info.height))
 529                goto invalid_pitch_exit;
 530
 531        /* Check for 8 byte alignment */
 532        if (!ISALIGNED(hpitch)) {
 533                vpif_err("invalid pitch alignment\n");
 534                return -EINVAL;
 535        }
 536        pixfmt->width = common->fmt.fmt.pix.width;
 537        pixfmt->height = common->fmt.fmt.pix.height;
 538
 539        return 0;
 540
 541invalid_fmt_exit:
 542        vpif_err("invalid field format\n");
 543        return -EINVAL;
 544
 545invalid_pitch_exit:
 546        vpif_err("invalid pitch\n");
 547        return -EINVAL;
 548}
 549
 550static void vpif_config_addr(struct channel_obj *ch, int muxmode)
 551{
 552        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 553
 554        if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
 555                common->set_addr = ch3_set_videobuf_addr;
 556        } else {
 557                if (2 == muxmode)
 558                        common->set_addr = ch2_set_videobuf_addr_yc_nmux;
 559                else
 560                        common->set_addr = ch2_set_videobuf_addr;
 561        }
 562}
 563
 564/*
 565 * vpif_mmap: It is used to map kernel space buffers into user spaces
 566 */
 567static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
 568{
 569        struct vpif_fh *fh = filep->private_data;
 570        struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
 571
 572        return videobuf_mmap_mapper(&common->buffer_queue, vma);
 573}
 574
 575/*
 576 * vpif_poll: It is used for select/poll system call
 577 */
 578static unsigned int vpif_poll(struct file *filep, poll_table *wait)
 579{
 580        struct vpif_fh *fh = filep->private_data;
 581        struct channel_obj *ch = fh->channel;
 582        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 583
 584        if (common->started)
 585                return videobuf_poll_stream(filep, &common->buffer_queue, wait);
 586
 587        return 0;
 588}
 589
 590/*
 591 * vpif_open: It creates object of file handle structure and stores it in
 592 * private_data member of filepointer
 593 */
 594static int vpif_open(struct file *filep)
 595{
 596        struct video_device *vdev = video_devdata(filep);
 597        struct channel_obj *ch = NULL;
 598        struct vpif_fh *fh = NULL;
 599
 600        ch = video_get_drvdata(vdev);
 601        /* Allocate memory for the file handle object */
 602        fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
 603        if (fh == NULL) {
 604                vpif_err("unable to allocate memory for file handle object\n");
 605                return -ENOMEM;
 606        }
 607
 608        /* store pointer to fh in private_data member of filep */
 609        filep->private_data = fh;
 610        fh->channel = ch;
 611        fh->initialized = 0;
 612        if (!ch->initialized) {
 613                fh->initialized = 1;
 614                ch->initialized = 1;
 615                memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
 616        }
 617
 618        /* Increment channel usrs counter */
 619        atomic_inc(&ch->usrs);
 620        /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
 621        fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
 622        /* Initialize priority of this instance to default priority */
 623        fh->prio = V4L2_PRIORITY_UNSET;
 624        v4l2_prio_open(&ch->prio, &fh->prio);
 625
 626        return 0;
 627}
 628
 629/*
 630 * vpif_release: This function deletes buffer queue, frees the buffers and
 631 * the vpif file handle
 632 */
 633static int vpif_release(struct file *filep)
 634{
 635        struct vpif_fh *fh = filep->private_data;
 636        struct channel_obj *ch = fh->channel;
 637        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 638
 639        if (mutex_lock_interruptible(&common->lock))
 640                return -ERESTARTSYS;
 641
 642        /* if this instance is doing IO */
 643        if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
 644                /* Reset io_usrs member of channel object */
 645                common->io_usrs = 0;
 646                /* Disable channel */
 647                if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
 648                        enable_channel2(0);
 649                        channel2_intr_enable(0);
 650                }
 651                if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
 652                    (2 == common->started)) {
 653                        enable_channel3(0);
 654                        channel3_intr_enable(0);
 655                }
 656                common->started = 0;
 657                /* Free buffers allocated */
 658                videobuf_queue_cancel(&common->buffer_queue);
 659                videobuf_mmap_free(&common->buffer_queue);
 660                common->numbuffers =
 661                    config_params.numbuffers[ch->channel_id];
 662        }
 663
 664        mutex_unlock(&common->lock);
 665
 666        /* Decrement channel usrs counter */
 667        atomic_dec(&ch->usrs);
 668        /* If this file handle has initialize encoder device, reset it */
 669        if (fh->initialized)
 670                ch->initialized = 0;
 671
 672        /* Close the priority */
 673        v4l2_prio_close(&ch->prio, &fh->prio);
 674        filep->private_data = NULL;
 675        fh->initialized = 0;
 676        kfree(fh);
 677
 678        return 0;
 679}
 680
 681/* functions implementing ioctls */
 682
 683static int vpif_querycap(struct file *file, void  *priv,
 684                                struct v4l2_capability *cap)
 685{
 686        struct vpif_display_config *config = vpif_dev->platform_data;
 687
 688        cap->version = VPIF_DISPLAY_VERSION_CODE;
 689        cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
 690        strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
 691        strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
 692        strlcpy(cap->card, config->card_name, sizeof(cap->card));
 693
 694        return 0;
 695}
 696
 697static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
 698                                        struct v4l2_fmtdesc *fmt)
 699{
 700        if (fmt->index != 0) {
 701                vpif_err("Invalid format index\n");
 702                return -EINVAL;
 703        }
 704
 705        /* Fill in the information about format */
 706        fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 707        strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
 708        fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
 709
 710        return 0;
 711}
 712
 713static int vpif_g_fmt_vid_out(struct file *file, void *priv,
 714                                struct v4l2_format *fmt)
 715{
 716        struct vpif_fh *fh = priv;
 717        struct channel_obj *ch = fh->channel;
 718        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 719
 720        /* Check the validity of the buffer type */
 721        if (common->fmt.type != fmt->type)
 722                return -EINVAL;
 723
 724        /* Fill in the information about format */
 725        if (mutex_lock_interruptible(&common->lock))
 726                return -ERESTARTSYS;
 727
 728        if (vpif_get_std_info(ch)) {
 729                vpif_err("Error getting the standard info\n");
 730                return -EINVAL;
 731        }
 732
 733        *fmt = common->fmt;
 734        mutex_unlock(&common->lock);
 735        return 0;
 736}
 737
 738static int vpif_s_fmt_vid_out(struct file *file, void *priv,
 739                                struct v4l2_format *fmt)
 740{
 741        struct vpif_fh *fh = priv;
 742        struct v4l2_pix_format *pixfmt;
 743        struct channel_obj *ch = fh->channel;
 744        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 745        int ret = 0;
 746
 747        if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
 748            || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
 749                if (!fh->initialized) {
 750                        vpif_dbg(1, debug, "Channel Busy\n");
 751                        return -EBUSY;
 752                }
 753
 754                /* Check for the priority */
 755                ret = v4l2_prio_check(&ch->prio, &fh->prio);
 756                if (0 != ret)
 757                        return ret;
 758                fh->initialized = 1;
 759        }
 760
 761        if (common->started) {
 762                vpif_dbg(1, debug, "Streaming in progress\n");
 763                return -EBUSY;
 764        }
 765
 766        pixfmt = &fmt->fmt.pix;
 767        /* Check for valid field format */
 768        ret = vpif_check_format(ch, pixfmt);
 769        if (ret)
 770                return ret;
 771
 772        /* store the pix format in the channel object */
 773        common->fmt.fmt.pix = *pixfmt;
 774        /* store the format in the channel object */
 775        if (mutex_lock_interruptible(&common->lock))
 776                return -ERESTARTSYS;
 777
 778        common->fmt = *fmt;
 779        mutex_unlock(&common->lock);
 780
 781        return 0;
 782}
 783
 784static int vpif_try_fmt_vid_out(struct file *file, void *priv,
 785                                struct v4l2_format *fmt)
 786{
 787        struct vpif_fh *fh = priv;
 788        struct channel_obj *ch = fh->channel;
 789        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 790        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 791        int ret = 0;
 792
 793        ret = vpif_check_format(ch, pixfmt);
 794        if (ret) {
 795                *pixfmt = common->fmt.fmt.pix;
 796                pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
 797        }
 798
 799        return ret;
 800}
 801
 802static int vpif_reqbufs(struct file *file, void *priv,
 803                        struct v4l2_requestbuffers *reqbuf)
 804{
 805        struct vpif_fh *fh = priv;
 806        struct channel_obj *ch = fh->channel;
 807        struct common_obj *common;
 808        enum v4l2_field field;
 809        u8 index = 0;
 810        int ret = 0;
 811
 812        /* This file handle has not initialized the channel,
 813           It is not allowed to do settings */
 814        if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
 815            || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
 816                if (!fh->initialized) {
 817                        vpif_err("Channel Busy\n");
 818                        return -EBUSY;
 819                }
 820        }
 821
 822        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
 823                return -EINVAL;
 824
 825        index = VPIF_VIDEO_INDEX;
 826
 827        common = &ch->common[index];
 828        if (mutex_lock_interruptible(&common->lock))
 829                return -ERESTARTSYS;
 830
 831        if (common->fmt.type != reqbuf->type) {
 832                ret = -EINVAL;
 833                goto reqbuf_exit;
 834        }
 835
 836        if (0 != common->io_usrs) {
 837                ret = -EBUSY;
 838                goto reqbuf_exit;
 839        }
 840
 841        if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 842                if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
 843                        field = V4L2_FIELD_INTERLACED;
 844                else
 845                        field = common->fmt.fmt.pix.field;
 846        } else {
 847                field = V4L2_VBI_INTERLACED;
 848        }
 849
 850        /* Initialize videobuf queue as per the buffer type */
 851        videobuf_queue_dma_contig_init(&common->buffer_queue,
 852                                            &video_qops, NULL,
 853                                            &common->irqlock,
 854                                            reqbuf->type, field,
 855                                            sizeof(struct videobuf_buffer), fh);
 856
 857        /* Set io allowed member of file handle to TRUE */
 858        fh->io_allowed[index] = 1;
 859        /* Increment io usrs member of channel object to 1 */
 860        common->io_usrs = 1;
 861        /* Store type of memory requested in channel object */
 862        common->memory = reqbuf->memory;
 863        INIT_LIST_HEAD(&common->dma_queue);
 864
 865        /* Allocate buffers */
 866        ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
 867
 868reqbuf_exit:
 869        mutex_unlock(&common->lock);
 870        return ret;
 871}
 872
 873static int vpif_querybuf(struct file *file, void *priv,
 874                                struct v4l2_buffer *tbuf)
 875{
 876        struct vpif_fh *fh = priv;
 877        struct channel_obj *ch = fh->channel;
 878        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 879
 880        if (common->fmt.type != tbuf->type)
 881                return -EINVAL;
 882
 883        return videobuf_querybuf(&common->buffer_queue, tbuf);
 884}
 885
 886static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 887{
 888
 889        struct vpif_fh *fh = priv;
 890        struct channel_obj *ch = fh->channel;
 891        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 892        struct v4l2_buffer tbuf = *buf;
 893        struct videobuf_buffer *buf1;
 894        unsigned long addr = 0;
 895        unsigned long flags;
 896        int ret = 0;
 897
 898        if (common->fmt.type != tbuf.type)
 899                return -EINVAL;
 900
 901        if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
 902                vpif_err("fh->io_allowed\n");
 903                return -EACCES;
 904        }
 905
 906        if (!(list_empty(&common->dma_queue)) ||
 907            (common->cur_frm != common->next_frm) ||
 908            !(common->started) ||
 909            (common->started && (0 == ch->field_id)))
 910                return videobuf_qbuf(&common->buffer_queue, buf);
 911
 912        /* bufferqueue is empty store buffer address in VPIF registers */
 913        mutex_lock(&common->buffer_queue.vb_lock);
 914        buf1 = common->buffer_queue.bufs[tbuf.index];
 915        if (buf1->memory != tbuf.memory) {
 916                vpif_err("invalid buffer type\n");
 917                goto qbuf_exit;
 918        }
 919
 920        if ((buf1->state == VIDEOBUF_QUEUED) ||
 921            (buf1->state == VIDEOBUF_ACTIVE)) {
 922                vpif_err("invalid state\n");
 923                goto qbuf_exit;
 924        }
 925
 926        switch (buf1->memory) {
 927        case V4L2_MEMORY_MMAP:
 928                if (buf1->baddr == 0)
 929                        goto qbuf_exit;
 930                break;
 931
 932        case V4L2_MEMORY_USERPTR:
 933                if (tbuf.length < buf1->bsize)
 934                        goto qbuf_exit;
 935
 936                if ((VIDEOBUF_NEEDS_INIT != buf1->state)
 937                            && (buf1->baddr != tbuf.m.userptr))
 938                        vpif_buffer_release(&common->buffer_queue, buf1);
 939                        buf1->baddr = tbuf.m.userptr;
 940                break;
 941
 942        default:
 943                goto qbuf_exit;
 944        }
 945
 946        local_irq_save(flags);
 947        ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
 948                                        common->buffer_queue.field);
 949        if (ret < 0) {
 950                local_irq_restore(flags);
 951                goto qbuf_exit;
 952        }
 953
 954        buf1->state = VIDEOBUF_ACTIVE;
 955        addr = buf1->boff;
 956        common->next_frm = buf1;
 957        if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
 958                common->set_addr((addr + common->ytop_off),
 959                                 (addr + common->ybtm_off),
 960                                 (addr + common->ctop_off),
 961                                 (addr + common->cbtm_off));
 962        }
 963
 964        local_irq_restore(flags);
 965        list_add_tail(&buf1->stream, &common->buffer_queue.stream);
 966        mutex_unlock(&common->buffer_queue.vb_lock);
 967        return 0;
 968
 969qbuf_exit:
 970        mutex_unlock(&common->buffer_queue.vb_lock);
 971        return -EINVAL;
 972}
 973
 974static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
 975{
 976        struct vpif_fh *fh = priv;
 977        struct channel_obj *ch = fh->channel;
 978        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 979        int ret = 0;
 980
 981        if (!(*std_id & DM646X_V4L2_STD))
 982                return -EINVAL;
 983
 984        if (common->started) {
 985                vpif_err("streaming in progress\n");
 986                return -EBUSY;
 987        }
 988
 989        /* Call encoder subdevice function to set the standard */
 990        if (mutex_lock_interruptible(&common->lock))
 991                return -ERESTARTSYS;
 992
 993        ch->video.stdid = *std_id;
 994        /* Get the information about the standard */
 995        if (vpif_get_std_info(ch)) {
 996                vpif_err("Error getting the standard info\n");
 997                return -EINVAL;
 998        }
 999
1000        if ((ch->vpifparams.std_info.width *
1001                ch->vpifparams.std_info.height * 2) >
1002                config_params.channel_bufsize[ch->channel_id]) {
1003                vpif_err("invalid std for this size\n");
1004                ret = -EINVAL;
1005                goto s_std_exit;
1006        }
1007
1008        common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1009        /* Configure the default format information */
1010        vpif_config_format(ch);
1011
1012        ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1013                                                s_std_output, *std_id);
1014        if (ret < 0) {
1015                vpif_err("Failed to set output standard\n");
1016                goto s_std_exit;
1017        }
1018
1019        ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1020                                                        s_std, *std_id);
1021        if (ret < 0)
1022                vpif_err("Failed to set standard for sub devices\n");
1023
1024s_std_exit:
1025        mutex_unlock(&common->lock);
1026        return ret;
1027}
1028
1029static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1030{
1031        struct vpif_fh *fh = priv;
1032        struct channel_obj *ch = fh->channel;
1033
1034        *std = ch->video.stdid;
1035        return 0;
1036}
1037
1038static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1039{
1040        struct vpif_fh *fh = priv;
1041        struct channel_obj *ch = fh->channel;
1042        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1043
1044        return videobuf_dqbuf(&common->buffer_queue, p,
1045                                        (file->f_flags & O_NONBLOCK));
1046}
1047
1048static int vpif_streamon(struct file *file, void *priv,
1049                                enum v4l2_buf_type buftype)
1050{
1051        struct vpif_fh *fh = priv;
1052        struct channel_obj *ch = fh->channel;
1053        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1054        struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1055        struct vpif_params *vpif = &ch->vpifparams;
1056        struct vpif_display_config *vpif_config_data =
1057                                        vpif_dev->platform_data;
1058        unsigned long addr = 0;
1059        int ret = 0;
1060
1061        if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1062                vpif_err("buffer type not supported\n");
1063                return -EINVAL;
1064        }
1065
1066        if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1067                vpif_err("fh->io_allowed\n");
1068                return -EACCES;
1069        }
1070
1071        /* If Streaming is already started, return error */
1072        if (common->started) {
1073                vpif_err("channel->started\n");
1074                return -EBUSY;
1075        }
1076
1077        if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1078                && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1079                ch->vpifparams.std_info.ycmux_mode == 0)
1080                || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1081                && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1082                vpif_err("other channel is using\n");
1083                return -EBUSY;
1084        }
1085
1086        ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1087        if (ret < 0)
1088                return ret;
1089
1090        /* Call videobuf_streamon to start streaming  in videobuf */
1091        ret = videobuf_streamon(&common->buffer_queue);
1092        if (ret < 0) {
1093                vpif_err("videobuf_streamon\n");
1094                return ret;
1095        }
1096
1097        if (mutex_lock_interruptible(&common->lock))
1098                return -ERESTARTSYS;
1099
1100        /* If buffer queue is empty, return error */
1101        if (list_empty(&common->dma_queue)) {
1102                vpif_err("buffer queue is empty\n");
1103                ret = -EIO;
1104                goto streamon_exit;
1105        }
1106
1107        /* Get the next frame from the buffer queue */
1108        common->next_frm = common->cur_frm =
1109                            list_entry(common->dma_queue.next,
1110                                       struct videobuf_buffer, queue);
1111
1112        list_del(&common->cur_frm->queue);
1113        /* Mark state of the current frame to active */
1114        common->cur_frm->state = VIDEOBUF_ACTIVE;
1115
1116        /* Initialize field_id and started member */
1117        ch->field_id = 0;
1118        common->started = 1;
1119        if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1120                addr = common->cur_frm->boff;
1121                /* Calculate the offset for Y and C data  in the buffer */
1122                vpif_calculate_offsets(ch);
1123
1124                if ((ch->vpifparams.std_info.frm_fmt &&
1125                        ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
1126                        && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
1127                        || (!ch->vpifparams.std_info.frm_fmt
1128                        && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1129                        vpif_err("conflict in field format and std format\n");
1130                        ret = -EINVAL;
1131                        goto streamon_exit;
1132                }
1133
1134                /* clock settings */
1135                ret =
1136                 vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
1137                                                ch->vpifparams.std_info.hd_sd);
1138                if (ret < 0) {
1139                        vpif_err("can't set clock\n");
1140                        goto streamon_exit;
1141                }
1142
1143                /* set the parameters and addresses */
1144                ret = vpif_set_video_params(vpif, ch->channel_id + 2);
1145                if (ret < 0)
1146                        goto streamon_exit;
1147
1148                common->started = ret;
1149                vpif_config_addr(ch, ret);
1150                common->set_addr((addr + common->ytop_off),
1151                                 (addr + common->ybtm_off),
1152                                 (addr + common->ctop_off),
1153                                 (addr + common->cbtm_off));
1154
1155                /* Set interrupt for both the fields in VPIF
1156                   Register enable channel in VPIF register */
1157                if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1158                        channel2_intr_assert();
1159                        channel2_intr_enable(1);
1160                        enable_channel2(1);
1161                }
1162
1163                if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
1164                        || (common->started == 2)) {
1165                        channel3_intr_assert();
1166                        channel3_intr_enable(1);
1167                        enable_channel3(1);
1168                }
1169                channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1170        }
1171
1172streamon_exit:
1173        mutex_unlock(&common->lock);
1174        return ret;
1175}
1176
1177static int vpif_streamoff(struct file *file, void *priv,
1178                                enum v4l2_buf_type buftype)
1179{
1180        struct vpif_fh *fh = priv;
1181        struct channel_obj *ch = fh->channel;
1182        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1183
1184        if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1185                vpif_err("buffer type not supported\n");
1186                return -EINVAL;
1187        }
1188
1189        if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1190                vpif_err("fh->io_allowed\n");
1191                return -EACCES;
1192        }
1193
1194        if (!common->started) {
1195                vpif_err("channel->started\n");
1196                return -EINVAL;
1197        }
1198
1199        if (mutex_lock_interruptible(&common->lock))
1200                return -ERESTARTSYS;
1201
1202        if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1203                /* disable channel */
1204                if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1205                        enable_channel2(0);
1206                        channel2_intr_enable(0);
1207                }
1208                if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1209                                        (2 == common->started)) {
1210                        enable_channel3(0);
1211                        channel3_intr_enable(0);
1212                }
1213        }
1214
1215        common->started = 0;
1216        mutex_unlock(&common->lock);
1217
1218        return videobuf_streamoff(&common->buffer_queue);
1219}
1220
1221static int vpif_cropcap(struct file *file, void *priv,
1222                        struct v4l2_cropcap *crop)
1223{
1224        struct vpif_fh *fh = priv;
1225        struct channel_obj *ch = fh->channel;
1226        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1227        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1228                return -EINVAL;
1229
1230        crop->bounds.left = crop->bounds.top = 0;
1231        crop->defrect.left = crop->defrect.top = 0;
1232        crop->defrect.height = crop->bounds.height = common->height;
1233        crop->defrect.width = crop->bounds.width = common->width;
1234
1235        return 0;
1236}
1237
1238static int vpif_enum_output(struct file *file, void *fh,
1239                                struct v4l2_output *output)
1240{
1241
1242        struct vpif_display_config *config = vpif_dev->platform_data;
1243
1244        if (output->index >= config->output_count) {
1245                vpif_dbg(1, debug, "Invalid output index\n");
1246                return -EINVAL;
1247        }
1248
1249        strcpy(output->name, config->output[output->index]);
1250        output->type = V4L2_OUTPUT_TYPE_ANALOG;
1251        output->std = DM646X_V4L2_STD;
1252
1253        return 0;
1254}
1255
1256static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1257{
1258        struct vpif_fh *fh = priv;
1259        struct channel_obj *ch = fh->channel;
1260        struct video_obj *vid_ch = &ch->video;
1261        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1262        int ret = 0;
1263
1264        if (mutex_lock_interruptible(&common->lock))
1265                return -ERESTARTSYS;
1266
1267        if (common->started) {
1268                vpif_err("Streaming in progress\n");
1269                ret = -EBUSY;
1270                goto s_output_exit;
1271        }
1272
1273        ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1274                                                        s_routing, 0, i, 0);
1275
1276        if (ret < 0)
1277                vpif_err("Failed to set output standard\n");
1278
1279        vid_ch->output_id = i;
1280
1281s_output_exit:
1282        mutex_unlock(&common->lock);
1283        return ret;
1284}
1285
1286static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1287{
1288        struct vpif_fh *fh = priv;
1289        struct channel_obj *ch = fh->channel;
1290        struct video_obj *vid_ch = &ch->video;
1291
1292        *i = vid_ch->output_id;
1293
1294        return 0;
1295}
1296
1297static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1298{
1299        struct vpif_fh *fh = priv;
1300        struct channel_obj *ch = fh->channel;
1301
1302        *p = v4l2_prio_max(&ch->prio);
1303
1304        return 0;
1305}
1306
1307static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1308{
1309        struct vpif_fh *fh = priv;
1310        struct channel_obj *ch = fh->channel;
1311
1312        return v4l2_prio_change(&ch->prio, &fh->prio, p);
1313}
1314
1315/* vpif display ioctl operations */
1316static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1317        .vidioc_querycap                = vpif_querycap,
1318        .vidioc_g_priority              = vpif_g_priority,
1319        .vidioc_s_priority              = vpif_s_priority,
1320        .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1321        .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1322        .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1323        .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1324        .vidioc_reqbufs                 = vpif_reqbufs,
1325        .vidioc_querybuf                = vpif_querybuf,
1326        .vidioc_qbuf                    = vpif_qbuf,
1327        .vidioc_dqbuf                   = vpif_dqbuf,
1328        .vidioc_streamon                = vpif_streamon,
1329        .vidioc_streamoff               = vpif_streamoff,
1330        .vidioc_s_std                   = vpif_s_std,
1331        .vidioc_g_std                   = vpif_g_std,
1332        .vidioc_enum_output             = vpif_enum_output,
1333        .vidioc_s_output                = vpif_s_output,
1334        .vidioc_g_output                = vpif_g_output,
1335        .vidioc_cropcap                 = vpif_cropcap,
1336};
1337
1338static const struct v4l2_file_operations vpif_fops = {
1339        .owner          = THIS_MODULE,
1340        .open           = vpif_open,
1341        .release        = vpif_release,
1342        .ioctl          = video_ioctl2,
1343        .mmap           = vpif_mmap,
1344        .poll           = vpif_poll
1345};
1346
1347static struct video_device vpif_video_template = {
1348        .name           = "vpif",
1349        .fops           = &vpif_fops,
1350        .minor          = -1,
1351        .ioctl_ops      = &vpif_ioctl_ops,
1352        .tvnorms        = DM646X_V4L2_STD,
1353        .current_norm   = V4L2_STD_625_50,
1354
1355};
1356
1357/*Configure the channels, buffer sizei, request irq */
1358static int initialize_vpif(void)
1359{
1360        int free_channel_objects_index;
1361        int free_buffer_channel_index;
1362        int free_buffer_index;
1363        int err = 0, i, j;
1364
1365        /* Default number of buffers should be 3 */
1366        if ((ch2_numbuffers > 0) &&
1367            (ch2_numbuffers < config_params.min_numbuffers))
1368                ch2_numbuffers = config_params.min_numbuffers;
1369        if ((ch3_numbuffers > 0) &&
1370            (ch3_numbuffers < config_params.min_numbuffers))
1371                ch3_numbuffers = config_params.min_numbuffers;
1372
1373        /* Set buffer size to min buffers size if invalid buffer size is
1374         * given */
1375        if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1376                ch2_bufsize =
1377                    config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1378        if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1379                ch3_bufsize =
1380                    config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1381
1382        config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1383
1384        if (ch2_numbuffers) {
1385                config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1386                                                        ch2_bufsize;
1387        }
1388        config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1389
1390        if (ch3_numbuffers) {
1391                config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1392                                                        ch3_bufsize;
1393        }
1394
1395        /* Allocate memory for six channel objects */
1396        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1397                vpif_obj.dev[i] =
1398                    kmalloc(sizeof(struct channel_obj), GFP_KERNEL);
1399                /* If memory allocation fails, return error */
1400                if (!vpif_obj.dev[i]) {
1401                        free_channel_objects_index = i;
1402                        err = -ENOMEM;
1403                        goto vpif_init_free_channel_objects;
1404                }
1405        }
1406
1407        free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1408        free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1409        free_buffer_index = config_params.numbuffers[i - 1];
1410
1411        return 0;
1412
1413vpif_init_free_channel_objects:
1414        for (j = 0; j < free_channel_objects_index; j++)
1415                kfree(vpif_obj.dev[j]);
1416        return err;
1417}
1418
1419/*
1420 * vpif_probe: This function creates device entries by register itself to the
1421 * V4L2 driver and initializes fields of each channel objects
1422 */
1423static __init int vpif_probe(struct platform_device *pdev)
1424{
1425        struct vpif_subdev_info *subdevdata;
1426        struct vpif_display_config *config;
1427        int i, j = 0, k, q, m, err = 0;
1428        struct i2c_adapter *i2c_adap;
1429        struct common_obj *common;
1430        struct channel_obj *ch;
1431        struct video_device *vfd;
1432        struct resource *res;
1433        int subdev_count;
1434
1435        vpif_dev = &pdev->dev;
1436
1437        err = initialize_vpif();
1438
1439        if (err) {
1440                v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1441                return err;
1442        }
1443
1444        err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1445        if (err) {
1446                v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1447                return err;
1448        }
1449
1450        k = 0;
1451        while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
1452                for (i = res->start; i <= res->end; i++) {
1453                        if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
1454                                        "DM646x_Display",
1455                                (void *)(&vpif_obj.dev[k]->channel_id))) {
1456                                err = -EBUSY;
1457                                goto vpif_int_err;
1458                        }
1459                }
1460                k++;
1461        }
1462
1463        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1464
1465                /* Get the pointer to the channel object */
1466                ch = vpif_obj.dev[i];
1467
1468                /* Allocate memory for video device */
1469                vfd = video_device_alloc();
1470                if (vfd == NULL) {
1471                        for (j = 0; j < i; j++) {
1472                                ch = vpif_obj.dev[j];
1473                                video_device_release(ch->video_dev);
1474                        }
1475                        err = -ENOMEM;
1476                        goto vpif_int_err;
1477                }
1478
1479                /* Initialize field of video device */
1480                *vfd = vpif_video_template;
1481                vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1482                vfd->release = video_device_release;
1483                snprintf(vfd->name, sizeof(vfd->name),
1484                         "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
1485                         (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
1486                         (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
1487                         (VPIF_DISPLAY_VERSION_CODE) & 0xff);
1488
1489                /* Set video_dev to the video device */
1490                ch->video_dev = vfd;
1491        }
1492
1493        for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1494                ch = vpif_obj.dev[j];
1495                /* Initialize field of the channel objects */
1496                atomic_set(&ch->usrs, 0);
1497                for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1498                        ch->common[k].numbuffers = 0;
1499                        common = &ch->common[k];
1500                        common->io_usrs = 0;
1501                        common->started = 0;
1502                        spin_lock_init(&common->irqlock);
1503                        mutex_init(&common->lock);
1504                        common->numbuffers = 0;
1505                        common->set_addr = NULL;
1506                        common->ytop_off = common->ybtm_off = 0;
1507                        common->ctop_off = common->cbtm_off = 0;
1508                        common->cur_frm = common->next_frm = NULL;
1509                        memset(&common->fmt, 0, sizeof(common->fmt));
1510                        common->numbuffers = config_params.numbuffers[k];
1511
1512                }
1513                ch->initialized = 0;
1514                ch->channel_id = j;
1515                if (j < 2)
1516                        ch->common[VPIF_VIDEO_INDEX].numbuffers =
1517                            config_params.numbuffers[ch->channel_id];
1518                else
1519                        ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1520
1521                memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1522
1523                /* Initialize prio member of channel object */
1524                v4l2_prio_init(&ch->prio);
1525                ch->common[VPIF_VIDEO_INDEX].fmt.type =
1526                                                V4L2_BUF_TYPE_VIDEO_OUTPUT;
1527
1528                /* register video device */
1529                vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1530                                (int)ch, (int)&ch->video_dev);
1531
1532                err = video_register_device(ch->video_dev,
1533                                          VFL_TYPE_GRABBER, (j ? 3 : 2));
1534                if (err < 0)
1535                        goto probe_out;
1536
1537                video_set_drvdata(ch->video_dev, ch);
1538        }
1539
1540        i2c_adap = i2c_get_adapter(1);
1541        config = pdev->dev.platform_data;
1542        subdev_count = config->subdev_count;
1543        subdevdata = config->subdevinfo;
1544        vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1545                                                                GFP_KERNEL);
1546        if (vpif_obj.sd == NULL) {
1547                vpif_err("unable to allocate memory for subdevice pointers\n");
1548                err = -ENOMEM;
1549                goto probe_out;
1550        }
1551
1552        for (i = 0; i < subdev_count; i++) {
1553                vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1554                                                i2c_adap, subdevdata[i].name,
1555                                                &subdevdata[i].board_info,
1556                                                NULL);
1557                if (!vpif_obj.sd[i]) {
1558                        vpif_err("Error registering v4l2 subdevice\n");
1559                        goto probe_subdev_out;
1560                }
1561
1562                if (vpif_obj.sd[i])
1563                        vpif_obj.sd[i]->grp_id = 1 << i;
1564        }
1565
1566        return 0;
1567
1568probe_subdev_out:
1569        kfree(vpif_obj.sd);
1570probe_out:
1571        for (k = 0; k < j; k++) {
1572                ch = vpif_obj.dev[k];
1573                video_unregister_device(ch->video_dev);
1574                video_device_release(ch->video_dev);
1575                ch->video_dev = NULL;
1576        }
1577vpif_int_err:
1578        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1579        vpif_err("VPIF IRQ request failed\n");
1580        for (q = k; k >= 0; k--) {
1581                for (m = i; m >= res->start; m--)
1582                        free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
1583                res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
1584                m = res->end;
1585        }
1586
1587        return err;
1588}
1589
1590/*
1591 * vpif_remove: It un-register channels from V4L2 driver
1592 */
1593static int vpif_remove(struct platform_device *device)
1594{
1595        struct channel_obj *ch;
1596        int i;
1597
1598        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1599
1600        /* un-register device */
1601        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1602                /* Get the pointer to the channel object */
1603                ch = vpif_obj.dev[i];
1604                /* Unregister video device */
1605                video_unregister_device(ch->video_dev);
1606
1607                ch->video_dev = NULL;
1608        }
1609
1610        return 0;
1611}
1612
1613static struct platform_driver vpif_driver = {
1614        .driver = {
1615                        .name   = "vpif_display",
1616                        .owner  = THIS_MODULE,
1617        },
1618        .probe  = vpif_probe,
1619        .remove = vpif_remove,
1620};
1621
1622static __init int vpif_init(void)
1623{
1624        return platform_driver_register(&vpif_driver);
1625}
1626
1627/*
1628 * vpif_cleanup: This function un-registers device and driver to the kernel,
1629 * frees requested irq handler and de-allocates memory allocated for channel
1630 * objects.
1631 */
1632static void vpif_cleanup(void)
1633{
1634        struct platform_device *pdev;
1635        struct resource *res;
1636        int irq_num;
1637        int i = 0;
1638
1639        pdev = container_of(vpif_dev, struct platform_device, dev);
1640
1641        while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
1642                for (irq_num = res->start; irq_num <= res->end; irq_num++)
1643                        free_irq(irq_num,
1644                                 (void *)(&vpif_obj.dev[i]->channel_id));
1645                i++;
1646        }
1647
1648        platform_driver_unregister(&vpif_driver);
1649        kfree(vpif_obj.sd);
1650        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
1651                kfree(vpif_obj.dev[i]);
1652}
1653
1654module_init(vpif_init);
1655module_exit(vpif_cleanup);
1656