linux/drivers/media/platform/am437x/am437x-vpfe.c
<<
>>
Prefs
   1/*
   2 * TI VPFE capture Driver
   3 *
   4 * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
   5 *
   6 * Benoit Parrot <bparrot@ti.com>
   7 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
   8 *
   9 * This program is free software; you may redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; version 2 of the License.
  12 *
  13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20 * SOFTWARE.
  21 */
  22
  23#include <linux/delay.h>
  24#include <linux/err.h>
  25#include <linux/init.h>
  26#include <linux/interrupt.h>
  27#include <linux/io.h>
  28#include <linux/module.h>
  29#include <linux/of_graph.h>
  30#include <linux/pinctrl/consumer.h>
  31#include <linux/platform_device.h>
  32#include <linux/pm_runtime.h>
  33#include <linux/slab.h>
  34#include <linux/uaccess.h>
  35#include <linux/videodev2.h>
  36
  37#include <media/v4l2-common.h>
  38#include <media/v4l2-ctrls.h>
  39#include <media/v4l2-event.h>
  40#include <media/v4l2-fwnode.h>
  41
  42#include "am437x-vpfe.h"
  43
  44#define VPFE_MODULE_NAME        "vpfe"
  45#define VPFE_VERSION            "0.1.0"
  46
  47static int debug;
  48module_param(debug, int, 0644);
  49MODULE_PARM_DESC(debug, "Debug level 0-8");
  50
  51#define vpfe_dbg(level, dev, fmt, arg...)       \
  52                v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
  53#define vpfe_info(dev, fmt, arg...)     \
  54                v4l2_info(&dev->v4l2_dev, fmt, ##arg)
  55#define vpfe_err(dev, fmt, arg...)      \
  56                v4l2_err(&dev->v4l2_dev, fmt, ##arg)
  57
  58/* standard information */
  59struct vpfe_standard {
  60        v4l2_std_id std_id;
  61        unsigned int width;
  62        unsigned int height;
  63        struct v4l2_fract pixelaspect;
  64        int frame_format;
  65};
  66
  67static const struct vpfe_standard vpfe_standards[] = {
  68        {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
  69        {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
  70};
  71
  72struct bus_format {
  73        unsigned int width;
  74        unsigned int bpp;
  75};
  76
  77/*
  78 * struct vpfe_fmt - VPFE media bus format information
  79 * @name: V4L2 format description
  80 * @code: V4L2 media bus format code
  81 * @shifted: V4L2 media bus format code for the same pixel layout but
  82 *      shifted to be 8 bits per pixel. =0 if format is not shiftable.
  83 * @pixelformat: V4L2 pixel format FCC identifier
  84 * @width: Bits per pixel (when transferred over a bus)
  85 * @bpp: Bytes per pixel (when stored in memory)
  86 * @supported: Indicates format supported by subdev
  87 */
  88struct vpfe_fmt {
  89        const char *name;
  90        u32 fourcc;
  91        u32 code;
  92        struct bus_format l;
  93        struct bus_format s;
  94        bool supported;
  95        u32 index;
  96};
  97
  98static struct vpfe_fmt formats[] = {
  99        {
 100                .name           = "YUV 4:2:2 packed, YCbYCr",
 101                .fourcc         = V4L2_PIX_FMT_YUYV,
 102                .code           = MEDIA_BUS_FMT_YUYV8_2X8,
 103                .l.width        = 10,
 104                .l.bpp          = 4,
 105                .s.width        = 8,
 106                .s.bpp          = 2,
 107                .supported      = false,
 108        }, {
 109                .name           = "YUV 4:2:2 packed, CbYCrY",
 110                .fourcc         = V4L2_PIX_FMT_UYVY,
 111                .code           = MEDIA_BUS_FMT_UYVY8_2X8,
 112                .l.width        = 10,
 113                .l.bpp          = 4,
 114                .s.width        = 8,
 115                .s.bpp          = 2,
 116                .supported      = false,
 117        }, {
 118                .name           = "YUV 4:2:2 packed, YCrYCb",
 119                .fourcc         = V4L2_PIX_FMT_YVYU,
 120                .code           = MEDIA_BUS_FMT_YVYU8_2X8,
 121                .l.width        = 10,
 122                .l.bpp          = 4,
 123                .s.width        = 8,
 124                .s.bpp          = 2,
 125                .supported      = false,
 126        }, {
 127                .name           = "YUV 4:2:2 packed, CrYCbY",
 128                .fourcc         = V4L2_PIX_FMT_VYUY,
 129                .code           = MEDIA_BUS_FMT_VYUY8_2X8,
 130                .l.width        = 10,
 131                .l.bpp          = 4,
 132                .s.width        = 8,
 133                .s.bpp          = 2,
 134                .supported      = false,
 135        }, {
 136                .name           = "RAW8 BGGR",
 137                .fourcc         = V4L2_PIX_FMT_SBGGR8,
 138                .code           = MEDIA_BUS_FMT_SBGGR8_1X8,
 139                .l.width        = 10,
 140                .l.bpp          = 2,
 141                .s.width        = 8,
 142                .s.bpp          = 1,
 143                .supported      = false,
 144        }, {
 145                .name           = "RAW8 GBRG",
 146                .fourcc         = V4L2_PIX_FMT_SGBRG8,
 147                .code           = MEDIA_BUS_FMT_SGBRG8_1X8,
 148                .l.width        = 10,
 149                .l.bpp          = 2,
 150                .s.width        = 8,
 151                .s.bpp          = 1,
 152                .supported      = false,
 153        }, {
 154                .name           = "RAW8 GRBG",
 155                .fourcc         = V4L2_PIX_FMT_SGRBG8,
 156                .code           = MEDIA_BUS_FMT_SGRBG8_1X8,
 157                .l.width        = 10,
 158                .l.bpp          = 2,
 159                .s.width        = 8,
 160                .s.bpp          = 1,
 161                .supported      = false,
 162        }, {
 163                .name           = "RAW8 RGGB",
 164                .fourcc         = V4L2_PIX_FMT_SRGGB8,
 165                .code           = MEDIA_BUS_FMT_SRGGB8_1X8,
 166                .l.width        = 10,
 167                .l.bpp          = 2,
 168                .s.width        = 8,
 169                .s.bpp          = 1,
 170                .supported      = false,
 171        }, {
 172                .name           = "RGB565 (LE)",
 173                .fourcc         = V4L2_PIX_FMT_RGB565,
 174                .code           = MEDIA_BUS_FMT_RGB565_2X8_LE,
 175                .l.width        = 10,
 176                .l.bpp          = 4,
 177                .s.width        = 8,
 178                .s.bpp          = 2,
 179                .supported      = false,
 180        }, {
 181                .name           = "RGB565 (BE)",
 182                .fourcc         = V4L2_PIX_FMT_RGB565X,
 183                .code           = MEDIA_BUS_FMT_RGB565_2X8_BE,
 184                .l.width        = 10,
 185                .l.bpp          = 4,
 186                .s.width        = 8,
 187                .s.bpp          = 2,
 188                .supported      = false,
 189        },
 190};
 191
 192static int
 193__vpfe_get_format(struct vpfe_device *vpfe,
 194                  struct v4l2_format *format, unsigned int *bpp);
 195
 196static struct vpfe_fmt *find_format_by_code(unsigned int code)
 197{
 198        struct vpfe_fmt *fmt;
 199        unsigned int k;
 200
 201        for (k = 0; k < ARRAY_SIZE(formats); k++) {
 202                fmt = &formats[k];
 203                if (fmt->code == code)
 204                        return fmt;
 205        }
 206
 207        return NULL;
 208}
 209
 210static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
 211{
 212        struct vpfe_fmt *fmt;
 213        unsigned int k;
 214
 215        for (k = 0; k < ARRAY_SIZE(formats); k++) {
 216                fmt = &formats[k];
 217                if (fmt->fourcc == pixelformat)
 218                        return fmt;
 219        }
 220
 221        return NULL;
 222}
 223
 224static void
 225mbus_to_pix(struct vpfe_device *vpfe,
 226            const struct v4l2_mbus_framefmt *mbus,
 227            struct v4l2_pix_format *pix, unsigned int *bpp)
 228{
 229        struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
 230        unsigned int bus_width = sdinfo->vpfe_param.bus_width;
 231        struct vpfe_fmt *fmt;
 232
 233        fmt = find_format_by_code(mbus->code);
 234        if (WARN_ON(fmt == NULL)) {
 235                pr_err("Invalid mbus code set\n");
 236                *bpp = 1;
 237                return;
 238        }
 239
 240        memset(pix, 0, sizeof(*pix));
 241        v4l2_fill_pix_format(pix, mbus);
 242        pix->pixelformat = fmt->fourcc;
 243        *bpp = (bus_width == 10) ?  fmt->l.bpp : fmt->s.bpp;
 244
 245        /* pitch should be 32 bytes aligned */
 246        pix->bytesperline = ALIGN(pix->width * *bpp, 32);
 247        pix->sizeimage = pix->bytesperline * pix->height;
 248}
 249
 250static void pix_to_mbus(struct vpfe_device *vpfe,
 251                        struct v4l2_pix_format *pix_fmt,
 252                        struct v4l2_mbus_framefmt *mbus_fmt)
 253{
 254        struct vpfe_fmt *fmt;
 255
 256        fmt = find_format_by_pix(pix_fmt->pixelformat);
 257        if (!fmt) {
 258                /* default to first entry */
 259                vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
 260                        pix_fmt->pixelformat);
 261                fmt = &formats[0];
 262        }
 263
 264        memset(mbus_fmt, 0, sizeof(*mbus_fmt));
 265        v4l2_fill_mbus_format(mbus_fmt, pix_fmt, fmt->code);
 266}
 267
 268/*  Print Four-character-code (FOURCC) */
 269static char *print_fourcc(u32 fmt)
 270{
 271        static char code[5];
 272
 273        code[0] = (unsigned char)(fmt & 0xff);
 274        code[1] = (unsigned char)((fmt >> 8) & 0xff);
 275        code[2] = (unsigned char)((fmt >> 16) & 0xff);
 276        code[3] = (unsigned char)((fmt >> 24) & 0xff);
 277        code[4] = '\0';
 278
 279        return code;
 280}
 281
 282static int
 283cmp_v4l2_format(const struct v4l2_format *lhs, const struct v4l2_format *rhs)
 284{
 285        return lhs->type == rhs->type &&
 286                lhs->fmt.pix.width == rhs->fmt.pix.width &&
 287                lhs->fmt.pix.height == rhs->fmt.pix.height &&
 288                lhs->fmt.pix.pixelformat == rhs->fmt.pix.pixelformat &&
 289                lhs->fmt.pix.field == rhs->fmt.pix.field &&
 290                lhs->fmt.pix.colorspace == rhs->fmt.pix.colorspace &&
 291                lhs->fmt.pix.ycbcr_enc == rhs->fmt.pix.ycbcr_enc &&
 292                lhs->fmt.pix.quantization == rhs->fmt.pix.quantization &&
 293                lhs->fmt.pix.xfer_func == rhs->fmt.pix.xfer_func;
 294}
 295
 296static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
 297{
 298        return ioread32(ccdc->ccdc_cfg.base_addr + offset);
 299}
 300
 301static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
 302{
 303        iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
 304}
 305
 306static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
 307{
 308        return container_of(ccdc, struct vpfe_device, ccdc);
 309}
 310
 311static inline
 312struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
 313{
 314        return container_of(vb, struct vpfe_cap_buffer, vb);
 315}
 316
 317static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
 318{
 319        vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
 320}
 321
 322static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
 323{
 324        unsigned int cfg;
 325
 326        if (!flag) {
 327                cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
 328                cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
 329        } else {
 330                cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
 331        }
 332
 333        vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
 334}
 335
 336static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
 337                             struct v4l2_rect *image_win,
 338                             enum ccdc_frmfmt frm_fmt,
 339                             int bpp)
 340{
 341        int horz_start, horz_nr_pixels;
 342        int vert_start, vert_nr_lines;
 343        int val, mid_img;
 344
 345        /*
 346         * ppc - per pixel count. indicates how many pixels per cell
 347         * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
 348         * raw capture this is 1
 349         */
 350        horz_start = image_win->left * bpp;
 351        horz_nr_pixels = (image_win->width * bpp) - 1;
 352        vpfe_reg_write(ccdc, (horz_start << VPFE_HORZ_INFO_SPH_SHIFT) |
 353                                horz_nr_pixels, VPFE_HORZ_INFO);
 354
 355        vert_start = image_win->top;
 356
 357        if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
 358                vert_nr_lines = (image_win->height >> 1) - 1;
 359                vert_start >>= 1;
 360                /* Since first line doesn't have any data */
 361                vert_start += 1;
 362                /* configure VDINT0 */
 363                val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
 364        } else {
 365                /* Since first line doesn't have any data */
 366                vert_start += 1;
 367                vert_nr_lines = image_win->height - 1;
 368                /*
 369                 * configure VDINT0 and VDINT1. VDINT1 will be at half
 370                 * of image height
 371                 */
 372                mid_img = vert_start + (image_win->height / 2);
 373                val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
 374                                (mid_img & VPFE_VDINT_VDINT1_MASK);
 375        }
 376
 377        vpfe_reg_write(ccdc, val, VPFE_VDINT);
 378
 379        vpfe_reg_write(ccdc, (vert_start << VPFE_VERT_START_SLV0_SHIFT) |
 380                                vert_start, VPFE_VERT_START);
 381        vpfe_reg_write(ccdc, vert_nr_lines, VPFE_VERT_LINES);
 382}
 383
 384static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
 385{
 386        struct vpfe_device *vpfe = to_vpfe(ccdc);
 387
 388        vpfe_dbg(3, vpfe, "ALAW: 0x%x\n", vpfe_reg_read(ccdc, VPFE_ALAW));
 389        vpfe_dbg(3, vpfe, "CLAMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_CLAMP));
 390        vpfe_dbg(3, vpfe, "DCSUB: 0x%x\n", vpfe_reg_read(ccdc, VPFE_DCSUB));
 391        vpfe_dbg(3, vpfe, "BLKCMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_BLKCMP));
 392        vpfe_dbg(3, vpfe, "COLPTN: 0x%x\n", vpfe_reg_read(ccdc, VPFE_COLPTN));
 393        vpfe_dbg(3, vpfe, "SDOFST: 0x%x\n", vpfe_reg_read(ccdc, VPFE_SDOFST));
 394        vpfe_dbg(3, vpfe, "SYN_MODE: 0x%x\n",
 395                 vpfe_reg_read(ccdc, VPFE_SYNMODE));
 396        vpfe_dbg(3, vpfe, "HSIZE_OFF: 0x%x\n",
 397                 vpfe_reg_read(ccdc, VPFE_HSIZE_OFF));
 398        vpfe_dbg(3, vpfe, "HORZ_INFO: 0x%x\n",
 399                 vpfe_reg_read(ccdc, VPFE_HORZ_INFO));
 400        vpfe_dbg(3, vpfe, "VERT_START: 0x%x\n",
 401                 vpfe_reg_read(ccdc, VPFE_VERT_START));
 402        vpfe_dbg(3, vpfe, "VERT_LINES: 0x%x\n",
 403                 vpfe_reg_read(ccdc, VPFE_VERT_LINES));
 404}
 405
 406static int
 407vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
 408                         struct vpfe_ccdc_config_params_raw *ccdcparam)
 409{
 410        struct vpfe_device *vpfe = to_vpfe(ccdc);
 411        u8 max_gamma, max_data;
 412
 413        if (!ccdcparam->alaw.enable)
 414                return 0;
 415
 416        max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
 417        max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
 418
 419        if (ccdcparam->alaw.gamma_wd > VPFE_CCDC_GAMMA_BITS_09_0 ||
 420            ccdcparam->alaw.gamma_wd < VPFE_CCDC_GAMMA_BITS_15_6 ||
 421            max_gamma > max_data) {
 422                vpfe_dbg(1, vpfe, "Invalid data line select\n");
 423                return -EINVAL;
 424        }
 425
 426        return 0;
 427}
 428
 429static void
 430vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
 431                            struct vpfe_ccdc_config_params_raw *raw_params)
 432{
 433        struct vpfe_ccdc_config_params_raw *config_params =
 434                                &ccdc->ccdc_cfg.bayer.config_params;
 435
 436        *config_params = *raw_params;
 437}
 438
 439/*
 440 * vpfe_ccdc_restore_defaults()
 441 * This function will write defaults to all CCDC registers
 442 */
 443static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
 444{
 445        int i;
 446
 447        /* Disable CCDC */
 448        vpfe_pcr_enable(ccdc, 0);
 449
 450        /* set all registers to default value */
 451        for (i = 4; i <= 0x94; i += 4)
 452                vpfe_reg_write(ccdc, 0,  i);
 453
 454        vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
 455        vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
 456}
 457
 458static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
 459{
 460        int dma_cntl, i, pcr;
 461
 462        /* If the CCDC module is still busy wait for it to be done */
 463        for (i = 0; i < 10; i++) {
 464                usleep_range(5000, 6000);
 465                pcr = vpfe_reg_read(ccdc, VPFE_PCR);
 466                if (!pcr)
 467                        break;
 468
 469                /* make sure it it is disabled */
 470                vpfe_pcr_enable(ccdc, 0);
 471        }
 472
 473        /* Disable CCDC by resetting all register to default POR values */
 474        vpfe_ccdc_restore_defaults(ccdc);
 475
 476        /* if DMA_CNTL overflow bit is set. Clear it
 477         *  It appears to take a while for this to become quiescent ~20ms
 478         */
 479        for (i = 0; i < 10; i++) {
 480                dma_cntl = vpfe_reg_read(ccdc, VPFE_DMA_CNTL);
 481                if (!(dma_cntl & VPFE_DMA_CNTL_OVERFLOW))
 482                        break;
 483
 484                /* Clear the overflow bit */
 485                vpfe_reg_write(ccdc, dma_cntl, VPFE_DMA_CNTL);
 486                usleep_range(5000, 6000);
 487        }
 488
 489        /* Disabled the module at the CONFIG level */
 490        vpfe_config_enable(ccdc, 0);
 491
 492        pm_runtime_put_sync(dev);
 493
 494        return 0;
 495}
 496
 497static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
 498{
 499        struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
 500        struct vpfe_ccdc_config_params_raw raw_params;
 501        int x;
 502
 503        if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
 504                return -EINVAL;
 505
 506        x = copy_from_user(&raw_params, params, sizeof(raw_params));
 507        if (x) {
 508                vpfe_dbg(1, vpfe,
 509                        "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
 510                        x);
 511                return -EFAULT;
 512        }
 513
 514        if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
 515                vpfe_ccdc_update_raw_params(ccdc, &raw_params);
 516                return 0;
 517        }
 518
 519        return -EINVAL;
 520}
 521
 522/*
 523 * vpfe_ccdc_config_ycbcr()
 524 * This function will configure CCDC for YCbCr video capture
 525 */
 526static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
 527{
 528        struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
 529        struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
 530        u32 syn_mode;
 531
 532        vpfe_dbg(3, vpfe, "vpfe_ccdc_config_ycbcr:\n");
 533        /*
 534         * first restore the CCDC registers to default values
 535         * This is important since we assume default values to be set in
 536         * a lot of registers that we didn't touch
 537         */
 538        vpfe_ccdc_restore_defaults(ccdc);
 539
 540        /*
 541         * configure pixel format, frame format, configure video frame
 542         * format, enable output to SDRAM, enable internal timing generator
 543         * and 8bit pack mode
 544         */
 545        syn_mode = (((params->pix_fmt & VPFE_SYN_MODE_INPMOD_MASK) <<
 546                    VPFE_SYN_MODE_INPMOD_SHIFT) |
 547                    ((params->frm_fmt & VPFE_SYN_FLDMODE_MASK) <<
 548                    VPFE_SYN_FLDMODE_SHIFT) | VPFE_VDHDEN_ENABLE |
 549                    VPFE_WEN_ENABLE | VPFE_DATA_PACK_ENABLE);
 550
 551        /* setup BT.656 sync mode */
 552        if (params->bt656_enable) {
 553                vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
 554
 555                /*
 556                 * configure the FID, VD, HD pin polarity,
 557                 * fld,hd pol positive, vd negative, 8-bit data
 558                 */
 559                syn_mode |= VPFE_SYN_MODE_VD_POL_NEGATIVE;
 560                if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
 561                        syn_mode |= VPFE_SYN_MODE_10BITS;
 562                else
 563                        syn_mode |= VPFE_SYN_MODE_8BITS;
 564        } else {
 565                /* y/c external sync mode */
 566                syn_mode |= (((params->fid_pol & VPFE_FID_POL_MASK) <<
 567                             VPFE_FID_POL_SHIFT) |
 568                             ((params->hd_pol & VPFE_HD_POL_MASK) <<
 569                             VPFE_HD_POL_SHIFT) |
 570                             ((params->vd_pol & VPFE_VD_POL_MASK) <<
 571                             VPFE_VD_POL_SHIFT));
 572        }
 573        vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
 574
 575        /* configure video window */
 576        vpfe_ccdc_setwin(ccdc, &params->win,
 577                         params->frm_fmt, params->bytesperpixel);
 578
 579        /*
 580         * configure the order of y cb cr in SDRAM, and disable latch
 581         * internal register on vsync
 582         */
 583        if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
 584                vpfe_reg_write(ccdc,
 585                               (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
 586                               VPFE_LATCH_ON_VSYNC_DISABLE |
 587                               VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
 588        else
 589                vpfe_reg_write(ccdc,
 590                               (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
 591                               VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
 592
 593        /*
 594         * configure the horizontal line offset. This should be a
 595         * on 32 byte boundary. So clear LSB 5 bits
 596         */
 597        vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
 598
 599        /* configure the memory line offset */
 600        if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
 601                /* two fields are interleaved in memory */
 602                vpfe_reg_write(ccdc, VPFE_SDOFST_FIELD_INTERLEAVED,
 603                               VPFE_SDOFST);
 604}
 605
 606static void
 607vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
 608                             struct vpfe_ccdc_black_clamp *bclamp)
 609{
 610        u32 val;
 611
 612        if (!bclamp->enable) {
 613                /* configure DCSub */
 614                val = (bclamp->dc_sub) & VPFE_BLK_DC_SUB_MASK;
 615                vpfe_reg_write(ccdc, val, VPFE_DCSUB);
 616                vpfe_reg_write(ccdc, VPFE_CLAMP_DEFAULT_VAL, VPFE_CLAMP);
 617                return;
 618        }
 619        /*
 620         * Configure gain,  Start pixel, No of line to be avg,
 621         * No of pixel/line to be avg, & Enable the Black clamping
 622         */
 623        val = ((bclamp->sgain & VPFE_BLK_SGAIN_MASK) |
 624               ((bclamp->start_pixel & VPFE_BLK_ST_PXL_MASK) <<
 625                VPFE_BLK_ST_PXL_SHIFT) |
 626               ((bclamp->sample_ln & VPFE_BLK_SAMPLE_LINE_MASK) <<
 627                VPFE_BLK_SAMPLE_LINE_SHIFT) |
 628               ((bclamp->sample_pixel & VPFE_BLK_SAMPLE_LN_MASK) <<
 629                VPFE_BLK_SAMPLE_LN_SHIFT) | VPFE_BLK_CLAMP_ENABLE);
 630        vpfe_reg_write(ccdc, val, VPFE_CLAMP);
 631        /* If Black clamping is enable then make dcsub 0 */
 632        vpfe_reg_write(ccdc, VPFE_DCSUB_DEFAULT_VAL, VPFE_DCSUB);
 633}
 634
 635static void
 636vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
 637                                struct vpfe_ccdc_black_compensation *bcomp)
 638{
 639        u32 val;
 640
 641        val = ((bcomp->b & VPFE_BLK_COMP_MASK) |
 642              ((bcomp->gb & VPFE_BLK_COMP_MASK) <<
 643               VPFE_BLK_COMP_GB_COMP_SHIFT) |
 644              ((bcomp->gr & VPFE_BLK_COMP_MASK) <<
 645               VPFE_BLK_COMP_GR_COMP_SHIFT) |
 646              ((bcomp->r & VPFE_BLK_COMP_MASK) <<
 647               VPFE_BLK_COMP_R_COMP_SHIFT));
 648        vpfe_reg_write(ccdc, val, VPFE_BLKCMP);
 649}
 650
 651/*
 652 * vpfe_ccdc_config_raw()
 653 * This function will configure CCDC for Raw capture mode
 654 */
 655static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
 656{
 657        struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
 658        struct vpfe_ccdc_config_params_raw *config_params =
 659                                &ccdc->ccdc_cfg.bayer.config_params;
 660        struct ccdc_params_raw *params = &ccdc->ccdc_cfg.bayer;
 661        unsigned int syn_mode;
 662        unsigned int val;
 663
 664        vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
 665
 666        /* Reset CCDC */
 667        vpfe_ccdc_restore_defaults(ccdc);
 668
 669        /* Disable latching function registers on VSYNC  */
 670        vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
 671
 672        /*
 673         * Configure the vertical sync polarity(SYN_MODE.VDPOL),
 674         * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
 675         * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
 676         * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
 677         * SDRAM, enable internal timing generator
 678         */
 679        syn_mode = (((params->vd_pol & VPFE_VD_POL_MASK) << VPFE_VD_POL_SHIFT) |
 680                   ((params->hd_pol & VPFE_HD_POL_MASK) << VPFE_HD_POL_SHIFT) |
 681                   ((params->fid_pol & VPFE_FID_POL_MASK) <<
 682                   VPFE_FID_POL_SHIFT) | ((params->frm_fmt &
 683                   VPFE_FRM_FMT_MASK) << VPFE_FRM_FMT_SHIFT) |
 684                   ((config_params->data_sz & VPFE_DATA_SZ_MASK) <<
 685                   VPFE_DATA_SZ_SHIFT) | ((params->pix_fmt &
 686                   VPFE_PIX_FMT_MASK) << VPFE_PIX_FMT_SHIFT) |
 687                   VPFE_WEN_ENABLE | VPFE_VDHDEN_ENABLE);
 688
 689        /* Enable and configure aLaw register if needed */
 690        if (config_params->alaw.enable) {
 691                val = ((config_params->alaw.gamma_wd &
 692                      VPFE_ALAW_GAMMA_WD_MASK) | VPFE_ALAW_ENABLE);
 693                vpfe_reg_write(ccdc, val, VPFE_ALAW);
 694                vpfe_dbg(3, vpfe, "\nWriting 0x%x to ALAW...\n", val);
 695        }
 696
 697        /* Configure video window */
 698        vpfe_ccdc_setwin(ccdc, &params->win, params->frm_fmt,
 699                         params->bytesperpixel);
 700
 701        /* Configure Black Clamp */
 702        vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
 703
 704        /* Configure Black level compensation */
 705        vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
 706
 707        /* If data size is 8 bit then pack the data */
 708        if ((config_params->data_sz == VPFE_CCDC_DATA_8BITS) ||
 709            config_params->alaw.enable)
 710                syn_mode |= VPFE_DATA_PACK_ENABLE;
 711
 712        /*
 713         * Configure Horizontal offset register. If pack 8 is enabled then
 714         * 1 pixel will take 1 byte
 715         */
 716        vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
 717
 718        vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
 719                params->bytesperline, params->bytesperline);
 720
 721        /* Set value for SDOFST */
 722        if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
 723                if (params->image_invert_enable) {
 724                        /* For interlace inverse mode */
 725                        vpfe_reg_write(ccdc, VPFE_INTERLACED_IMAGE_INVERT,
 726                                   VPFE_SDOFST);
 727                } else {
 728                        /* For interlace non inverse mode */
 729                        vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
 730                                   VPFE_SDOFST);
 731                }
 732        } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
 733                vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
 734                           VPFE_SDOFST);
 735        }
 736
 737        vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
 738
 739        vpfe_reg_dump(ccdc);
 740}
 741
 742static inline int
 743vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
 744                      enum ccdc_buftype buf_type)
 745{
 746        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 747                ccdc->ccdc_cfg.bayer.buf_type = buf_type;
 748        else
 749                ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
 750
 751        return 0;
 752}
 753
 754static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
 755{
 756        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 757                return ccdc->ccdc_cfg.bayer.buf_type;
 758
 759        return ccdc->ccdc_cfg.ycbcr.buf_type;
 760}
 761
 762static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
 763{
 764        struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
 765
 766        vpfe_dbg(1, vpfe, "vpfe_ccdc_set_pixel_format: if_type: %d, pixfmt:%s\n",
 767                 ccdc->ccdc_cfg.if_type, print_fourcc(pixfmt));
 768
 769        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
 770                ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
 771                /*
 772                 * Need to clear it in case it was left on
 773                 * after the last capture.
 774                 */
 775                ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
 776
 777                switch (pixfmt) {
 778                case V4L2_PIX_FMT_SBGGR8:
 779                        ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
 780                        break;
 781
 782                case V4L2_PIX_FMT_YUYV:
 783                case V4L2_PIX_FMT_UYVY:
 784                case V4L2_PIX_FMT_YUV420:
 785                case V4L2_PIX_FMT_NV12:
 786                case V4L2_PIX_FMT_RGB565X:
 787                        break;
 788
 789                case V4L2_PIX_FMT_SBGGR16:
 790                default:
 791                        return -EINVAL;
 792                }
 793        } else {
 794                switch (pixfmt) {
 795                case V4L2_PIX_FMT_YUYV:
 796                        ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
 797                        break;
 798
 799                case V4L2_PIX_FMT_UYVY:
 800                        ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
 801                        break;
 802
 803                default:
 804                        return -EINVAL;
 805                }
 806        }
 807
 808        return 0;
 809}
 810
 811static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
 812{
 813        u32 pixfmt;
 814
 815        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
 816                pixfmt = V4L2_PIX_FMT_YUYV;
 817        } else {
 818                if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
 819                        pixfmt = V4L2_PIX_FMT_YUYV;
 820                else
 821                        pixfmt = V4L2_PIX_FMT_UYVY;
 822        }
 823
 824        return pixfmt;
 825}
 826
 827static int
 828vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
 829                           struct v4l2_rect *win, unsigned int bpp)
 830{
 831        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
 832                ccdc->ccdc_cfg.bayer.win = *win;
 833                ccdc->ccdc_cfg.bayer.bytesperpixel = bpp;
 834                ccdc->ccdc_cfg.bayer.bytesperline = ALIGN(win->width * bpp, 32);
 835        } else {
 836                ccdc->ccdc_cfg.ycbcr.win = *win;
 837                ccdc->ccdc_cfg.ycbcr.bytesperpixel = bpp;
 838                ccdc->ccdc_cfg.ycbcr.bytesperline = ALIGN(win->width * bpp, 32);
 839        }
 840
 841        return 0;
 842}
 843
 844static inline void
 845vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
 846                           struct v4l2_rect *win)
 847{
 848        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 849                *win = ccdc->ccdc_cfg.bayer.win;
 850        else
 851                *win = ccdc->ccdc_cfg.ycbcr.win;
 852}
 853
 854static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
 855{
 856        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 857                return ccdc->ccdc_cfg.bayer.bytesperline;
 858
 859        return ccdc->ccdc_cfg.ycbcr.bytesperline;
 860}
 861
 862static inline int
 863vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
 864                           enum ccdc_frmfmt frm_fmt)
 865{
 866        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 867                ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
 868        else
 869                ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
 870
 871        return 0;
 872}
 873
 874static inline enum ccdc_frmfmt
 875vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
 876{
 877        if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
 878                return ccdc->ccdc_cfg.bayer.frm_fmt;
 879
 880        return ccdc->ccdc_cfg.ycbcr.frm_fmt;
 881}
 882
 883static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
 884{
 885        return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
 886}
 887
 888static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
 889{
 890        vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
 891}
 892
 893static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
 894                                      struct vpfe_hw_if_param *params)
 895{
 896        struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
 897
 898        ccdc->ccdc_cfg.if_type = params->if_type;
 899
 900        switch (params->if_type) {
 901        case VPFE_BT656:
 902        case VPFE_YCBCR_SYNC_16:
 903        case VPFE_YCBCR_SYNC_8:
 904        case VPFE_BT656_10BIT:
 905                ccdc->ccdc_cfg.ycbcr.vd_pol = params->vdpol;
 906                ccdc->ccdc_cfg.ycbcr.hd_pol = params->hdpol;
 907                break;
 908
 909        case VPFE_RAW_BAYER:
 910                ccdc->ccdc_cfg.bayer.vd_pol = params->vdpol;
 911                ccdc->ccdc_cfg.bayer.hd_pol = params->hdpol;
 912                if (params->bus_width == 10)
 913                        ccdc->ccdc_cfg.bayer.config_params.data_sz =
 914                                VPFE_CCDC_DATA_10BITS;
 915                else
 916                        ccdc->ccdc_cfg.bayer.config_params.data_sz =
 917                                VPFE_CCDC_DATA_8BITS;
 918                vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
 919                        params->bus_width);
 920                vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
 921                        ccdc->ccdc_cfg.bayer.config_params.data_sz);
 922                break;
 923
 924        default:
 925                return -EINVAL;
 926        }
 927
 928        return 0;
 929}
 930
 931static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
 932{
 933        unsigned int vpfe_int_status;
 934
 935        vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
 936
 937        switch (vdint) {
 938        /* VD0 interrupt */
 939        case VPFE_VDINT0:
 940                vpfe_int_status &= ~VPFE_VDINT0;
 941                vpfe_int_status |= VPFE_VDINT0;
 942                break;
 943
 944        /* VD1 interrupt */
 945        case VPFE_VDINT1:
 946                vpfe_int_status &= ~VPFE_VDINT1;
 947                vpfe_int_status |= VPFE_VDINT1;
 948                break;
 949
 950        /* VD2 interrupt */
 951        case VPFE_VDINT2:
 952                vpfe_int_status &= ~VPFE_VDINT2;
 953                vpfe_int_status |= VPFE_VDINT2;
 954                break;
 955
 956        /* Clear all interrupts */
 957        default:
 958                vpfe_int_status &= ~(VPFE_VDINT0 |
 959                                VPFE_VDINT1 |
 960                                VPFE_VDINT2);
 961                vpfe_int_status |= (VPFE_VDINT0 |
 962                                VPFE_VDINT1 |
 963                                VPFE_VDINT2);
 964                break;
 965        }
 966        /* Clear specific VDINT from the status register */
 967        vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
 968
 969        vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
 970
 971        /* Acknowledge that we are done with all interrupts */
 972        vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
 973}
 974
 975static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
 976{
 977        ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
 978
 979        ccdc->ccdc_cfg.ycbcr.pix_fmt = CCDC_PIXFMT_YCBCR_8BIT;
 980        ccdc->ccdc_cfg.ycbcr.frm_fmt = CCDC_FRMFMT_INTERLACED;
 981        ccdc->ccdc_cfg.ycbcr.fid_pol = VPFE_PINPOL_POSITIVE;
 982        ccdc->ccdc_cfg.ycbcr.vd_pol = VPFE_PINPOL_POSITIVE;
 983        ccdc->ccdc_cfg.ycbcr.hd_pol = VPFE_PINPOL_POSITIVE;
 984        ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
 985        ccdc->ccdc_cfg.ycbcr.buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED;
 986
 987        ccdc->ccdc_cfg.ycbcr.win.left = 0;
 988        ccdc->ccdc_cfg.ycbcr.win.top = 0;
 989        ccdc->ccdc_cfg.ycbcr.win.width = 720;
 990        ccdc->ccdc_cfg.ycbcr.win.height = 576;
 991        ccdc->ccdc_cfg.ycbcr.bt656_enable = 1;
 992
 993        ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
 994        ccdc->ccdc_cfg.bayer.frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
 995        ccdc->ccdc_cfg.bayer.fid_pol = VPFE_PINPOL_POSITIVE;
 996        ccdc->ccdc_cfg.bayer.vd_pol = VPFE_PINPOL_POSITIVE;
 997        ccdc->ccdc_cfg.bayer.hd_pol = VPFE_PINPOL_POSITIVE;
 998
 999        ccdc->ccdc_cfg.bayer.win.left = 0;
1000        ccdc->ccdc_cfg.bayer.win.top = 0;
1001        ccdc->ccdc_cfg.bayer.win.width = 800;
1002        ccdc->ccdc_cfg.bayer.win.height = 600;
1003        ccdc->ccdc_cfg.bayer.config_params.data_sz = VPFE_CCDC_DATA_8BITS;
1004        ccdc->ccdc_cfg.bayer.config_params.alaw.gamma_wd =
1005                                                VPFE_CCDC_GAMMA_BITS_09_0;
1006}
1007
1008/*
1009 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
1010 */
1011static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
1012                                      struct v4l2_format *f)
1013{
1014        struct v4l2_rect image_win;
1015        enum ccdc_buftype buf_type;
1016        enum ccdc_frmfmt frm_fmt;
1017
1018        memset(f, 0, sizeof(*f));
1019        f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1020        vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1021        f->fmt.pix.width = image_win.width;
1022        f->fmt.pix.height = image_win.height;
1023        f->fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
1024        f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
1025                                f->fmt.pix.height;
1026        buf_type = vpfe_ccdc_get_buftype(&vpfe->ccdc);
1027        f->fmt.pix.pixelformat = vpfe_ccdc_get_pixel_format(&vpfe->ccdc);
1028        frm_fmt = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1029
1030        if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
1031                f->fmt.pix.field = V4L2_FIELD_NONE;
1032        } else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
1033                if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
1034                        f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1035                 } else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED) {
1036                        f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
1037                } else {
1038                        vpfe_err(vpfe, "Invalid buf_type\n");
1039                        return -EINVAL;
1040                }
1041        } else {
1042                vpfe_err(vpfe, "Invalid frm_fmt\n");
1043                return -EINVAL;
1044        }
1045        return 0;
1046}
1047
1048static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1049{
1050        enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1051        int ret = 0;
1052
1053        vpfe_dbg(2, vpfe, "vpfe_config_ccdc_image_format\n");
1054
1055        vpfe_dbg(1, vpfe, "pixelformat: %s\n",
1056                print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
1057
1058        if (vpfe_ccdc_set_pixel_format(&vpfe->ccdc,
1059                        vpfe->fmt.fmt.pix.pixelformat) < 0) {
1060                vpfe_err(vpfe, "couldn't set pix format in ccdc\n");
1061                return -EINVAL;
1062        }
1063
1064        /* configure the image window */
1065        vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, vpfe->bpp);
1066
1067        switch (vpfe->fmt.fmt.pix.field) {
1068        case V4L2_FIELD_INTERLACED:
1069                /* do nothing, since it is default */
1070                ret = vpfe_ccdc_set_buftype(
1071                                &vpfe->ccdc,
1072                                CCDC_BUFTYPE_FLD_INTERLEAVED);
1073                break;
1074
1075        case V4L2_FIELD_NONE:
1076                frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1077                /* buffer type only applicable for interlaced scan */
1078                break;
1079
1080        case V4L2_FIELD_SEQ_TB:
1081                ret = vpfe_ccdc_set_buftype(
1082                                &vpfe->ccdc,
1083                                CCDC_BUFTYPE_FLD_SEPARATED);
1084                break;
1085
1086        default:
1087                return -EINVAL;
1088        }
1089
1090        if (ret)
1091                return ret;
1092
1093        return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
1094}
1095
1096/*
1097 * vpfe_config_image_format()
1098 * For a given standard, this functions sets up the default
1099 * pix format & crop values in the vpfe device and ccdc.  It first
1100 * starts with defaults based values from the standard table.
1101 * It then checks if sub device supports get_fmt and then override the
1102 * values based on that.Sets crop values to match with scan resolution
1103 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
1104 * values in ccdc
1105 */
1106static int vpfe_config_image_format(struct vpfe_device *vpfe,
1107                                    v4l2_std_id std_id)
1108{
1109        struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
1110        int i, ret;
1111
1112        for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
1113                if (vpfe_standards[i].std_id & std_id) {
1114                        vpfe->std_info.active_pixels =
1115                                        vpfe_standards[i].width;
1116                        vpfe->std_info.active_lines =
1117                                        vpfe_standards[i].height;
1118                        vpfe->std_info.frame_format =
1119                                        vpfe_standards[i].frame_format;
1120                        vpfe->std_index = i;
1121
1122                        break;
1123                }
1124        }
1125
1126        if (i ==  ARRAY_SIZE(vpfe_standards)) {
1127                vpfe_err(vpfe, "standard not supported\n");
1128                return -EINVAL;
1129        }
1130
1131        vpfe->crop.top = vpfe->crop.left = 0;
1132        vpfe->crop.width = vpfe->std_info.active_pixels;
1133        vpfe->crop.height = vpfe->std_info.active_lines;
1134        pix->width = vpfe->crop.width;
1135        pix->height = vpfe->crop.height;
1136        pix->pixelformat = V4L2_PIX_FMT_YUYV;
1137
1138        /* first field and frame format based on standard frame format */
1139        if (vpfe->std_info.frame_format)
1140                pix->field = V4L2_FIELD_INTERLACED;
1141        else
1142                pix->field = V4L2_FIELD_NONE;
1143
1144        ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1145        if (ret)
1146                return ret;
1147
1148        /* Update the crop window based on found values */
1149        vpfe->crop.width = pix->width;
1150        vpfe->crop.height = pix->height;
1151
1152        return vpfe_config_ccdc_image_format(vpfe);
1153}
1154
1155static int vpfe_initialize_device(struct vpfe_device *vpfe)
1156{
1157        struct vpfe_subdev_info *sdinfo;
1158        int ret;
1159
1160        sdinfo = &vpfe->cfg->sub_devs[0];
1161        sdinfo->sd = vpfe->sd[0];
1162        vpfe->current_input = 0;
1163        vpfe->std_index = 0;
1164        /* Configure the default format information */
1165        ret = vpfe_config_image_format(vpfe,
1166                                       vpfe_standards[vpfe->std_index].std_id);
1167        if (ret)
1168                return ret;
1169
1170        pm_runtime_get_sync(vpfe->pdev);
1171
1172        vpfe_config_enable(&vpfe->ccdc, 1);
1173
1174        vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1175
1176        /* Clear all VPFE interrupts */
1177        vpfe_clear_intr(&vpfe->ccdc, -1);
1178
1179        return ret;
1180}
1181
1182/*
1183 * vpfe_release : This function is based on the vb2_fop_release
1184 * helper function.
1185 * It has been augmented to handle module power management,
1186 * by disabling/enabling h/w module fcntl clock when necessary.
1187 */
1188static int vpfe_release(struct file *file)
1189{
1190        struct vpfe_device *vpfe = video_drvdata(file);
1191        bool fh_singular;
1192        int ret;
1193
1194        mutex_lock(&vpfe->lock);
1195
1196        /* Save the singular status before we call the clean-up helper */
1197        fh_singular = v4l2_fh_is_singular_file(file);
1198
1199        /* the release helper will cleanup any on-going streaming */
1200        ret = _vb2_fop_release(file, NULL);
1201
1202        /*
1203         * If this was the last open file.
1204         * Then de-initialize hw module.
1205         */
1206        if (fh_singular)
1207                vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1208
1209        mutex_unlock(&vpfe->lock);
1210
1211        return ret;
1212}
1213
1214/*
1215 * vpfe_open : This function is based on the v4l2_fh_open helper function.
1216 * It has been augmented to handle module power management,
1217 * by disabling/enabling h/w module fcntl clock when necessary.
1218 */
1219static int vpfe_open(struct file *file)
1220{
1221        struct vpfe_device *vpfe = video_drvdata(file);
1222        int ret;
1223
1224        mutex_lock(&vpfe->lock);
1225
1226        ret = v4l2_fh_open(file);
1227        if (ret) {
1228                vpfe_err(vpfe, "v4l2_fh_open failed\n");
1229                goto unlock;
1230        }
1231
1232        if (!v4l2_fh_is_singular_file(file))
1233                goto unlock;
1234
1235        if (vpfe_initialize_device(vpfe)) {
1236                v4l2_fh_release(file);
1237                ret = -ENODEV;
1238        }
1239
1240unlock:
1241        mutex_unlock(&vpfe->lock);
1242        return ret;
1243}
1244
1245/**
1246 * vpfe_schedule_next_buffer: set next buffer address for capture
1247 * @vpfe : ptr to vpfe device
1248 *
1249 * This function will get next buffer from the dma queue and
1250 * set the buffer address in the vpfe register for capture.
1251 * the buffer is marked active
1252 *
1253 * Assumes caller is holding vpfe->dma_queue_lock already
1254 */
1255static inline void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1256{
1257        vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1258                                    struct vpfe_cap_buffer, list);
1259        list_del(&vpfe->next_frm->list);
1260
1261        vpfe_set_sdr_addr(&vpfe->ccdc,
1262               vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0));
1263}
1264
1265static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1266{
1267        unsigned long addr;
1268
1269        addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1270                                        vpfe->field_off;
1271
1272        vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1273}
1274
1275/*
1276 * vpfe_process_buffer_complete: process a completed buffer
1277 * @vpfe : ptr to vpfe device
1278 *
1279 * This function time stamp the buffer and mark it as DONE. It also
1280 * wake up any process waiting on the QUEUE and set the next buffer
1281 * as current
1282 */
1283static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1284{
1285        vpfe->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1286        vpfe->cur_frm->vb.field = vpfe->fmt.fmt.pix.field;
1287        vpfe->cur_frm->vb.sequence = vpfe->sequence++;
1288        vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1289        vpfe->cur_frm = vpfe->next_frm;
1290}
1291
1292/*
1293 * vpfe_isr : ISR handler for vpfe capture (VINT0)
1294 * @irq: irq number
1295 * @dev_id: dev_id ptr
1296 *
1297 * It changes status of the captured buffer, takes next buffer from the queue
1298 * and sets its address in VPFE registers
1299 */
1300static irqreturn_t vpfe_isr(int irq, void *dev)
1301{
1302        struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1303        enum v4l2_field field;
1304        int intr_status;
1305        int fid;
1306
1307        intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1308
1309        if (intr_status & VPFE_VDINT0) {
1310                field = vpfe->fmt.fmt.pix.field;
1311
1312                if (field == V4L2_FIELD_NONE) {
1313                        /* handle progressive frame capture */
1314                        if (vpfe->cur_frm != vpfe->next_frm)
1315                                vpfe_process_buffer_complete(vpfe);
1316                        goto next_intr;
1317                }
1318
1319                /* interlaced or TB capture check which field
1320                   we are in hardware */
1321                fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1322
1323                /* switch the software maintained field id */
1324                vpfe->field ^= 1;
1325                if (fid == vpfe->field) {
1326                        /* we are in-sync here,continue */
1327                        if (fid == 0) {
1328                                /*
1329                                 * One frame is just being captured. If the
1330                                 * next frame is available, release the
1331                                 * current frame and move on
1332                                 */
1333                                if (vpfe->cur_frm != vpfe->next_frm)
1334                                        vpfe_process_buffer_complete(vpfe);
1335                                /*
1336                                 * based on whether the two fields are stored
1337                                 * interleave or separately in memory,
1338                                 * reconfigure the CCDC memory address
1339                                 */
1340                                if (field == V4L2_FIELD_SEQ_TB)
1341                                        vpfe_schedule_bottom_field(vpfe);
1342
1343                                goto next_intr;
1344                        }
1345                        /*
1346                         * if one field is just being captured configure
1347                         * the next frame get the next frame from the empty
1348                         * queue if no frame is available hold on to the
1349                         * current buffer
1350                         */
1351                        spin_lock(&vpfe->dma_queue_lock);
1352                        if (!list_empty(&vpfe->dma_queue) &&
1353                            vpfe->cur_frm == vpfe->next_frm)
1354                                vpfe_schedule_next_buffer(vpfe);
1355                        spin_unlock(&vpfe->dma_queue_lock);
1356                } else if (fid == 0) {
1357                        /*
1358                         * out of sync. Recover from any hardware out-of-sync.
1359                         * May loose one frame
1360                         */
1361                        vpfe->field = fid;
1362                }
1363        }
1364
1365next_intr:
1366        if (intr_status & VPFE_VDINT1) {
1367                spin_lock(&vpfe->dma_queue_lock);
1368                if (vpfe->fmt.fmt.pix.field == V4L2_FIELD_NONE &&
1369                    !list_empty(&vpfe->dma_queue) &&
1370                    vpfe->cur_frm == vpfe->next_frm)
1371                        vpfe_schedule_next_buffer(vpfe);
1372                spin_unlock(&vpfe->dma_queue_lock);
1373        }
1374
1375        vpfe_clear_intr(&vpfe->ccdc, intr_status);
1376
1377        return IRQ_HANDLED;
1378}
1379
1380static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1381{
1382        unsigned int intr = VPFE_VDINT0;
1383        enum ccdc_frmfmt frame_format;
1384
1385        frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1386        if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1387                intr |= VPFE_VDINT1;
1388
1389        vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1390}
1391
1392static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1393{
1394        unsigned int intr = VPFE_VDINT0;
1395        enum ccdc_frmfmt frame_format;
1396
1397        frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1398        if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1399                intr |= VPFE_VDINT1;
1400
1401        vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1402}
1403
1404static int vpfe_querycap(struct file *file, void  *priv,
1405                         struct v4l2_capability *cap)
1406{
1407        struct vpfe_device *vpfe = video_drvdata(file);
1408
1409        vpfe_dbg(2, vpfe, "vpfe_querycap\n");
1410
1411        strscpy(cap->driver, VPFE_MODULE_NAME, sizeof(cap->driver));
1412        strscpy(cap->card, "TI AM437x VPFE", sizeof(cap->card));
1413        snprintf(cap->bus_info, sizeof(cap->bus_info),
1414                        "platform:%s", vpfe->v4l2_dev.name);
1415        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1416                            V4L2_CAP_READWRITE;
1417        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1418
1419        return 0;
1420}
1421
1422/* get the format set at output pad of the adjacent subdev */
1423static int __vpfe_get_format(struct vpfe_device *vpfe,
1424                             struct v4l2_format *format, unsigned int *bpp)
1425{
1426        struct v4l2_mbus_framefmt mbus_fmt;
1427        struct vpfe_subdev_info *sdinfo;
1428        struct v4l2_subdev_format fmt;
1429        int ret;
1430
1431        sdinfo = vpfe->current_subdev;
1432        if (!sdinfo->sd)
1433                return -EINVAL;
1434
1435        fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1436        fmt.pad = 0;
1437
1438        ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1439        if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1440                return ret;
1441
1442        if (!ret) {
1443                v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1444                mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1445        } else {
1446                ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1447                                                 sdinfo->grp_id,
1448                                                 pad, get_fmt,
1449                                                 NULL, &fmt);
1450                if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1451                        return ret;
1452                v4l2_fill_pix_format(&format->fmt.pix, &mbus_fmt);
1453                mbus_to_pix(vpfe, &mbus_fmt, &format->fmt.pix, bpp);
1454        }
1455
1456        format->type = vpfe->fmt.type;
1457
1458        vpfe_dbg(1, vpfe,
1459                 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1460                 __func__, format->fmt.pix.width, format->fmt.pix.height,
1461                 print_fourcc(format->fmt.pix.pixelformat),
1462                 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1463
1464        return 0;
1465}
1466
1467/* set the format at output pad of the adjacent subdev */
1468static int __vpfe_set_format(struct vpfe_device *vpfe,
1469                             struct v4l2_format *format, unsigned int *bpp)
1470{
1471        struct vpfe_subdev_info *sdinfo;
1472        struct v4l2_subdev_format fmt;
1473        int ret;
1474
1475        vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1476
1477        sdinfo = vpfe->current_subdev;
1478        if (!sdinfo->sd)
1479                return -EINVAL;
1480
1481        fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1482        fmt.pad = 0;
1483
1484        pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1485
1486        ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1487        if (ret)
1488                return ret;
1489
1490        v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1491        mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1492
1493        format->type = vpfe->fmt.type;
1494
1495        vpfe_dbg(1, vpfe,
1496                 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1497                 __func__,  format->fmt.pix.width, format->fmt.pix.height,
1498                 print_fourcc(format->fmt.pix.pixelformat),
1499                 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1500
1501        return 0;
1502}
1503
1504static int vpfe_g_fmt(struct file *file, void *priv,
1505                      struct v4l2_format *fmt)
1506{
1507        struct vpfe_device *vpfe = video_drvdata(file);
1508
1509        vpfe_dbg(2, vpfe, "vpfe_g_fmt\n");
1510
1511        *fmt = vpfe->fmt;
1512
1513        return 0;
1514}
1515
1516static int vpfe_enum_fmt(struct file *file, void  *priv,
1517                         struct v4l2_fmtdesc *f)
1518{
1519        struct vpfe_device *vpfe = video_drvdata(file);
1520        struct vpfe_subdev_info *sdinfo;
1521        struct vpfe_fmt *fmt = NULL;
1522        unsigned int k;
1523
1524        vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1525                f->index);
1526
1527        sdinfo = vpfe->current_subdev;
1528        if (!sdinfo->sd)
1529                return -EINVAL;
1530
1531        if (f->index > ARRAY_SIZE(formats))
1532                return -EINVAL;
1533
1534        for (k = 0; k < ARRAY_SIZE(formats); k++) {
1535                if (formats[k].index == f->index) {
1536                        fmt = &formats[k];
1537                        break;
1538                }
1539        }
1540        if (!fmt)
1541                return -EINVAL;
1542
1543        strscpy(f->description, fmt->name, sizeof(f->description));
1544        f->pixelformat = fmt->fourcc;
1545        f->type = vpfe->fmt.type;
1546
1547        vpfe_dbg(1, vpfe, "vpfe_enum_format: mbus index: %d code: %x pixelformat: %s [%s]\n",
1548                f->index, fmt->code, print_fourcc(fmt->fourcc), fmt->name);
1549
1550        return 0;
1551}
1552
1553static int vpfe_try_fmt(struct file *file, void *priv,
1554                        struct v4l2_format *fmt)
1555{
1556        struct vpfe_device *vpfe = video_drvdata(file);
1557        unsigned int bpp;
1558
1559        vpfe_dbg(2, vpfe, "vpfe_try_fmt\n");
1560
1561        return __vpfe_get_format(vpfe, fmt, &bpp);
1562}
1563
1564static int vpfe_s_fmt(struct file *file, void *priv,
1565                      struct v4l2_format *fmt)
1566{
1567        struct vpfe_device *vpfe = video_drvdata(file);
1568        struct v4l2_format format;
1569        unsigned int bpp;
1570        int ret;
1571
1572        vpfe_dbg(2, vpfe, "vpfe_s_fmt\n");
1573
1574        /* If streaming is started, return error */
1575        if (vb2_is_busy(&vpfe->buffer_queue)) {
1576                vpfe_err(vpfe, "%s device busy\n", __func__);
1577                return -EBUSY;
1578        }
1579
1580        ret = __vpfe_get_format(vpfe, &format, &bpp);
1581        if (ret)
1582                return ret;
1583
1584
1585        if (!cmp_v4l2_format(fmt, &format)) {
1586                /* Sensor format is different from the requested format
1587                 * so we need to change it
1588                 */
1589                ret = __vpfe_set_format(vpfe, fmt, &bpp);
1590                if (ret)
1591                        return ret;
1592        } else /* Just make sure all of the fields are consistent */
1593                *fmt = format;
1594
1595        /* First detach any IRQ if currently attached */
1596        vpfe_detach_irq(vpfe);
1597        vpfe->fmt = *fmt;
1598        vpfe->bpp = bpp;
1599
1600        /* Update the crop window based on found values */
1601        vpfe->crop.width = fmt->fmt.pix.width;
1602        vpfe->crop.height = fmt->fmt.pix.height;
1603
1604        /* set image capture parameters in the ccdc */
1605        return vpfe_config_ccdc_image_format(vpfe);
1606}
1607
1608static int vpfe_enum_size(struct file *file, void  *priv,
1609                          struct v4l2_frmsizeenum *fsize)
1610{
1611        struct vpfe_device *vpfe = video_drvdata(file);
1612        struct v4l2_subdev_frame_size_enum fse;
1613        struct vpfe_subdev_info *sdinfo;
1614        struct v4l2_mbus_framefmt mbus;
1615        struct v4l2_pix_format pix;
1616        struct vpfe_fmt *fmt;
1617        int ret;
1618
1619        vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1620
1621        /* check for valid format */
1622        fmt = find_format_by_pix(fsize->pixel_format);
1623        if (!fmt) {
1624                vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1625                        fsize->pixel_format);
1626                return -EINVAL;
1627        }
1628
1629        memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1630
1631        sdinfo = vpfe->current_subdev;
1632        if (!sdinfo->sd)
1633                return -EINVAL;
1634
1635        memset(&pix, 0x0, sizeof(pix));
1636        /* Construct pix from parameter and use default for the rest */
1637        pix.pixelformat = fsize->pixel_format;
1638        pix.width = 640;
1639        pix.height = 480;
1640        pix.colorspace = V4L2_COLORSPACE_SRGB;
1641        pix.field = V4L2_FIELD_NONE;
1642        pix_to_mbus(vpfe, &pix, &mbus);
1643
1644        memset(&fse, 0x0, sizeof(fse));
1645        fse.index = fsize->index;
1646        fse.pad = 0;
1647        fse.code = mbus.code;
1648        fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1649        ret = v4l2_subdev_call(sdinfo->sd, pad, enum_frame_size, NULL, &fse);
1650        if (ret)
1651                return -EINVAL;
1652
1653        vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1654                fse.index, fse.code, fse.min_width, fse.max_width,
1655                fse.min_height, fse.max_height);
1656
1657        fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1658        fsize->discrete.width = fse.max_width;
1659        fsize->discrete.height = fse.max_height;
1660
1661        vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d pixformat: %s size: %dx%d\n",
1662                fsize->index, print_fourcc(fsize->pixel_format),
1663                fsize->discrete.width, fsize->discrete.height);
1664
1665        return 0;
1666}
1667
1668/*
1669 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1670 * given app input index
1671 */
1672static int
1673vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1674                            int *subdev_index,
1675                            int *subdev_input_index,
1676                            int app_input_index)
1677{
1678        int i, j = 0;
1679
1680        for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1681                if (app_input_index < (j + 1)) {
1682                        *subdev_index = i;
1683                        *subdev_input_index = app_input_index - j;
1684                        return 0;
1685                }
1686                j++;
1687        }
1688        return -EINVAL;
1689}
1690
1691/*
1692 * vpfe_get_app_input - Get app input index for a given subdev input index
1693 * driver stores the input index of the current sub device and translate it
1694 * when application request the current input
1695 */
1696static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1697                                    int *app_input_index)
1698{
1699        struct vpfe_config *cfg = vpfe->cfg;
1700        struct vpfe_subdev_info *sdinfo;
1701        struct i2c_client *client;
1702        struct i2c_client *curr_client;
1703        int i, j = 0;
1704
1705        curr_client = v4l2_get_subdevdata(vpfe->current_subdev->sd);
1706        for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1707                sdinfo = &cfg->sub_devs[i];
1708                client = v4l2_get_subdevdata(sdinfo->sd);
1709                if (client->addr == curr_client->addr &&
1710                    client->adapter->nr == curr_client->adapter->nr) {
1711                        if (vpfe->current_input >= 1)
1712                                return -1;
1713                        *app_input_index = j + vpfe->current_input;
1714                        return 0;
1715                }
1716                j++;
1717        }
1718        return -EINVAL;
1719}
1720
1721static int vpfe_enum_input(struct file *file, void *priv,
1722                           struct v4l2_input *inp)
1723{
1724        struct vpfe_device *vpfe = video_drvdata(file);
1725        struct vpfe_subdev_info *sdinfo;
1726        int subdev, index;
1727
1728        vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1729
1730        if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1731                                        inp->index) < 0) {
1732                vpfe_dbg(1, vpfe,
1733                        "input information not found for the subdev\n");
1734                return -EINVAL;
1735        }
1736        sdinfo = &vpfe->cfg->sub_devs[subdev];
1737        *inp = sdinfo->inputs[index];
1738
1739        return 0;
1740}
1741
1742static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1743{
1744        struct vpfe_device *vpfe = video_drvdata(file);
1745
1746        vpfe_dbg(2, vpfe, "vpfe_g_input\n");
1747
1748        return vpfe_get_app_input_index(vpfe, index);
1749}
1750
1751/* Assumes caller is holding vpfe_dev->lock */
1752static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1753{
1754        int subdev_index = 0, inp_index = 0;
1755        struct vpfe_subdev_info *sdinfo;
1756        struct vpfe_route *route;
1757        u32 input, output;
1758        int ret;
1759
1760        vpfe_dbg(2, vpfe, "vpfe_set_input: index: %d\n", index);
1761
1762        /* If streaming is started, return error */
1763        if (vb2_is_busy(&vpfe->buffer_queue)) {
1764                vpfe_err(vpfe, "%s device busy\n", __func__);
1765                return -EBUSY;
1766        }
1767        ret = vpfe_get_subdev_input_index(vpfe,
1768                                          &subdev_index,
1769                                          &inp_index,
1770                                          index);
1771        if (ret < 0) {
1772                vpfe_err(vpfe, "invalid input index: %d\n", index);
1773                goto get_out;
1774        }
1775
1776        sdinfo = &vpfe->cfg->sub_devs[subdev_index];
1777        sdinfo->sd = vpfe->sd[subdev_index];
1778        route = &sdinfo->routes[inp_index];
1779        if (route && sdinfo->can_route) {
1780                input = route->input;
1781                output = route->output;
1782                if (sdinfo->sd) {
1783                        ret = v4l2_subdev_call(sdinfo->sd, video,
1784                                        s_routing, input, output, 0);
1785                        if (ret) {
1786                                vpfe_err(vpfe, "s_routing failed\n");
1787                                ret = -EINVAL;
1788                                goto get_out;
1789                        }
1790                }
1791
1792        }
1793
1794        vpfe->current_subdev = sdinfo;
1795        if (sdinfo->sd)
1796                vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1797        vpfe->current_input = index;
1798        vpfe->std_index = 0;
1799
1800        /* set the bus/interface parameter for the sub device in ccdc */
1801        ret = vpfe_ccdc_set_hw_if_params(&vpfe->ccdc, &sdinfo->vpfe_param);
1802        if (ret)
1803                return ret;
1804
1805        /* set the default image parameters in the device */
1806        return vpfe_config_image_format(vpfe,
1807                                        vpfe_standards[vpfe->std_index].std_id);
1808
1809get_out:
1810        return ret;
1811}
1812
1813static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1814{
1815        struct vpfe_device *vpfe = video_drvdata(file);
1816
1817        vpfe_dbg(2, vpfe,
1818                "vpfe_s_input: index: %d\n", index);
1819
1820        return vpfe_set_input(vpfe, index);
1821}
1822
1823static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1824{
1825        struct vpfe_device *vpfe = video_drvdata(file);
1826        struct vpfe_subdev_info *sdinfo;
1827
1828        vpfe_dbg(2, vpfe, "vpfe_querystd\n");
1829
1830        sdinfo = vpfe->current_subdev;
1831        if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1832                return -ENODATA;
1833
1834        /* Call querystd function of decoder device */
1835        return v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1836                                         video, querystd, std_id);
1837}
1838
1839static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1840{
1841        struct vpfe_device *vpfe = video_drvdata(file);
1842        struct vpfe_subdev_info *sdinfo;
1843        int ret;
1844
1845        vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1846
1847        sdinfo = vpfe->current_subdev;
1848        if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1849                return -ENODATA;
1850
1851        /* If streaming is started, return error */
1852        if (vb2_is_busy(&vpfe->buffer_queue)) {
1853                vpfe_err(vpfe, "%s device busy\n", __func__);
1854                ret = -EBUSY;
1855                return ret;
1856        }
1857
1858        ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1859                                         video, s_std, std_id);
1860        if (ret < 0) {
1861                vpfe_err(vpfe, "Failed to set standard\n");
1862                return ret;
1863        }
1864        ret = vpfe_config_image_format(vpfe, std_id);
1865
1866        return ret;
1867}
1868
1869static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1870{
1871        struct vpfe_device *vpfe = video_drvdata(file);
1872        struct vpfe_subdev_info *sdinfo;
1873
1874        vpfe_dbg(2, vpfe, "vpfe_g_std\n");
1875
1876        sdinfo = vpfe->current_subdev;
1877        if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1878                return -ENODATA;
1879
1880        *std_id = vpfe_standards[vpfe->std_index].std_id;
1881
1882        return 0;
1883}
1884
1885/*
1886 * vpfe_calculate_offsets : This function calculates buffers offset
1887 * for top and bottom field
1888 */
1889static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1890{
1891        struct v4l2_rect image_win;
1892
1893        vpfe_dbg(2, vpfe, "vpfe_calculate_offsets\n");
1894
1895        vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1896        vpfe->field_off = image_win.height * image_win.width;
1897}
1898
1899/*
1900 * vpfe_queue_setup - Callback function for buffer setup.
1901 * @vq: vb2_queue ptr
1902 * @nbuffers: ptr to number of buffers requested by application
1903 * @nplanes:: contains number of distinct video planes needed to hold a frame
1904 * @sizes[]: contains the size (in bytes) of each plane.
1905 * @alloc_devs: ptr to allocation context
1906 *
1907 * This callback function is called when reqbuf() is called to adjust
1908 * the buffer count and buffer size
1909 */
1910static int vpfe_queue_setup(struct vb2_queue *vq,
1911                            unsigned int *nbuffers, unsigned int *nplanes,
1912                            unsigned int sizes[], struct device *alloc_devs[])
1913{
1914        struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1915        unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1916
1917        if (vq->num_buffers + *nbuffers < 3)
1918                *nbuffers = 3 - vq->num_buffers;
1919
1920        if (*nplanes) {
1921                if (sizes[0] < size)
1922                        return -EINVAL;
1923                size = sizes[0];
1924        }
1925
1926        *nplanes = 1;
1927        sizes[0] = size;
1928
1929        vpfe_dbg(1, vpfe,
1930                "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1931
1932        /* Calculate field offset */
1933        vpfe_calculate_offsets(vpfe);
1934
1935        return 0;
1936}
1937
1938/*
1939 * vpfe_buffer_prepare :  callback function for buffer prepare
1940 * @vb: ptr to vb2_buffer
1941 *
1942 * This is the callback function for buffer prepare when vb2_qbuf()
1943 * function is called. The buffer is prepared and user space virtual address
1944 * or user address is converted into  physical address
1945 */
1946static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1947{
1948        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1949        struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1950
1951        vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1952
1953        if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1954                return -EINVAL;
1955
1956        vbuf->field = vpfe->fmt.fmt.pix.field;
1957
1958        return 0;
1959}
1960
1961/*
1962 * vpfe_buffer_queue : Callback function to add buffer to DMA queue
1963 * @vb: ptr to vb2_buffer
1964 */
1965static void vpfe_buffer_queue(struct vb2_buffer *vb)
1966{
1967        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1968        struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1969        struct vpfe_cap_buffer *buf = to_vpfe_buffer(vbuf);
1970        unsigned long flags = 0;
1971
1972        /* add the buffer to the DMA queue */
1973        spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1974        list_add_tail(&buf->list, &vpfe->dma_queue);
1975        spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1976}
1977
1978/*
1979 * vpfe_start_streaming : Starts the DMA engine for streaming
1980 * @vb: ptr to vb2_buffer
1981 * @count: number of buffers
1982 */
1983static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1984{
1985        struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1986        struct vpfe_cap_buffer *buf, *tmp;
1987        struct vpfe_subdev_info *sdinfo;
1988        unsigned long flags;
1989        unsigned long addr;
1990        int ret;
1991
1992        spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1993
1994        vpfe->field = 0;
1995        vpfe->sequence = 0;
1996
1997        sdinfo = vpfe->current_subdev;
1998
1999        vpfe_attach_irq(vpfe);
2000
2001        if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
2002                vpfe_ccdc_config_raw(&vpfe->ccdc);
2003        else
2004                vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
2005
2006        /* Get the next frame from the buffer queue */
2007        vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2008                                    struct vpfe_cap_buffer, list);
2009        vpfe->cur_frm = vpfe->next_frm;
2010        /* Remove buffer from the buffer queue */
2011        list_del(&vpfe->cur_frm->list);
2012        spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2013
2014        addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
2015
2016        vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
2017
2018        vpfe_pcr_enable(&vpfe->ccdc, 1);
2019
2020        ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
2021        if (ret < 0) {
2022                vpfe_err(vpfe, "Error in attaching interrupt handle\n");
2023                goto err;
2024        }
2025
2026        return 0;
2027
2028err:
2029        list_for_each_entry_safe(buf, tmp, &vpfe->dma_queue, list) {
2030                list_del(&buf->list);
2031                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
2032        }
2033
2034        return ret;
2035}
2036
2037/*
2038 * vpfe_stop_streaming : Stop the DMA engine
2039 * @vq: ptr to vb2_queue
2040 *
2041 * This callback stops the DMA engine and any remaining buffers
2042 * in the DMA queue are released.
2043 */
2044static void vpfe_stop_streaming(struct vb2_queue *vq)
2045{
2046        struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
2047        struct vpfe_subdev_info *sdinfo;
2048        unsigned long flags;
2049        int ret;
2050
2051        vpfe_pcr_enable(&vpfe->ccdc, 0);
2052
2053        vpfe_detach_irq(vpfe);
2054
2055        sdinfo = vpfe->current_subdev;
2056        ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 0);
2057        if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
2058                vpfe_dbg(1, vpfe, "stream off failed in subdev\n");
2059
2060        /* release all active buffers */
2061        spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
2062        if (vpfe->cur_frm == vpfe->next_frm) {
2063                vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2064                                VB2_BUF_STATE_ERROR);
2065        } else {
2066                if (vpfe->cur_frm != NULL)
2067                        vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2068                                        VB2_BUF_STATE_ERROR);
2069                if (vpfe->next_frm != NULL)
2070                        vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2071                                        VB2_BUF_STATE_ERROR);
2072        }
2073
2074        while (!list_empty(&vpfe->dma_queue)) {
2075                vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2076                                                struct vpfe_cap_buffer, list);
2077                list_del(&vpfe->next_frm->list);
2078                vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2079                                VB2_BUF_STATE_ERROR);
2080        }
2081        spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2082}
2083
2084static int vpfe_g_pixelaspect(struct file *file, void *priv,
2085                              int type, struct v4l2_fract *f)
2086{
2087        struct vpfe_device *vpfe = video_drvdata(file);
2088
2089        vpfe_dbg(2, vpfe, "vpfe_g_pixelaspect\n");
2090
2091        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2092            vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2093                return -EINVAL;
2094
2095        *f = vpfe_standards[vpfe->std_index].pixelaspect;
2096
2097        return 0;
2098}
2099
2100static int
2101vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2102{
2103        struct vpfe_device *vpfe = video_drvdata(file);
2104
2105        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2106            vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2107                return -EINVAL;
2108
2109        switch (s->target) {
2110        case V4L2_SEL_TGT_CROP_BOUNDS:
2111        case V4L2_SEL_TGT_CROP_DEFAULT:
2112                s->r.left = 0;
2113                s->r.top = 0;
2114                s->r.width = vpfe_standards[vpfe->std_index].width;
2115                s->r.height = vpfe_standards[vpfe->std_index].height;
2116                break;
2117
2118        case V4L2_SEL_TGT_CROP:
2119                s->r = vpfe->crop;
2120                break;
2121
2122        default:
2123                return -EINVAL;
2124        }
2125
2126        return 0;
2127}
2128
2129static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2130{
2131        if (a->left < b->left || a->top < b->top)
2132                return 0;
2133
2134        if (a->left + a->width > b->left + b->width)
2135                return 0;
2136
2137        if (a->top + a->height > b->top + b->height)
2138                return 0;
2139
2140        return 1;
2141}
2142
2143static int
2144vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2145{
2146        struct vpfe_device *vpfe = video_drvdata(file);
2147        struct v4l2_rect cr = vpfe->crop;
2148        struct v4l2_rect r = s->r;
2149
2150        /* If streaming is started, return error */
2151        if (vb2_is_busy(&vpfe->buffer_queue)) {
2152                vpfe_err(vpfe, "%s device busy\n", __func__);
2153                return -EBUSY;
2154        }
2155
2156        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2157                        s->target != V4L2_SEL_TGT_CROP)
2158                return -EINVAL;
2159
2160        v4l_bound_align_image(&r.width, 0, cr.width, 0,
2161                              &r.height, 0, cr.height, 0, 0);
2162
2163        r.left = clamp_t(unsigned int, r.left, 0, cr.width - r.width);
2164        r.top  = clamp_t(unsigned int, r.top, 0, cr.height - r.height);
2165
2166        if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2167                return -ERANGE;
2168
2169        if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2170                return -ERANGE;
2171
2172        s->r = vpfe->crop = r;
2173
2174        vpfe_ccdc_set_image_window(&vpfe->ccdc, &r, vpfe->bpp);
2175        vpfe->fmt.fmt.pix.width = r.width;
2176        vpfe->fmt.fmt.pix.height = r.height;
2177        vpfe->fmt.fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
2178        vpfe->fmt.fmt.pix.sizeimage = vpfe->fmt.fmt.pix.bytesperline *
2179                                                vpfe->fmt.fmt.pix.height;
2180
2181        vpfe_dbg(1, vpfe, "cropped (%d,%d)/%dx%d of %dx%d\n",
2182                 r.left, r.top, r.width, r.height, cr.width, cr.height);
2183
2184        return 0;
2185}
2186
2187static long vpfe_ioctl_default(struct file *file, void *priv,
2188                               bool valid_prio, unsigned int cmd, void *param)
2189{
2190        struct vpfe_device *vpfe = video_drvdata(file);
2191        int ret;
2192
2193        vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2194
2195        if (!valid_prio) {
2196                vpfe_err(vpfe, "%s device busy\n", __func__);
2197                return -EBUSY;
2198        }
2199
2200        /* If streaming is started, return error */
2201        if (vb2_is_busy(&vpfe->buffer_queue)) {
2202                vpfe_err(vpfe, "%s device busy\n", __func__);
2203                return -EBUSY;
2204        }
2205
2206        switch (cmd) {
2207        case VIDIOC_AM437X_CCDC_CFG:
2208                ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2209                if (ret) {
2210                        vpfe_dbg(2, vpfe,
2211                                "Error setting parameters in CCDC\n");
2212                        return ret;
2213                }
2214                ret = vpfe_get_ccdc_image_format(vpfe,
2215                                                 &vpfe->fmt);
2216                if (ret < 0) {
2217                        vpfe_dbg(2, vpfe,
2218                                "Invalid image format at CCDC\n");
2219                        return ret;
2220                }
2221                break;
2222
2223        default:
2224                ret = -ENOTTY;
2225                break;
2226        }
2227
2228        return ret;
2229}
2230
2231static const struct vb2_ops vpfe_video_qops = {
2232        .wait_prepare           = vb2_ops_wait_prepare,
2233        .wait_finish            = vb2_ops_wait_finish,
2234        .queue_setup            = vpfe_queue_setup,
2235        .buf_prepare            = vpfe_buffer_prepare,
2236        .buf_queue              = vpfe_buffer_queue,
2237        .start_streaming        = vpfe_start_streaming,
2238        .stop_streaming         = vpfe_stop_streaming,
2239};
2240
2241/* vpfe capture driver file operations */
2242static const struct v4l2_file_operations vpfe_fops = {
2243        .owner          = THIS_MODULE,
2244        .open           = vpfe_open,
2245        .release        = vpfe_release,
2246        .read           = vb2_fop_read,
2247        .poll           = vb2_fop_poll,
2248        .unlocked_ioctl = video_ioctl2,
2249        .mmap           = vb2_fop_mmap,
2250};
2251
2252/* vpfe capture ioctl operations */
2253static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
2254        .vidioc_querycap                = vpfe_querycap,
2255        .vidioc_enum_fmt_vid_cap        = vpfe_enum_fmt,
2256        .vidioc_g_fmt_vid_cap           = vpfe_g_fmt,
2257        .vidioc_s_fmt_vid_cap           = vpfe_s_fmt,
2258        .vidioc_try_fmt_vid_cap         = vpfe_try_fmt,
2259
2260        .vidioc_enum_framesizes         = vpfe_enum_size,
2261
2262        .vidioc_enum_input              = vpfe_enum_input,
2263        .vidioc_g_input                 = vpfe_g_input,
2264        .vidioc_s_input                 = vpfe_s_input,
2265
2266        .vidioc_querystd                = vpfe_querystd,
2267        .vidioc_s_std                   = vpfe_s_std,
2268        .vidioc_g_std                   = vpfe_g_std,
2269
2270        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
2271        .vidioc_create_bufs             = vb2_ioctl_create_bufs,
2272        .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
2273        .vidioc_querybuf                = vb2_ioctl_querybuf,
2274        .vidioc_qbuf                    = vb2_ioctl_qbuf,
2275        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
2276        .vidioc_expbuf                  = vb2_ioctl_expbuf,
2277        .vidioc_streamon                = vb2_ioctl_streamon,
2278        .vidioc_streamoff               = vb2_ioctl_streamoff,
2279
2280        .vidioc_log_status              = v4l2_ctrl_log_status,
2281        .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
2282        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
2283
2284        .vidioc_g_pixelaspect           = vpfe_g_pixelaspect,
2285        .vidioc_g_selection             = vpfe_g_selection,
2286        .vidioc_s_selection             = vpfe_s_selection,
2287
2288        .vidioc_default                 = vpfe_ioctl_default,
2289};
2290
2291static int
2292vpfe_async_bound(struct v4l2_async_notifier *notifier,
2293                 struct v4l2_subdev *subdev,
2294                 struct v4l2_async_subdev *asd)
2295{
2296        struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2297                                               struct vpfe_device, v4l2_dev);
2298        struct v4l2_subdev_mbus_code_enum mbus_code;
2299        struct vpfe_subdev_info *sdinfo;
2300        bool found = false;
2301        int i, j;
2302
2303        vpfe_dbg(1, vpfe, "vpfe_async_bound\n");
2304
2305        for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2306                if (vpfe->cfg->asd[i]->match.fwnode ==
2307                    asd[i].match.fwnode) {
2308                        sdinfo = &vpfe->cfg->sub_devs[i];
2309                        vpfe->sd[i] = subdev;
2310                        vpfe->sd[i]->grp_id = sdinfo->grp_id;
2311                        found = true;
2312                        break;
2313                }
2314        }
2315
2316        if (!found) {
2317                vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2318                return -EINVAL;
2319        }
2320
2321        vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2322
2323        /* setup the supported formats & indexes */
2324        for (j = 0, i = 0; ; ++j) {
2325                struct vpfe_fmt *fmt;
2326                int ret;
2327
2328                memset(&mbus_code, 0, sizeof(mbus_code));
2329                mbus_code.index = j;
2330                mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2331                ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2332                               NULL, &mbus_code);
2333                if (ret)
2334                        break;
2335
2336                fmt = find_format_by_code(mbus_code.code);
2337                if (!fmt)
2338                        continue;
2339
2340                fmt->supported = true;
2341                fmt->index = i++;
2342        }
2343
2344        return 0;
2345}
2346
2347static int vpfe_probe_complete(struct vpfe_device *vpfe)
2348{
2349        struct video_device *vdev;
2350        struct vb2_queue *q;
2351        int err;
2352
2353        spin_lock_init(&vpfe->dma_queue_lock);
2354        mutex_init(&vpfe->lock);
2355
2356        vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2357
2358        /* set first sub device as current one */
2359        vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2360        vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2361
2362        err = vpfe_set_input(vpfe, 0);
2363        if (err)
2364                goto probe_out;
2365
2366        /* Initialize videobuf2 queue as per the buffer type */
2367        q = &vpfe->buffer_queue;
2368        q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2369        q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2370        q->drv_priv = vpfe;
2371        q->ops = &vpfe_video_qops;
2372        q->mem_ops = &vb2_dma_contig_memops;
2373        q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2374        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2375        q->lock = &vpfe->lock;
2376        q->min_buffers_needed = 1;
2377        q->dev = vpfe->pdev;
2378
2379        err = vb2_queue_init(q);
2380        if (err) {
2381                vpfe_err(vpfe, "vb2_queue_init() failed\n");
2382                goto probe_out;
2383        }
2384
2385        INIT_LIST_HEAD(&vpfe->dma_queue);
2386
2387        vdev = &vpfe->video_dev;
2388        strscpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2389        vdev->release = video_device_release_empty;
2390        vdev->fops = &vpfe_fops;
2391        vdev->ioctl_ops = &vpfe_ioctl_ops;
2392        vdev->v4l2_dev = &vpfe->v4l2_dev;
2393        vdev->vfl_dir = VFL_DIR_RX;
2394        vdev->queue = q;
2395        vdev->lock = &vpfe->lock;
2396        video_set_drvdata(vdev, vpfe);
2397        err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2398        if (err) {
2399                vpfe_err(vpfe,
2400                        "Unable to register video device.\n");
2401                goto probe_out;
2402        }
2403
2404        return 0;
2405
2406probe_out:
2407        v4l2_device_unregister(&vpfe->v4l2_dev);
2408        return err;
2409}
2410
2411static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2412{
2413        struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2414                                        struct vpfe_device, v4l2_dev);
2415
2416        return vpfe_probe_complete(vpfe);
2417}
2418
2419static const struct v4l2_async_notifier_operations vpfe_async_ops = {
2420        .bound = vpfe_async_bound,
2421        .complete = vpfe_async_complete,
2422};
2423
2424static struct vpfe_config *
2425vpfe_get_pdata(struct vpfe_device *vpfe)
2426{
2427        struct device_node *endpoint = NULL;
2428        struct device *dev = vpfe->pdev;
2429        struct vpfe_subdev_info *sdinfo;
2430        struct vpfe_config *pdata;
2431        unsigned int flags;
2432        unsigned int i;
2433        int err;
2434
2435        dev_dbg(dev, "vpfe_get_pdata\n");
2436
2437        v4l2_async_notifier_init(&vpfe->notifier);
2438
2439        if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
2440                return dev->platform_data;
2441
2442        pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2443        if (!pdata)
2444                return NULL;
2445
2446        for (i = 0; ; i++) {
2447                struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2448                struct device_node *rem;
2449
2450                endpoint = of_graph_get_next_endpoint(dev->of_node, endpoint);
2451                if (!endpoint)
2452                        break;
2453
2454                sdinfo = &pdata->sub_devs[i];
2455                sdinfo->grp_id = 0;
2456
2457                /* we only support camera */
2458                sdinfo->inputs[0].index = i;
2459                strscpy(sdinfo->inputs[0].name, "Camera",
2460                        sizeof(sdinfo->inputs[0].name));
2461                sdinfo->inputs[0].type = V4L2_INPUT_TYPE_CAMERA;
2462                sdinfo->inputs[0].std = V4L2_STD_ALL;
2463                sdinfo->inputs[0].capabilities = V4L2_IN_CAP_STD;
2464
2465                sdinfo->can_route = 0;
2466                sdinfo->routes = NULL;
2467
2468                of_property_read_u32(endpoint, "ti,am437x-vpfe-interface",
2469                                     &sdinfo->vpfe_param.if_type);
2470                if (sdinfo->vpfe_param.if_type < 0 ||
2471                        sdinfo->vpfe_param.if_type > 4) {
2472                        sdinfo->vpfe_param.if_type = VPFE_RAW_BAYER;
2473                }
2474
2475                err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
2476                                                 &bus_cfg);
2477                if (err) {
2478                        dev_err(dev, "Could not parse the endpoint\n");
2479                        goto cleanup;
2480                }
2481
2482                sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2483
2484                if (sdinfo->vpfe_param.bus_width < 8 ||
2485                        sdinfo->vpfe_param.bus_width > 16) {
2486                        dev_err(dev, "Invalid bus width.\n");
2487                        goto cleanup;
2488                }
2489
2490                flags = bus_cfg.bus.parallel.flags;
2491
2492                if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2493                        sdinfo->vpfe_param.hdpol = 1;
2494
2495                if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2496                        sdinfo->vpfe_param.vdpol = 1;
2497
2498                rem = of_graph_get_remote_port_parent(endpoint);
2499                if (!rem) {
2500                        dev_err(dev, "Remote device at %pOF not found\n",
2501                                endpoint);
2502                        goto cleanup;
2503                }
2504
2505                pdata->asd[i] = v4l2_async_notifier_add_fwnode_subdev(
2506                        &vpfe->notifier, of_fwnode_handle(rem),
2507                        sizeof(struct v4l2_async_subdev));
2508                if (IS_ERR(pdata->asd[i])) {
2509                        of_node_put(rem);
2510                        goto cleanup;
2511                }
2512        }
2513
2514        of_node_put(endpoint);
2515        return pdata;
2516
2517cleanup:
2518        v4l2_async_notifier_cleanup(&vpfe->notifier);
2519        of_node_put(endpoint);
2520        return NULL;
2521}
2522
2523/*
2524 * vpfe_probe : This function creates device entries by register
2525 * itself to the V4L2 driver and initializes fields of each
2526 * device objects
2527 */
2528static int vpfe_probe(struct platform_device *pdev)
2529{
2530        struct vpfe_config *vpfe_cfg;
2531        struct vpfe_device *vpfe;
2532        struct vpfe_ccdc *ccdc;
2533        struct resource *res;
2534        int ret;
2535
2536        vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2537        if (!vpfe)
2538                return -ENOMEM;
2539
2540        vpfe->pdev = &pdev->dev;
2541
2542        vpfe_cfg = vpfe_get_pdata(vpfe);
2543        if (!vpfe_cfg) {
2544                dev_err(&pdev->dev, "No platform data\n");
2545                return -EINVAL;
2546        }
2547
2548        vpfe->cfg = vpfe_cfg;
2549        ccdc = &vpfe->ccdc;
2550
2551        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2552        ccdc->ccdc_cfg.base_addr = devm_ioremap_resource(&pdev->dev, res);
2553        if (IS_ERR(ccdc->ccdc_cfg.base_addr)) {
2554                ret = PTR_ERR(ccdc->ccdc_cfg.base_addr);
2555                goto probe_out_cleanup;
2556        }
2557
2558        ret = platform_get_irq(pdev, 0);
2559        if (ret <= 0) {
2560                dev_err(&pdev->dev, "No IRQ resource\n");
2561                ret = -ENODEV;
2562                goto probe_out_cleanup;
2563        }
2564        vpfe->irq = ret;
2565
2566        ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2567                               "vpfe_capture0", vpfe);
2568        if (ret) {
2569                dev_err(&pdev->dev, "Unable to request interrupt\n");
2570                ret = -EINVAL;
2571                goto probe_out_cleanup;
2572        }
2573
2574        ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2575        if (ret) {
2576                vpfe_err(vpfe,
2577                        "Unable to register v4l2 device.\n");
2578                goto probe_out_cleanup;
2579        }
2580
2581        /* set the driver data in platform device */
2582        platform_set_drvdata(pdev, vpfe);
2583        /* Enabling module functional clock */
2584        pm_runtime_enable(&pdev->dev);
2585
2586        /* for now just enable it here instead of waiting for the open */
2587        pm_runtime_get_sync(&pdev->dev);
2588
2589        vpfe_ccdc_config_defaults(ccdc);
2590
2591        pm_runtime_put_sync(&pdev->dev);
2592
2593        vpfe->sd = devm_kcalloc(&pdev->dev,
2594                                ARRAY_SIZE(vpfe->cfg->asd),
2595                                sizeof(struct v4l2_subdev *),
2596                                GFP_KERNEL);
2597        if (!vpfe->sd) {
2598                ret = -ENOMEM;
2599                goto probe_out_v4l2_unregister;
2600        }
2601
2602        vpfe->notifier.ops = &vpfe_async_ops;
2603        ret = v4l2_async_notifier_register(&vpfe->v4l2_dev, &vpfe->notifier);
2604        if (ret) {
2605                vpfe_err(vpfe, "Error registering async notifier\n");
2606                ret = -EINVAL;
2607                goto probe_out_v4l2_unregister;
2608        }
2609
2610        return 0;
2611
2612probe_out_v4l2_unregister:
2613        v4l2_device_unregister(&vpfe->v4l2_dev);
2614probe_out_cleanup:
2615        v4l2_async_notifier_cleanup(&vpfe->notifier);
2616        return ret;
2617}
2618
2619/*
2620 * vpfe_remove : It un-register device from V4L2 driver
2621 */
2622static int vpfe_remove(struct platform_device *pdev)
2623{
2624        struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2625
2626        vpfe_dbg(2, vpfe, "vpfe_remove\n");
2627
2628        pm_runtime_disable(&pdev->dev);
2629
2630        v4l2_async_notifier_unregister(&vpfe->notifier);
2631        v4l2_async_notifier_cleanup(&vpfe->notifier);
2632        v4l2_device_unregister(&vpfe->v4l2_dev);
2633        video_unregister_device(&vpfe->video_dev);
2634
2635        return 0;
2636}
2637
2638#ifdef CONFIG_PM_SLEEP
2639
2640static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2641{
2642        ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2643        ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2644        ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2645        ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2646        ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2647        ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2648        ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2649        ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2650        ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2651        ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2652        ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2653        ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2654        ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2655        ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2656                                                            VPFE_HD_VD_WID);
2657        ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2658                                                            VPFE_PIX_LINES);
2659        ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2660                                                            VPFE_HORZ_INFO);
2661        ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2662                                                             VPFE_VERT_START);
2663        ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2664                                                             VPFE_VERT_LINES);
2665        ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2666                                                            VPFE_HSIZE_OFF);
2667}
2668
2669static int vpfe_suspend(struct device *dev)
2670{
2671        struct vpfe_device *vpfe = dev_get_drvdata(dev);
2672        struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2673
2674        /* if streaming has not started we don't care */
2675        if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2676                return 0;
2677
2678        pm_runtime_get_sync(dev);
2679        vpfe_config_enable(ccdc, 1);
2680
2681        /* Save VPFE context */
2682        vpfe_save_context(ccdc);
2683
2684        /* Disable CCDC */
2685        vpfe_pcr_enable(ccdc, 0);
2686        vpfe_config_enable(ccdc, 0);
2687
2688        /* Disable both master and slave clock */
2689        pm_runtime_put_sync(dev);
2690
2691        /* Select sleep pin state */
2692        pinctrl_pm_select_sleep_state(dev);
2693
2694        return 0;
2695}
2696
2697static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2698{
2699        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2700        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2701        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2702        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2703        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2704        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2705        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2706        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2707        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2708        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2709        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2710        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2711        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2712        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2713                                                VPFE_HD_VD_WID);
2714        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2715                                                VPFE_PIX_LINES);
2716        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2717                                                VPFE_HORZ_INFO);
2718        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2719                                                VPFE_VERT_START);
2720        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2721                                                VPFE_VERT_LINES);
2722        vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2723                                                VPFE_HSIZE_OFF);
2724}
2725
2726static int vpfe_resume(struct device *dev)
2727{
2728        struct vpfe_device *vpfe = dev_get_drvdata(dev);
2729        struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2730
2731        /* if streaming has not started we don't care */
2732        if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2733                return 0;
2734
2735        /* Enable both master and slave clock */
2736        pm_runtime_get_sync(dev);
2737        vpfe_config_enable(ccdc, 1);
2738
2739        /* Restore VPFE context */
2740        vpfe_restore_context(ccdc);
2741
2742        vpfe_config_enable(ccdc, 0);
2743        pm_runtime_put_sync(dev);
2744
2745        /* Select default pin state */
2746        pinctrl_pm_select_default_state(dev);
2747
2748        return 0;
2749}
2750
2751#endif
2752
2753static SIMPLE_DEV_PM_OPS(vpfe_pm_ops, vpfe_suspend, vpfe_resume);
2754
2755static const struct of_device_id vpfe_of_match[] = {
2756        { .compatible = "ti,am437x-vpfe", },
2757        { /* sentinel */ },
2758};
2759MODULE_DEVICE_TABLE(of, vpfe_of_match);
2760
2761static struct platform_driver vpfe_driver = {
2762        .probe          = vpfe_probe,
2763        .remove         = vpfe_remove,
2764        .driver = {
2765                .name   = VPFE_MODULE_NAME,
2766                .pm     = &vpfe_pm_ops,
2767                .of_match_table = of_match_ptr(vpfe_of_match),
2768        },
2769};
2770
2771module_platform_driver(vpfe_driver);
2772
2773MODULE_AUTHOR("Texas Instruments");
2774MODULE_DESCRIPTION("TI AM437x VPFE driver");
2775MODULE_LICENSE("GPL");
2776MODULE_VERSION(VPFE_VERSION);
2777