linux/drivers/media/platform/davinci/vpbe_display.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
   3 *
   4 * This program is free software; you can redistribute it and/or
   5 * modify it under the terms of the GNU General Public License as
   6 * published by the Free Software Foundation version 2.
   7 *
   8 * This program is distributed WITHOUT ANY WARRANTY of any
   9 * kind, whether express or implied; without even the implied warranty
  10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11 * GNU General Public License for more details.
  12 */
  13#include <linux/kernel.h>
  14#include <linux/init.h>
  15#include <linux/module.h>
  16#include <linux/errno.h>
  17#include <linux/interrupt.h>
  18#include <linux/string.h>
  19#include <linux/wait.h>
  20#include <linux/time.h>
  21#include <linux/platform_device.h>
  22#include <linux/irq.h>
  23#include <linux/mm.h>
  24#include <linux/mutex.h>
  25#include <linux/videodev2.h>
  26#include <linux/slab.h>
  27
  28#include <asm/pgtable.h>
  29#include <mach/cputype.h>
  30
  31#include <media/v4l2-dev.h>
  32#include <media/v4l2-common.h>
  33#include <media/v4l2-ioctl.h>
  34#include <media/v4l2-device.h>
  35#include <media/davinci/vpbe_display.h>
  36#include <media/davinci/vpbe_types.h>
  37#include <media/davinci/vpbe.h>
  38#include <media/davinci/vpbe_venc.h>
  39#include <media/davinci/vpbe_osd.h>
  40#include "vpbe_venc_regs.h"
  41
  42#define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
  43
  44static int debug;
  45
  46#define VPBE_DEFAULT_NUM_BUFS 3
  47
  48module_param(debug, int, 0644);
  49
  50static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  51                        struct vpbe_layer *layer);
  52
  53static int venc_is_second_field(struct vpbe_display *disp_dev)
  54{
  55        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  56        int ret;
  57        int val;
  58
  59        ret = v4l2_subdev_call(vpbe_dev->venc,
  60                               core,
  61                               ioctl,
  62                               VENC_GET_FLD,
  63                               &val);
  64        if (ret < 0) {
  65                v4l2_err(&vpbe_dev->v4l2_dev,
  66                         "Error in getting Field ID 0\n");
  67        }
  68        return val;
  69}
  70
  71static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
  72                                struct vpbe_layer *layer)
  73{
  74        if (layer->cur_frm == layer->next_frm)
  75                return;
  76
  77        layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  78        vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  79        /* Make cur_frm pointing to next_frm */
  80        layer->cur_frm = layer->next_frm;
  81}
  82
  83static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  84                                struct vpbe_layer *layer)
  85{
  86        struct osd_state *osd_device = disp_obj->osd_device;
  87        unsigned long addr;
  88
  89        spin_lock(&disp_obj->dma_queue_lock);
  90        if (list_empty(&layer->dma_queue) ||
  91                (layer->cur_frm != layer->next_frm)) {
  92                spin_unlock(&disp_obj->dma_queue_lock);
  93                return;
  94        }
  95        /*
  96         * one field is displayed configure
  97         * the next frame if it is available
  98         * otherwise hold on current frame
  99         * Get next from the buffer queue
 100         */
 101        layer->next_frm = list_entry(layer->dma_queue.next,
 102                          struct  vpbe_disp_buffer, list);
 103        /* Remove that from the buffer queue */
 104        list_del(&layer->next_frm->list);
 105        spin_unlock(&disp_obj->dma_queue_lock);
 106        /* Mark state of the frame to active */
 107        layer->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
 108        addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb.vb2_buf, 0);
 109        osd_device->ops.start_layer(osd_device,
 110                        layer->layer_info.id,
 111                        addr,
 112                        disp_obj->cbcr_ofst);
 113}
 114
 115/* interrupt service routine */
 116static irqreturn_t venc_isr(int irq, void *arg)
 117{
 118        struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
 119        struct vpbe_layer *layer;
 120        static unsigned last_event;
 121        unsigned event = 0;
 122        int fid;
 123        int i;
 124
 125        if ((NULL == arg) || (NULL == disp_dev->dev[0]))
 126                return IRQ_HANDLED;
 127
 128        if (venc_is_second_field(disp_dev))
 129                event |= VENC_SECOND_FIELD;
 130        else
 131                event |= VENC_FIRST_FIELD;
 132
 133        if (event == (last_event & ~VENC_END_OF_FRAME)) {
 134                /*
 135                * If the display is non-interlaced, then we need to flag the
 136                * end-of-frame event at every interrupt regardless of the
 137                * value of the FIDST bit.  We can conclude that the display is
 138                * non-interlaced if the value of the FIDST bit is unchanged
 139                * from the previous interrupt.
 140                */
 141                event |= VENC_END_OF_FRAME;
 142        } else if (event == VENC_SECOND_FIELD) {
 143                /* end-of-frame for interlaced display */
 144                event |= VENC_END_OF_FRAME;
 145        }
 146        last_event = event;
 147
 148        for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
 149                layer = disp_dev->dev[i];
 150
 151                if (!vb2_start_streaming_called(&layer->buffer_queue))
 152                        continue;
 153
 154                if (layer->layer_first_int) {
 155                        layer->layer_first_int = 0;
 156                        continue;
 157                }
 158                /* Check the field format */
 159                if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
 160                        (event & VENC_END_OF_FRAME)) {
 161                        /* Progressive mode */
 162
 163                        vpbe_isr_even_field(disp_dev, layer);
 164                        vpbe_isr_odd_field(disp_dev, layer);
 165                } else {
 166                /* Interlaced mode */
 167
 168                        layer->field_id ^= 1;
 169                        if (event & VENC_FIRST_FIELD)
 170                                fid = 0;
 171                        else
 172                                fid = 1;
 173
 174                        /*
 175                        * If field id does not match with store
 176                        * field id
 177                        */
 178                        if (fid != layer->field_id) {
 179                                /* Make them in sync */
 180                                layer->field_id = fid;
 181                                continue;
 182                        }
 183                        /*
 184                        * device field id and local field id are
 185                        * in sync. If this is even field
 186                        */
 187                        if (0 == fid)
 188                                vpbe_isr_even_field(disp_dev, layer);
 189                        else  /* odd field */
 190                                vpbe_isr_odd_field(disp_dev, layer);
 191                }
 192        }
 193
 194        return IRQ_HANDLED;
 195}
 196
 197/*
 198 * vpbe_buffer_prepare()
 199 * This is the callback function called from vb2_qbuf() function
 200 * the buffer is prepared and user space virtual address is converted into
 201 * physical address
 202 */
 203static int vpbe_buffer_prepare(struct vb2_buffer *vb)
 204{
 205        struct vb2_queue *q = vb->vb2_queue;
 206        struct vpbe_layer *layer = vb2_get_drv_priv(q);
 207        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 208        unsigned long addr;
 209
 210        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 211                                "vpbe_buffer_prepare\n");
 212
 213        vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
 214        if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
 215                return -EINVAL;
 216
 217        addr = vb2_dma_contig_plane_dma_addr(vb, 0);
 218        if (!IS_ALIGNED(addr, 8)) {
 219                v4l2_err(&vpbe_dev->v4l2_dev,
 220                         "buffer_prepare:offset is not aligned to 32 bytes\n");
 221                return -EINVAL;
 222        }
 223        return 0;
 224}
 225
 226/*
 227 * vpbe_buffer_setup()
 228 * This function allocates memory for the buffers
 229 */
 230static int
 231vpbe_buffer_queue_setup(struct vb2_queue *vq,
 232                        unsigned int *nbuffers, unsigned int *nplanes,
 233                        unsigned int sizes[], void *alloc_ctxs[])
 234
 235{
 236        /* Get the file handle object and layer object */
 237        struct vpbe_layer *layer = vb2_get_drv_priv(vq);
 238        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 239
 240        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
 241
 242        /* Store number of buffers allocated in numbuffer member */
 243        if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
 244                *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
 245        alloc_ctxs[0] = layer->alloc_ctx;
 246
 247        if (*nplanes)
 248                return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
 249
 250        *nplanes = 1;
 251        sizes[0] = layer->pix_fmt.sizeimage;
 252
 253        return 0;
 254}
 255
 256/*
 257 * vpbe_buffer_queue()
 258 * This function adds the buffer to DMA queue
 259 */
 260static void vpbe_buffer_queue(struct vb2_buffer *vb)
 261{
 262        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 263        /* Get the file handle object and layer object */
 264        struct vpbe_disp_buffer *buf = container_of(vbuf,
 265                                struct vpbe_disp_buffer, vb);
 266        struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
 267        struct vpbe_display *disp = layer->disp_dev;
 268        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 269        unsigned long flags;
 270
 271        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 272                        "vpbe_buffer_queue\n");
 273
 274        /* add the buffer to the DMA queue */
 275        spin_lock_irqsave(&disp->dma_queue_lock, flags);
 276        list_add_tail(&buf->list, &layer->dma_queue);
 277        spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
 278}
 279
 280static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
 281{
 282        struct vpbe_layer *layer = vb2_get_drv_priv(vq);
 283        struct osd_state *osd_device = layer->disp_dev->osd_device;
 284        int ret;
 285
 286         osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
 287
 288        /* Get the next frame from the buffer queue */
 289        layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
 290                                struct vpbe_disp_buffer, list);
 291        /* Remove buffer from the buffer queue */
 292        list_del(&layer->cur_frm->list);
 293        /* Mark state of the current frame to active */
 294        layer->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
 295        /* Initialize field_id and started member */
 296        layer->field_id = 0;
 297
 298        /* Set parameters in OSD and VENC */
 299        ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
 300        if (ret < 0) {
 301                struct vpbe_disp_buffer *buf, *tmp;
 302
 303                vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
 304                                VB2_BUF_STATE_QUEUED);
 305                list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
 306                        list_del(&buf->list);
 307                        vb2_buffer_done(&buf->vb.vb2_buf,
 308                                        VB2_BUF_STATE_QUEUED);
 309                }
 310
 311                return ret;
 312        }
 313
 314        /*
 315         * if request format is yuv420 semiplanar, need to
 316         * enable both video windows
 317         */
 318        layer->layer_first_int = 1;
 319
 320        return ret;
 321}
 322
 323static void vpbe_stop_streaming(struct vb2_queue *vq)
 324{
 325        struct vpbe_layer *layer = vb2_get_drv_priv(vq);
 326        struct osd_state *osd_device = layer->disp_dev->osd_device;
 327        struct vpbe_display *disp = layer->disp_dev;
 328        unsigned long flags;
 329
 330        if (!vb2_is_streaming(vq))
 331                return;
 332
 333        osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
 334
 335        /* release all active buffers */
 336        spin_lock_irqsave(&disp->dma_queue_lock, flags);
 337        if (layer->cur_frm == layer->next_frm) {
 338                vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
 339                                VB2_BUF_STATE_ERROR);
 340        } else {
 341                if (layer->cur_frm != NULL)
 342                        vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
 343                                        VB2_BUF_STATE_ERROR);
 344                if (layer->next_frm != NULL)
 345                        vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
 346                                        VB2_BUF_STATE_ERROR);
 347        }
 348
 349        while (!list_empty(&layer->dma_queue)) {
 350                layer->next_frm = list_entry(layer->dma_queue.next,
 351                                                struct vpbe_disp_buffer, list);
 352                list_del(&layer->next_frm->list);
 353                vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
 354                                VB2_BUF_STATE_ERROR);
 355        }
 356        spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
 357}
 358
 359static struct vb2_ops video_qops = {
 360        .queue_setup = vpbe_buffer_queue_setup,
 361        .wait_prepare = vb2_ops_wait_prepare,
 362        .wait_finish = vb2_ops_wait_finish,
 363        .buf_prepare = vpbe_buffer_prepare,
 364        .start_streaming = vpbe_start_streaming,
 365        .stop_streaming = vpbe_stop_streaming,
 366        .buf_queue = vpbe_buffer_queue,
 367};
 368
 369static
 370struct vpbe_layer*
 371_vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
 372                        struct vpbe_layer *layer)
 373{
 374        enum vpbe_display_device_id thiswin, otherwin;
 375        thiswin = layer->device_id;
 376
 377        otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
 378        VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
 379        return disp_dev->dev[otherwin];
 380}
 381
 382static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
 383                        struct vpbe_layer *layer)
 384{
 385        struct osd_layer_config *cfg  = &layer->layer_info.config;
 386        struct osd_state *osd_device = disp_dev->osd_device;
 387        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 388        unsigned long addr;
 389        int ret;
 390
 391        addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb.vb2_buf, 0);
 392        /* Set address in the display registers */
 393        osd_device->ops.start_layer(osd_device,
 394                                    layer->layer_info.id,
 395                                    addr,
 396                                    disp_dev->cbcr_ofst);
 397
 398        ret = osd_device->ops.enable_layer(osd_device,
 399                                layer->layer_info.id, 0);
 400        if (ret < 0) {
 401                v4l2_err(&vpbe_dev->v4l2_dev,
 402                        "Error in enabling osd window layer 0\n");
 403                return -1;
 404        }
 405
 406        /* Enable the window */
 407        layer->layer_info.enable = 1;
 408        if (cfg->pixfmt == PIXFMT_NV12) {
 409                struct vpbe_layer *otherlayer =
 410                        _vpbe_display_get_other_win_layer(disp_dev, layer);
 411
 412                ret = osd_device->ops.enable_layer(osd_device,
 413                                otherlayer->layer_info.id, 1);
 414                if (ret < 0) {
 415                        v4l2_err(&vpbe_dev->v4l2_dev,
 416                                "Error in enabling osd window layer 1\n");
 417                        return -1;
 418                }
 419                otherlayer->layer_info.enable = 1;
 420        }
 421        return 0;
 422}
 423
 424static void
 425vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
 426                        struct vpbe_layer *layer,
 427                        int expected_xsize, int expected_ysize)
 428{
 429        struct display_layer_info *layer_info = &layer->layer_info;
 430        struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
 431        struct osd_layer_config *cfg  = &layer->layer_info.config;
 432        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 433        int calculated_xsize;
 434        int h_exp = 0;
 435        int v_exp = 0;
 436        int h_scale;
 437        int v_scale;
 438
 439        v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
 440
 441        /*
 442         * Application initially set the image format. Current display
 443         * size is obtained from the vpbe display controller. expected_xsize
 444         * and expected_ysize are set through S_CROP ioctl. Based on this,
 445         * driver will calculate the scale factors for vertical and
 446         * horizontal direction so that the image is displayed scaled
 447         * and expanded. Application uses expansion to display the image
 448         * in a square pixel. Otherwise it is displayed using displays
 449         * pixel aspect ratio.It is expected that application chooses
 450         * the crop coordinates for cropped or scaled display. if crop
 451         * size is less than the image size, it is displayed cropped or
 452         * it is displayed scaled and/or expanded.
 453         *
 454         * to begin with, set the crop window same as expected. Later we
 455         * will override with scaled window size
 456         */
 457
 458        cfg->xsize = pixfmt->width;
 459        cfg->ysize = pixfmt->height;
 460        layer_info->h_zoom = ZOOM_X1;   /* no horizontal zoom */
 461        layer_info->v_zoom = ZOOM_X1;   /* no horizontal zoom */
 462        layer_info->h_exp = H_EXP_OFF;  /* no horizontal zoom */
 463        layer_info->v_exp = V_EXP_OFF;  /* no horizontal zoom */
 464
 465        if (pixfmt->width < expected_xsize) {
 466                h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
 467                if (h_scale < 2)
 468                        h_scale = 1;
 469                else if (h_scale >= 4)
 470                        h_scale = 4;
 471                else
 472                        h_scale = 2;
 473                cfg->xsize *= h_scale;
 474                if (cfg->xsize < expected_xsize) {
 475                        if ((standard_id & V4L2_STD_525_60) ||
 476                        (standard_id & V4L2_STD_625_50)) {
 477                                calculated_xsize = (cfg->xsize *
 478                                        VPBE_DISPLAY_H_EXP_RATIO_N) /
 479                                        VPBE_DISPLAY_H_EXP_RATIO_D;
 480                                if (calculated_xsize <= expected_xsize) {
 481                                        h_exp = 1;
 482                                        cfg->xsize = calculated_xsize;
 483                                }
 484                        }
 485                }
 486                if (h_scale == 2)
 487                        layer_info->h_zoom = ZOOM_X2;
 488                else if (h_scale == 4)
 489                        layer_info->h_zoom = ZOOM_X4;
 490                if (h_exp)
 491                        layer_info->h_exp = H_EXP_9_OVER_8;
 492        } else {
 493                /* no scaling, only cropping. Set display area to crop area */
 494                cfg->xsize = expected_xsize;
 495        }
 496
 497        if (pixfmt->height < expected_ysize) {
 498                v_scale = expected_ysize / pixfmt->height;
 499                if (v_scale < 2)
 500                        v_scale = 1;
 501                else if (v_scale >= 4)
 502                        v_scale = 4;
 503                else
 504                        v_scale = 2;
 505                cfg->ysize *= v_scale;
 506                if (cfg->ysize < expected_ysize) {
 507                        if ((standard_id & V4L2_STD_625_50)) {
 508                                calculated_xsize = (cfg->ysize *
 509                                        VPBE_DISPLAY_V_EXP_RATIO_N) /
 510                                        VPBE_DISPLAY_V_EXP_RATIO_D;
 511                                if (calculated_xsize <= expected_ysize) {
 512                                        v_exp = 1;
 513                                        cfg->ysize = calculated_xsize;
 514                                }
 515                        }
 516                }
 517                if (v_scale == 2)
 518                        layer_info->v_zoom = ZOOM_X2;
 519                else if (v_scale == 4)
 520                        layer_info->v_zoom = ZOOM_X4;
 521                if (v_exp)
 522                        layer_info->h_exp = V_EXP_6_OVER_5;
 523        } else {
 524                /* no scaling, only cropping. Set display area to crop area */
 525                cfg->ysize = expected_ysize;
 526        }
 527        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 528                "crop display xsize = %d, ysize = %d\n",
 529                cfg->xsize, cfg->ysize);
 530}
 531
 532static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
 533                        struct vpbe_layer *layer,
 534                        int top, int left)
 535{
 536        struct osd_layer_config *cfg = &layer->layer_info.config;
 537        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 538
 539        cfg->xpos = min((unsigned int)left,
 540                        vpbe_dev->current_timings.xres - cfg->xsize);
 541        cfg->ypos = min((unsigned int)top,
 542                        vpbe_dev->current_timings.yres - cfg->ysize);
 543
 544        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 545                "new xpos = %d, ypos = %d\n",
 546                cfg->xpos, cfg->ypos);
 547}
 548
 549static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
 550                        struct v4l2_rect *c)
 551{
 552        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 553
 554        if ((c->width == 0) ||
 555          ((c->width + c->left) > vpbe_dev->current_timings.xres))
 556                c->width = vpbe_dev->current_timings.xres - c->left;
 557
 558        if ((c->height == 0) || ((c->height + c->top) >
 559          vpbe_dev->current_timings.yres))
 560                c->height = vpbe_dev->current_timings.yres - c->top;
 561
 562        /* window height must be even for interlaced display */
 563        if (vpbe_dev->current_timings.interlaced)
 564                c->height &= (~0x01);
 565
 566}
 567
 568/**
 569 * vpbe_try_format()
 570 * If user application provides width and height, and have bytesperline set
 571 * to zero, driver calculates bytesperline and sizeimage based on hardware
 572 * limits.
 573 */
 574static int vpbe_try_format(struct vpbe_display *disp_dev,
 575                        struct v4l2_pix_format *pixfmt, int check)
 576{
 577        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 578        int min_height = 1;
 579        int min_width = 32;
 580        int max_height;
 581        int max_width;
 582        int bpp;
 583
 584        if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
 585            (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
 586                /* choose default as V4L2_PIX_FMT_UYVY */
 587                pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
 588
 589        /* Check the field format */
 590        if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
 591                (pixfmt->field != V4L2_FIELD_NONE)) {
 592                if (vpbe_dev->current_timings.interlaced)
 593                        pixfmt->field = V4L2_FIELD_INTERLACED;
 594                else
 595                        pixfmt->field = V4L2_FIELD_NONE;
 596        }
 597
 598        if (pixfmt->field == V4L2_FIELD_INTERLACED)
 599                min_height = 2;
 600
 601        if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
 602                bpp = 1;
 603        else
 604                bpp = 2;
 605
 606        max_width = vpbe_dev->current_timings.xres;
 607        max_height = vpbe_dev->current_timings.yres;
 608
 609        min_width /= bpp;
 610
 611        if (!pixfmt->width || (pixfmt->width < min_width) ||
 612                (pixfmt->width > max_width)) {
 613                pixfmt->width = vpbe_dev->current_timings.xres;
 614        }
 615
 616        if (!pixfmt->height || (pixfmt->height  < min_height) ||
 617                (pixfmt->height  > max_height)) {
 618                pixfmt->height = vpbe_dev->current_timings.yres;
 619        }
 620
 621        if (pixfmt->bytesperline < (pixfmt->width * bpp))
 622                pixfmt->bytesperline = pixfmt->width * bpp;
 623
 624        /* Make the bytesperline 32 byte aligned */
 625        pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
 626
 627        if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
 628                pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
 629                                (pixfmt->bytesperline * pixfmt->height >> 1);
 630        else
 631                pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
 632
 633        return 0;
 634}
 635
 636static int vpbe_display_querycap(struct file *file, void  *priv,
 637                               struct v4l2_capability *cap)
 638{
 639        struct vpbe_layer *layer = video_drvdata(file);
 640        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 641
 642        cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
 643        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 644        snprintf(cap->driver, sizeof(cap->driver), "%s",
 645                dev_name(vpbe_dev->pdev));
 646        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
 647                 dev_name(vpbe_dev->pdev));
 648        strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
 649
 650        return 0;
 651}
 652
 653static int vpbe_display_s_crop(struct file *file, void *priv,
 654                             const struct v4l2_crop *crop)
 655{
 656        struct vpbe_layer *layer = video_drvdata(file);
 657        struct vpbe_display *disp_dev = layer->disp_dev;
 658        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 659        struct osd_layer_config *cfg = &layer->layer_info.config;
 660        struct osd_state *osd_device = disp_dev->osd_device;
 661        struct v4l2_rect rect = crop->c;
 662        int ret;
 663
 664        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 665                "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
 666
 667        if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 668                v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
 669                return -EINVAL;
 670        }
 671
 672        if (rect.top < 0)
 673                rect.top = 0;
 674        if (rect.left < 0)
 675                rect.left = 0;
 676
 677        vpbe_disp_check_window_params(disp_dev, &rect);
 678
 679        osd_device->ops.get_layer_config(osd_device,
 680                        layer->layer_info.id, cfg);
 681
 682        vpbe_disp_calculate_scale_factor(disp_dev, layer,
 683                                        rect.width,
 684                                        rect.height);
 685        vpbe_disp_adj_position(disp_dev, layer, rect.top,
 686                                        rect.left);
 687        ret = osd_device->ops.set_layer_config(osd_device,
 688                                layer->layer_info.id, cfg);
 689        if (ret < 0) {
 690                v4l2_err(&vpbe_dev->v4l2_dev,
 691                        "Error in set layer config:\n");
 692                return -EINVAL;
 693        }
 694
 695        /* apply zooming and h or v expansion */
 696        osd_device->ops.set_zoom(osd_device,
 697                        layer->layer_info.id,
 698                        layer->layer_info.h_zoom,
 699                        layer->layer_info.v_zoom);
 700        ret = osd_device->ops.set_vid_expansion(osd_device,
 701                        layer->layer_info.h_exp,
 702                        layer->layer_info.v_exp);
 703        if (ret < 0) {
 704                v4l2_err(&vpbe_dev->v4l2_dev,
 705                "Error in set vid expansion:\n");
 706                return -EINVAL;
 707        }
 708
 709        if ((layer->layer_info.h_zoom != ZOOM_X1) ||
 710                (layer->layer_info.v_zoom != ZOOM_X1) ||
 711                (layer->layer_info.h_exp != H_EXP_OFF) ||
 712                (layer->layer_info.v_exp != V_EXP_OFF))
 713                /* Enable expansion filter */
 714                osd_device->ops.set_interpolation_filter(osd_device, 1);
 715        else
 716                osd_device->ops.set_interpolation_filter(osd_device, 0);
 717
 718        return 0;
 719}
 720
 721static int vpbe_display_g_crop(struct file *file, void *priv,
 722                             struct v4l2_crop *crop)
 723{
 724        struct vpbe_layer *layer = video_drvdata(file);
 725        struct osd_layer_config *cfg = &layer->layer_info.config;
 726        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 727        struct osd_state *osd_device = layer->disp_dev->osd_device;
 728        struct v4l2_rect *rect = &crop->c;
 729
 730        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 731                        "VIDIOC_G_CROP, layer id = %d\n",
 732                        layer->device_id);
 733
 734        if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 735                v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
 736                return -EINVAL;
 737        }
 738        osd_device->ops.get_layer_config(osd_device,
 739                                layer->layer_info.id, cfg);
 740        rect->top = cfg->ypos;
 741        rect->left = cfg->xpos;
 742        rect->width = cfg->xsize;
 743        rect->height = cfg->ysize;
 744
 745        return 0;
 746}
 747
 748static int vpbe_display_cropcap(struct file *file, void *priv,
 749                              struct v4l2_cropcap *cropcap)
 750{
 751        struct vpbe_layer *layer = video_drvdata(file);
 752        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 753
 754        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
 755
 756        cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 757        cropcap->bounds.left = 0;
 758        cropcap->bounds.top = 0;
 759        cropcap->bounds.width = vpbe_dev->current_timings.xres;
 760        cropcap->bounds.height = vpbe_dev->current_timings.yres;
 761        cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
 762        cropcap->defrect = cropcap->bounds;
 763        return 0;
 764}
 765
 766static int vpbe_display_g_fmt(struct file *file, void *priv,
 767                                struct v4l2_format *fmt)
 768{
 769        struct vpbe_layer *layer = video_drvdata(file);
 770        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 771
 772        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 773                        "VIDIOC_G_FMT, layer id = %d\n",
 774                        layer->device_id);
 775
 776        /* If buffer type is video output */
 777        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
 778                v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
 779                return -EINVAL;
 780        }
 781        /* Fill in the information about format */
 782        fmt->fmt.pix = layer->pix_fmt;
 783
 784        return 0;
 785}
 786
 787static int vpbe_display_enum_fmt(struct file *file, void  *priv,
 788                                   struct v4l2_fmtdesc *fmt)
 789{
 790        struct vpbe_layer *layer = video_drvdata(file);
 791        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 792        unsigned int index = 0;
 793
 794        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 795                                "VIDIOC_ENUM_FMT, layer id = %d\n",
 796                                layer->device_id);
 797        if (fmt->index > 1) {
 798                v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
 799                return -EINVAL;
 800        }
 801
 802        /* Fill in the information about format */
 803        index = fmt->index;
 804        memset(fmt, 0, sizeof(*fmt));
 805        fmt->index = index;
 806        fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 807        if (index == 0) {
 808                strcpy(fmt->description, "YUV 4:2:2 - UYVY");
 809                fmt->pixelformat = V4L2_PIX_FMT_UYVY;
 810        } else {
 811                strcpy(fmt->description, "Y/CbCr 4:2:0");
 812                fmt->pixelformat = V4L2_PIX_FMT_NV12;
 813        }
 814
 815        return 0;
 816}
 817
 818static int vpbe_display_s_fmt(struct file *file, void *priv,
 819                                struct v4l2_format *fmt)
 820{
 821        struct vpbe_layer *layer = video_drvdata(file);
 822        struct vpbe_display *disp_dev = layer->disp_dev;
 823        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
 824        struct osd_layer_config *cfg  = &layer->layer_info.config;
 825        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 826        struct osd_state *osd_device = disp_dev->osd_device;
 827        int ret;
 828
 829        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
 830                        "VIDIOC_S_FMT, layer id = %d\n",
 831                        layer->device_id);
 832
 833        if (vb2_is_busy(&layer->buffer_queue))
 834                return -EBUSY;
 835
 836        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
 837                v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
 838                return -EINVAL;
 839        }
 840        /* Check for valid pixel format */
 841        ret = vpbe_try_format(disp_dev, pixfmt, 1);
 842        if (ret)
 843                return ret;
 844
 845        /* YUV420 is requested, check availability of the
 846        other video window */
 847
 848        layer->pix_fmt = *pixfmt;
 849        if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
 850                struct vpbe_layer *otherlayer;
 851
 852                otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
 853                /* if other layer is available, only
 854                 * claim it, do not configure it
 855                 */
 856                ret = osd_device->ops.request_layer(osd_device,
 857                                                    otherlayer->layer_info.id);
 858                if (ret < 0) {
 859                        v4l2_err(&vpbe_dev->v4l2_dev,
 860                                 "Display Manager failed to allocate layer\n");
 861                        return -EBUSY;
 862                }
 863        }
 864
 865        /* Get osd layer config */
 866        osd_device->ops.get_layer_config(osd_device,
 867                        layer->layer_info.id, cfg);
 868        /* Store the pixel format in the layer object */
 869        cfg->xsize = pixfmt->width;
 870        cfg->ysize = pixfmt->height;
 871        cfg->line_length = pixfmt->bytesperline;
 872        cfg->ypos = 0;
 873        cfg->xpos = 0;
 874        cfg->interlaced = vpbe_dev->current_timings.interlaced;
 875
 876        if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
 877                cfg->pixfmt = PIXFMT_YCBCRI;
 878
 879        /* Change of the default pixel format for both video windows */
 880        if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
 881                struct vpbe_layer *otherlayer;
 882                cfg->pixfmt = PIXFMT_NV12;
 883                otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
 884                                                                layer);
 885                otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
 886        }
 887
 888        /* Set the layer config in the osd window */
 889        ret = osd_device->ops.set_layer_config(osd_device,
 890                                layer->layer_info.id, cfg);
 891        if (ret < 0) {
 892                v4l2_err(&vpbe_dev->v4l2_dev,
 893                                "Error in S_FMT params:\n");
 894                return -EINVAL;
 895        }
 896
 897        /* Readback and fill the local copy of current pix format */
 898        osd_device->ops.get_layer_config(osd_device,
 899                        layer->layer_info.id, cfg);
 900
 901        return 0;
 902}
 903
 904static int vpbe_display_try_fmt(struct file *file, void *priv,
 905                                  struct v4l2_format *fmt)
 906{
 907        struct vpbe_layer *layer = video_drvdata(file);
 908        struct vpbe_display *disp_dev = layer->disp_dev;
 909        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 910        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 911
 912        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
 913
 914        if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
 915                v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
 916                return -EINVAL;
 917        }
 918
 919        /* Check for valid field format */
 920        return  vpbe_try_format(disp_dev, pixfmt, 0);
 921
 922}
 923
 924/**
 925 * vpbe_display_s_std - Set the given standard in the encoder
 926 *
 927 * Sets the standard if supported by the current encoder. Return the status.
 928 * 0 - success & -EINVAL on error
 929 */
 930static int vpbe_display_s_std(struct file *file, void *priv,
 931                                v4l2_std_id std_id)
 932{
 933        struct vpbe_layer *layer = video_drvdata(file);
 934        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 935        int ret;
 936
 937        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
 938
 939        if (vb2_is_busy(&layer->buffer_queue))
 940                return -EBUSY;
 941
 942        if (NULL != vpbe_dev->ops.s_std) {
 943                ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
 944                if (ret) {
 945                        v4l2_err(&vpbe_dev->v4l2_dev,
 946                        "Failed to set standard for sub devices\n");
 947                        return -EINVAL;
 948                }
 949        } else {
 950                return -EINVAL;
 951        }
 952
 953        return 0;
 954}
 955
 956/**
 957 * vpbe_display_g_std - Get the standard in the current encoder
 958 *
 959 * Get the standard in the current encoder. Return the status. 0 - success
 960 * -EINVAL on error
 961 */
 962static int vpbe_display_g_std(struct file *file, void *priv,
 963                                v4l2_std_id *std_id)
 964{
 965        struct vpbe_layer *layer = video_drvdata(file);
 966        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 967
 968        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
 969
 970        /* Get the standard from the current encoder */
 971        if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
 972                *std_id = vpbe_dev->current_timings.std_id;
 973                return 0;
 974        }
 975
 976        return -EINVAL;
 977}
 978
 979/**
 980 * vpbe_display_enum_output - enumerate outputs
 981 *
 982 * Enumerates the outputs available at the vpbe display
 983 * returns the status, -EINVAL if end of output list
 984 */
 985static int vpbe_display_enum_output(struct file *file, void *priv,
 986                                    struct v4l2_output *output)
 987{
 988        struct vpbe_layer *layer = video_drvdata(file);
 989        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
 990        int ret;
 991
 992        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
 993
 994        /* Enumerate outputs */
 995
 996        if (NULL == vpbe_dev->ops.enum_outputs)
 997                return -EINVAL;
 998
 999        ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1000        if (ret) {
1001                v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1002                        "Failed to enumerate outputs\n");
1003                return -EINVAL;
1004        }
1005
1006        return 0;
1007}
1008
1009/**
1010 * vpbe_display_s_output - Set output to
1011 * the output specified by the index
1012 */
1013static int vpbe_display_s_output(struct file *file, void *priv,
1014                                unsigned int i)
1015{
1016        struct vpbe_layer *layer = video_drvdata(file);
1017        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
1018        int ret;
1019
1020        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
1021
1022        if (vb2_is_busy(&layer->buffer_queue))
1023                return -EBUSY;
1024
1025        if (NULL == vpbe_dev->ops.set_output)
1026                return -EINVAL;
1027
1028        ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1029        if (ret) {
1030                v4l2_err(&vpbe_dev->v4l2_dev,
1031                        "Failed to set output for sub devices\n");
1032                return -EINVAL;
1033        }
1034
1035        return 0;
1036}
1037
1038/**
1039 * vpbe_display_g_output - Get output from subdevice
1040 * for a given by the index
1041 */
1042static int vpbe_display_g_output(struct file *file, void *priv,
1043                                unsigned int *i)
1044{
1045        struct vpbe_layer *layer = video_drvdata(file);
1046        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
1047
1048        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1049        /* Get the standard from the current encoder */
1050        *i = vpbe_dev->current_out_index;
1051
1052        return 0;
1053}
1054
1055/**
1056 * vpbe_display_enum_dv_timings - Enumerate the dv timings
1057 *
1058 * enum the timings in the current encoder. Return the status. 0 - success
1059 * -EINVAL on error
1060 */
1061static int
1062vpbe_display_enum_dv_timings(struct file *file, void *priv,
1063                        struct v4l2_enum_dv_timings *timings)
1064{
1065        struct vpbe_layer *layer = video_drvdata(file);
1066        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
1067        int ret;
1068
1069        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
1070
1071        /* Enumerate outputs */
1072        if (NULL == vpbe_dev->ops.enum_dv_timings)
1073                return -EINVAL;
1074
1075        ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
1076        if (ret) {
1077                v4l2_err(&vpbe_dev->v4l2_dev,
1078                        "Failed to enumerate dv timings info\n");
1079                return -EINVAL;
1080        }
1081
1082        return 0;
1083}
1084
1085/**
1086 * vpbe_display_s_dv_timings - Set the dv timings
1087 *
1088 * Set the timings in the current encoder. Return the status. 0 - success
1089 * -EINVAL on error
1090 */
1091static int
1092vpbe_display_s_dv_timings(struct file *file, void *priv,
1093                                struct v4l2_dv_timings *timings)
1094{
1095        struct vpbe_layer *layer = video_drvdata(file);
1096        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
1097        int ret;
1098
1099        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
1100
1101        if (vb2_is_busy(&layer->buffer_queue))
1102                return -EBUSY;
1103
1104        /* Set the given standard in the encoder */
1105        if (!vpbe_dev->ops.s_dv_timings)
1106                return -EINVAL;
1107
1108        ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
1109        if (ret) {
1110                v4l2_err(&vpbe_dev->v4l2_dev,
1111                        "Failed to set the dv timings info\n");
1112                return -EINVAL;
1113        }
1114
1115        return 0;
1116}
1117
1118/**
1119 * vpbe_display_g_dv_timings - Set the dv timings
1120 *
1121 * Get the timings in the current encoder. Return the status. 0 - success
1122 * -EINVAL on error
1123 */
1124static int
1125vpbe_display_g_dv_timings(struct file *file, void *priv,
1126                                struct v4l2_dv_timings *dv_timings)
1127{
1128        struct vpbe_layer *layer = video_drvdata(file);
1129        struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
1130
1131        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
1132
1133        /* Get the given standard in the encoder */
1134
1135        if (vpbe_dev->current_timings.timings_type &
1136                                VPBE_ENC_DV_TIMINGS) {
1137                *dv_timings = vpbe_dev->current_timings.dv_timings;
1138        } else {
1139                return -EINVAL;
1140        }
1141
1142        return 0;
1143}
1144
1145/*
1146 * vpbe_display_open()
1147 * It creates object of file handle structure and stores it in private_data
1148 * member of filepointer
1149 */
1150static int vpbe_display_open(struct file *file)
1151{
1152        struct vpbe_layer *layer = video_drvdata(file);
1153        struct vpbe_display *disp_dev = layer->disp_dev;
1154        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1155        struct osd_state *osd_device = disp_dev->osd_device;
1156        int err;
1157
1158        /* creating context for file descriptor */
1159        err = v4l2_fh_open(file);
1160        if (err) {
1161                v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
1162                return err;
1163        }
1164
1165        /* leaving if layer is already initialized */
1166        if (!v4l2_fh_is_singular_file(file))
1167                return err;
1168
1169        if (!layer->usrs) {
1170                if (mutex_lock_interruptible(&layer->opslock))
1171                        return -ERESTARTSYS;
1172                /* First claim the layer for this device */
1173                err = osd_device->ops.request_layer(osd_device,
1174                                                layer->layer_info.id);
1175                mutex_unlock(&layer->opslock);
1176                if (err < 0) {
1177                        /* Couldn't get layer */
1178                        v4l2_err(&vpbe_dev->v4l2_dev,
1179                                "Display Manager failed to allocate layer\n");
1180                        v4l2_fh_release(file);
1181                        return -EINVAL;
1182                }
1183        }
1184        /* Increment layer usrs counter */
1185        layer->usrs++;
1186        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1187                        "vpbe display device opened successfully\n");
1188        return 0;
1189}
1190
1191/*
1192 * vpbe_display_release()
1193 * This function deletes buffer queue, frees the buffers and the davinci
1194 * display file * handle
1195 */
1196static int vpbe_display_release(struct file *file)
1197{
1198        struct vpbe_layer *layer = video_drvdata(file);
1199        struct osd_layer_config *cfg  = &layer->layer_info.config;
1200        struct vpbe_display *disp_dev = layer->disp_dev;
1201        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1202        struct osd_state *osd_device = disp_dev->osd_device;
1203
1204        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1205
1206        mutex_lock(&layer->opslock);
1207
1208        osd_device->ops.disable_layer(osd_device,
1209                        layer->layer_info.id);
1210        /* Decrement layer usrs counter */
1211        layer->usrs--;
1212        /* If this file handle has initialize encoder device, reset it */
1213        if (!layer->usrs) {
1214                if (cfg->pixfmt == PIXFMT_NV12) {
1215                        struct vpbe_layer *otherlayer;
1216                        otherlayer =
1217                        _vpbe_display_get_other_win_layer(disp_dev, layer);
1218                        osd_device->ops.disable_layer(osd_device,
1219                                        otherlayer->layer_info.id);
1220                        osd_device->ops.release_layer(osd_device,
1221                                        otherlayer->layer_info.id);
1222                }
1223                osd_device->ops.disable_layer(osd_device,
1224                                layer->layer_info.id);
1225                osd_device->ops.release_layer(osd_device,
1226                                layer->layer_info.id);
1227        }
1228
1229        _vb2_fop_release(file, NULL);
1230        mutex_unlock(&layer->opslock);
1231
1232        disp_dev->cbcr_ofst = 0;
1233
1234        return 0;
1235}
1236
1237/* vpbe capture ioctl operations */
1238static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1239        .vidioc_querycap         = vpbe_display_querycap,
1240        .vidioc_g_fmt_vid_out    = vpbe_display_g_fmt,
1241        .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1242        .vidioc_s_fmt_vid_out    = vpbe_display_s_fmt,
1243        .vidioc_try_fmt_vid_out  = vpbe_display_try_fmt,
1244
1245        .vidioc_reqbufs          = vb2_ioctl_reqbufs,
1246        .vidioc_create_bufs      = vb2_ioctl_create_bufs,
1247        .vidioc_querybuf         = vb2_ioctl_querybuf,
1248        .vidioc_qbuf             = vb2_ioctl_qbuf,
1249        .vidioc_dqbuf            = vb2_ioctl_dqbuf,
1250        .vidioc_streamon         = vb2_ioctl_streamon,
1251        .vidioc_streamoff        = vb2_ioctl_streamoff,
1252        .vidioc_expbuf           = vb2_ioctl_expbuf,
1253
1254        .vidioc_cropcap          = vpbe_display_cropcap,
1255        .vidioc_g_crop           = vpbe_display_g_crop,
1256        .vidioc_s_crop           = vpbe_display_s_crop,
1257
1258        .vidioc_s_std            = vpbe_display_s_std,
1259        .vidioc_g_std            = vpbe_display_g_std,
1260
1261        .vidioc_enum_output      = vpbe_display_enum_output,
1262        .vidioc_s_output         = vpbe_display_s_output,
1263        .vidioc_g_output         = vpbe_display_g_output,
1264
1265        .vidioc_s_dv_timings     = vpbe_display_s_dv_timings,
1266        .vidioc_g_dv_timings     = vpbe_display_g_dv_timings,
1267        .vidioc_enum_dv_timings  = vpbe_display_enum_dv_timings,
1268};
1269
1270static struct v4l2_file_operations vpbe_fops = {
1271        .owner = THIS_MODULE,
1272        .open = vpbe_display_open,
1273        .release = vpbe_display_release,
1274        .unlocked_ioctl = video_ioctl2,
1275        .mmap = vb2_fop_mmap,
1276        .poll =  vb2_fop_poll,
1277};
1278
1279static int vpbe_device_get(struct device *dev, void *data)
1280{
1281        struct platform_device *pdev = to_platform_device(dev);
1282        struct vpbe_display *vpbe_disp  = data;
1283
1284        if (strcmp("vpbe_controller", pdev->name) == 0)
1285                vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1286
1287        if (strstr(pdev->name, "vpbe-osd") != NULL)
1288                vpbe_disp->osd_device = platform_get_drvdata(pdev);
1289
1290        return 0;
1291}
1292
1293static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1294                           struct platform_device *pdev)
1295{
1296        struct vpbe_layer *vpbe_display_layer = NULL;
1297        struct video_device *vbd = NULL;
1298
1299        /* Allocate memory for four plane display objects */
1300
1301        disp_dev->dev[i] =
1302                kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1303
1304        /* If memory allocation fails, return error */
1305        if (!disp_dev->dev[i]) {
1306                printk(KERN_ERR "ran out of memory\n");
1307                return  -ENOMEM;
1308        }
1309        spin_lock_init(&disp_dev->dev[i]->irqlock);
1310        mutex_init(&disp_dev->dev[i]->opslock);
1311
1312        /* Get the pointer to the layer object */
1313        vpbe_display_layer = disp_dev->dev[i];
1314        vbd = &vpbe_display_layer->video_dev;
1315        /* Initialize field of video device */
1316        vbd->release    = video_device_release_empty;
1317        vbd->fops       = &vpbe_fops;
1318        vbd->ioctl_ops  = &vpbe_ioctl_ops;
1319        vbd->minor      = -1;
1320        vbd->v4l2_dev   = &disp_dev->vpbe_dev->v4l2_dev;
1321        vbd->lock       = &vpbe_display_layer->opslock;
1322        vbd->vfl_dir    = VFL_DIR_TX;
1323
1324        if (disp_dev->vpbe_dev->current_timings.timings_type &
1325                        VPBE_ENC_STD)
1326                vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
1327
1328        snprintf(vbd->name, sizeof(vbd->name),
1329                        "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1330                        (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1331                        (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1332                        (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1333
1334        vpbe_display_layer->device_id = i;
1335
1336        vpbe_display_layer->layer_info.id =
1337                ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1338
1339
1340        return 0;
1341}
1342
1343static int register_device(struct vpbe_layer *vpbe_display_layer,
1344                           struct vpbe_display *disp_dev,
1345                           struct platform_device *pdev)
1346{
1347        int err;
1348
1349        v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1350                  "Trying to register VPBE display device.\n");
1351        v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1352                  "layer=%x,layer->video_dev=%x\n",
1353                  (int)vpbe_display_layer,
1354                  (int)&vpbe_display_layer->video_dev);
1355
1356        vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
1357        err = video_register_device(&vpbe_display_layer->video_dev,
1358                                    VFL_TYPE_GRABBER,
1359                                    -1);
1360        if (err)
1361                return -ENODEV;
1362
1363        vpbe_display_layer->disp_dev = disp_dev;
1364        /* set the driver data in platform device */
1365        platform_set_drvdata(pdev, disp_dev);
1366        video_set_drvdata(&vpbe_display_layer->video_dev,
1367                          vpbe_display_layer);
1368
1369        return 0;
1370}
1371
1372
1373
1374/*
1375 * vpbe_display_probe()
1376 * This function creates device entries by register itself to the V4L2 driver
1377 * and initializes fields of each layer objects
1378 */
1379static int vpbe_display_probe(struct platform_device *pdev)
1380{
1381        struct vpbe_display *disp_dev;
1382        struct v4l2_device *v4l2_dev;
1383        struct resource *res = NULL;
1384        struct vb2_queue *q;
1385        int k;
1386        int i;
1387        int err;
1388        int irq;
1389
1390        printk(KERN_DEBUG "vpbe_display_probe\n");
1391        /* Allocate memory for vpbe_display */
1392        disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1393                                GFP_KERNEL);
1394        if (!disp_dev)
1395                return -ENOMEM;
1396
1397        spin_lock_init(&disp_dev->dma_queue_lock);
1398        /*
1399         * Scan all the platform devices to find the vpbe
1400         * controller device and get the vpbe_dev object
1401         */
1402        err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1403                        vpbe_device_get);
1404        if (err < 0)
1405                return err;
1406
1407        v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1408        /* Initialize the vpbe display controller */
1409        if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1410                err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1411                                                         disp_dev->vpbe_dev);
1412                if (err) {
1413                        v4l2_err(v4l2_dev, "Error initing vpbe\n");
1414                        err = -ENOMEM;
1415                        goto probe_out;
1416                }
1417        }
1418
1419        for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1420                if (init_vpbe_layer(i, disp_dev, pdev)) {
1421                        err = -ENODEV;
1422                        goto probe_out;
1423                }
1424        }
1425
1426        res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1427        if (!res) {
1428                v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
1429                err = -ENODEV;
1430                goto probe_out;
1431        }
1432
1433        irq = res->start;
1434        err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
1435                               VPBE_DISPLAY_DRIVER, disp_dev);
1436        if (err) {
1437                v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
1438                goto probe_out;
1439        }
1440
1441        for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1442                /* initialize vb2 queue */
1443                q = &disp_dev->dev[i]->buffer_queue;
1444                memset(q, 0, sizeof(*q));
1445                q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1446                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1447                q->drv_priv = disp_dev->dev[i];
1448                q->ops = &video_qops;
1449                q->mem_ops = &vb2_dma_contig_memops;
1450                q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1451                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1452                q->min_buffers_needed = 1;
1453                q->lock = &disp_dev->dev[i]->opslock;
1454                err = vb2_queue_init(q);
1455                if (err) {
1456                        v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1457                        goto probe_out;
1458                }
1459
1460                disp_dev->dev[i]->alloc_ctx =
1461                        vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
1462                if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
1463                        v4l2_err(v4l2_dev, "Failed to get the context\n");
1464                        err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
1465                        goto probe_out;
1466                }
1467
1468                INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1469
1470                if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1471                        err = -ENODEV;
1472                        goto probe_out;
1473                }
1474        }
1475
1476        v4l2_dbg(1, debug, v4l2_dev,
1477                 "Successfully completed the probing of vpbe v4l2 device\n");
1478
1479        return 0;
1480
1481probe_out:
1482        for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
1483                /* Unregister video device */
1484                if (disp_dev->dev[k] != NULL) {
1485                        vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
1486                        video_unregister_device(&disp_dev->dev[k]->video_dev);
1487                        kfree(disp_dev->dev[k]);
1488                }
1489        }
1490        return err;
1491}
1492
1493/*
1494 * vpbe_display_remove()
1495 * It un-register hardware layer from V4L2 driver
1496 */
1497static int vpbe_display_remove(struct platform_device *pdev)
1498{
1499        struct vpbe_layer *vpbe_display_layer;
1500        struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1501        struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1502        int i;
1503
1504        v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1505
1506        /* deinitialize the vpbe display controller */
1507        if (NULL != vpbe_dev->ops.deinitialize)
1508                vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1509        /* un-register device */
1510        for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1511                /* Get the pointer to the layer object */
1512                vpbe_display_layer = disp_dev->dev[i];
1513                vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
1514                /* Unregister video device */
1515                video_unregister_device(&vpbe_display_layer->video_dev);
1516
1517        }
1518        for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1519                kfree(disp_dev->dev[i]);
1520                disp_dev->dev[i] = NULL;
1521        }
1522
1523        return 0;
1524}
1525
1526static struct platform_driver vpbe_display_driver = {
1527        .driver = {
1528                .name = VPBE_DISPLAY_DRIVER,
1529                .bus = &platform_bus_type,
1530        },
1531        .probe = vpbe_display_probe,
1532        .remove = vpbe_display_remove,
1533};
1534
1535module_platform_driver(vpbe_display_driver);
1536
1537MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1538MODULE_LICENSE("GPL");
1539MODULE_AUTHOR("Texas Instruments");
1540