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