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