linux/drivers/media/platform/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/slab.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#include <media/v4l2-chip-ident.h>
  41
  42#include "vpif_display.h"
  43#include "vpif.h"
  44
  45MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  46MODULE_LICENSE("GPL");
  47MODULE_VERSION(VPIF_DISPLAY_VERSION);
  48
  49#define VPIF_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;
  85static void vpif_calculate_offsets(struct channel_obj *ch);
  86static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  87
  88/*
  89 * buffer_prepare: This is the callback function called from vb2_qbuf()
  90 * function the buffer is prepared and user space virtual address is converted
  91 * into physical address
  92 */
  93static int vpif_buffer_prepare(struct vb2_buffer *vb)
  94{
  95        struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  96        struct vb2_queue *q = vb->vb2_queue;
  97        struct common_obj *common;
  98        unsigned long addr;
  99
 100        common = &fh->channel->common[VPIF_VIDEO_INDEX];
 101        if (vb->state != VB2_BUF_STATE_ACTIVE &&
 102                vb->state != VB2_BUF_STATE_PREPARED) {
 103                vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
 104                if (vb2_plane_vaddr(vb, 0) &&
 105                vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
 106                        goto buf_align_exit;
 107
 108                addr = vb2_dma_contig_plane_dma_addr(vb, 0);
 109                if (q->streaming &&
 110                        (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
 111                        if (!ISALIGNED(addr + common->ytop_off) ||
 112                        !ISALIGNED(addr + common->ybtm_off) ||
 113                        !ISALIGNED(addr + common->ctop_off) ||
 114                        !ISALIGNED(addr + common->cbtm_off))
 115                                goto buf_align_exit;
 116                }
 117        }
 118        return 0;
 119
 120buf_align_exit:
 121        vpif_err("buffer offset not aligned to 8 bytes\n");
 122        return -EINVAL;
 123}
 124
 125/*
 126 * vpif_buffer_queue_setup: This function allocates memory for the buffers
 127 */
 128static int vpif_buffer_queue_setup(struct vb2_queue *vq,
 129                                const struct v4l2_format *fmt,
 130                                unsigned int *nbuffers, unsigned int *nplanes,
 131                                unsigned int sizes[], void *alloc_ctxs[])
 132{
 133        struct vpif_fh *fh = vb2_get_drv_priv(vq);
 134        struct channel_obj *ch = fh->channel;
 135        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 136        unsigned long size;
 137
 138        if (V4L2_MEMORY_MMAP == common->memory) {
 139                size = config_params.channel_bufsize[ch->channel_id];
 140                /*
 141                * Checking if the buffer size exceeds the available buffer
 142                * ycmux_mode = 0 means 1 channel mode HD and
 143                * ycmux_mode = 1 means 2 channels mode SD
 144                */
 145                if (ch->vpifparams.std_info.ycmux_mode == 0) {
 146                        if (config_params.video_limit[ch->channel_id])
 147                                while (size * *nbuffers >
 148                                        (config_params.video_limit[0]
 149                                                + config_params.video_limit[1]))
 150                                        (*nbuffers)--;
 151                } else {
 152                        if (config_params.video_limit[ch->channel_id])
 153                                while (size * *nbuffers >
 154                                config_params.video_limit[ch->channel_id])
 155                                        (*nbuffers)--;
 156                }
 157        } else {
 158                size = common->fmt.fmt.pix.sizeimage;
 159        }
 160
 161        if (*nbuffers < config_params.min_numbuffers)
 162                        *nbuffers = config_params.min_numbuffers;
 163
 164        *nplanes = 1;
 165        sizes[0] = size;
 166        alloc_ctxs[0] = common->alloc_ctx;
 167        return 0;
 168}
 169
 170/*
 171 * vpif_buffer_queue: This function adds the buffer to DMA queue
 172 */
 173static void vpif_buffer_queue(struct vb2_buffer *vb)
 174{
 175        struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
 176        struct vpif_disp_buffer *buf = container_of(vb,
 177                                struct vpif_disp_buffer, vb);
 178        struct channel_obj *ch = fh->channel;
 179        struct common_obj *common;
 180        unsigned long flags;
 181
 182        common = &ch->common[VPIF_VIDEO_INDEX];
 183
 184        /* add the buffer to the DMA queue */
 185        spin_lock_irqsave(&common->irqlock, flags);
 186        list_add_tail(&buf->list, &common->dma_queue);
 187        spin_unlock_irqrestore(&common->irqlock, flags);
 188}
 189
 190/*
 191 * vpif_buf_cleanup: This function is called from the videobuf2 layer to
 192 * free memory allocated to the buffers
 193 */
 194static void vpif_buf_cleanup(struct vb2_buffer *vb)
 195{
 196        struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
 197        struct vpif_disp_buffer *buf = container_of(vb,
 198                                        struct vpif_disp_buffer, vb);
 199        struct channel_obj *ch = fh->channel;
 200        struct common_obj *common;
 201        unsigned long flags;
 202
 203        common = &ch->common[VPIF_VIDEO_INDEX];
 204
 205        spin_lock_irqsave(&common->irqlock, flags);
 206        if (vb->state == VB2_BUF_STATE_ACTIVE)
 207                list_del_init(&buf->list);
 208        spin_unlock_irqrestore(&common->irqlock, flags);
 209}
 210
 211static void vpif_wait_prepare(struct vb2_queue *vq)
 212{
 213        struct vpif_fh *fh = vb2_get_drv_priv(vq);
 214        struct channel_obj *ch = fh->channel;
 215        struct common_obj *common;
 216
 217        common = &ch->common[VPIF_VIDEO_INDEX];
 218        mutex_unlock(&common->lock);
 219}
 220
 221static void vpif_wait_finish(struct vb2_queue *vq)
 222{
 223        struct vpif_fh *fh = vb2_get_drv_priv(vq);
 224        struct channel_obj *ch = fh->channel;
 225        struct common_obj *common;
 226
 227        common = &ch->common[VPIF_VIDEO_INDEX];
 228        mutex_lock(&common->lock);
 229}
 230
 231static int vpif_buffer_init(struct vb2_buffer *vb)
 232{
 233        struct vpif_disp_buffer *buf = container_of(vb,
 234                                        struct vpif_disp_buffer, vb);
 235
 236        INIT_LIST_HEAD(&buf->list);
 237
 238        return 0;
 239}
 240
 241static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
 242
 243static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
 244{
 245        struct vpif_display_config *vpif_config_data =
 246                                        vpif_dev->platform_data;
 247        struct vpif_fh *fh = vb2_get_drv_priv(vq);
 248        struct channel_obj *ch = fh->channel;
 249        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 250        struct vpif_params *vpif = &ch->vpifparams;
 251        unsigned long addr = 0;
 252        unsigned long flags;
 253        int ret;
 254
 255        /* If buffer queue is empty, return error */
 256        spin_lock_irqsave(&common->irqlock, flags);
 257        if (list_empty(&common->dma_queue)) {
 258                spin_unlock_irqrestore(&common->irqlock, flags);
 259                vpif_err("buffer queue is empty\n");
 260                return -EIO;
 261        }
 262
 263        /* Get the next frame from the buffer queue */
 264        common->next_frm = common->cur_frm =
 265                            list_entry(common->dma_queue.next,
 266                                       struct vpif_disp_buffer, list);
 267
 268        list_del(&common->cur_frm->list);
 269        spin_unlock_irqrestore(&common->irqlock, flags);
 270        /* Mark state of the current frame to active */
 271        common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
 272
 273        /* Initialize field_id and started member */
 274        ch->field_id = 0;
 275        common->started = 1;
 276        addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
 277        /* Calculate the offset for Y and C data  in the buffer */
 278        vpif_calculate_offsets(ch);
 279
 280        if ((ch->vpifparams.std_info.frm_fmt &&
 281                ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
 282                && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
 283                || (!ch->vpifparams.std_info.frm_fmt
 284                && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
 285                vpif_err("conflict in field format and std format\n");
 286                return -EINVAL;
 287        }
 288
 289        /* clock settings */
 290        if (vpif_config_data->set_clock) {
 291                ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
 292                ycmux_mode, ch->vpifparams.std_info.hd_sd);
 293                if (ret < 0) {
 294                        vpif_err("can't set clock\n");
 295                        return ret;
 296                }
 297        }
 298
 299        /* set the parameters and addresses */
 300        ret = vpif_set_video_params(vpif, ch->channel_id + 2);
 301        if (ret < 0)
 302                return ret;
 303
 304        common->started = ret;
 305        vpif_config_addr(ch, ret);
 306        common->set_addr((addr + common->ytop_off),
 307                            (addr + common->ybtm_off),
 308                            (addr + common->ctop_off),
 309                            (addr + common->cbtm_off));
 310
 311        /* Set interrupt for both the fields in VPIF
 312            Register enable channel in VPIF register */
 313        channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
 314        if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
 315                channel2_intr_assert();
 316                channel2_intr_enable(1);
 317                enable_channel2(1);
 318                if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
 319                        channel2_clipping_enable(1);
 320        }
 321
 322        if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
 323                || (common->started == 2)) {
 324                channel3_intr_assert();
 325                channel3_intr_enable(1);
 326                enable_channel3(1);
 327                if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
 328                        channel3_clipping_enable(1);
 329        }
 330
 331        return 0;
 332}
 333
 334/* abort streaming and wait for last buffer */
 335static int vpif_stop_streaming(struct vb2_queue *vq)
 336{
 337        struct vpif_fh *fh = vb2_get_drv_priv(vq);
 338        struct channel_obj *ch = fh->channel;
 339        struct common_obj *common;
 340        unsigned long flags;
 341
 342        if (!vb2_is_streaming(vq))
 343                return 0;
 344
 345        common = &ch->common[VPIF_VIDEO_INDEX];
 346
 347        /* release all active buffers */
 348        spin_lock_irqsave(&common->irqlock, flags);
 349        while (!list_empty(&common->dma_queue)) {
 350                common->next_frm = list_entry(common->dma_queue.next,
 351                                                struct vpif_disp_buffer, list);
 352                list_del(&common->next_frm->list);
 353                vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
 354        }
 355        spin_unlock_irqrestore(&common->irqlock, flags);
 356
 357        return 0;
 358}
 359
 360static struct vb2_ops video_qops = {
 361        .queue_setup            = vpif_buffer_queue_setup,
 362        .wait_prepare           = vpif_wait_prepare,
 363        .wait_finish            = vpif_wait_finish,
 364        .buf_init               = vpif_buffer_init,
 365        .buf_prepare            = vpif_buffer_prepare,
 366        .start_streaming        = vpif_start_streaming,
 367        .stop_streaming         = vpif_stop_streaming,
 368        .buf_cleanup            = vpif_buf_cleanup,
 369        .buf_queue              = vpif_buffer_queue,
 370};
 371
 372static void process_progressive_mode(struct common_obj *common)
 373{
 374        unsigned long addr = 0;
 375
 376        spin_lock(&common->irqlock);
 377        /* Get the next buffer from buffer queue */
 378        common->next_frm = list_entry(common->dma_queue.next,
 379                                struct vpif_disp_buffer, list);
 380        /* Remove that buffer from the buffer queue */
 381        list_del(&common->next_frm->list);
 382        spin_unlock(&common->irqlock);
 383        /* Mark status of the buffer as active */
 384        common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
 385
 386        /* Set top and bottom field addrs in VPIF registers */
 387        addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
 388        common->set_addr(addr + common->ytop_off,
 389                                 addr + common->ybtm_off,
 390                                 addr + common->ctop_off,
 391                                 addr + common->cbtm_off);
 392}
 393
 394static void process_interlaced_mode(int fid, struct common_obj *common)
 395{
 396        /* device field id and local field id are in sync */
 397        /* If this is even field */
 398        if (0 == fid) {
 399                if (common->cur_frm == common->next_frm)
 400                        return;
 401
 402                /* one frame is displayed If next frame is
 403                 *  available, release cur_frm and move on */
 404                /* Copy frame display time */
 405                v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
 406                /* Change status of the cur_frm */
 407                vb2_buffer_done(&common->cur_frm->vb,
 408                                            VB2_BUF_STATE_DONE);
 409                /* Make cur_frm pointing to next_frm */
 410                common->cur_frm = common->next_frm;
 411
 412        } else if (1 == fid) {  /* odd field */
 413                spin_lock(&common->irqlock);
 414                if (list_empty(&common->dma_queue)
 415                    || (common->cur_frm != common->next_frm)) {
 416                        spin_unlock(&common->irqlock);
 417                        return;
 418                }
 419                spin_unlock(&common->irqlock);
 420                /* one field is displayed configure the next
 421                 * frame if it is available else hold on current
 422                 * frame */
 423                /* Get next from the buffer queue */
 424                process_progressive_mode(common);
 425        }
 426}
 427
 428/*
 429 * vpif_channel_isr: It changes status of the displayed buffer, takes next
 430 * buffer from the queue and sets its address in VPIF registers
 431 */
 432static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
 433{
 434        struct vpif_device *dev = &vpif_obj;
 435        struct channel_obj *ch;
 436        struct common_obj *common;
 437        enum v4l2_field field;
 438        int fid = -1, i;
 439        int channel_id = 0;
 440
 441        channel_id = *(int *)(dev_id);
 442        if (!vpif_intr_status(channel_id + 2))
 443                return IRQ_NONE;
 444
 445        ch = dev->dev[channel_id];
 446        field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
 447        for (i = 0; i < VPIF_NUMOBJECTS; i++) {
 448                common = &ch->common[i];
 449                /* If streaming is started in this channel */
 450                if (0 == common->started)
 451                        continue;
 452
 453                if (1 == ch->vpifparams.std_info.frm_fmt) {
 454                        spin_lock(&common->irqlock);
 455                        if (list_empty(&common->dma_queue)) {
 456                                spin_unlock(&common->irqlock);
 457                                continue;
 458                        }
 459                        spin_unlock(&common->irqlock);
 460
 461                        /* Progressive mode */
 462                        if (!channel_first_int[i][channel_id]) {
 463                                /* Mark status of the cur_frm to
 464                                 * done and unlock semaphore on it */
 465                                v4l2_get_timestamp(&common->cur_frm->vb.
 466                                                   v4l2_buf.timestamp);
 467                                vb2_buffer_done(&common->cur_frm->vb,
 468                                            VB2_BUF_STATE_DONE);
 469                                /* Make cur_frm pointing to next_frm */
 470                                common->cur_frm = common->next_frm;
 471                        }
 472
 473                        channel_first_int[i][channel_id] = 0;
 474                        process_progressive_mode(common);
 475                } else {
 476                        /* Interlaced mode */
 477                        /* If it is first interrupt, ignore it */
 478
 479                        if (channel_first_int[i][channel_id]) {
 480                                channel_first_int[i][channel_id] = 0;
 481                                continue;
 482                        }
 483
 484                        if (0 == i) {
 485                                ch->field_id ^= 1;
 486                                /* Get field id from VPIF registers */
 487                                fid = vpif_channel_getfid(ch->channel_id + 2);
 488                                /* If fid does not match with stored field id */
 489                                if (fid != ch->field_id) {
 490                                        /* Make them in sync */
 491                                        if (0 == fid)
 492                                                ch->field_id = fid;
 493
 494                                        return IRQ_HANDLED;
 495                                }
 496                        }
 497                        process_interlaced_mode(fid, common);
 498                }
 499        }
 500
 501        return IRQ_HANDLED;
 502}
 503
 504static int vpif_update_std_info(struct channel_obj *ch)
 505{
 506        struct video_obj *vid_ch = &ch->video;
 507        struct vpif_params *vpifparams = &ch->vpifparams;
 508        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
 509        const struct vpif_channel_config_params *config;
 510
 511        int i;
 512
 513        for (i = 0; i < vpif_ch_params_count; i++) {
 514                config = &ch_params[i];
 515                if (config->hd_sd == 0) {
 516                        vpif_dbg(2, debug, "SD format\n");
 517                        if (config->stdid & vid_ch->stdid) {
 518                                memcpy(std_info, config, sizeof(*config));
 519                                break;
 520                        }
 521                }
 522        }
 523
 524        if (i == vpif_ch_params_count) {
 525                vpif_dbg(1, debug, "Format not found\n");
 526                return -EINVAL;
 527        }
 528
 529        return 0;
 530}
 531
 532static int vpif_update_resolution(struct channel_obj *ch)
 533{
 534        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 535        struct video_obj *vid_ch = &ch->video;
 536        struct vpif_params *vpifparams = &ch->vpifparams;
 537        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
 538
 539        if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
 540                return -EINVAL;
 541
 542        if (vid_ch->stdid) {
 543                if (vpif_update_std_info(ch))
 544                        return -EINVAL;
 545        }
 546
 547        common->fmt.fmt.pix.width = std_info->width;
 548        common->fmt.fmt.pix.height = std_info->height;
 549        vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
 550                        common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
 551
 552        /* Set height and width paramateres */
 553        common->height = std_info->height;
 554        common->width = std_info->width;
 555
 556        return 0;
 557}
 558
 559/*
 560 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
 561 * in the top and bottom field
 562 */
 563static void vpif_calculate_offsets(struct channel_obj *ch)
 564{
 565        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 566        struct vpif_params *vpifparams = &ch->vpifparams;
 567        enum v4l2_field field = common->fmt.fmt.pix.field;
 568        struct video_obj *vid_ch = &ch->video;
 569        unsigned int hpitch, vpitch, sizeimage;
 570
 571        if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
 572                if (ch->vpifparams.std_info.frm_fmt)
 573                        vid_ch->buf_field = V4L2_FIELD_NONE;
 574                else
 575                        vid_ch->buf_field = V4L2_FIELD_INTERLACED;
 576        } else {
 577                vid_ch->buf_field = common->fmt.fmt.pix.field;
 578        }
 579
 580        sizeimage = common->fmt.fmt.pix.sizeimage;
 581
 582        hpitch = common->fmt.fmt.pix.bytesperline;
 583        vpitch = sizeimage / (hpitch * 2);
 584        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 585            (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
 586                common->ytop_off = 0;
 587                common->ybtm_off = hpitch;
 588                common->ctop_off = sizeimage / 2;
 589                common->cbtm_off = sizeimage / 2 + hpitch;
 590        } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
 591                common->ytop_off = 0;
 592                common->ybtm_off = sizeimage / 4;
 593                common->ctop_off = sizeimage / 2;
 594                common->cbtm_off = common->ctop_off + sizeimage / 4;
 595        } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
 596                common->ybtm_off = 0;
 597                common->ytop_off = sizeimage / 4;
 598                common->cbtm_off = sizeimage / 2;
 599                common->ctop_off = common->cbtm_off + sizeimage / 4;
 600        }
 601
 602        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 603            (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
 604                vpifparams->video_params.storage_mode = 1;
 605        } else {
 606                vpifparams->video_params.storage_mode = 0;
 607        }
 608
 609        if (ch->vpifparams.std_info.frm_fmt == 1) {
 610                vpifparams->video_params.hpitch =
 611                    common->fmt.fmt.pix.bytesperline;
 612        } else {
 613                if ((field == V4L2_FIELD_ANY) ||
 614                        (field == V4L2_FIELD_INTERLACED))
 615                        vpifparams->video_params.hpitch =
 616                            common->fmt.fmt.pix.bytesperline * 2;
 617                else
 618                        vpifparams->video_params.hpitch =
 619                            common->fmt.fmt.pix.bytesperline;
 620        }
 621
 622        ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
 623}
 624
 625static void vpif_config_format(struct channel_obj *ch)
 626{
 627        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 628
 629        common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
 630        if (config_params.numbuffers[ch->channel_id] == 0)
 631                common->memory = V4L2_MEMORY_USERPTR;
 632        else
 633                common->memory = V4L2_MEMORY_MMAP;
 634
 635        common->fmt.fmt.pix.sizeimage =
 636                        config_params.channel_bufsize[ch->channel_id];
 637        common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
 638        common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 639}
 640
 641static int vpif_check_format(struct channel_obj *ch,
 642                             struct v4l2_pix_format *pixfmt)
 643{
 644        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 645        enum v4l2_field field = pixfmt->field;
 646        u32 sizeimage, hpitch, vpitch;
 647
 648        if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
 649                goto invalid_fmt_exit;
 650
 651        if (!(VPIF_VALID_FIELD(field)))
 652                goto invalid_fmt_exit;
 653
 654        if (pixfmt->bytesperline <= 0)
 655                goto invalid_pitch_exit;
 656
 657        sizeimage = pixfmt->sizeimage;
 658
 659        if (vpif_update_resolution(ch))
 660                return -EINVAL;
 661
 662        hpitch = pixfmt->bytesperline;
 663        vpitch = sizeimage / (hpitch * 2);
 664
 665        /* Check for valid value of pitch */
 666        if ((hpitch < ch->vpifparams.std_info.width) ||
 667            (vpitch < ch->vpifparams.std_info.height))
 668                goto invalid_pitch_exit;
 669
 670        /* Check for 8 byte alignment */
 671        if (!ISALIGNED(hpitch)) {
 672                vpif_err("invalid pitch alignment\n");
 673                return -EINVAL;
 674        }
 675        pixfmt->width = common->fmt.fmt.pix.width;
 676        pixfmt->height = common->fmt.fmt.pix.height;
 677
 678        return 0;
 679
 680invalid_fmt_exit:
 681        vpif_err("invalid field format\n");
 682        return -EINVAL;
 683
 684invalid_pitch_exit:
 685        vpif_err("invalid pitch\n");
 686        return -EINVAL;
 687}
 688
 689static void vpif_config_addr(struct channel_obj *ch, int muxmode)
 690{
 691        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 692
 693        if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
 694                common->set_addr = ch3_set_videobuf_addr;
 695        } else {
 696                if (2 == muxmode)
 697                        common->set_addr = ch2_set_videobuf_addr_yc_nmux;
 698                else
 699                        common->set_addr = ch2_set_videobuf_addr;
 700        }
 701}
 702
 703/*
 704 * vpif_mmap: It is used to map kernel space buffers into user spaces
 705 */
 706static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
 707{
 708        struct vpif_fh *fh = filep->private_data;
 709        struct channel_obj *ch = fh->channel;
 710        struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
 711        int ret;
 712
 713        vpif_dbg(2, debug, "vpif_mmap\n");
 714
 715        if (mutex_lock_interruptible(&common->lock))
 716                return -ERESTARTSYS;
 717        ret = vb2_mmap(&common->buffer_queue, vma);
 718        mutex_unlock(&common->lock);
 719        return ret;
 720}
 721
 722/*
 723 * vpif_poll: It is used for select/poll system call
 724 */
 725static unsigned int vpif_poll(struct file *filep, poll_table *wait)
 726{
 727        struct vpif_fh *fh = filep->private_data;
 728        struct channel_obj *ch = fh->channel;
 729        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 730        unsigned int res = 0;
 731
 732        if (common->started) {
 733                mutex_lock(&common->lock);
 734                res = vb2_poll(&common->buffer_queue, filep, wait);
 735                mutex_unlock(&common->lock);
 736        }
 737
 738        return res;
 739}
 740
 741/*
 742 * vpif_open: It creates object of file handle structure and stores it in
 743 * private_data member of filepointer
 744 */
 745static int vpif_open(struct file *filep)
 746{
 747        struct video_device *vdev = video_devdata(filep);
 748        struct channel_obj *ch = video_get_drvdata(vdev);
 749        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 750        struct vpif_fh *fh;
 751
 752        /* Allocate memory for the file handle object */
 753        fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
 754        if (fh == NULL) {
 755                vpif_err("unable to allocate memory for file handle object\n");
 756                return -ENOMEM;
 757        }
 758
 759        if (mutex_lock_interruptible(&common->lock)) {
 760                kfree(fh);
 761                return -ERESTARTSYS;
 762        }
 763        /* store pointer to fh in private_data member of filep */
 764        filep->private_data = fh;
 765        fh->channel = ch;
 766        fh->initialized = 0;
 767        if (!ch->initialized) {
 768                fh->initialized = 1;
 769                ch->initialized = 1;
 770                memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
 771        }
 772
 773        /* Increment channel usrs counter */
 774        atomic_inc(&ch->usrs);
 775        /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
 776        fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
 777        /* Initialize priority of this instance to default priority */
 778        fh->prio = V4L2_PRIORITY_UNSET;
 779        v4l2_prio_open(&ch->prio, &fh->prio);
 780        mutex_unlock(&common->lock);
 781
 782        return 0;
 783}
 784
 785/*
 786 * vpif_release: This function deletes buffer queue, frees the buffers and
 787 * the vpif file handle
 788 */
 789static int vpif_release(struct file *filep)
 790{
 791        struct vpif_fh *fh = filep->private_data;
 792        struct channel_obj *ch = fh->channel;
 793        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 794
 795        mutex_lock(&common->lock);
 796        /* if this instance is doing IO */
 797        if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
 798                /* Reset io_usrs member of channel object */
 799                common->io_usrs = 0;
 800                /* Disable channel */
 801                if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
 802                        enable_channel2(0);
 803                        channel2_intr_enable(0);
 804                }
 805                if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
 806                    (2 == common->started)) {
 807                        enable_channel3(0);
 808                        channel3_intr_enable(0);
 809                }
 810                common->started = 0;
 811
 812                /* Free buffers allocated */
 813                vb2_queue_release(&common->buffer_queue);
 814                vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
 815
 816                common->numbuffers =
 817                    config_params.numbuffers[ch->channel_id];
 818        }
 819
 820        /* Decrement channel usrs counter */
 821        atomic_dec(&ch->usrs);
 822        /* If this file handle has initialize encoder device, reset it */
 823        if (fh->initialized)
 824                ch->initialized = 0;
 825
 826        /* Close the priority */
 827        v4l2_prio_close(&ch->prio, fh->prio);
 828        filep->private_data = NULL;
 829        fh->initialized = 0;
 830        mutex_unlock(&common->lock);
 831        kfree(fh);
 832
 833        return 0;
 834}
 835
 836/* functions implementing ioctls */
 837/**
 838 * vpif_querycap() - QUERYCAP handler
 839 * @file: file ptr
 840 * @priv: file handle
 841 * @cap: ptr to v4l2_capability structure
 842 */
 843static int vpif_querycap(struct file *file, void  *priv,
 844                                struct v4l2_capability *cap)
 845{
 846        struct vpif_display_config *config = vpif_dev->platform_data;
 847
 848        cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
 849        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 850        snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
 851        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
 852                 dev_name(vpif_dev));
 853        strlcpy(cap->card, config->card_name, sizeof(cap->card));
 854
 855        return 0;
 856}
 857
 858static int vpif_enum_fmt_vid_out(struct file *file, void  *priv,
 859                                        struct v4l2_fmtdesc *fmt)
 860{
 861        if (fmt->index != 0) {
 862                vpif_err("Invalid format index\n");
 863                return -EINVAL;
 864        }
 865
 866        /* Fill in the information about format */
 867        fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 868        strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
 869        fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
 870
 871        return 0;
 872}
 873
 874static int vpif_g_fmt_vid_out(struct file *file, void *priv,
 875                                struct v4l2_format *fmt)
 876{
 877        struct vpif_fh *fh = priv;
 878        struct channel_obj *ch = fh->channel;
 879        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 880
 881        /* Check the validity of the buffer type */
 882        if (common->fmt.type != fmt->type)
 883                return -EINVAL;
 884
 885        if (vpif_update_resolution(ch))
 886                return -EINVAL;
 887        *fmt = common->fmt;
 888        return 0;
 889}
 890
 891static int vpif_s_fmt_vid_out(struct file *file, void *priv,
 892                                struct v4l2_format *fmt)
 893{
 894        struct vpif_fh *fh = priv;
 895        struct v4l2_pix_format *pixfmt;
 896        struct channel_obj *ch = fh->channel;
 897        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 898        int ret = 0;
 899
 900        if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
 901            || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
 902                if (!fh->initialized) {
 903                        vpif_dbg(1, debug, "Channel Busy\n");
 904                        return -EBUSY;
 905                }
 906
 907                /* Check for the priority */
 908                ret = v4l2_prio_check(&ch->prio, fh->prio);
 909                if (0 != ret)
 910                        return ret;
 911                fh->initialized = 1;
 912        }
 913
 914        if (common->started) {
 915                vpif_dbg(1, debug, "Streaming in progress\n");
 916                return -EBUSY;
 917        }
 918
 919        pixfmt = &fmt->fmt.pix;
 920        /* Check for valid field format */
 921        ret = vpif_check_format(ch, pixfmt);
 922        if (ret)
 923                return ret;
 924
 925        /* store the pix format in the channel object */
 926        common->fmt.fmt.pix = *pixfmt;
 927        /* store the format in the channel object */
 928        common->fmt = *fmt;
 929        return 0;
 930}
 931
 932static int vpif_try_fmt_vid_out(struct file *file, void *priv,
 933                                struct v4l2_format *fmt)
 934{
 935        struct vpif_fh *fh = priv;
 936        struct channel_obj *ch = fh->channel;
 937        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 938        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 939        int ret = 0;
 940
 941        ret = vpif_check_format(ch, pixfmt);
 942        if (ret) {
 943                *pixfmt = common->fmt.fmt.pix;
 944                pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
 945        }
 946
 947        return ret;
 948}
 949
 950static int vpif_reqbufs(struct file *file, void *priv,
 951                        struct v4l2_requestbuffers *reqbuf)
 952{
 953        struct vpif_fh *fh = priv;
 954        struct channel_obj *ch = fh->channel;
 955        struct common_obj *common;
 956        enum v4l2_field field;
 957        struct vb2_queue *q;
 958        u8 index = 0;
 959        int ret;
 960
 961        /* This file handle has not initialized the channel,
 962           It is not allowed to do settings */
 963        if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
 964            || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
 965                if (!fh->initialized) {
 966                        vpif_err("Channel Busy\n");
 967                        return -EBUSY;
 968                }
 969        }
 970
 971        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
 972                return -EINVAL;
 973
 974        index = VPIF_VIDEO_INDEX;
 975
 976        common = &ch->common[index];
 977
 978        if (common->fmt.type != reqbuf->type || !vpif_dev)
 979                return -EINVAL;
 980        if (0 != common->io_usrs)
 981                return -EBUSY;
 982
 983        if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 984                if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
 985                        field = V4L2_FIELD_INTERLACED;
 986                else
 987                        field = common->fmt.fmt.pix.field;
 988        } else {
 989                field = V4L2_VBI_INTERLACED;
 990        }
 991        /* Initialize videobuf2 queue as per the buffer type */
 992        common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
 993        if (IS_ERR(common->alloc_ctx)) {
 994                vpif_err("Failed to get the context\n");
 995                return PTR_ERR(common->alloc_ctx);
 996        }
 997        q = &common->buffer_queue;
 998        q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 999        q->io_modes = VB2_MMAP | VB2_USERPTR;
1000        q->drv_priv = fh;
1001        q->ops = &video_qops;
1002        q->mem_ops = &vb2_dma_contig_memops;
1003        q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1004
1005        ret = vb2_queue_init(q);
1006        if (ret) {
1007                vpif_err("vpif_display: vb2_queue_init() failed\n");
1008                vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1009                return ret;
1010        }
1011        /* Set io allowed member of file handle to TRUE */
1012        fh->io_allowed[index] = 1;
1013        /* Increment io usrs member of channel object to 1 */
1014        common->io_usrs = 1;
1015        /* Store type of memory requested in channel object */
1016        common->memory = reqbuf->memory;
1017        INIT_LIST_HEAD(&common->dma_queue);
1018        /* Allocate buffers */
1019        return vb2_reqbufs(&common->buffer_queue, reqbuf);
1020}
1021
1022static int vpif_querybuf(struct file *file, void *priv,
1023                                struct v4l2_buffer *tbuf)
1024{
1025        struct vpif_fh *fh = priv;
1026        struct channel_obj *ch = fh->channel;
1027        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1028
1029        if (common->fmt.type != tbuf->type)
1030                return -EINVAL;
1031
1032        return vb2_querybuf(&common->buffer_queue, tbuf);
1033}
1034
1035static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1036{
1037        struct vpif_fh *fh = NULL;
1038        struct channel_obj *ch = NULL;
1039        struct common_obj *common = NULL;
1040
1041        if (!buf || !priv)
1042                return -EINVAL;
1043
1044        fh = priv;
1045        ch = fh->channel;
1046        if (!ch)
1047                return -EINVAL;
1048
1049        common = &(ch->common[VPIF_VIDEO_INDEX]);
1050        if (common->fmt.type != buf->type)
1051                return -EINVAL;
1052
1053        if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1054                vpif_err("fh->io_allowed\n");
1055                return -EACCES;
1056        }
1057
1058        return vb2_qbuf(&common->buffer_queue, buf);
1059}
1060
1061static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1062{
1063        struct vpif_fh *fh = priv;
1064        struct channel_obj *ch = fh->channel;
1065        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1066        int ret = 0;
1067
1068        if (!(*std_id & VPIF_V4L2_STD))
1069                return -EINVAL;
1070
1071        if (common->started) {
1072                vpif_err("streaming in progress\n");
1073                return -EBUSY;
1074        }
1075
1076        /* Call encoder subdevice function to set the standard */
1077        ch->video.stdid = *std_id;
1078        memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1079        /* Get the information about the standard */
1080        if (vpif_update_resolution(ch))
1081                return -EINVAL;
1082
1083        if ((ch->vpifparams.std_info.width *
1084                ch->vpifparams.std_info.height * 2) >
1085                config_params.channel_bufsize[ch->channel_id]) {
1086                vpif_err("invalid std for this size\n");
1087                return -EINVAL;
1088        }
1089
1090        common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
1091        /* Configure the default format information */
1092        vpif_config_format(ch);
1093
1094        ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
1095                                                s_std_output, *std_id);
1096        if (ret < 0) {
1097                vpif_err("Failed to set output standard\n");
1098                return ret;
1099        }
1100
1101        ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
1102                                                        s_std, *std_id);
1103        if (ret < 0)
1104                vpif_err("Failed to set standard for sub devices\n");
1105        return ret;
1106}
1107
1108static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1109{
1110        struct vpif_fh *fh = priv;
1111        struct channel_obj *ch = fh->channel;
1112
1113        *std = ch->video.stdid;
1114        return 0;
1115}
1116
1117static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1118{
1119        struct vpif_fh *fh = priv;
1120        struct channel_obj *ch = fh->channel;
1121        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1122
1123        return vb2_dqbuf(&common->buffer_queue, p,
1124                                        (file->f_flags & O_NONBLOCK));
1125}
1126
1127static int vpif_streamon(struct file *file, void *priv,
1128                                enum v4l2_buf_type buftype)
1129{
1130        struct vpif_fh *fh = priv;
1131        struct channel_obj *ch = fh->channel;
1132        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1133        struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1134        int ret = 0;
1135
1136        if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1137                vpif_err("buffer type not supported\n");
1138                return -EINVAL;
1139        }
1140
1141        if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1142                vpif_err("fh->io_allowed\n");
1143                return -EACCES;
1144        }
1145
1146        /* If Streaming is already started, return error */
1147        if (common->started) {
1148                vpif_err("channel->started\n");
1149                return -EBUSY;
1150        }
1151
1152        if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
1153                && oth_ch->common[VPIF_VIDEO_INDEX].started &&
1154                ch->vpifparams.std_info.ycmux_mode == 0)
1155                || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
1156                && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1157                vpif_err("other channel is using\n");
1158                return -EBUSY;
1159        }
1160
1161        ret = vpif_check_format(ch, &common->fmt.fmt.pix);
1162        if (ret < 0)
1163                return ret;
1164
1165        /* Call vb2_streamon to start streaming in videobuf2 */
1166        ret = vb2_streamon(&common->buffer_queue, buftype);
1167        if (ret < 0) {
1168                vpif_err("vb2_streamon\n");
1169                return ret;
1170        }
1171
1172        return ret;
1173}
1174
1175static int vpif_streamoff(struct file *file, void *priv,
1176                                enum v4l2_buf_type buftype)
1177{
1178        struct vpif_fh *fh = priv;
1179        struct channel_obj *ch = fh->channel;
1180        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1181        struct vpif_display_config *vpif_config_data =
1182                                        vpif_dev->platform_data;
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 (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1200                /* disable channel */
1201                if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
1202                        if (vpif_config_data->
1203                                chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
1204                                channel2_clipping_enable(0);
1205                        enable_channel2(0);
1206                        channel2_intr_enable(0);
1207                }
1208                if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
1209                                        (2 == common->started)) {
1210                        if (vpif_config_data->
1211                                chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
1212                                channel3_clipping_enable(0);
1213                        enable_channel3(0);
1214                        channel3_intr_enable(0);
1215                }
1216        }
1217
1218        common->started = 0;
1219        return vb2_streamoff(&common->buffer_queue, buftype);
1220}
1221
1222static int vpif_cropcap(struct file *file, void *priv,
1223                        struct v4l2_cropcap *crop)
1224{
1225        struct vpif_fh *fh = priv;
1226        struct channel_obj *ch = fh->channel;
1227        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1228        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
1229                return -EINVAL;
1230
1231        crop->bounds.left = crop->bounds.top = 0;
1232        crop->defrect.left = crop->defrect.top = 0;
1233        crop->defrect.height = crop->bounds.height = common->height;
1234        crop->defrect.width = crop->bounds.width = common->width;
1235
1236        return 0;
1237}
1238
1239static int vpif_enum_output(struct file *file, void *fh,
1240                                struct v4l2_output *output)
1241{
1242
1243        struct vpif_display_config *config = vpif_dev->platform_data;
1244        struct vpif_display_chan_config *chan_cfg;
1245        struct vpif_fh *vpif_handler = fh;
1246        struct channel_obj *ch = vpif_handler->channel;
1247
1248        chan_cfg = &config->chan_config[ch->channel_id];
1249        if (output->index >= chan_cfg->output_count) {
1250                vpif_dbg(1, debug, "Invalid output index\n");
1251                return -EINVAL;
1252        }
1253
1254        *output = chan_cfg->outputs[output->index].output;
1255        return 0;
1256}
1257
1258/**
1259 * vpif_output_to_subdev() - Maps output to sub device
1260 * @vpif_cfg - global config ptr
1261 * @chan_cfg - channel config ptr
1262 * @index - Given output index from application
1263 *
1264 * lookup the sub device information for a given output index.
1265 * we report all the output to application. output table also
1266 * has sub device name for the each output
1267 */
1268static int
1269vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
1270                      struct vpif_display_chan_config *chan_cfg, int index)
1271{
1272        struct vpif_subdev_info *subdev_info;
1273        const char *subdev_name;
1274        int i;
1275
1276        vpif_dbg(2, debug, "vpif_output_to_subdev\n");
1277
1278        if (chan_cfg->outputs == NULL)
1279                return -1;
1280
1281        subdev_name = chan_cfg->outputs[index].subdev_name;
1282        if (subdev_name == NULL)
1283                return -1;
1284
1285        /* loop through the sub device list to get the sub device info */
1286        for (i = 0; i < vpif_cfg->subdev_count; i++) {
1287                subdev_info = &vpif_cfg->subdevinfo[i];
1288                if (!strcmp(subdev_info->name, subdev_name))
1289                        return i;
1290        }
1291        return -1;
1292}
1293
1294/**
1295 * vpif_set_output() - Select an output
1296 * @vpif_cfg - global config ptr
1297 * @ch - channel
1298 * @index - Given output index from application
1299 *
1300 * Select the given output.
1301 */
1302static int vpif_set_output(struct vpif_display_config *vpif_cfg,
1303                      struct channel_obj *ch, int index)
1304{
1305        struct vpif_display_chan_config *chan_cfg =
1306                &vpif_cfg->chan_config[ch->channel_id];
1307        struct vpif_subdev_info *subdev_info = NULL;
1308        struct v4l2_subdev *sd = NULL;
1309        u32 input = 0, output = 0;
1310        int sd_index;
1311        int ret;
1312
1313        sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
1314        if (sd_index >= 0) {
1315                sd = vpif_obj.sd[sd_index];
1316                subdev_info = &vpif_cfg->subdevinfo[sd_index];
1317        }
1318
1319        if (sd) {
1320                input = chan_cfg->outputs[index].input_route;
1321                output = chan_cfg->outputs[index].output_route;
1322                ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
1323                if (ret < 0 && ret != -ENOIOCTLCMD) {
1324                        vpif_err("Failed to set output\n");
1325                        return ret;
1326                }
1327
1328        }
1329        ch->output_idx = index;
1330        ch->sd = sd;
1331        if (chan_cfg->outputs != NULL)
1332                /* update tvnorms from the sub device output info */
1333                ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
1334        return 0;
1335}
1336
1337static int vpif_s_output(struct file *file, void *priv, unsigned int i)
1338{
1339        struct vpif_display_config *config = vpif_dev->platform_data;
1340        struct vpif_display_chan_config *chan_cfg;
1341        struct vpif_fh *fh = priv;
1342        struct channel_obj *ch = fh->channel;
1343        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1344
1345        chan_cfg = &config->chan_config[ch->channel_id];
1346
1347        if (i >= chan_cfg->output_count)
1348                return -EINVAL;
1349
1350        if (common->started) {
1351                vpif_err("Streaming in progress\n");
1352                return -EBUSY;
1353        }
1354
1355        return vpif_set_output(config, ch, i);
1356}
1357
1358static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
1359{
1360        struct vpif_fh *fh = priv;
1361        struct channel_obj *ch = fh->channel;
1362
1363        *i = ch->output_idx;
1364
1365        return 0;
1366}
1367
1368static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
1369{
1370        struct vpif_fh *fh = priv;
1371        struct channel_obj *ch = fh->channel;
1372
1373        *p = v4l2_prio_max(&ch->prio);
1374
1375        return 0;
1376}
1377
1378static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1379{
1380        struct vpif_fh *fh = priv;
1381        struct channel_obj *ch = fh->channel;
1382
1383        return v4l2_prio_change(&ch->prio, &fh->prio, p);
1384}
1385
1386/**
1387 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1388 * @file: file ptr
1389 * @priv: file handle
1390 * @timings: input timings
1391 */
1392static int
1393vpif_enum_dv_timings(struct file *file, void *priv,
1394                     struct v4l2_enum_dv_timings *timings)
1395{
1396        struct vpif_fh *fh = priv;
1397        struct channel_obj *ch = fh->channel;
1398        int ret;
1399
1400        ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1401        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1402                return -EINVAL;
1403        return ret;
1404}
1405
1406/**
1407 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1408 * @file: file ptr
1409 * @priv: file handle
1410 * @timings: digital video timings
1411 */
1412static int vpif_s_dv_timings(struct file *file, void *priv,
1413                struct v4l2_dv_timings *timings)
1414{
1415        struct vpif_fh *fh = priv;
1416        struct channel_obj *ch = fh->channel;
1417        struct vpif_params *vpifparams = &ch->vpifparams;
1418        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1419        struct video_obj *vid_ch = &ch->video;
1420        struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1421        int ret;
1422
1423        if (timings->type != V4L2_DV_BT_656_1120) {
1424                vpif_dbg(2, debug, "Timing type not defined\n");
1425                return -EINVAL;
1426        }
1427
1428        /* Configure subdevice timings, if any */
1429        ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1430        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1431                ret = 0;
1432        if (ret < 0) {
1433                vpif_dbg(2, debug, "Error setting custom DV timings\n");
1434                return ret;
1435        }
1436
1437        if (!(timings->bt.width && timings->bt.height &&
1438                                (timings->bt.hbackporch ||
1439                                 timings->bt.hfrontporch ||
1440                                 timings->bt.hsync) &&
1441                                timings->bt.vfrontporch &&
1442                                (timings->bt.vbackporch ||
1443                                 timings->bt.vsync))) {
1444                vpif_dbg(2, debug, "Timings for width, height, "
1445                                "horizontal back porch, horizontal sync, "
1446                                "horizontal front porch, vertical back porch, "
1447                                "vertical sync and vertical back porch "
1448                                "must be defined\n");
1449                return -EINVAL;
1450        }
1451
1452        vid_ch->dv_timings = *timings;
1453
1454        /* Configure video port timings */
1455
1456        std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1457                bt->hsync - 8;
1458        std_info->sav2eav = bt->width;
1459
1460        std_info->l1 = 1;
1461        std_info->l3 = bt->vsync + bt->vbackporch + 1;
1462
1463        if (bt->interlaced) {
1464                if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1465                        std_info->vsize = bt->height * 2 +
1466                                bt->vfrontporch + bt->vsync + bt->vbackporch +
1467                                bt->il_vfrontporch + bt->il_vsync +
1468                                bt->il_vbackporch;
1469                        std_info->l5 = std_info->vsize/2 -
1470                                (bt->vfrontporch - 1);
1471                        std_info->l7 = std_info->vsize/2 + 1;
1472                        std_info->l9 = std_info->l7 + bt->il_vsync +
1473                                bt->il_vbackporch + 1;
1474                        std_info->l11 = std_info->vsize -
1475                                (bt->il_vfrontporch - 1);
1476                } else {
1477                        vpif_dbg(2, debug, "Required timing values for "
1478                                        "interlaced BT format missing\n");
1479                        return -EINVAL;
1480                }
1481        } else {
1482                std_info->vsize = bt->height + bt->vfrontporch +
1483                        bt->vsync + bt->vbackporch;
1484                std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1485        }
1486        strncpy(std_info->name, "Custom timings BT656/1120",
1487                        VPIF_MAX_NAME);
1488        std_info->width = bt->width;
1489        std_info->height = bt->height;
1490        std_info->frm_fmt = bt->interlaced ? 0 : 1;
1491        std_info->ycmux_mode = 0;
1492        std_info->capture_format = 0;
1493        std_info->vbi_supported = 0;
1494        std_info->hd_sd = 1;
1495        std_info->stdid = 0;
1496        vid_ch->stdid = 0;
1497
1498        return 0;
1499}
1500
1501/**
1502 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1503 * @file: file ptr
1504 * @priv: file handle
1505 * @timings: digital video timings
1506 */
1507static int vpif_g_dv_timings(struct file *file, void *priv,
1508                struct v4l2_dv_timings *timings)
1509{
1510        struct vpif_fh *fh = priv;
1511        struct channel_obj *ch = fh->channel;
1512        struct video_obj *vid_ch = &ch->video;
1513
1514        *timings = vid_ch->dv_timings;
1515
1516        return 0;
1517}
1518
1519/*
1520 * vpif_g_chip_ident() - Identify the chip
1521 * @file: file ptr
1522 * @priv: file handle
1523 * @chip: chip identity
1524 *
1525 * Returns zero or -EINVAL if read operations fails.
1526 */
1527static int vpif_g_chip_ident(struct file *file, void *priv,
1528                struct v4l2_dbg_chip_ident *chip)
1529{
1530        chip->ident = V4L2_IDENT_NONE;
1531        chip->revision = 0;
1532        if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1533                        chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1534                vpif_dbg(2, debug, "match_type is invalid.\n");
1535                return -EINVAL;
1536        }
1537
1538        return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1539                        g_chip_ident, chip);
1540}
1541
1542#ifdef CONFIG_VIDEO_ADV_DEBUG
1543/*
1544 * vpif_dbg_g_register() - Read register
1545 * @file: file ptr
1546 * @priv: file handle
1547 * @reg: register to be read
1548 *
1549 * Debugging only
1550 * Returns zero or -EINVAL if read operations fails.
1551 */
1552static int vpif_dbg_g_register(struct file *file, void *priv,
1553                struct v4l2_dbg_register *reg){
1554        struct vpif_fh *fh = priv;
1555        struct channel_obj *ch = fh->channel;
1556
1557        return v4l2_subdev_call(ch->sd, core, g_register, reg);
1558}
1559
1560/*
1561 * vpif_dbg_s_register() - Write to register
1562 * @file: file ptr
1563 * @priv: file handle
1564 * @reg: register to be modified
1565 *
1566 * Debugging only
1567 * Returns zero or -EINVAL if write operations fails.
1568 */
1569static int vpif_dbg_s_register(struct file *file, void *priv,
1570                struct v4l2_dbg_register *reg){
1571        struct vpif_fh *fh = priv;
1572        struct channel_obj *ch = fh->channel;
1573
1574        return v4l2_subdev_call(ch->sd, core, s_register, reg);
1575}
1576#endif
1577
1578/*
1579 * vpif_log_status() - Status information
1580 * @file: file ptr
1581 * @priv: file handle
1582 *
1583 * Returns zero.
1584 */
1585static int vpif_log_status(struct file *filep, void *priv)
1586{
1587        /* status for sub devices */
1588        v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1589
1590        return 0;
1591}
1592
1593/* vpif display ioctl operations */
1594static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1595        .vidioc_querycap                = vpif_querycap,
1596        .vidioc_g_priority              = vpif_g_priority,
1597        .vidioc_s_priority              = vpif_s_priority,
1598        .vidioc_enum_fmt_vid_out        = vpif_enum_fmt_vid_out,
1599        .vidioc_g_fmt_vid_out           = vpif_g_fmt_vid_out,
1600        .vidioc_s_fmt_vid_out           = vpif_s_fmt_vid_out,
1601        .vidioc_try_fmt_vid_out         = vpif_try_fmt_vid_out,
1602        .vidioc_reqbufs                 = vpif_reqbufs,
1603        .vidioc_querybuf                = vpif_querybuf,
1604        .vidioc_qbuf                    = vpif_qbuf,
1605        .vidioc_dqbuf                   = vpif_dqbuf,
1606        .vidioc_streamon                = vpif_streamon,
1607        .vidioc_streamoff               = vpif_streamoff,
1608        .vidioc_s_std                   = vpif_s_std,
1609        .vidioc_g_std                   = vpif_g_std,
1610        .vidioc_enum_output             = vpif_enum_output,
1611        .vidioc_s_output                = vpif_s_output,
1612        .vidioc_g_output                = vpif_g_output,
1613        .vidioc_cropcap                 = vpif_cropcap,
1614        .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1615        .vidioc_s_dv_timings            = vpif_s_dv_timings,
1616        .vidioc_g_dv_timings            = vpif_g_dv_timings,
1617        .vidioc_g_chip_ident            = vpif_g_chip_ident,
1618#ifdef CONFIG_VIDEO_ADV_DEBUG
1619        .vidioc_g_register              = vpif_dbg_g_register,
1620        .vidioc_s_register              = vpif_dbg_s_register,
1621#endif
1622        .vidioc_log_status              = vpif_log_status,
1623};
1624
1625static const struct v4l2_file_operations vpif_fops = {
1626        .owner          = THIS_MODULE,
1627        .open           = vpif_open,
1628        .release        = vpif_release,
1629        .unlocked_ioctl = video_ioctl2,
1630        .mmap           = vpif_mmap,
1631        .poll           = vpif_poll
1632};
1633
1634static struct video_device vpif_video_template = {
1635        .name           = "vpif",
1636        .fops           = &vpif_fops,
1637        .ioctl_ops      = &vpif_ioctl_ops,
1638};
1639
1640/*Configure the channels, buffer sizei, request irq */
1641static int initialize_vpif(void)
1642{
1643        int free_channel_objects_index;
1644        int free_buffer_channel_index;
1645        int free_buffer_index;
1646        int err = 0, i, j;
1647
1648        /* Default number of buffers should be 3 */
1649        if ((ch2_numbuffers > 0) &&
1650            (ch2_numbuffers < config_params.min_numbuffers))
1651                ch2_numbuffers = config_params.min_numbuffers;
1652        if ((ch3_numbuffers > 0) &&
1653            (ch3_numbuffers < config_params.min_numbuffers))
1654                ch3_numbuffers = config_params.min_numbuffers;
1655
1656        /* Set buffer size to min buffers size if invalid buffer size is
1657         * given */
1658        if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
1659                ch2_bufsize =
1660                    config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
1661        if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
1662                ch3_bufsize =
1663                    config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
1664
1665        config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
1666
1667        if (ch2_numbuffers) {
1668                config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
1669                                                        ch2_bufsize;
1670        }
1671        config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
1672
1673        if (ch3_numbuffers) {
1674                config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
1675                                                        ch3_bufsize;
1676        }
1677
1678        /* Allocate memory for six channel objects */
1679        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1680                vpif_obj.dev[i] =
1681                    kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
1682                /* If memory allocation fails, return error */
1683                if (!vpif_obj.dev[i]) {
1684                        free_channel_objects_index = i;
1685                        err = -ENOMEM;
1686                        goto vpif_init_free_channel_objects;
1687                }
1688        }
1689
1690        free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
1691        free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
1692        free_buffer_index = config_params.numbuffers[i - 1];
1693
1694        return 0;
1695
1696vpif_init_free_channel_objects:
1697        for (j = 0; j < free_channel_objects_index; j++)
1698                kfree(vpif_obj.dev[j]);
1699        return err;
1700}
1701
1702/*
1703 * vpif_probe: This function creates device entries by register itself to the
1704 * V4L2 driver and initializes fields of each channel objects
1705 */
1706static __init int vpif_probe(struct platform_device *pdev)
1707{
1708        struct vpif_subdev_info *subdevdata;
1709        struct vpif_display_config *config;
1710        int i, j = 0, k, err = 0;
1711        int res_idx = 0;
1712        struct i2c_adapter *i2c_adap;
1713        struct common_obj *common;
1714        struct channel_obj *ch;
1715        struct video_device *vfd;
1716        struct resource *res;
1717        int subdev_count;
1718        size_t size;
1719
1720        vpif_dev = &pdev->dev;
1721        err = initialize_vpif();
1722
1723        if (err) {
1724                v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1725                return err;
1726        }
1727
1728        err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1729        if (err) {
1730                v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1731                return err;
1732        }
1733
1734        while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1735                for (i = res->start; i <= res->end; i++) {
1736                        if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
1737                                        "VPIF_Display", (void *)
1738                                        (&vpif_obj.dev[res_idx]->channel_id))) {
1739                                err = -EBUSY;
1740                                for (j = 0; j < i; j++)
1741                                        free_irq(j, (void *)
1742                                        (&vpif_obj.dev[res_idx]->channel_id));
1743                                goto vpif_int_err;
1744                        }
1745                }
1746                res_idx++;
1747        }
1748
1749        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1750                /* Get the pointer to the channel object */
1751                ch = vpif_obj.dev[i];
1752
1753                /* Allocate memory for video device */
1754                vfd = video_device_alloc();
1755                if (vfd == NULL) {
1756                        for (j = 0; j < i; j++) {
1757                                ch = vpif_obj.dev[j];
1758                                video_device_release(ch->video_dev);
1759                        }
1760                        err = -ENOMEM;
1761                        goto vpif_int_err;
1762                }
1763
1764                /* Initialize field of video device */
1765                *vfd = vpif_video_template;
1766                vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1767                vfd->release = video_device_release;
1768                vfd->vfl_dir = VFL_DIR_TX;
1769                snprintf(vfd->name, sizeof(vfd->name),
1770                         "VPIF_Display_DRIVER_V%s",
1771                         VPIF_DISPLAY_VERSION);
1772
1773                /* Set video_dev to the video device */
1774                ch->video_dev = vfd;
1775        }
1776
1777        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1778        if (res) {
1779                size = resource_size(res);
1780                /* The resources are divided into two equal memory and when
1781                 * we have HD output we can add them together
1782                 */
1783                for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1784                        ch = vpif_obj.dev[j];
1785                        ch->channel_id = j;
1786
1787                        /* only enabled if second resource exists */
1788                        config_params.video_limit[ch->channel_id] = 0;
1789                        if (size)
1790                                config_params.video_limit[ch->channel_id] =
1791                                                                        size/2;
1792                }
1793        }
1794
1795        i2c_adap = i2c_get_adapter(1);
1796        config = pdev->dev.platform_data;
1797        subdev_count = config->subdev_count;
1798        subdevdata = config->subdevinfo;
1799        vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1800                                                                GFP_KERNEL);
1801        if (vpif_obj.sd == NULL) {
1802                vpif_err("unable to allocate memory for subdevice pointers\n");
1803                err = -ENOMEM;
1804                goto vpif_sd_error;
1805        }
1806
1807        for (i = 0; i < subdev_count; i++) {
1808                vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1809                                                i2c_adap,
1810                                                &subdevdata[i].board_info,
1811                                                NULL);
1812                if (!vpif_obj.sd[i]) {
1813                        vpif_err("Error registering v4l2 subdevice\n");
1814                        goto probe_subdev_out;
1815                }
1816
1817                if (vpif_obj.sd[i])
1818                        vpif_obj.sd[i]->grp_id = 1 << i;
1819        }
1820
1821        for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1822                ch = vpif_obj.dev[j];
1823                /* Initialize field of the channel objects */
1824                atomic_set(&ch->usrs, 0);
1825                for (k = 0; k < VPIF_NUMOBJECTS; k++) {
1826                        ch->common[k].numbuffers = 0;
1827                        common = &ch->common[k];
1828                        common->io_usrs = 0;
1829                        common->started = 0;
1830                        spin_lock_init(&common->irqlock);
1831                        mutex_init(&common->lock);
1832                        common->numbuffers = 0;
1833                        common->set_addr = NULL;
1834                        common->ytop_off = common->ybtm_off = 0;
1835                        common->ctop_off = common->cbtm_off = 0;
1836                        common->cur_frm = common->next_frm = NULL;
1837                        memset(&common->fmt, 0, sizeof(common->fmt));
1838                        common->numbuffers = config_params.numbuffers[k];
1839
1840                }
1841                ch->initialized = 0;
1842                if (subdev_count)
1843                        ch->sd = vpif_obj.sd[0];
1844                ch->channel_id = j;
1845                if (j < 2)
1846                        ch->common[VPIF_VIDEO_INDEX].numbuffers =
1847                            config_params.numbuffers[ch->channel_id];
1848                else
1849                        ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
1850
1851                memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1852
1853                /* Initialize prio member of channel object */
1854                v4l2_prio_init(&ch->prio);
1855                ch->common[VPIF_VIDEO_INDEX].fmt.type =
1856                                                V4L2_BUF_TYPE_VIDEO_OUTPUT;
1857                ch->video_dev->lock = &common->lock;
1858                video_set_drvdata(ch->video_dev, ch);
1859
1860                /* select output 0 */
1861                err = vpif_set_output(config, ch, 0);
1862                if (err)
1863                        goto probe_out;
1864
1865                /* register video device */
1866                vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
1867                                (int)ch, (int)&ch->video_dev);
1868
1869                err = video_register_device(ch->video_dev,
1870                                          VFL_TYPE_GRABBER, (j ? 3 : 2));
1871                if (err < 0)
1872                        goto probe_out;
1873        }
1874
1875        v4l2_info(&vpif_obj.v4l2_dev,
1876                        " VPIF display driver initialized\n");
1877        return 0;
1878
1879probe_out:
1880        for (k = 0; k < j; k++) {
1881                ch = vpif_obj.dev[k];
1882                video_unregister_device(ch->video_dev);
1883                video_device_release(ch->video_dev);
1884                ch->video_dev = NULL;
1885        }
1886probe_subdev_out:
1887        kfree(vpif_obj.sd);
1888vpif_sd_error:
1889        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1890                ch = vpif_obj.dev[i];
1891                /* Note: does nothing if ch->video_dev == NULL */
1892                video_device_release(ch->video_dev);
1893        }
1894vpif_int_err:
1895        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1896        vpif_err("VPIF IRQ request failed\n");
1897        for (i = 0; i < res_idx; i++) {
1898                res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1899                for (j = res->start; j <= res->end; j++)
1900                        free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
1901        }
1902
1903        return err;
1904}
1905
1906/*
1907 * vpif_remove: It un-register channels from V4L2 driver
1908 */
1909static int vpif_remove(struct platform_device *device)
1910{
1911        struct channel_obj *ch;
1912        int i;
1913
1914        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1915
1916        /* un-register device */
1917        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1918                /* Get the pointer to the channel object */
1919                ch = vpif_obj.dev[i];
1920                /* Unregister video device */
1921                video_unregister_device(ch->video_dev);
1922
1923                ch->video_dev = NULL;
1924        }
1925
1926        return 0;
1927}
1928
1929#ifdef CONFIG_PM
1930static int vpif_suspend(struct device *dev)
1931{
1932        struct common_obj *common;
1933        struct channel_obj *ch;
1934        int i;
1935
1936        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1937                /* Get the pointer to the channel object */
1938                ch = vpif_obj.dev[i];
1939                common = &ch->common[VPIF_VIDEO_INDEX];
1940                mutex_lock(&common->lock);
1941                if (atomic_read(&ch->usrs) && common->io_usrs) {
1942                        /* Disable channel */
1943                        if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1944                                enable_channel2(0);
1945                                channel2_intr_enable(0);
1946                        }
1947                        if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1948                                        common->started == 2) {
1949                                enable_channel3(0);
1950                                channel3_intr_enable(0);
1951                        }
1952                }
1953                mutex_unlock(&common->lock);
1954        }
1955
1956        return 0;
1957}
1958
1959static int vpif_resume(struct device *dev)
1960{
1961
1962        struct common_obj *common;
1963        struct channel_obj *ch;
1964        int i;
1965
1966        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1967                /* Get the pointer to the channel object */
1968                ch = vpif_obj.dev[i];
1969                common = &ch->common[VPIF_VIDEO_INDEX];
1970                mutex_lock(&common->lock);
1971                if (atomic_read(&ch->usrs) && common->io_usrs) {
1972                        /* Enable channel */
1973                        if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1974                                enable_channel2(1);
1975                                channel2_intr_enable(1);
1976                        }
1977                        if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1978                                        common->started == 2) {
1979                                enable_channel3(1);
1980                                channel3_intr_enable(1);
1981                        }
1982                }
1983                mutex_unlock(&common->lock);
1984        }
1985
1986        return 0;
1987}
1988
1989static const struct dev_pm_ops vpif_pm = {
1990        .suspend        = vpif_suspend,
1991        .resume         = vpif_resume,
1992};
1993
1994#define vpif_pm_ops (&vpif_pm)
1995#else
1996#define vpif_pm_ops NULL
1997#endif
1998
1999static __refdata struct platform_driver vpif_driver = {
2000        .driver = {
2001                        .name   = "vpif_display",
2002                        .owner  = THIS_MODULE,
2003                        .pm     = vpif_pm_ops,
2004        },
2005        .probe  = vpif_probe,
2006        .remove = vpif_remove,
2007};
2008
2009static __init int vpif_init(void)
2010{
2011        return platform_driver_register(&vpif_driver);
2012}
2013
2014/*
2015 * vpif_cleanup: This function un-registers device and driver to the kernel,
2016 * frees requested irq handler and de-allocates memory allocated for channel
2017 * objects.
2018 */
2019static void vpif_cleanup(void)
2020{
2021        struct platform_device *pdev;
2022        struct resource *res;
2023        int irq_num;
2024        int i = 0;
2025
2026        pdev = container_of(vpif_dev, struct platform_device, dev);
2027
2028        while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2029                for (irq_num = res->start; irq_num <= res->end; irq_num++)
2030                        free_irq(irq_num,
2031                                 (void *)(&vpif_obj.dev[i]->channel_id));
2032                i++;
2033        }
2034
2035        platform_driver_unregister(&vpif_driver);
2036        kfree(vpif_obj.sd);
2037        for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
2038                kfree(vpif_obj.dev[i]);
2039}
2040
2041module_init(vpif_init);
2042module_exit(vpif_cleanup);
2043