linux/drivers/media/video/tvp514x.c
<<
>>
Prefs
   1/*
   2 * drivers/media/video/tvp514x.c
   3 *
   4 * TI TVP5146/47 decoder driver
   5 *
   6 * Copyright (C) 2008 Texas Instruments Inc
   7 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
   8 *
   9 * Contributors:
  10 *     Sivaraj R <sivaraj@ti.com>
  11 *     Brijesh R Jadav <brijesh.j@ti.com>
  12 *     Hardik Shah <hardik.shah@ti.com>
  13 *     Manjunath Hadli <mrh@ti.com>
  14 *     Karicheri Muralidharan <m-karicheri2@ti.com>
  15 *
  16 * This package is free software; you can redistribute it and/or modify
  17 * it under the terms of the GNU General Public License version 2 as
  18 * published by the Free Software Foundation.
  19 *
  20 * This program is distributed in the hope that it will be useful,
  21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23 * GNU General Public License for more details.
  24 *
  25 * You should have received a copy of the GNU General Public License
  26 * along with this program; if not, write to the Free Software
  27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28 *
  29 */
  30
  31#include <linux/i2c.h>
  32#include <linux/delay.h>
  33#include <linux/videodev2.h>
  34
  35#include <media/v4l2-device.h>
  36#include <media/v4l2-common.h>
  37#include <media/v4l2-chip-ident.h>
  38#include <media/tvp514x.h>
  39
  40#include "tvp514x_regs.h"
  41
  42/* Module Name */
  43#define TVP514X_MODULE_NAME             "tvp514x"
  44
  45/* Private macros for TVP */
  46#define I2C_RETRY_COUNT                 (5)
  47#define LOCK_RETRY_COUNT                (5)
  48#define LOCK_RETRY_DELAY                (200)
  49
  50/* Debug functions */
  51static int debug;
  52module_param(debug, bool, 0644);
  53MODULE_PARM_DESC(debug, "Debug level (0-1)");
  54
  55MODULE_AUTHOR("Texas Instruments");
  56MODULE_DESCRIPTION("TVP514X linux decoder driver");
  57MODULE_LICENSE("GPL");
  58
  59/* enum tvp514x_std - enum for supported standards */
  60enum tvp514x_std {
  61        STD_NTSC_MJ = 0,
  62        STD_PAL_BDGHIN,
  63        STD_INVALID
  64};
  65
  66/**
  67 * struct tvp514x_std_info - Structure to store standard informations
  68 * @width: Line width in pixels
  69 * @height:Number of active lines
  70 * @video_std: Value to write in REG_VIDEO_STD register
  71 * @standard: v4l2 standard structure information
  72 */
  73struct tvp514x_std_info {
  74        unsigned long width;
  75        unsigned long height;
  76        u8 video_std;
  77        struct v4l2_standard standard;
  78};
  79
  80static struct tvp514x_reg tvp514x_reg_list_default[0x40];
  81/**
  82 * struct tvp514x_decoder - TVP5146/47 decoder object
  83 * @sd: Subdevice Slave handle
  84 * @tvp514x_regs: copy of hw's regs with preset values.
  85 * @pdata: Board specific
  86 * @ver: Chip version
  87 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
  88 * @pix: Current pixel format
  89 * @num_fmts: Number of formats
  90 * @fmt_list: Format list
  91 * @current_std: Current standard
  92 * @num_stds: Number of standards
  93 * @std_list: Standards list
  94 * @input: Input routing at chip level
  95 * @output: Output routing at chip level
  96 */
  97struct tvp514x_decoder {
  98        struct v4l2_subdev sd;
  99        struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
 100        const struct tvp514x_platform_data *pdata;
 101
 102        int ver;
 103        int streaming;
 104
 105        struct v4l2_pix_format pix;
 106        int num_fmts;
 107        const struct v4l2_fmtdesc *fmt_list;
 108
 109        enum tvp514x_std current_std;
 110        int num_stds;
 111        struct tvp514x_std_info *std_list;
 112        /* Input and Output Routing parameters */
 113        u32 input;
 114        u32 output;
 115};
 116
 117/* TVP514x default register values */
 118static struct tvp514x_reg tvp514x_reg_list_default[] = {
 119        /* Composite selected */
 120        {TOK_WRITE, REG_INPUT_SEL, 0x05},
 121        {TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
 122        /* Auto mode */
 123        {TOK_WRITE, REG_VIDEO_STD, 0x00},
 124        {TOK_WRITE, REG_OPERATION_MODE, 0x00},
 125        {TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
 126        {TOK_WRITE, REG_COLOR_KILLER, 0x10},
 127        {TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
 128        {TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
 129        {TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
 130        {TOK_WRITE, REG_BRIGHTNESS, 0x80},
 131        {TOK_WRITE, REG_CONTRAST, 0x80},
 132        {TOK_WRITE, REG_SATURATION, 0x80},
 133        {TOK_WRITE, REG_HUE, 0x00},
 134        {TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
 135        {TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
 136        /* Reserved */
 137        {TOK_SKIP, 0x0F, 0x00},
 138        {TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
 139        {TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
 140        {TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
 141        /* Reserved */
 142        {TOK_SKIP, 0x13, 0x00},
 143        {TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
 144        /* Reserved */
 145        {TOK_SKIP, 0x15, 0x00},
 146        /* NTSC timing */
 147        {TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
 148        {TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
 149        {TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
 150        {TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
 151        /* NTSC timing */
 152        {TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
 153        {TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
 154        {TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
 155        {TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
 156        /* NTSC timing */
 157        {TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
 158        {TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
 159        {TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
 160        {TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
 161        /* NTSC timing */
 162        {TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
 163        {TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
 164        {TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
 165        {TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
 166        /* Reserved */
 167        {TOK_SKIP, 0x26, 0x00},
 168        /* Reserved */
 169        {TOK_SKIP, 0x27, 0x00},
 170        {TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
 171        /* Reserved */
 172        {TOK_SKIP, 0x29, 0x00},
 173        {TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
 174        /* Reserved */
 175        {TOK_SKIP, 0x2B, 0x00},
 176        {TOK_SKIP, REG_SCART_DELAY, 0x00},
 177        {TOK_SKIP, REG_CTI_DELAY, 0x00},
 178        {TOK_SKIP, REG_CTI_CONTROL, 0x00},
 179        /* Reserved */
 180        {TOK_SKIP, 0x2F, 0x00},
 181        /* Reserved */
 182        {TOK_SKIP, 0x30, 0x00},
 183        /* Reserved */
 184        {TOK_SKIP, 0x31, 0x00},
 185        /* HS, VS active high */
 186        {TOK_WRITE, REG_SYNC_CONTROL, 0x00},
 187        /* 10-bit BT.656 */
 188        {TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
 189        /* Enable clk & data */
 190        {TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
 191        /* Enable AVID & FLD */
 192        {TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
 193        /* Enable VS & HS */
 194        {TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
 195        {TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
 196        {TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
 197        /* Clear status */
 198        {TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
 199        {TOK_TERM, 0, 0},
 200};
 201
 202/**
 203 * List of image formats supported by TVP5146/47 decoder
 204 * Currently we are using 8 bit mode only, but can be
 205 * extended to 10/20 bit mode.
 206 */
 207static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
 208        {
 209         .index = 0,
 210         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
 211         .flags = 0,
 212         .description = "8-bit UYVY 4:2:2 Format",
 213         .pixelformat = V4L2_PIX_FMT_UYVY,
 214        },
 215};
 216
 217/**
 218 * Supported standards -
 219 *
 220 * Currently supports two standards only, need to add support for rest of the
 221 * modes, like SECAM, etc...
 222 */
 223static struct tvp514x_std_info tvp514x_std_list[] = {
 224        /* Standard: STD_NTSC_MJ */
 225        [STD_NTSC_MJ] = {
 226         .width = NTSC_NUM_ACTIVE_PIXELS,
 227         .height = NTSC_NUM_ACTIVE_LINES,
 228         .video_std = VIDEO_STD_NTSC_MJ_BIT,
 229         .standard = {
 230                      .index = 0,
 231                      .id = V4L2_STD_NTSC,
 232                      .name = "NTSC",
 233                      .frameperiod = {1001, 30000},
 234                      .framelines = 525
 235                     },
 236        /* Standard: STD_PAL_BDGHIN */
 237        },
 238        [STD_PAL_BDGHIN] = {
 239         .width = PAL_NUM_ACTIVE_PIXELS,
 240         .height = PAL_NUM_ACTIVE_LINES,
 241         .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
 242         .standard = {
 243                      .index = 1,
 244                      .id = V4L2_STD_PAL,
 245                      .name = "PAL",
 246                      .frameperiod = {1, 25},
 247                      .framelines = 625
 248                     },
 249        },
 250        /* Standard: need to add for additional standard */
 251};
 252
 253
 254static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
 255{
 256        return container_of(sd, struct tvp514x_decoder, sd);
 257}
 258
 259
 260/**
 261 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
 262 * @sd: ptr to v4l2_subdev struct
 263 * @reg: TVP5146/47 register address
 264 *
 265 * Returns value read if successful, or non-zero (-1) otherwise.
 266 */
 267static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
 268{
 269        int err, retry = 0;
 270        struct i2c_client *client = v4l2_get_subdevdata(sd);
 271
 272read_again:
 273
 274        err = i2c_smbus_read_byte_data(client, reg);
 275        if (err == -1) {
 276                if (retry <= I2C_RETRY_COUNT) {
 277                        v4l2_warn(sd, "Read: retry ... %d\n", retry);
 278                        retry++;
 279                        msleep_interruptible(10);
 280                        goto read_again;
 281                }
 282        }
 283
 284        return err;
 285}
 286
 287/**
 288 * dump_reg() - dump the register content of TVP5146/47.
 289 * @sd: ptr to v4l2_subdev struct
 290 * @reg: TVP5146/47 register address
 291 */
 292static void dump_reg(struct v4l2_subdev *sd, u8 reg)
 293{
 294        u32 val;
 295
 296        val = tvp514x_read_reg(sd, reg);
 297        v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
 298}
 299
 300/**
 301 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
 302 * @sd: ptr to v4l2_subdev struct
 303 * @reg: TVP5146/47 register address
 304 * @val: value to be written to the register
 305 *
 306 * Write a value to a register in an TVP5146/47 decoder device.
 307 * Returns zero if successful, or non-zero otherwise.
 308 */
 309static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
 310{
 311        int err, retry = 0;
 312        struct i2c_client *client = v4l2_get_subdevdata(sd);
 313
 314write_again:
 315
 316        err = i2c_smbus_write_byte_data(client, reg, val);
 317        if (err) {
 318                if (retry <= I2C_RETRY_COUNT) {
 319                        v4l2_warn(sd, "Write: retry ... %d\n", retry);
 320                        retry++;
 321                        msleep_interruptible(10);
 322                        goto write_again;
 323                }
 324        }
 325
 326        return err;
 327}
 328
 329/**
 330 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
 331 * @sd: ptr to v4l2_subdev struct
 332 * @reglist: list of TVP5146/47 registers and values
 333 *
 334 * Initializes a list of TVP5146/47 registers:-
 335 *              if token is TOK_TERM, then entire write operation terminates
 336 *              if token is TOK_DELAY, then a delay of 'val' msec is introduced
 337 *              if token is TOK_SKIP, then the register write is skipped
 338 *              if token is TOK_WRITE, then the register write is performed
 339 * Returns zero if successful, or non-zero otherwise.
 340 */
 341static int tvp514x_write_regs(struct v4l2_subdev *sd,
 342                              const struct tvp514x_reg reglist[])
 343{
 344        int err;
 345        const struct tvp514x_reg *next = reglist;
 346
 347        for (; next->token != TOK_TERM; next++) {
 348                if (next->token == TOK_DELAY) {
 349                        msleep(next->val);
 350                        continue;
 351                }
 352
 353                if (next->token == TOK_SKIP)
 354                        continue;
 355
 356                err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
 357                if (err) {
 358                        v4l2_err(sd, "Write failed. Err[%d]\n", err);
 359                        return err;
 360                }
 361        }
 362        return 0;
 363}
 364
 365/**
 366 * tvp514x_get_current_std() : Get the current standard detected by TVP5146/47
 367 * @sd: ptr to v4l2_subdev struct
 368 *
 369 * Get current standard detected by TVP5146/47, STD_INVALID if there is no
 370 * standard detected.
 371 */
 372static enum tvp514x_std tvp514x_get_current_std(struct v4l2_subdev *sd)
 373{
 374        u8 std, std_status;
 375
 376        std = tvp514x_read_reg(sd, REG_VIDEO_STD);
 377        if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
 378                /* use the standard status register */
 379                std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
 380        else
 381                /* use the standard register itself */
 382                std_status = std;
 383
 384        switch (std_status & VIDEO_STD_MASK) {
 385        case VIDEO_STD_NTSC_MJ_BIT:
 386                return STD_NTSC_MJ;
 387
 388        case VIDEO_STD_PAL_BDGHIN_BIT:
 389                return STD_PAL_BDGHIN;
 390
 391        default:
 392                return STD_INVALID;
 393        }
 394
 395        return STD_INVALID;
 396}
 397
 398/* TVP5146/47 register dump function */
 399static void tvp514x_reg_dump(struct v4l2_subdev *sd)
 400{
 401        dump_reg(sd, REG_INPUT_SEL);
 402        dump_reg(sd, REG_AFE_GAIN_CTRL);
 403        dump_reg(sd, REG_VIDEO_STD);
 404        dump_reg(sd, REG_OPERATION_MODE);
 405        dump_reg(sd, REG_COLOR_KILLER);
 406        dump_reg(sd, REG_LUMA_CONTROL1);
 407        dump_reg(sd, REG_LUMA_CONTROL2);
 408        dump_reg(sd, REG_LUMA_CONTROL3);
 409        dump_reg(sd, REG_BRIGHTNESS);
 410        dump_reg(sd, REG_CONTRAST);
 411        dump_reg(sd, REG_SATURATION);
 412        dump_reg(sd, REG_HUE);
 413        dump_reg(sd, REG_CHROMA_CONTROL1);
 414        dump_reg(sd, REG_CHROMA_CONTROL2);
 415        dump_reg(sd, REG_COMP_PR_SATURATION);
 416        dump_reg(sd, REG_COMP_Y_CONTRAST);
 417        dump_reg(sd, REG_COMP_PB_SATURATION);
 418        dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
 419        dump_reg(sd, REG_AVID_START_PIXEL_LSB);
 420        dump_reg(sd, REG_AVID_START_PIXEL_MSB);
 421        dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
 422        dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
 423        dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
 424        dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
 425        dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
 426        dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
 427        dump_reg(sd, REG_VSYNC_START_LINE_LSB);
 428        dump_reg(sd, REG_VSYNC_START_LINE_MSB);
 429        dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
 430        dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
 431        dump_reg(sd, REG_VBLK_START_LINE_LSB);
 432        dump_reg(sd, REG_VBLK_START_LINE_MSB);
 433        dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
 434        dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
 435        dump_reg(sd, REG_SYNC_CONTROL);
 436        dump_reg(sd, REG_OUTPUT_FORMATTER1);
 437        dump_reg(sd, REG_OUTPUT_FORMATTER2);
 438        dump_reg(sd, REG_OUTPUT_FORMATTER3);
 439        dump_reg(sd, REG_OUTPUT_FORMATTER4);
 440        dump_reg(sd, REG_OUTPUT_FORMATTER5);
 441        dump_reg(sd, REG_OUTPUT_FORMATTER6);
 442        dump_reg(sd, REG_CLEAR_LOST_LOCK);
 443}
 444
 445/**
 446 * tvp514x_configure() - Configure the TVP5146/47 registers
 447 * @sd: ptr to v4l2_subdev struct
 448 * @decoder: ptr to tvp514x_decoder structure
 449 *
 450 * Returns zero if successful, or non-zero otherwise.
 451 */
 452static int tvp514x_configure(struct v4l2_subdev *sd,
 453                struct tvp514x_decoder *decoder)
 454{
 455        int err;
 456
 457        /* common register initialization */
 458        err =
 459            tvp514x_write_regs(sd, decoder->tvp514x_regs);
 460        if (err)
 461                return err;
 462
 463        if (debug)
 464                tvp514x_reg_dump(sd);
 465
 466        return 0;
 467}
 468
 469/**
 470 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
 471 * @sd: pointer to standard V4L2 sub-device structure
 472 * @decoder: pointer to tvp514x_decoder structure
 473 *
 474 * A device is considered to be detected if the chip ID (LSB and MSB)
 475 * registers match the expected values.
 476 * Any value of the rom version register is accepted.
 477 * Returns ENODEV error number if no device is detected, or zero
 478 * if a device is detected.
 479 */
 480static int tvp514x_detect(struct v4l2_subdev *sd,
 481                struct tvp514x_decoder *decoder)
 482{
 483        u8 chip_id_msb, chip_id_lsb, rom_ver;
 484        struct i2c_client *client = v4l2_get_subdevdata(sd);
 485
 486        chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
 487        chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
 488        rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
 489
 490        v4l2_dbg(1, debug, sd,
 491                 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
 492                 chip_id_msb, chip_id_lsb, rom_ver);
 493        if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
 494                || ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
 495                && (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
 496                /* We didn't read the values we expected, so this must not be
 497                 * an TVP5146/47.
 498                 */
 499                v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
 500                                chip_id_msb, chip_id_lsb);
 501                return -ENODEV;
 502        }
 503
 504        decoder->ver = rom_ver;
 505
 506        v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
 507                        client->name, decoder->ver,
 508                        client->addr << 1, client->adapter->name);
 509        return 0;
 510}
 511
 512/**
 513 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
 514 * @sd: pointer to standard V4L2 sub-device structure
 515 * @std_id: standard V4L2 std_id ioctl enum
 516 *
 517 * Returns the current standard detected by TVP5146/47. If no active input is
 518 * detected, returns -EINVAL
 519 */
 520static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
 521{
 522        struct tvp514x_decoder *decoder = to_decoder(sd);
 523        enum tvp514x_std current_std;
 524        enum tvp514x_input input_sel;
 525        u8 sync_lock_status, lock_mask;
 526
 527        if (std_id == NULL)
 528                return -EINVAL;
 529
 530        /* get the current standard */
 531        current_std = tvp514x_get_current_std(sd);
 532        if (current_std == STD_INVALID)
 533                return -EINVAL;
 534
 535        input_sel = decoder->input;
 536
 537        switch (input_sel) {
 538        case INPUT_CVBS_VI1A:
 539        case INPUT_CVBS_VI1B:
 540        case INPUT_CVBS_VI1C:
 541        case INPUT_CVBS_VI2A:
 542        case INPUT_CVBS_VI2B:
 543        case INPUT_CVBS_VI2C:
 544        case INPUT_CVBS_VI3A:
 545        case INPUT_CVBS_VI3B:
 546        case INPUT_CVBS_VI3C:
 547        case INPUT_CVBS_VI4A:
 548                lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
 549                        STATUS_HORZ_SYNC_LOCK_BIT |
 550                        STATUS_VIRT_SYNC_LOCK_BIT;
 551                break;
 552
 553        case INPUT_SVIDEO_VI2A_VI1A:
 554        case INPUT_SVIDEO_VI2B_VI1B:
 555        case INPUT_SVIDEO_VI2C_VI1C:
 556        case INPUT_SVIDEO_VI2A_VI3A:
 557        case INPUT_SVIDEO_VI2B_VI3B:
 558        case INPUT_SVIDEO_VI2C_VI3C:
 559        case INPUT_SVIDEO_VI4A_VI1A:
 560        case INPUT_SVIDEO_VI4A_VI1B:
 561        case INPUT_SVIDEO_VI4A_VI1C:
 562        case INPUT_SVIDEO_VI4A_VI3A:
 563        case INPUT_SVIDEO_VI4A_VI3B:
 564        case INPUT_SVIDEO_VI4A_VI3C:
 565                lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
 566                        STATUS_VIRT_SYNC_LOCK_BIT;
 567                break;
 568                /*Need to add other interfaces*/
 569        default:
 570                return -EINVAL;
 571        }
 572        /* check whether signal is locked */
 573        sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
 574        if (lock_mask != (sync_lock_status & lock_mask))
 575                return -EINVAL; /* No input detected */
 576
 577        decoder->current_std = current_std;
 578        *std_id = decoder->std_list[current_std].standard.id;
 579
 580        v4l2_dbg(1, debug, sd, "Current STD: %s",
 581                        decoder->std_list[current_std].standard.name);
 582        return 0;
 583}
 584
 585/**
 586 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
 587 * @sd: pointer to standard V4L2 sub-device structure
 588 * @std_id: standard V4L2 v4l2_std_id ioctl enum
 589 *
 590 * If std_id is supported, sets the requested standard. Otherwise, returns
 591 * -EINVAL
 592 */
 593static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
 594{
 595        struct tvp514x_decoder *decoder = to_decoder(sd);
 596        int err, i;
 597
 598        for (i = 0; i < decoder->num_stds; i++)
 599                if (std_id & decoder->std_list[i].standard.id)
 600                        break;
 601
 602        if ((i == decoder->num_stds) || (i == STD_INVALID))
 603                return -EINVAL;
 604
 605        err = tvp514x_write_reg(sd, REG_VIDEO_STD,
 606                                decoder->std_list[i].video_std);
 607        if (err)
 608                return err;
 609
 610        decoder->current_std = i;
 611        decoder->tvp514x_regs[REG_VIDEO_STD].val =
 612                decoder->std_list[i].video_std;
 613
 614        v4l2_dbg(1, debug, sd, "Standard set to: %s",
 615                        decoder->std_list[i].standard.name);
 616        return 0;
 617}
 618
 619/**
 620 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
 621 * @sd: pointer to standard V4L2 sub-device structure
 622 * @input: input selector for routing the signal
 623 * @output: output selector for routing the signal
 624 * @config: config value. Not used
 625 *
 626 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
 627 * the input is not supported or there is no active signal present in the
 628 * selected input.
 629 */
 630static int tvp514x_s_routing(struct v4l2_subdev *sd,
 631                                u32 input, u32 output, u32 config)
 632{
 633        struct tvp514x_decoder *decoder = to_decoder(sd);
 634        int err;
 635        enum tvp514x_input input_sel;
 636        enum tvp514x_output output_sel;
 637        enum tvp514x_std current_std = STD_INVALID;
 638        u8 sync_lock_status, lock_mask;
 639        int try_count = LOCK_RETRY_COUNT;
 640
 641        if ((input >= INPUT_INVALID) ||
 642                        (output >= OUTPUT_INVALID))
 643                /* Index out of bound */
 644                return -EINVAL;
 645
 646        input_sel = input;
 647        output_sel = output;
 648
 649        err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
 650        if (err)
 651                return err;
 652
 653        output_sel |= tvp514x_read_reg(sd,
 654                        REG_OUTPUT_FORMATTER1) & 0x7;
 655        err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
 656                        output_sel);
 657        if (err)
 658                return err;
 659
 660        decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
 661        decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
 662
 663        /* Clear status */
 664        msleep(LOCK_RETRY_DELAY);
 665        err =
 666            tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
 667        if (err)
 668                return err;
 669
 670        switch (input_sel) {
 671        case INPUT_CVBS_VI1A:
 672        case INPUT_CVBS_VI1B:
 673        case INPUT_CVBS_VI1C:
 674        case INPUT_CVBS_VI2A:
 675        case INPUT_CVBS_VI2B:
 676        case INPUT_CVBS_VI2C:
 677        case INPUT_CVBS_VI3A:
 678        case INPUT_CVBS_VI3B:
 679        case INPUT_CVBS_VI3C:
 680        case INPUT_CVBS_VI4A:
 681                lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
 682                        STATUS_HORZ_SYNC_LOCK_BIT |
 683                        STATUS_VIRT_SYNC_LOCK_BIT;
 684                break;
 685
 686        case INPUT_SVIDEO_VI2A_VI1A:
 687        case INPUT_SVIDEO_VI2B_VI1B:
 688        case INPUT_SVIDEO_VI2C_VI1C:
 689        case INPUT_SVIDEO_VI2A_VI3A:
 690        case INPUT_SVIDEO_VI2B_VI3B:
 691        case INPUT_SVIDEO_VI2C_VI3C:
 692        case INPUT_SVIDEO_VI4A_VI1A:
 693        case INPUT_SVIDEO_VI4A_VI1B:
 694        case INPUT_SVIDEO_VI4A_VI1C:
 695        case INPUT_SVIDEO_VI4A_VI3A:
 696        case INPUT_SVIDEO_VI4A_VI3B:
 697        case INPUT_SVIDEO_VI4A_VI3C:
 698                lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
 699                        STATUS_VIRT_SYNC_LOCK_BIT;
 700                break;
 701        /* Need to add other interfaces*/
 702        default:
 703                return -EINVAL;
 704        }
 705
 706        while (try_count-- > 0) {
 707                /* Allow decoder to sync up with new input */
 708                msleep(LOCK_RETRY_DELAY);
 709
 710                /* get the current standard for future reference */
 711                current_std = tvp514x_get_current_std(sd);
 712                if (current_std == STD_INVALID)
 713                        continue;
 714
 715                sync_lock_status = tvp514x_read_reg(sd,
 716                                REG_STATUS1);
 717                if (lock_mask == (sync_lock_status & lock_mask))
 718                        /* Input detected */
 719                        break;
 720        }
 721
 722        if ((current_std == STD_INVALID) || (try_count < 0))
 723                return -EINVAL;
 724
 725        decoder->current_std = current_std;
 726        decoder->input = input;
 727        decoder->output = output;
 728
 729        v4l2_dbg(1, debug, sd, "Input set to: %d, std : %d",
 730                        input_sel, current_std);
 731
 732        return 0;
 733}
 734
 735/**
 736 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
 737 * @sd: pointer to standard V4L2 sub-device structure
 738 * @qctrl: standard V4L2 v4l2_queryctrl structure
 739 *
 740 * If the requested control is supported, returns the control information.
 741 * Otherwise, returns -EINVAL if the control is not supported.
 742 */
 743static int
 744tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
 745{
 746        int err = -EINVAL;
 747
 748        if (qctrl == NULL)
 749                return err;
 750
 751        switch (qctrl->id) {
 752        case V4L2_CID_BRIGHTNESS:
 753                /* Brightness supported is (0-255), */
 754                err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
 755                break;
 756        case V4L2_CID_CONTRAST:
 757        case V4L2_CID_SATURATION:
 758                /**
 759                 * Saturation and Contrast supported is -
 760                 *      Contrast: 0 - 255 (Default - 128)
 761                 *      Saturation: 0 - 255 (Default - 128)
 762                 */
 763                err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
 764                break;
 765        case V4L2_CID_HUE:
 766                /* Hue Supported is -
 767                 *      Hue - -180 - +180 (Default - 0, Step - +180)
 768                 */
 769                err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
 770                break;
 771        case V4L2_CID_AUTOGAIN:
 772                /**
 773                 * Auto Gain supported is -
 774                 *      0 - 1 (Default - 1)
 775                 */
 776                err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
 777                break;
 778        default:
 779                v4l2_err(sd, "invalid control id %d\n", qctrl->id);
 780                return err;
 781        }
 782
 783        v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
 784                        qctrl->name, qctrl->minimum, qctrl->maximum,
 785                        qctrl->default_value);
 786
 787        return err;
 788}
 789
 790/**
 791 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
 792 * @sd: pointer to standard V4L2 sub-device structure
 793 * @ctrl: pointer to v4l2_control structure
 794 *
 795 * If the requested control is supported, returns the control's current
 796 * value from the decoder. Otherwise, returns -EINVAL if the control is not
 797 * supported.
 798 */
 799static int
 800tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 801{
 802        struct tvp514x_decoder *decoder = to_decoder(sd);
 803
 804        if (ctrl == NULL)
 805                return -EINVAL;
 806
 807        switch (ctrl->id) {
 808        case V4L2_CID_BRIGHTNESS:
 809                ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
 810                break;
 811        case V4L2_CID_CONTRAST:
 812                ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
 813                break;
 814        case V4L2_CID_SATURATION:
 815                ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
 816                break;
 817        case V4L2_CID_HUE:
 818                ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
 819                if (ctrl->value == 0x7F)
 820                        ctrl->value = 180;
 821                else if (ctrl->value == 0x80)
 822                        ctrl->value = -180;
 823                else
 824                        ctrl->value = 0;
 825
 826                break;
 827        case V4L2_CID_AUTOGAIN:
 828                ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
 829                if ((ctrl->value & 0x3) == 3)
 830                        ctrl->value = 1;
 831                else
 832                        ctrl->value = 0;
 833
 834                break;
 835        default:
 836                v4l2_err(sd, "invalid control id %d\n", ctrl->id);
 837                return -EINVAL;
 838        }
 839
 840        v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
 841                        ctrl->id, ctrl->value);
 842        return 0;
 843}
 844
 845/**
 846 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
 847 * @sd: pointer to standard V4L2 sub-device structure
 848 * @ctrl: pointer to v4l2_control structure
 849 *
 850 * If the requested control is supported, sets the control's current
 851 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
 852 */
 853static int
 854tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 855{
 856        struct tvp514x_decoder *decoder = to_decoder(sd);
 857        int err = -EINVAL, value;
 858
 859        if (ctrl == NULL)
 860                return err;
 861
 862        value = ctrl->value;
 863
 864        switch (ctrl->id) {
 865        case V4L2_CID_BRIGHTNESS:
 866                if (ctrl->value < 0 || ctrl->value > 255) {
 867                        v4l2_err(sd, "invalid brightness setting %d\n",
 868                                        ctrl->value);
 869                        return -ERANGE;
 870                }
 871                err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
 872                                value);
 873                if (err)
 874                        return err;
 875
 876                decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
 877                break;
 878        case V4L2_CID_CONTRAST:
 879                if (ctrl->value < 0 || ctrl->value > 255) {
 880                        v4l2_err(sd, "invalid contrast setting %d\n",
 881                                        ctrl->value);
 882                        return -ERANGE;
 883                }
 884                err = tvp514x_write_reg(sd, REG_CONTRAST, value);
 885                if (err)
 886                        return err;
 887
 888                decoder->tvp514x_regs[REG_CONTRAST].val = value;
 889                break;
 890        case V4L2_CID_SATURATION:
 891                if (ctrl->value < 0 || ctrl->value > 255) {
 892                        v4l2_err(sd, "invalid saturation setting %d\n",
 893                                        ctrl->value);
 894                        return -ERANGE;
 895                }
 896                err = tvp514x_write_reg(sd, REG_SATURATION, value);
 897                if (err)
 898                        return err;
 899
 900                decoder->tvp514x_regs[REG_SATURATION].val = value;
 901                break;
 902        case V4L2_CID_HUE:
 903                if (value == 180)
 904                        value = 0x7F;
 905                else if (value == -180)
 906                        value = 0x80;
 907                else if (value == 0)
 908                        value = 0;
 909                else {
 910                        v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
 911                        return -ERANGE;
 912                }
 913                err = tvp514x_write_reg(sd, REG_HUE, value);
 914                if (err)
 915                        return err;
 916
 917                decoder->tvp514x_regs[REG_HUE].val = value;
 918                break;
 919        case V4L2_CID_AUTOGAIN:
 920                if (value == 1)
 921                        value = 0x0F;
 922                else if (value == 0)
 923                        value = 0x0C;
 924                else {
 925                        v4l2_err(sd, "invalid auto gain setting %d\n",
 926                                        ctrl->value);
 927                        return -ERANGE;
 928                }
 929                err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
 930                if (err)
 931                        return err;
 932
 933                decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
 934                break;
 935        default:
 936                v4l2_err(sd, "invalid control id %d\n", ctrl->id);
 937                return err;
 938        }
 939
 940        v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
 941                        ctrl->id, ctrl->value);
 942
 943        return err;
 944}
 945
 946/**
 947 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
 948 * @sd: pointer to standard V4L2 sub-device structure
 949 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
 950 *
 951 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
 952 */
 953static int
 954tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
 955{
 956        struct tvp514x_decoder *decoder = to_decoder(sd);
 957        int index;
 958
 959        if (fmt == NULL)
 960                return -EINVAL;
 961
 962        index = fmt->index;
 963        if ((index >= decoder->num_fmts) || (index < 0))
 964                /* Index out of bound */
 965                return -EINVAL;
 966
 967        if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 968                /* only capture is supported */
 969                return -EINVAL;
 970
 971        memcpy(fmt, &decoder->fmt_list[index],
 972                sizeof(struct v4l2_fmtdesc));
 973
 974        v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
 975                        decoder->fmt_list[index].index,
 976                        decoder->fmt_list[index].description);
 977        return 0;
 978}
 979
 980/**
 981 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
 982 * @sd: pointer to standard V4L2 sub-device structure
 983 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
 984 *
 985 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
 986 * ioctl is used to negotiate the image capture size and pixel format
 987 * without actually making it take effect.
 988 */
 989static int
 990tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
 991{
 992        struct tvp514x_decoder *decoder = to_decoder(sd);
 993        int ifmt;
 994        struct v4l2_pix_format *pix;
 995        enum tvp514x_std current_std;
 996
 997        if (f == NULL)
 998                return -EINVAL;
 999
1000        if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1001                /* only capture is supported */
1002                f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1003
1004        pix = &f->fmt.pix;
1005
1006        /* Calculate height and width based on current standard */
1007        current_std = tvp514x_get_current_std(sd);
1008        if (current_std == STD_INVALID)
1009                return -EINVAL;
1010
1011        decoder->current_std = current_std;
1012        pix->width = decoder->std_list[current_std].width;
1013        pix->height = decoder->std_list[current_std].height;
1014
1015        for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
1016                if (pix->pixelformat ==
1017                        decoder->fmt_list[ifmt].pixelformat)
1018                        break;
1019        }
1020        if (ifmt == decoder->num_fmts)
1021                /* None of the format matched, select default */
1022                ifmt = 0;
1023        pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;
1024
1025        pix->field = V4L2_FIELD_INTERLACED;
1026        pix->bytesperline = pix->width * 2;
1027        pix->sizeimage = pix->bytesperline * pix->height;
1028        pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1029        pix->priv = 0;
1030
1031        v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
1032                        "Width - %d, Height - %d",
1033                        decoder->fmt_list[ifmt].description, pix->bytesperline,
1034                        pix->width, pix->height);
1035        return 0;
1036}
1037
1038/**
1039 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
1040 * @sd: pointer to standard V4L2 sub-device structure
1041 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1042 *
1043 * If the requested format is supported, configures the HW to use that
1044 * format, returns error code if format not supported or HW can't be
1045 * correctly configured.
1046 */
1047static int
1048tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1049{
1050        struct tvp514x_decoder *decoder = to_decoder(sd);
1051        struct v4l2_pix_format *pix;
1052        int rval;
1053
1054        if (f == NULL)
1055                return -EINVAL;
1056
1057        if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1058                /* only capture is supported */
1059                return -EINVAL;
1060
1061        pix = &f->fmt.pix;
1062        rval = tvp514x_try_fmt_cap(sd, f);
1063        if (rval)
1064                return rval;
1065
1066                decoder->pix = *pix;
1067
1068        return rval;
1069}
1070
1071/**
1072 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
1073 * @sd: pointer to standard V4L2 sub-device structure
1074 * @f: pointer to standard V4L2 v4l2_format structure
1075 *
1076 * Returns the decoder's current pixel format in the v4l2_format
1077 * parameter.
1078 */
1079static int
1080tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1081{
1082        struct tvp514x_decoder *decoder = to_decoder(sd);
1083
1084        if (f == NULL)
1085                return -EINVAL;
1086
1087        if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1088                /* only capture is supported */
1089                return -EINVAL;
1090
1091        f->fmt.pix = decoder->pix;
1092
1093        v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
1094                        "Width - %d, Height - %d",
1095                        decoder->pix.bytesperline,
1096                        decoder->pix.width, decoder->pix.height);
1097        return 0;
1098}
1099
1100/**
1101 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1102 * @sd: pointer to standard V4L2 sub-device structure
1103 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1104 *
1105 * Returns the decoder's video CAPTURE parameters.
1106 */
1107static int
1108tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1109{
1110        struct tvp514x_decoder *decoder = to_decoder(sd);
1111        struct v4l2_captureparm *cparm;
1112        enum tvp514x_std current_std;
1113
1114        if (a == NULL)
1115                return -EINVAL;
1116
1117        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1118                /* only capture is supported */
1119                return -EINVAL;
1120
1121        memset(a, 0, sizeof(*a));
1122        a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1123
1124        /* get the current standard */
1125        current_std = tvp514x_get_current_std(sd);
1126        if (current_std == STD_INVALID)
1127                return -EINVAL;
1128
1129        decoder->current_std = current_std;
1130
1131        cparm = &a->parm.capture;
1132        cparm->capability = V4L2_CAP_TIMEPERFRAME;
1133        cparm->timeperframe =
1134                decoder->std_list[current_std].standard.frameperiod;
1135
1136        return 0;
1137}
1138
1139/**
1140 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1141 * @sd: pointer to standard V4L2 sub-device structure
1142 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1143 *
1144 * Configures the decoder to use the input parameters, if possible. If
1145 * not possible, returns the appropriate error code.
1146 */
1147static int
1148tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1149{
1150        struct tvp514x_decoder *decoder = to_decoder(sd);
1151        struct v4l2_fract *timeperframe;
1152        enum tvp514x_std current_std;
1153
1154        if (a == NULL)
1155                return -EINVAL;
1156
1157        if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1158                /* only capture is supported */
1159                return -EINVAL;
1160
1161        timeperframe = &a->parm.capture.timeperframe;
1162
1163        /* get the current standard */
1164        current_std = tvp514x_get_current_std(sd);
1165        if (current_std == STD_INVALID)
1166                return -EINVAL;
1167
1168        decoder->current_std = current_std;
1169
1170        *timeperframe =
1171            decoder->std_list[current_std].standard.frameperiod;
1172
1173        return 0;
1174}
1175
1176/**
1177 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1178 * @sd: pointer to standard V4L2 sub-device structure
1179 * @enable: streaming enable or disable
1180 *
1181 * Sets streaming to enable or disable, if possible.
1182 */
1183static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1184{
1185        int err = 0;
1186        struct i2c_client *client = v4l2_get_subdevdata(sd);
1187        struct tvp514x_decoder *decoder = to_decoder(sd);
1188
1189        if (decoder->streaming == enable)
1190                return 0;
1191
1192        switch (enable) {
1193        case 0:
1194        {
1195                /* Power Down Sequence */
1196                err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
1197                if (err) {
1198                        v4l2_err(sd, "Unable to turn off decoder\n");
1199                        return err;
1200                }
1201                decoder->streaming = enable;
1202                break;
1203        }
1204        case 1:
1205        {
1206                struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
1207                                client->driver->id_table->driver_data;
1208
1209                /* Power Up Sequence */
1210                err = tvp514x_write_regs(sd, int_seq);
1211                if (err) {
1212                        v4l2_err(sd, "Unable to turn on decoder\n");
1213                        return err;
1214                }
1215                /* Detect if not already detected */
1216                err = tvp514x_detect(sd, decoder);
1217                if (err) {
1218                        v4l2_err(sd, "Unable to detect decoder\n");
1219                        return err;
1220                }
1221                err = tvp514x_configure(sd, decoder);
1222                if (err) {
1223                        v4l2_err(sd, "Unable to configure decoder\n");
1224                        return err;
1225                }
1226                decoder->streaming = enable;
1227                break;
1228        }
1229        default:
1230                err = -ENODEV;
1231                break;
1232        }
1233
1234        return err;
1235}
1236
1237static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
1238        .queryctrl = tvp514x_queryctrl,
1239        .g_ctrl = tvp514x_g_ctrl,
1240        .s_ctrl = tvp514x_s_ctrl,
1241        .s_std = tvp514x_s_std,
1242};
1243
1244static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
1245        .s_routing = tvp514x_s_routing,
1246        .querystd = tvp514x_querystd,
1247        .enum_fmt = tvp514x_enum_fmt_cap,
1248        .g_fmt = tvp514x_g_fmt_cap,
1249        .try_fmt = tvp514x_try_fmt_cap,
1250        .s_fmt = tvp514x_s_fmt_cap,
1251        .g_parm = tvp514x_g_parm,
1252        .s_parm = tvp514x_s_parm,
1253        .s_stream = tvp514x_s_stream,
1254};
1255
1256static const struct v4l2_subdev_ops tvp514x_ops = {
1257        .core = &tvp514x_core_ops,
1258        .video = &tvp514x_video_ops,
1259};
1260
1261static struct tvp514x_decoder tvp514x_dev = {
1262        .streaming = 0,
1263
1264        .fmt_list = tvp514x_fmt_list,
1265        .num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
1266
1267        .pix = {
1268                /* Default to NTSC 8-bit YUV 422 */
1269                .width = NTSC_NUM_ACTIVE_PIXELS,
1270                .height = NTSC_NUM_ACTIVE_LINES,
1271                .pixelformat = V4L2_PIX_FMT_UYVY,
1272                .field = V4L2_FIELD_INTERLACED,
1273                .bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
1274                .sizeimage =
1275                NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
1276                .colorspace = V4L2_COLORSPACE_SMPTE170M,
1277                },
1278
1279        .current_std = STD_NTSC_MJ,
1280        .std_list = tvp514x_std_list,
1281        .num_stds = ARRAY_SIZE(tvp514x_std_list),
1282
1283};
1284
1285/**
1286 * tvp514x_probe() - decoder driver i2c probe handler
1287 * @client: i2c driver client device structure
1288 * @id: i2c driver id table
1289 *
1290 * Register decoder as an i2c client device and V4L2
1291 * device.
1292 */
1293static int
1294tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1295{
1296        struct tvp514x_decoder *decoder;
1297        struct v4l2_subdev *sd;
1298
1299        /* Check if the adapter supports the needed features */
1300        if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1301                return -EIO;
1302
1303        if (!client->dev.platform_data) {
1304                v4l2_err(client, "No platform data!!\n");
1305                return -ENODEV;
1306        }
1307
1308        decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
1309        if (!decoder)
1310                return -ENOMEM;
1311
1312        /* Initialize the tvp514x_decoder with default configuration */
1313        *decoder = tvp514x_dev;
1314        /* Copy default register configuration */
1315        memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
1316                        sizeof(tvp514x_reg_list_default));
1317
1318        /* Copy board specific information here */
1319        decoder->pdata = client->dev.platform_data;
1320
1321        /**
1322         * Fetch platform specific data, and configure the
1323         * tvp514x_reg_list[] accordingly. Since this is one
1324         * time configuration, no need to preserve.
1325         */
1326        decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1327                (decoder->pdata->clk_polarity << 1);
1328        decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1329                ((decoder->pdata->hs_polarity << 2) |
1330                 (decoder->pdata->vs_polarity << 3));
1331        /* Set default standard to auto */
1332        decoder->tvp514x_regs[REG_VIDEO_STD].val =
1333                VIDEO_STD_AUTO_SWITCH_BIT;
1334
1335        /* Register with V4L2 layer as slave device */
1336        sd = &decoder->sd;
1337        v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1338
1339        v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1340
1341        return 0;
1342
1343}
1344
1345/**
1346 * tvp514x_remove() - decoder driver i2c remove handler
1347 * @client: i2c driver client device structure
1348 *
1349 * Unregister decoder as an i2c client device and V4L2
1350 * device. Complement of tvp514x_probe().
1351 */
1352static int tvp514x_remove(struct i2c_client *client)
1353{
1354        struct v4l2_subdev *sd = i2c_get_clientdata(client);
1355        struct tvp514x_decoder *decoder = to_decoder(sd);
1356
1357        v4l2_device_unregister_subdev(sd);
1358        kfree(decoder);
1359        return 0;
1360}
1361/* TVP5146 Init/Power on Sequence */
1362static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
1363        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1364        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1365        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1366        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1367        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1368        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1369        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1370        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1371        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1372        {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1373        {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1374        {TOK_TERM, 0, 0},
1375};
1376
1377/* TVP5147 Init/Power on Sequence */
1378static const struct tvp514x_reg tvp5147_init_reg_seq[] =        {
1379        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
1380        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1381        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
1382        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1383        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1384        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1385        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1386        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
1387        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
1388        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1389        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
1390        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
1391        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
1392        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
1393        {TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
1394        {TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
1395        {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1396        {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1397        {TOK_TERM, 0, 0},
1398};
1399
1400/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1401static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
1402        {TOK_WRITE, REG_OPERATION_MODE, 0x01},
1403        {TOK_WRITE, REG_OPERATION_MODE, 0x00},
1404        {TOK_TERM, 0, 0},
1405};
1406
1407/**
1408 * I2C Device Table -
1409 *
1410 * name - Name of the actual device/chip.
1411 * driver_data - Driver data
1412 */
1413static const struct i2c_device_id tvp514x_id[] = {
1414        {"tvp5146", (unsigned long)tvp5146_init_reg_seq},
1415        {"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
1416        {"tvp5147", (unsigned long)tvp5147_init_reg_seq},
1417        {"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1418        {},
1419};
1420
1421MODULE_DEVICE_TABLE(i2c, tvp514x_id);
1422
1423static struct i2c_driver tvp514x_driver = {
1424        .driver = {
1425                .owner = THIS_MODULE,
1426                .name = TVP514X_MODULE_NAME,
1427        },
1428        .probe = tvp514x_probe,
1429        .remove = tvp514x_remove,
1430        .id_table = tvp514x_id,
1431};
1432
1433static int __init tvp514x_init(void)
1434{
1435        return i2c_add_driver(&tvp514x_driver);
1436}
1437
1438static void __exit tvp514x_exit(void)
1439{
1440        i2c_del_driver(&tvp514x_driver);
1441}
1442
1443module_init(tvp514x_init);
1444module_exit(tvp514x_exit);
1445