linux/drivers/media/platform/davinci/vpif_capture.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2009 Texas Instruments Inc
   3 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; either version 2 of the License, or
   8 * (at your option) any later version.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program; if not, write to the Free Software
  17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18 *
  19 * TODO : add support for VBI & HBI data service
  20 *        add static buffer allocation
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/interrupt.h>
  25#include <linux/platform_device.h>
  26#include <linux/slab.h>
  27
  28#include <media/v4l2-ioctl.h>
  29
  30#include "vpif.h"
  31#include "vpif_capture.h"
  32
  33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
  34MODULE_LICENSE("GPL");
  35MODULE_VERSION(VPIF_CAPTURE_VERSION);
  36
  37#define vpif_err(fmt, arg...)   v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  38#define vpif_dbg(level, debug, fmt, arg...)     \
  39                v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  40
  41static int debug = 1;
  42
  43module_param(debug, int, 0644);
  44
  45MODULE_PARM_DESC(debug, "Debug level 0-1");
  46
  47#define VPIF_DRIVER_NAME        "vpif_capture"
  48
  49/* global variables */
  50static struct vpif_device vpif_obj = { {NULL} };
  51static struct device *vpif_dev;
  52static void vpif_calculate_offsets(struct channel_obj *ch);
  53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  54
  55static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
  56
  57/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
  58static int ycmux_mode;
  59
  60static inline
  61struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
  62{
  63        return container_of(vb, struct vpif_cap_buffer, vb);
  64}
  65
  66/**
  67 * vpif_buffer_prepare :  callback function for buffer prepare
  68 * @vb: ptr to vb2_buffer
  69 *
  70 * This is the callback function for buffer prepare when vb2_qbuf()
  71 * function is called. The buffer is prepared and user space virtual address
  72 * or user address is converted into  physical address
  73 */
  74static int vpif_buffer_prepare(struct vb2_buffer *vb)
  75{
  76        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  77        struct vb2_queue *q = vb->vb2_queue;
  78        struct channel_obj *ch = vb2_get_drv_priv(q);
  79        struct common_obj *common;
  80        unsigned long addr;
  81
  82        vpif_dbg(2, debug, "vpif_buffer_prepare\n");
  83
  84        common = &ch->common[VPIF_VIDEO_INDEX];
  85
  86        vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  87        if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  88                return -EINVAL;
  89
  90        vbuf->field = common->fmt.fmt.pix.field;
  91
  92        addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  93        if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
  94                !IS_ALIGNED((addr + common->ybtm_off), 8) ||
  95                !IS_ALIGNED((addr + common->ctop_off), 8) ||
  96                !IS_ALIGNED((addr + common->cbtm_off), 8)) {
  97                vpif_dbg(1, debug, "offset is not aligned\n");
  98                return -EINVAL;
  99        }
 100
 101        return 0;
 102}
 103
 104/**
 105 * vpif_buffer_queue_setup : Callback function for buffer setup.
 106 * @vq: vb2_queue ptr
 107 * @nbuffers: ptr to number of buffers requested by application
 108 * @nplanes:: contains number of distinct video planes needed to hold a frame
 109 * @sizes[]: contains the size (in bytes) of each plane.
 110 * @alloc_devs: ptr to allocation context
 111 *
 112 * This callback function is called when reqbuf() is called to adjust
 113 * the buffer count and buffer size
 114 */
 115static int vpif_buffer_queue_setup(struct vb2_queue *vq,
 116                                unsigned int *nbuffers, unsigned int *nplanes,
 117                                unsigned int sizes[], struct device *alloc_devs[])
 118{
 119        struct channel_obj *ch = vb2_get_drv_priv(vq);
 120        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 121        unsigned size = common->fmt.fmt.pix.sizeimage;
 122
 123        vpif_dbg(2, debug, "vpif_buffer_setup\n");
 124
 125        if (*nplanes) {
 126                if (sizes[0] < size)
 127                        return -EINVAL;
 128                size = sizes[0];
 129        }
 130
 131        if (vq->num_buffers + *nbuffers < 3)
 132                *nbuffers = 3 - vq->num_buffers;
 133
 134        *nplanes = 1;
 135        sizes[0] = size;
 136
 137        /* Calculate the offset for Y and C data in the buffer */
 138        vpif_calculate_offsets(ch);
 139
 140        return 0;
 141}
 142
 143/**
 144 * vpif_buffer_queue : Callback function to add buffer to DMA queue
 145 * @vb: ptr to vb2_buffer
 146 */
 147static void vpif_buffer_queue(struct vb2_buffer *vb)
 148{
 149        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 150        struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
 151        struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
 152        struct common_obj *common;
 153        unsigned long flags;
 154
 155        common = &ch->common[VPIF_VIDEO_INDEX];
 156
 157        vpif_dbg(2, debug, "vpif_buffer_queue\n");
 158
 159        spin_lock_irqsave(&common->irqlock, flags);
 160        /* add the buffer to the DMA queue */
 161        list_add_tail(&buf->list, &common->dma_queue);
 162        spin_unlock_irqrestore(&common->irqlock, flags);
 163}
 164
 165/**
 166 * vpif_start_streaming : Starts the DMA engine for streaming
 167 * @vb: ptr to vb2_buffer
 168 * @count: number of buffers
 169 */
 170static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
 171{
 172        struct vpif_capture_config *vpif_config_data =
 173                                        vpif_dev->platform_data;
 174        struct channel_obj *ch = vb2_get_drv_priv(vq);
 175        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 176        struct vpif_params *vpif = &ch->vpifparams;
 177        struct vpif_cap_buffer *buf, *tmp;
 178        unsigned long addr, flags;
 179        int ret;
 180
 181        spin_lock_irqsave(&common->irqlock, flags);
 182
 183        /* Initialize field_id */
 184        ch->field_id = 0;
 185
 186        /* configure 1 or 2 channel mode */
 187        if (vpif_config_data->setup_input_channel_mode) {
 188                ret = vpif_config_data->
 189                        setup_input_channel_mode(vpif->std_info.ycmux_mode);
 190                if (ret < 0) {
 191                        vpif_dbg(1, debug, "can't set vpif channel mode\n");
 192                        goto err;
 193                }
 194        }
 195
 196        ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
 197        if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
 198                vpif_dbg(1, debug, "stream on failed in subdev\n");
 199                goto err;
 200        }
 201
 202        /* Call vpif_set_params function to set the parameters and addresses */
 203        ret = vpif_set_video_params(vpif, ch->channel_id);
 204        if (ret < 0) {
 205                vpif_dbg(1, debug, "can't set video params\n");
 206                goto err;
 207        }
 208
 209        ycmux_mode = ret;
 210        vpif_config_addr(ch, ret);
 211
 212        /* Get the next frame from the buffer queue */
 213        common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
 214                                    struct vpif_cap_buffer, list);
 215        /* Remove buffer from the buffer queue */
 216        list_del(&common->cur_frm->list);
 217        spin_unlock_irqrestore(&common->irqlock, flags);
 218
 219        addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
 220
 221        common->set_addr(addr + common->ytop_off,
 222                         addr + common->ybtm_off,
 223                         addr + common->ctop_off,
 224                         addr + common->cbtm_off);
 225
 226        /**
 227         * Set interrupt for both the fields in VPIF Register enable channel in
 228         * VPIF register
 229         */
 230        channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
 231        if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
 232                channel0_intr_assert();
 233                channel0_intr_enable(1);
 234                enable_channel0(1);
 235        }
 236        if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
 237                ycmux_mode == 2) {
 238                channel1_intr_assert();
 239                channel1_intr_enable(1);
 240                enable_channel1(1);
 241        }
 242
 243        return 0;
 244
 245err:
 246        list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
 247                list_del(&buf->list);
 248                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
 249        }
 250        spin_unlock_irqrestore(&common->irqlock, flags);
 251
 252        return ret;
 253}
 254
 255/**
 256 * vpif_stop_streaming : Stop the DMA engine
 257 * @vq: ptr to vb2_queue
 258 *
 259 * This callback stops the DMA engine and any remaining buffers
 260 * in the DMA queue are released.
 261 */
 262static void vpif_stop_streaming(struct vb2_queue *vq)
 263{
 264        struct channel_obj *ch = vb2_get_drv_priv(vq);
 265        struct common_obj *common;
 266        unsigned long flags;
 267        int ret;
 268
 269        common = &ch->common[VPIF_VIDEO_INDEX];
 270
 271        /* Disable channel as per its device type and channel id */
 272        if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
 273                enable_channel0(0);
 274                channel0_intr_enable(0);
 275        }
 276        if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
 277                ycmux_mode == 2) {
 278                enable_channel1(0);
 279                channel1_intr_enable(0);
 280        }
 281
 282        ycmux_mode = 0;
 283
 284        ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
 285        if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
 286                vpif_dbg(1, debug, "stream off failed in subdev\n");
 287
 288        /* release all active buffers */
 289        spin_lock_irqsave(&common->irqlock, flags);
 290        if (common->cur_frm == common->next_frm) {
 291                vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
 292                                VB2_BUF_STATE_ERROR);
 293        } else {
 294                if (common->cur_frm != NULL)
 295                        vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
 296                                        VB2_BUF_STATE_ERROR);
 297                if (common->next_frm != NULL)
 298                        vb2_buffer_done(&common->next_frm->vb.vb2_buf,
 299                                        VB2_BUF_STATE_ERROR);
 300        }
 301
 302        while (!list_empty(&common->dma_queue)) {
 303                common->next_frm = list_entry(common->dma_queue.next,
 304                                                struct vpif_cap_buffer, list);
 305                list_del(&common->next_frm->list);
 306                vb2_buffer_done(&common->next_frm->vb.vb2_buf,
 307                                VB2_BUF_STATE_ERROR);
 308        }
 309        spin_unlock_irqrestore(&common->irqlock, flags);
 310}
 311
 312static struct vb2_ops video_qops = {
 313        .queue_setup            = vpif_buffer_queue_setup,
 314        .buf_prepare            = vpif_buffer_prepare,
 315        .start_streaming        = vpif_start_streaming,
 316        .stop_streaming         = vpif_stop_streaming,
 317        .buf_queue              = vpif_buffer_queue,
 318        .wait_prepare           = vb2_ops_wait_prepare,
 319        .wait_finish            = vb2_ops_wait_finish,
 320};
 321
 322/**
 323 * vpif_process_buffer_complete: process a completed buffer
 324 * @common: ptr to common channel object
 325 *
 326 * This function time stamp the buffer and mark it as DONE. It also
 327 * wake up any process waiting on the QUEUE and set the next buffer
 328 * as current
 329 */
 330static void vpif_process_buffer_complete(struct common_obj *common)
 331{
 332        common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
 333        vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
 334        /* Make curFrm pointing to nextFrm */
 335        common->cur_frm = common->next_frm;
 336}
 337
 338/**
 339 * vpif_schedule_next_buffer: set next buffer address for capture
 340 * @common : ptr to common channel object
 341 *
 342 * This function will get next buffer from the dma queue and
 343 * set the buffer address in the vpif register for capture.
 344 * the buffer is marked active
 345 */
 346static void vpif_schedule_next_buffer(struct common_obj *common)
 347{
 348        unsigned long addr = 0;
 349
 350        spin_lock(&common->irqlock);
 351        common->next_frm = list_entry(common->dma_queue.next,
 352                                     struct vpif_cap_buffer, list);
 353        /* Remove that buffer from the buffer queue */
 354        list_del(&common->next_frm->list);
 355        spin_unlock(&common->irqlock);
 356        addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
 357
 358        /* Set top and bottom field addresses in VPIF registers */
 359        common->set_addr(addr + common->ytop_off,
 360                         addr + common->ybtm_off,
 361                         addr + common->ctop_off,
 362                         addr + common->cbtm_off);
 363}
 364
 365/**
 366 * vpif_channel_isr : ISR handler for vpif capture
 367 * @irq: irq number
 368 * @dev_id: dev_id ptr
 369 *
 370 * It changes status of the captured buffer, takes next buffer from the queue
 371 * and sets its address in VPIF registers
 372 */
 373static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
 374{
 375        struct vpif_device *dev = &vpif_obj;
 376        struct common_obj *common;
 377        struct channel_obj *ch;
 378        int channel_id = 0;
 379        int fid = -1, i;
 380
 381        channel_id = *(int *)(dev_id);
 382        if (!vpif_intr_status(channel_id))
 383                return IRQ_NONE;
 384
 385        ch = dev->dev[channel_id];
 386
 387        for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
 388                common = &ch->common[i];
 389                /* skip If streaming is not started in this channel */
 390                /* Check the field format */
 391                if (1 == ch->vpifparams.std_info.frm_fmt) {
 392                        /* Progressive mode */
 393                        spin_lock(&common->irqlock);
 394                        if (list_empty(&common->dma_queue)) {
 395                                spin_unlock(&common->irqlock);
 396                                continue;
 397                        }
 398                        spin_unlock(&common->irqlock);
 399
 400                        if (!channel_first_int[i][channel_id])
 401                                vpif_process_buffer_complete(common);
 402
 403                        channel_first_int[i][channel_id] = 0;
 404
 405                        vpif_schedule_next_buffer(common);
 406
 407
 408                        channel_first_int[i][channel_id] = 0;
 409                } else {
 410                        /**
 411                         * Interlaced mode. If it is first interrupt, ignore
 412                         * it
 413                         */
 414                        if (channel_first_int[i][channel_id]) {
 415                                channel_first_int[i][channel_id] = 0;
 416                                continue;
 417                        }
 418                        if (0 == i) {
 419                                ch->field_id ^= 1;
 420                                /* Get field id from VPIF registers */
 421                                fid = vpif_channel_getfid(ch->channel_id);
 422                                if (fid != ch->field_id) {
 423                                        /**
 424                                         * If field id does not match stored
 425                                         * field id, make them in sync
 426                                         */
 427                                        if (0 == fid)
 428                                                ch->field_id = fid;
 429                                        return IRQ_HANDLED;
 430                                }
 431                        }
 432                        /* device field id and local field id are in sync */
 433                        if (0 == fid) {
 434                                /* this is even field */
 435                                if (common->cur_frm == common->next_frm)
 436                                        continue;
 437
 438                                /* mark the current buffer as done */
 439                                vpif_process_buffer_complete(common);
 440                        } else if (1 == fid) {
 441                                /* odd field */
 442                                spin_lock(&common->irqlock);
 443                                if (list_empty(&common->dma_queue) ||
 444                                    (common->cur_frm != common->next_frm)) {
 445                                        spin_unlock(&common->irqlock);
 446                                        continue;
 447                                }
 448                                spin_unlock(&common->irqlock);
 449
 450                                vpif_schedule_next_buffer(common);
 451                        }
 452                }
 453        }
 454        return IRQ_HANDLED;
 455}
 456
 457/**
 458 * vpif_update_std_info() - update standard related info
 459 * @ch: ptr to channel object
 460 *
 461 * For a given standard selected by application, update values
 462 * in the device data structures
 463 */
 464static int vpif_update_std_info(struct channel_obj *ch)
 465{
 466        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 467        struct vpif_params *vpifparams = &ch->vpifparams;
 468        const struct vpif_channel_config_params *config;
 469        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
 470        struct video_obj *vid_ch = &ch->video;
 471        int index;
 472
 473        vpif_dbg(2, debug, "vpif_update_std_info\n");
 474
 475        for (index = 0; index < vpif_ch_params_count; index++) {
 476                config = &vpif_ch_params[index];
 477                if (config->hd_sd == 0) {
 478                        vpif_dbg(2, debug, "SD format\n");
 479                        if (config->stdid & vid_ch->stdid) {
 480                                memcpy(std_info, config, sizeof(*config));
 481                                break;
 482                        }
 483                } else {
 484                        vpif_dbg(2, debug, "HD format\n");
 485                        if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
 486                                sizeof(vid_ch->dv_timings))) {
 487                                memcpy(std_info, config, sizeof(*config));
 488                                break;
 489                        }
 490                }
 491        }
 492
 493        /* standard not found */
 494        if (index == vpif_ch_params_count)
 495                return -EINVAL;
 496
 497        common->fmt.fmt.pix.width = std_info->width;
 498        common->width = std_info->width;
 499        common->fmt.fmt.pix.height = std_info->height;
 500        common->height = std_info->height;
 501        common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
 502        common->fmt.fmt.pix.bytesperline = std_info->width;
 503        vpifparams->video_params.hpitch = std_info->width;
 504        vpifparams->video_params.storage_mode = std_info->frm_fmt;
 505
 506        if (vid_ch->stdid)
 507                common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 508        else
 509                common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
 510
 511        if (ch->vpifparams.std_info.frm_fmt)
 512                common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
 513        else
 514                common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
 515
 516        if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
 517                common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
 518        else
 519                common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
 520
 521        common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 522
 523        return 0;
 524}
 525
 526/**
 527 * vpif_calculate_offsets : This function calculates buffers offsets
 528 * @ch : ptr to channel object
 529 *
 530 * This function calculates buffer offsets for Y and C in the top and
 531 * bottom field
 532 */
 533static void vpif_calculate_offsets(struct channel_obj *ch)
 534{
 535        unsigned int hpitch, sizeimage;
 536        struct video_obj *vid_ch = &(ch->video);
 537        struct vpif_params *vpifparams = &ch->vpifparams;
 538        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 539        enum v4l2_field field = common->fmt.fmt.pix.field;
 540
 541        vpif_dbg(2, debug, "vpif_calculate_offsets\n");
 542
 543        if (V4L2_FIELD_ANY == field) {
 544                if (vpifparams->std_info.frm_fmt)
 545                        vid_ch->buf_field = V4L2_FIELD_NONE;
 546                else
 547                        vid_ch->buf_field = V4L2_FIELD_INTERLACED;
 548        } else
 549                vid_ch->buf_field = common->fmt.fmt.pix.field;
 550
 551        sizeimage = common->fmt.fmt.pix.sizeimage;
 552
 553        hpitch = common->fmt.fmt.pix.bytesperline;
 554
 555        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 556            (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
 557                /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
 558                common->ytop_off = 0;
 559                common->ybtm_off = hpitch;
 560                common->ctop_off = sizeimage / 2;
 561                common->cbtm_off = sizeimage / 2 + hpitch;
 562        } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
 563                /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
 564                common->ytop_off = 0;
 565                common->ybtm_off = sizeimage / 4;
 566                common->ctop_off = sizeimage / 2;
 567                common->cbtm_off = common->ctop_off + sizeimage / 4;
 568        } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
 569                /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
 570                common->ybtm_off = 0;
 571                common->ytop_off = sizeimage / 4;
 572                common->cbtm_off = sizeimage / 2;
 573                common->ctop_off = common->cbtm_off + sizeimage / 4;
 574        }
 575        if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
 576            (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
 577                vpifparams->video_params.storage_mode = 1;
 578        else
 579                vpifparams->video_params.storage_mode = 0;
 580
 581        if (1 == vpifparams->std_info.frm_fmt)
 582                vpifparams->video_params.hpitch =
 583                    common->fmt.fmt.pix.bytesperline;
 584        else {
 585                if ((field == V4L2_FIELD_ANY)
 586                    || (field == V4L2_FIELD_INTERLACED))
 587                        vpifparams->video_params.hpitch =
 588                            common->fmt.fmt.pix.bytesperline * 2;
 589                else
 590                        vpifparams->video_params.hpitch =
 591                            common->fmt.fmt.pix.bytesperline;
 592        }
 593
 594        ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
 595}
 596
 597/**
 598 * vpif_get_default_field() - Get default field type based on interface
 599 * @vpif_params - ptr to vpif params
 600 */
 601static inline enum v4l2_field vpif_get_default_field(
 602                                struct vpif_interface *iface)
 603{
 604        return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
 605                                                V4L2_FIELD_INTERLACED;
 606}
 607
 608/**
 609 * vpif_config_addr() - function to configure buffer address in vpif
 610 * @ch - channel ptr
 611 * @muxmode - channel mux mode
 612 */
 613static void vpif_config_addr(struct channel_obj *ch, int muxmode)
 614{
 615        struct common_obj *common;
 616
 617        vpif_dbg(2, debug, "vpif_config_addr\n");
 618
 619        common = &(ch->common[VPIF_VIDEO_INDEX]);
 620
 621        if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
 622                common->set_addr = ch1_set_videobuf_addr;
 623        else if (2 == muxmode)
 624                common->set_addr = ch0_set_videobuf_addr_yc_nmux;
 625        else
 626                common->set_addr = ch0_set_videobuf_addr;
 627}
 628
 629/**
 630 * vpif_input_to_subdev() - Maps input to sub device
 631 * @vpif_cfg - global config ptr
 632 * @chan_cfg - channel config ptr
 633 * @input_index - Given input index from application
 634 *
 635 * lookup the sub device information for a given input index.
 636 * we report all the inputs to application. inputs table also
 637 * has sub device name for the each input
 638 */
 639static int vpif_input_to_subdev(
 640                struct vpif_capture_config *vpif_cfg,
 641                struct vpif_capture_chan_config *chan_cfg,
 642                int input_index)
 643{
 644        struct vpif_subdev_info *subdev_info;
 645        const char *subdev_name;
 646        int i;
 647
 648        vpif_dbg(2, debug, "vpif_input_to_subdev\n");
 649
 650        subdev_name = chan_cfg->inputs[input_index].subdev_name;
 651        if (subdev_name == NULL)
 652                return -1;
 653
 654        /* loop through the sub device list to get the sub device info */
 655        for (i = 0; i < vpif_cfg->subdev_count; i++) {
 656                subdev_info = &vpif_cfg->subdev_info[i];
 657                if (!strcmp(subdev_info->name, subdev_name))
 658                        return i;
 659        }
 660        return -1;
 661}
 662
 663/**
 664 * vpif_set_input() - Select an input
 665 * @vpif_cfg - global config ptr
 666 * @ch - channel
 667 * @_index - Given input index from application
 668 *
 669 * Select the given input.
 670 */
 671static int vpif_set_input(
 672                struct vpif_capture_config *vpif_cfg,
 673                struct channel_obj *ch,
 674                int index)
 675{
 676        struct vpif_capture_chan_config *chan_cfg =
 677                        &vpif_cfg->chan_config[ch->channel_id];
 678        struct vpif_subdev_info *subdev_info = NULL;
 679        struct v4l2_subdev *sd = NULL;
 680        u32 input = 0, output = 0;
 681        int sd_index;
 682        int ret;
 683
 684        sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
 685        if (sd_index >= 0) {
 686                sd = vpif_obj.sd[sd_index];
 687                subdev_info = &vpif_cfg->subdev_info[sd_index];
 688        }
 689
 690        /* first setup input path from sub device to vpif */
 691        if (sd && vpif_cfg->setup_input_path) {
 692                ret = vpif_cfg->setup_input_path(ch->channel_id,
 693                                       subdev_info->name);
 694                if (ret < 0) {
 695                        vpif_dbg(1, debug, "couldn't setup input path for the" \
 696                        " sub device %s, for input index %d\n",
 697                        subdev_info->name, index);
 698                        return ret;
 699                }
 700        }
 701
 702        if (sd) {
 703                input = chan_cfg->inputs[index].input_route;
 704                output = chan_cfg->inputs[index].output_route;
 705                ret = v4l2_subdev_call(sd, video, s_routing,
 706                                input, output, 0);
 707                if (ret < 0 && ret != -ENOIOCTLCMD) {
 708                        vpif_dbg(1, debug, "Failed to set input\n");
 709                        return ret;
 710                }
 711        }
 712        ch->input_idx = index;
 713        ch->sd = sd;
 714        /* copy interface parameters to vpif */
 715        ch->vpifparams.iface = chan_cfg->vpif_if;
 716
 717        /* update tvnorms from the sub device input info */
 718        ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
 719        return 0;
 720}
 721
 722/**
 723 * vpif_querystd() - querystd handler
 724 * @file: file ptr
 725 * @priv: file handle
 726 * @std_id: ptr to std id
 727 *
 728 * This function is called to detect standard at the selected input
 729 */
 730static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
 731{
 732        struct video_device *vdev = video_devdata(file);
 733        struct channel_obj *ch = video_get_drvdata(vdev);
 734        int ret = 0;
 735
 736        vpif_dbg(2, debug, "vpif_querystd\n");
 737
 738        /* Call querystd function of decoder device */
 739        ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
 740
 741        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
 742                return -ENODATA;
 743        if (ret) {
 744                vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
 745                return ret;
 746        }
 747
 748        return 0;
 749}
 750
 751/**
 752 * vpif_g_std() - get STD handler
 753 * @file: file ptr
 754 * @priv: file handle
 755 * @std_id: ptr to std id
 756 */
 757static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
 758{
 759        struct vpif_capture_config *config = vpif_dev->platform_data;
 760        struct video_device *vdev = video_devdata(file);
 761        struct channel_obj *ch = video_get_drvdata(vdev);
 762        struct vpif_capture_chan_config *chan_cfg;
 763        struct v4l2_input input;
 764
 765        vpif_dbg(2, debug, "vpif_g_std\n");
 766
 767        if (config->chan_config[ch->channel_id].inputs == NULL)
 768                return -ENODATA;
 769
 770        chan_cfg = &config->chan_config[ch->channel_id];
 771        input = chan_cfg->inputs[ch->input_idx].input;
 772        if (input.capabilities != V4L2_IN_CAP_STD)
 773                return -ENODATA;
 774
 775        *std = ch->video.stdid;
 776        return 0;
 777}
 778
 779/**
 780 * vpif_s_std() - set STD handler
 781 * @file: file ptr
 782 * @priv: file handle
 783 * @std_id: ptr to std id
 784 */
 785static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
 786{
 787        struct vpif_capture_config *config = vpif_dev->platform_data;
 788        struct video_device *vdev = video_devdata(file);
 789        struct channel_obj *ch = video_get_drvdata(vdev);
 790        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 791        struct vpif_capture_chan_config *chan_cfg;
 792        struct v4l2_input input;
 793        int ret;
 794
 795        vpif_dbg(2, debug, "vpif_s_std\n");
 796
 797        if (config->chan_config[ch->channel_id].inputs == NULL)
 798                return -ENODATA;
 799
 800        chan_cfg = &config->chan_config[ch->channel_id];
 801        input = chan_cfg->inputs[ch->input_idx].input;
 802        if (input.capabilities != V4L2_IN_CAP_STD)
 803                return -ENODATA;
 804
 805        if (vb2_is_busy(&common->buffer_queue))
 806                return -EBUSY;
 807
 808        /* Call encoder subdevice function to set the standard */
 809        ch->video.stdid = std_id;
 810        memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
 811
 812        /* Get the information about the standard */
 813        if (vpif_update_std_info(ch)) {
 814                vpif_err("Error getting the standard info\n");
 815                return -EINVAL;
 816        }
 817
 818        /* set standard in the sub device */
 819        ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
 820        if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
 821                vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
 822                return ret;
 823        }
 824        return 0;
 825}
 826
 827/**
 828 * vpif_enum_input() - ENUMINPUT handler
 829 * @file: file ptr
 830 * @priv: file handle
 831 * @input: ptr to input structure
 832 */
 833static int vpif_enum_input(struct file *file, void *priv,
 834                                struct v4l2_input *input)
 835{
 836
 837        struct vpif_capture_config *config = vpif_dev->platform_data;
 838        struct video_device *vdev = video_devdata(file);
 839        struct channel_obj *ch = video_get_drvdata(vdev);
 840        struct vpif_capture_chan_config *chan_cfg;
 841
 842        chan_cfg = &config->chan_config[ch->channel_id];
 843
 844        if (input->index >= chan_cfg->input_count)
 845                return -EINVAL;
 846
 847        memcpy(input, &chan_cfg->inputs[input->index].input,
 848                sizeof(*input));
 849        return 0;
 850}
 851
 852/**
 853 * vpif_g_input() - Get INPUT handler
 854 * @file: file ptr
 855 * @priv: file handle
 856 * @index: ptr to input index
 857 */
 858static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
 859{
 860        struct video_device *vdev = video_devdata(file);
 861        struct channel_obj *ch = video_get_drvdata(vdev);
 862
 863        *index = ch->input_idx;
 864        return 0;
 865}
 866
 867/**
 868 * vpif_s_input() - Set INPUT handler
 869 * @file: file ptr
 870 * @priv: file handle
 871 * @index: input index
 872 */
 873static int vpif_s_input(struct file *file, void *priv, unsigned int index)
 874{
 875        struct vpif_capture_config *config = vpif_dev->platform_data;
 876        struct video_device *vdev = video_devdata(file);
 877        struct channel_obj *ch = video_get_drvdata(vdev);
 878        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 879        struct vpif_capture_chan_config *chan_cfg;
 880
 881        chan_cfg = &config->chan_config[ch->channel_id];
 882
 883        if (index >= chan_cfg->input_count)
 884                return -EINVAL;
 885
 886        if (vb2_is_busy(&common->buffer_queue))
 887                return -EBUSY;
 888
 889        return vpif_set_input(config, ch, index);
 890}
 891
 892/**
 893 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
 894 * @file: file ptr
 895 * @priv: file handle
 896 * @index: input index
 897 */
 898static int vpif_enum_fmt_vid_cap(struct file *file, void  *priv,
 899                                        struct v4l2_fmtdesc *fmt)
 900{
 901        struct video_device *vdev = video_devdata(file);
 902        struct channel_obj *ch = video_get_drvdata(vdev);
 903
 904        if (fmt->index != 0) {
 905                vpif_dbg(1, debug, "Invalid format index\n");
 906                return -EINVAL;
 907        }
 908
 909        /* Fill in the information about format */
 910        if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
 911                fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 912                strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
 913                fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
 914        } else {
 915                fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 916                strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
 917                fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
 918        }
 919        return 0;
 920}
 921
 922/**
 923 * vpif_try_fmt_vid_cap() - TRY_FMT handler
 924 * @file: file ptr
 925 * @priv: file handle
 926 * @fmt: ptr to v4l2 format structure
 927 */
 928static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
 929                                struct v4l2_format *fmt)
 930{
 931        struct video_device *vdev = video_devdata(file);
 932        struct channel_obj *ch = video_get_drvdata(vdev);
 933        struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
 934        struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
 935        struct vpif_params *vpif_params = &ch->vpifparams;
 936
 937        /*
 938         * to supress v4l-compliance warnings silently correct
 939         * the pixelformat
 940         */
 941        if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
 942                if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
 943                        pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
 944        } else {
 945                if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
 946                        pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
 947        }
 948
 949        common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
 950
 951        vpif_update_std_info(ch);
 952
 953        pixfmt->field = common->fmt.fmt.pix.field;
 954        pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
 955        pixfmt->bytesperline = common->fmt.fmt.pix.width;
 956        pixfmt->width = common->fmt.fmt.pix.width;
 957        pixfmt->height = common->fmt.fmt.pix.height;
 958        pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
 959        pixfmt->priv = 0;
 960
 961        return 0;
 962}
 963
 964
 965/**
 966 * vpif_g_fmt_vid_cap() - Set INPUT handler
 967 * @file: file ptr
 968 * @priv: file handle
 969 * @fmt: ptr to v4l2 format structure
 970 */
 971static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
 972                                struct v4l2_format *fmt)
 973{
 974        struct video_device *vdev = video_devdata(file);
 975        struct channel_obj *ch = video_get_drvdata(vdev);
 976        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 977
 978        /* Check the validity of the buffer type */
 979        if (common->fmt.type != fmt->type)
 980                return -EINVAL;
 981
 982        /* Fill in the information about format */
 983        *fmt = common->fmt;
 984        return 0;
 985}
 986
 987/**
 988 * vpif_s_fmt_vid_cap() - Set FMT handler
 989 * @file: file ptr
 990 * @priv: file handle
 991 * @fmt: ptr to v4l2 format structure
 992 */
 993static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
 994                                struct v4l2_format *fmt)
 995{
 996        struct video_device *vdev = video_devdata(file);
 997        struct channel_obj *ch = video_get_drvdata(vdev);
 998        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
 999        int ret;
1000
1001        vpif_dbg(2, debug, "%s\n", __func__);
1002
1003        if (vb2_is_busy(&common->buffer_queue))
1004                return -EBUSY;
1005
1006        ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1007        if (ret)
1008                return ret;
1009
1010        /* store the format in the channel object */
1011        common->fmt = *fmt;
1012        return 0;
1013}
1014
1015/**
1016 * vpif_querycap() - QUERYCAP handler
1017 * @file: file ptr
1018 * @priv: file handle
1019 * @cap: ptr to v4l2_capability structure
1020 */
1021static int vpif_querycap(struct file *file, void  *priv,
1022                                struct v4l2_capability *cap)
1023{
1024        struct vpif_capture_config *config = vpif_dev->platform_data;
1025
1026        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1027        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1028        strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1029        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1030                 dev_name(vpif_dev));
1031        strlcpy(cap->card, config->card_name, sizeof(cap->card));
1032
1033        return 0;
1034}
1035
1036/**
1037 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1038 * @file: file ptr
1039 * @priv: file handle
1040 * @timings: input timings
1041 */
1042static int
1043vpif_enum_dv_timings(struct file *file, void *priv,
1044                     struct v4l2_enum_dv_timings *timings)
1045{
1046        struct vpif_capture_config *config = vpif_dev->platform_data;
1047        struct video_device *vdev = video_devdata(file);
1048        struct channel_obj *ch = video_get_drvdata(vdev);
1049        struct vpif_capture_chan_config *chan_cfg;
1050        struct v4l2_input input;
1051        int ret;
1052
1053        if (config->chan_config[ch->channel_id].inputs == NULL)
1054                return -ENODATA;
1055
1056        chan_cfg = &config->chan_config[ch->channel_id];
1057        input = chan_cfg->inputs[ch->input_idx].input;
1058        if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1059                return -ENODATA;
1060
1061        timings->pad = 0;
1062
1063        ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1064        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1065                return -EINVAL;
1066
1067        return ret;
1068}
1069
1070/**
1071 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1072 * @file: file ptr
1073 * @priv: file handle
1074 * @timings: input timings
1075 */
1076static int
1077vpif_query_dv_timings(struct file *file, void *priv,
1078                      struct v4l2_dv_timings *timings)
1079{
1080        struct vpif_capture_config *config = vpif_dev->platform_data;
1081        struct video_device *vdev = video_devdata(file);
1082        struct channel_obj *ch = video_get_drvdata(vdev);
1083        struct vpif_capture_chan_config *chan_cfg;
1084        struct v4l2_input input;
1085        int ret;
1086
1087        if (config->chan_config[ch->channel_id].inputs == NULL)
1088                return -ENODATA;
1089
1090        chan_cfg = &config->chan_config[ch->channel_id];
1091        input = chan_cfg->inputs[ch->input_idx].input;
1092        if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1093                return -ENODATA;
1094
1095        ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1096        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1097                return -ENODATA;
1098
1099        return ret;
1100}
1101
1102/**
1103 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1104 * @file: file ptr
1105 * @priv: file handle
1106 * @timings: digital video timings
1107 */
1108static int vpif_s_dv_timings(struct file *file, void *priv,
1109                struct v4l2_dv_timings *timings)
1110{
1111        struct vpif_capture_config *config = vpif_dev->platform_data;
1112        struct video_device *vdev = video_devdata(file);
1113        struct channel_obj *ch = video_get_drvdata(vdev);
1114        struct vpif_params *vpifparams = &ch->vpifparams;
1115        struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1116        struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1117        struct video_obj *vid_ch = &ch->video;
1118        struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1119        struct vpif_capture_chan_config *chan_cfg;
1120        struct v4l2_input input;
1121        int ret;
1122
1123        if (config->chan_config[ch->channel_id].inputs == NULL)
1124                return -ENODATA;
1125
1126        chan_cfg = &config->chan_config[ch->channel_id];
1127        input = chan_cfg->inputs[ch->input_idx].input;
1128        if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1129                return -ENODATA;
1130
1131        if (timings->type != V4L2_DV_BT_656_1120) {
1132                vpif_dbg(2, debug, "Timing type not defined\n");
1133                return -EINVAL;
1134        }
1135
1136        if (vb2_is_busy(&common->buffer_queue))
1137                return -EBUSY;
1138
1139        /* Configure subdevice timings, if any */
1140        ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1141        if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1142                ret = 0;
1143        if (ret < 0) {
1144                vpif_dbg(2, debug, "Error setting custom DV timings\n");
1145                return ret;
1146        }
1147
1148        if (!(timings->bt.width && timings->bt.height &&
1149                                (timings->bt.hbackporch ||
1150                                 timings->bt.hfrontporch ||
1151                                 timings->bt.hsync) &&
1152                                timings->bt.vfrontporch &&
1153                                (timings->bt.vbackporch ||
1154                                 timings->bt.vsync))) {
1155                vpif_dbg(2, debug, "Timings for width, height, "
1156                                "horizontal back porch, horizontal sync, "
1157                                "horizontal front porch, vertical back porch, "
1158                                "vertical sync and vertical back porch "
1159                                "must be defined\n");
1160                return -EINVAL;
1161        }
1162
1163        vid_ch->dv_timings = *timings;
1164
1165        /* Configure video port timings */
1166
1167        std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1168        std_info->sav2eav = bt->width;
1169
1170        std_info->l1 = 1;
1171        std_info->l3 = bt->vsync + bt->vbackporch + 1;
1172
1173        std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1174        if (bt->interlaced) {
1175                if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1176                        std_info->l5 = std_info->vsize/2 -
1177                                (bt->vfrontporch - 1);
1178                        std_info->l7 = std_info->vsize/2 + 1;
1179                        std_info->l9 = std_info->l7 + bt->il_vsync +
1180                                bt->il_vbackporch + 1;
1181                        std_info->l11 = std_info->vsize -
1182                                (bt->il_vfrontporch - 1);
1183                } else {
1184                        vpif_dbg(2, debug, "Required timing values for "
1185                                        "interlaced BT format missing\n");
1186                        return -EINVAL;
1187                }
1188        } else {
1189                std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1190        }
1191        strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1192        std_info->width = bt->width;
1193        std_info->height = bt->height;
1194        std_info->frm_fmt = bt->interlaced ? 0 : 1;
1195        std_info->ycmux_mode = 0;
1196        std_info->capture_format = 0;
1197        std_info->vbi_supported = 0;
1198        std_info->hd_sd = 1;
1199        std_info->stdid = 0;
1200
1201        vid_ch->stdid = 0;
1202        return 0;
1203}
1204
1205/**
1206 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1207 * @file: file ptr
1208 * @priv: file handle
1209 * @timings: digital video timings
1210 */
1211static int vpif_g_dv_timings(struct file *file, void *priv,
1212                struct v4l2_dv_timings *timings)
1213{
1214        struct vpif_capture_config *config = vpif_dev->platform_data;
1215        struct video_device *vdev = video_devdata(file);
1216        struct channel_obj *ch = video_get_drvdata(vdev);
1217        struct video_obj *vid_ch = &ch->video;
1218        struct vpif_capture_chan_config *chan_cfg;
1219        struct v4l2_input input;
1220
1221        if (config->chan_config[ch->channel_id].inputs == NULL)
1222                return -ENODATA;
1223
1224        chan_cfg = &config->chan_config[ch->channel_id];
1225        input = chan_cfg->inputs[ch->input_idx].input;
1226        if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1227                return -ENODATA;
1228
1229        *timings = vid_ch->dv_timings;
1230
1231        return 0;
1232}
1233
1234/*
1235 * vpif_log_status() - Status information
1236 * @file: file ptr
1237 * @priv: file handle
1238 *
1239 * Returns zero.
1240 */
1241static int vpif_log_status(struct file *filep, void *priv)
1242{
1243        /* status for sub devices */
1244        v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1245
1246        return 0;
1247}
1248
1249/* vpif capture ioctl operations */
1250static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1251        .vidioc_querycap                = vpif_querycap,
1252        .vidioc_enum_fmt_vid_cap        = vpif_enum_fmt_vid_cap,
1253        .vidioc_g_fmt_vid_cap           = vpif_g_fmt_vid_cap,
1254        .vidioc_s_fmt_vid_cap           = vpif_s_fmt_vid_cap,
1255        .vidioc_try_fmt_vid_cap         = vpif_try_fmt_vid_cap,
1256
1257        .vidioc_enum_input              = vpif_enum_input,
1258        .vidioc_s_input                 = vpif_s_input,
1259        .vidioc_g_input                 = vpif_g_input,
1260
1261        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1262        .vidioc_create_bufs             = vb2_ioctl_create_bufs,
1263        .vidioc_querybuf                = vb2_ioctl_querybuf,
1264        .vidioc_qbuf                    = vb2_ioctl_qbuf,
1265        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1266        .vidioc_expbuf                  = vb2_ioctl_expbuf,
1267        .vidioc_streamon                = vb2_ioctl_streamon,
1268        .vidioc_streamoff               = vb2_ioctl_streamoff,
1269
1270        .vidioc_querystd                = vpif_querystd,
1271        .vidioc_s_std                   = vpif_s_std,
1272        .vidioc_g_std                   = vpif_g_std,
1273
1274        .vidioc_enum_dv_timings         = vpif_enum_dv_timings,
1275        .vidioc_query_dv_timings        = vpif_query_dv_timings,
1276        .vidioc_s_dv_timings            = vpif_s_dv_timings,
1277        .vidioc_g_dv_timings            = vpif_g_dv_timings,
1278
1279        .vidioc_log_status              = vpif_log_status,
1280};
1281
1282/* vpif file operations */
1283static struct v4l2_file_operations vpif_fops = {
1284        .owner = THIS_MODULE,
1285        .open = v4l2_fh_open,
1286        .release = vb2_fop_release,
1287        .unlocked_ioctl = video_ioctl2,
1288        .mmap = vb2_fop_mmap,
1289        .poll = vb2_fop_poll
1290};
1291
1292/**
1293 * initialize_vpif() - Initialize vpif data structures
1294 *
1295 * Allocate memory for data structures and initialize them
1296 */
1297static int initialize_vpif(void)
1298{
1299        int err, i, j;
1300        int free_channel_objects_index;
1301
1302        /* Allocate memory for six channel objects */
1303        for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1304                vpif_obj.dev[i] =
1305                    kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1306                /* If memory allocation fails, return error */
1307                if (!vpif_obj.dev[i]) {
1308                        free_channel_objects_index = i;
1309                        err = -ENOMEM;
1310                        goto vpif_init_free_channel_objects;
1311                }
1312        }
1313        return 0;
1314
1315vpif_init_free_channel_objects:
1316        for (j = 0; j < free_channel_objects_index; j++)
1317                kfree(vpif_obj.dev[j]);
1318        return err;
1319}
1320
1321static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1322                            struct v4l2_subdev *subdev,
1323                            struct v4l2_async_subdev *asd)
1324{
1325        int i;
1326
1327        for (i = 0; i < vpif_obj.config->subdev_count; i++)
1328                if (!strcmp(vpif_obj.config->subdev_info[i].name,
1329                            subdev->name)) {
1330                        vpif_obj.sd[i] = subdev;
1331                        return 0;
1332                }
1333
1334        return -EINVAL;
1335}
1336
1337static int vpif_probe_complete(void)
1338{
1339        struct common_obj *common;
1340        struct video_device *vdev;
1341        struct channel_obj *ch;
1342        struct vb2_queue *q;
1343        int j, err, k;
1344
1345        for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1346                ch = vpif_obj.dev[j];
1347                ch->channel_id = j;
1348                common = &(ch->common[VPIF_VIDEO_INDEX]);
1349                spin_lock_init(&common->irqlock);
1350                mutex_init(&common->lock);
1351
1352                /* select input 0 */
1353                err = vpif_set_input(vpif_obj.config, ch, 0);
1354                if (err)
1355                        goto probe_out;
1356
1357                /* set initial format */
1358                ch->video.stdid = V4L2_STD_525_60;
1359                memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1360                vpif_update_std_info(ch);
1361
1362                /* Initialize vb2 queue */
1363                q = &common->buffer_queue;
1364                q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1365                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1366                q->drv_priv = ch;
1367                q->ops = &video_qops;
1368                q->mem_ops = &vb2_dma_contig_memops;
1369                q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1370                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1371                q->min_buffers_needed = 1;
1372                q->lock = &common->lock;
1373                q->dev = vpif_dev;
1374
1375                err = vb2_queue_init(q);
1376                if (err) {
1377                        vpif_err("vpif_capture: vb2_queue_init() failed\n");
1378                        goto probe_out;
1379                }
1380
1381                INIT_LIST_HEAD(&common->dma_queue);
1382
1383                /* Initialize the video_device structure */
1384                vdev = &ch->video_dev;
1385                strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1386                vdev->release = video_device_release_empty;
1387                vdev->fops = &vpif_fops;
1388                vdev->ioctl_ops = &vpif_ioctl_ops;
1389                vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1390                vdev->vfl_dir = VFL_DIR_RX;
1391                vdev->queue = q;
1392                vdev->lock = &common->lock;
1393                video_set_drvdata(&ch->video_dev, ch);
1394                err = video_register_device(vdev,
1395                                            VFL_TYPE_GRABBER, (j ? 1 : 0));
1396                if (err)
1397                        goto probe_out;
1398        }
1399
1400        v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1401        return 0;
1402
1403probe_out:
1404        for (k = 0; k < j; k++) {
1405                /* Get the pointer to the channel object */
1406                ch = vpif_obj.dev[k];
1407                common = &ch->common[k];
1408                /* Unregister video device */
1409                video_unregister_device(&ch->video_dev);
1410        }
1411        kfree(vpif_obj.sd);
1412        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1413
1414        return err;
1415}
1416
1417static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1418{
1419        return vpif_probe_complete();
1420}
1421
1422/**
1423 * vpif_probe : This function probes the vpif capture driver
1424 * @pdev: platform device pointer
1425 *
1426 * This creates device entries by register itself to the V4L2 driver and
1427 * initializes fields of each channel objects
1428 */
1429static __init int vpif_probe(struct platform_device *pdev)
1430{
1431        struct vpif_subdev_info *subdevdata;
1432        struct i2c_adapter *i2c_adap;
1433        struct resource *res;
1434        int subdev_count;
1435        int res_idx = 0;
1436        int i, err;
1437
1438        vpif_dev = &pdev->dev;
1439
1440        err = initialize_vpif();
1441        if (err) {
1442                v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1443                return err;
1444        }
1445
1446        err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1447        if (err) {
1448                v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1449                return err;
1450        }
1451
1452        while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1453                err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1454                                        IRQF_SHARED, VPIF_DRIVER_NAME,
1455                                        (void *)(&vpif_obj.dev[res_idx]->
1456                                        channel_id));
1457                if (err) {
1458                        err = -EINVAL;
1459                        goto vpif_unregister;
1460                }
1461                res_idx++;
1462        }
1463
1464        vpif_obj.config = pdev->dev.platform_data;
1465
1466        subdev_count = vpif_obj.config->subdev_count;
1467        vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1468                                GFP_KERNEL);
1469        if (vpif_obj.sd == NULL) {
1470                vpif_err("unable to allocate memory for subdevice pointers\n");
1471                err = -ENOMEM;
1472                goto vpif_unregister;
1473        }
1474
1475        if (!vpif_obj.config->asd_sizes) {
1476                i2c_adap = i2c_get_adapter(1);
1477                for (i = 0; i < subdev_count; i++) {
1478                        subdevdata = &vpif_obj.config->subdev_info[i];
1479                        vpif_obj.sd[i] =
1480                                v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1481                                                          i2c_adap,
1482                                                          &subdevdata->
1483                                                          board_info,
1484                                                          NULL);
1485
1486                        if (!vpif_obj.sd[i]) {
1487                                vpif_err("Error registering v4l2 subdevice\n");
1488                                err = -ENODEV;
1489                                goto probe_subdev_out;
1490                        }
1491                        v4l2_info(&vpif_obj.v4l2_dev,
1492                                  "registered sub device %s\n",
1493                                   subdevdata->name);
1494                }
1495                vpif_probe_complete();
1496        } else {
1497                vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1498                vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1499                vpif_obj.notifier.bound = vpif_async_bound;
1500                vpif_obj.notifier.complete = vpif_async_complete;
1501                err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1502                                                   &vpif_obj.notifier);
1503                if (err) {
1504                        vpif_err("Error registering async notifier\n");
1505                        err = -EINVAL;
1506                        goto probe_subdev_out;
1507                }
1508        }
1509
1510        return 0;
1511
1512probe_subdev_out:
1513        /* free sub devices memory */
1514        kfree(vpif_obj.sd);
1515vpif_unregister:
1516        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1517
1518        return err;
1519}
1520
1521/**
1522 * vpif_remove() - driver remove handler
1523 * @device: ptr to platform device structure
1524 *
1525 * The vidoe device is unregistered
1526 */
1527static int vpif_remove(struct platform_device *device)
1528{
1529        struct common_obj *common;
1530        struct channel_obj *ch;
1531        int i;
1532
1533        v4l2_device_unregister(&vpif_obj.v4l2_dev);
1534
1535        kfree(vpif_obj.sd);
1536        /* un-register device */
1537        for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1538                /* Get the pointer to the channel object */
1539                ch = vpif_obj.dev[i];
1540                common = &ch->common[VPIF_VIDEO_INDEX];
1541                /* Unregister video device */
1542                video_unregister_device(&ch->video_dev);
1543                kfree(vpif_obj.dev[i]);
1544        }
1545        return 0;
1546}
1547
1548#ifdef CONFIG_PM_SLEEP
1549/**
1550 * vpif_suspend: vpif device suspend
1551 */
1552static int vpif_suspend(struct device *dev)
1553{
1554
1555        struct common_obj *common;
1556        struct channel_obj *ch;
1557        int i;
1558
1559        for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1560                /* Get the pointer to the channel object */
1561                ch = vpif_obj.dev[i];
1562                common = &ch->common[VPIF_VIDEO_INDEX];
1563
1564                if (!vb2_start_streaming_called(&common->buffer_queue))
1565                        continue;
1566
1567                mutex_lock(&common->lock);
1568                /* Disable channel */
1569                if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1570                        enable_channel0(0);
1571                        channel0_intr_enable(0);
1572                }
1573                if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1574                        ycmux_mode == 2) {
1575                        enable_channel1(0);
1576                        channel1_intr_enable(0);
1577                }
1578                mutex_unlock(&common->lock);
1579        }
1580
1581        return 0;
1582}
1583
1584/*
1585 * vpif_resume: vpif device suspend
1586 */
1587static int vpif_resume(struct device *dev)
1588{
1589        struct common_obj *common;
1590        struct channel_obj *ch;
1591        int i;
1592
1593        for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1594                /* Get the pointer to the channel object */
1595                ch = vpif_obj.dev[i];
1596                common = &ch->common[VPIF_VIDEO_INDEX];
1597
1598                if (!vb2_start_streaming_called(&common->buffer_queue))
1599                        continue;
1600
1601                mutex_lock(&common->lock);
1602                /* Enable channel */
1603                if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1604                        enable_channel0(1);
1605                        channel0_intr_enable(1);
1606                }
1607                if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1608                        ycmux_mode == 2) {
1609                        enable_channel1(1);
1610                        channel1_intr_enable(1);
1611                }
1612                mutex_unlock(&common->lock);
1613        }
1614
1615        return 0;
1616}
1617#endif
1618
1619static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1620
1621static __refdata struct platform_driver vpif_driver = {
1622        .driver = {
1623                .name   = VPIF_DRIVER_NAME,
1624                .pm     = &vpif_pm_ops,
1625        },
1626        .probe = vpif_probe,
1627        .remove = vpif_remove,
1628};
1629
1630module_platform_driver(vpif_driver);
1631