tvp514x.c 37.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * drivers/media/video/tvp514x.c
 *
 * TI TVP5146/47 decoder driver
 *
 * Copyright (C) 2008 Texas Instruments Inc
 * Author: Vaibhav Hiremath <hvaibhav@ti.com>
 *
 * Contributors:
 *     Sivaraj R <sivaraj@ti.com>
 *     Brijesh R Jadav <brijesh.j@ti.com>
 *     Hardik Shah <hardik.shah@ti.com>
 *     Manjunath Hadli <mrh@ti.com>
 *     Karicheri Muralidharan <m-karicheri2@ti.com>
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/i2c.h>
32
#include <linux/slab.h>
33 34
#include <linux/delay.h>
#include <linux/videodev2.h>
35 36 37 38

#include <media/v4l2-device.h>
#include <media/v4l2-common.h>
#include <media/v4l2-chip-ident.h>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <media/tvp514x.h>

#include "tvp514x_regs.h"

/* Module Name */
#define TVP514X_MODULE_NAME		"tvp514x"

/* Private macros for TVP */
#define I2C_RETRY_COUNT                 (5)
#define LOCK_RETRY_COUNT                (5)
#define LOCK_RETRY_DELAY                (200)

/* Debug functions */
static int debug;
module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "Debug level (0-1)");

56 57 58
MODULE_AUTHOR("Texas Instruments");
MODULE_DESCRIPTION("TVP514X linux decoder driver");
MODULE_LICENSE("GPL");
59

60
/* enum tvp514x_std - enum for supported standards */
61 62 63 64 65 66
enum tvp514x_std {
	STD_NTSC_MJ = 0,
	STD_PAL_BDGHIN,
	STD_INVALID
};

67
/**
68 69 70 71 72 73 74 75 76 77 78 79 80
 * struct tvp514x_std_info - Structure to store standard informations
 * @width: Line width in pixels
 * @height:Number of active lines
 * @video_std: Value to write in REG_VIDEO_STD register
 * @standard: v4l2 standard structure information
 */
struct tvp514x_std_info {
	unsigned long width;
	unsigned long height;
	u8 video_std;
	struct v4l2_standard standard;
};

81
static struct tvp514x_reg tvp514x_reg_list_default[0x40];
82 83

static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable);
84
/**
85
 * struct tvp514x_decoder - TVP5146/47 decoder object
86
 * @sd: Subdevice Slave handle
87
 * @tvp514x_regs: copy of hw's regs with preset values.
88 89
 * @pdata: Board specific
 * @ver: Chip version
90
 * @streaming: TVP5146/47 decoder streaming - enabled or disabled.
91 92 93 94 95 96
 * @pix: Current pixel format
 * @num_fmts: Number of formats
 * @fmt_list: Format list
 * @current_std: Current standard
 * @num_stds: Number of standards
 * @std_list: Standards list
97 98
 * @input: Input routing at chip level
 * @output: Output routing at chip level
99 100
 */
struct tvp514x_decoder {
101
	struct v4l2_subdev sd;
102
	struct tvp514x_reg tvp514x_regs[ARRAY_SIZE(tvp514x_reg_list_default)];
103 104 105
	const struct tvp514x_platform_data *pdata;

	int ver;
106
	int streaming;
107 108 109 110 111 112 113 114

	struct v4l2_pix_format pix;
	int num_fmts;
	const struct v4l2_fmtdesc *fmt_list;

	enum tvp514x_std current_std;
	int num_stds;
	struct tvp514x_std_info *std_list;
115
	/* Input and Output Routing parameters */
116 117
	u32 input;
	u32 output;
118 119 120
};

/* TVP514x default register values */
121
static struct tvp514x_reg tvp514x_reg_list_default[] = {
122 123
	/* Composite selected */
	{TOK_WRITE, REG_INPUT_SEL, 0x05},
124
	{TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
125 126
	/* Auto mode */
	{TOK_WRITE, REG_VIDEO_STD, 0x00},
127 128 129 130 131 132 133 134 135 136 137 138
	{TOK_WRITE, REG_OPERATION_MODE, 0x00},
	{TOK_SKIP, REG_AUTOSWITCH_MASK, 0x3F},
	{TOK_WRITE, REG_COLOR_KILLER, 0x10},
	{TOK_WRITE, REG_LUMA_CONTROL1, 0x00},
	{TOK_WRITE, REG_LUMA_CONTROL2, 0x00},
	{TOK_WRITE, REG_LUMA_CONTROL3, 0x02},
	{TOK_WRITE, REG_BRIGHTNESS, 0x80},
	{TOK_WRITE, REG_CONTRAST, 0x80},
	{TOK_WRITE, REG_SATURATION, 0x80},
	{TOK_WRITE, REG_HUE, 0x00},
	{TOK_WRITE, REG_CHROMA_CONTROL1, 0x00},
	{TOK_WRITE, REG_CHROMA_CONTROL2, 0x0E},
139 140
	/* Reserved */
	{TOK_SKIP, 0x0F, 0x00},
141 142 143
	{TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
	{TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
	{TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
144 145
	/* Reserved */
	{TOK_SKIP, 0x13, 0x00},
146
	{TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
147 148 149 150
	/* Reserved */
	{TOK_SKIP, 0x15, 0x00},
	/* NTSC timing */
	{TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
151 152 153
	{TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
	{TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
	{TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
154 155
	/* NTSC timing */
	{TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
156 157 158
	{TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
	{TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
	{TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
159 160
	/* NTSC timing */
	{TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
161 162 163
	{TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
	{TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
	{TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
164 165
	/* NTSC timing */
	{TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
166 167 168
	{TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
	{TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
	{TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
169 170 171 172
	/* Reserved */
	{TOK_SKIP, 0x26, 0x00},
	/* Reserved */
	{TOK_SKIP, 0x27, 0x00},
173
	{TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
174 175
	/* Reserved */
	{TOK_SKIP, 0x29, 0x00},
176
	{TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
177 178
	/* Reserved */
	{TOK_SKIP, 0x2B, 0x00},
179 180 181
	{TOK_SKIP, REG_SCART_DELAY, 0x00},
	{TOK_SKIP, REG_CTI_DELAY, 0x00},
	{TOK_SKIP, REG_CTI_CONTROL, 0x00},
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	/* Reserved */
	{TOK_SKIP, 0x2F, 0x00},
	/* Reserved */
	{TOK_SKIP, 0x30, 0x00},
	/* Reserved */
	{TOK_SKIP, 0x31, 0x00},
	/* HS, VS active high */
	{TOK_WRITE, REG_SYNC_CONTROL, 0x00},
	/* 10-bit BT.656 */
	{TOK_WRITE, REG_OUTPUT_FORMATTER1, 0x00},
	/* Enable clk & data */
	{TOK_WRITE, REG_OUTPUT_FORMATTER2, 0x11},
	/* Enable AVID & FLD */
	{TOK_WRITE, REG_OUTPUT_FORMATTER3, 0xEE},
	/* Enable VS & HS */
	{TOK_WRITE, REG_OUTPUT_FORMATTER4, 0xAF},
198 199
	{TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
	{TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
200 201
	/* Clear status */
	{TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
202 203 204
	{TOK_TERM, 0, 0},
};

205
/**
206
 * List of image formats supported by TVP5146/47 decoder
207 208 209 210 211 212 213 214 215 216 217 218 219
 * Currently we are using 8 bit mode only, but can be
 * extended to 10/20 bit mode.
 */
static const struct v4l2_fmtdesc tvp514x_fmt_list[] = {
	{
	 .index = 0,
	 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
	 .flags = 0,
	 .description = "8-bit UYVY 4:2:2 Format",
	 .pixelformat = V4L2_PIX_FMT_UYVY,
	},
};

220
/**
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
 * Supported standards -
 *
 * Currently supports two standards only, need to add support for rest of the
 * modes, like SECAM, etc...
 */
static struct tvp514x_std_info tvp514x_std_list[] = {
	/* Standard: STD_NTSC_MJ */
	[STD_NTSC_MJ] = {
	 .width = NTSC_NUM_ACTIVE_PIXELS,
	 .height = NTSC_NUM_ACTIVE_LINES,
	 .video_std = VIDEO_STD_NTSC_MJ_BIT,
	 .standard = {
		      .index = 0,
		      .id = V4L2_STD_NTSC,
		      .name = "NTSC",
		      .frameperiod = {1001, 30000},
		      .framelines = 525
		     },
	/* Standard: STD_PAL_BDGHIN */
	},
	[STD_PAL_BDGHIN] = {
	 .width = PAL_NUM_ACTIVE_PIXELS,
	 .height = PAL_NUM_ACTIVE_LINES,
	 .video_std = VIDEO_STD_PAL_BDGHIN_BIT,
	 .standard = {
		      .index = 1,
		      .id = V4L2_STD_PAL,
		      .name = "PAL",
		      .frameperiod = {1, 25},
		      .framelines = 625
		     },
	},
	/* Standard: need to add for additional standard */
};
255 256 257 258 259 260 261


static inline struct tvp514x_decoder *to_decoder(struct v4l2_subdev *sd)
{
	return container_of(sd, struct tvp514x_decoder, sd);
}

262

263 264 265 266 267
/**
 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
 * @sd: ptr to v4l2_subdev struct
 * @reg: TVP5146/47 register address
 *
268 269
 * Returns value read if successful, or non-zero (-1) otherwise.
 */
270
static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
271
{
272 273 274
	int err, retry = 0;
	struct i2c_client *client = v4l2_get_subdevdata(sd);

275 276 277
read_again:

	err = i2c_smbus_read_byte_data(client, reg);
278
	if (err < 0) {
279
		if (retry <= I2C_RETRY_COUNT) {
280
			v4l2_warn(sd, "Read: retry ... %d\n", retry);
281 282 283 284 285 286 287 288 289
			retry++;
			msleep_interruptible(10);
			goto read_again;
		}
	}

	return err;
}

290 291 292 293 294
/**
 * dump_reg() - dump the register content of TVP5146/47.
 * @sd: ptr to v4l2_subdev struct
 * @reg: TVP5146/47 register address
 */
295 296 297 298 299 300 301 302
static void dump_reg(struct v4l2_subdev *sd, u8 reg)
{
	u32 val;

	val = tvp514x_read_reg(sd, reg);
	v4l2_info(sd, "Reg(0x%.2X): 0x%.2X\n", reg, val);
}

303 304 305 306 307 308
/**
 * tvp514x_write_reg() - Write a value to a register in TVP5146/47
 * @sd: ptr to v4l2_subdev struct
 * @reg: TVP5146/47 register address
 * @val: value to be written to the register
 *
309 310 311
 * Write a value to a register in an TVP5146/47 decoder device.
 * Returns zero if successful, or non-zero otherwise.
 */
312
static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
313
{
314 315 316
	int err, retry = 0;
	struct i2c_client *client = v4l2_get_subdevdata(sd);

317 318 319 320 321
write_again:

	err = i2c_smbus_write_byte_data(client, reg, val);
	if (err) {
		if (retry <= I2C_RETRY_COUNT) {
322
			v4l2_warn(sd, "Write: retry ... %d\n", retry);
323 324 325 326 327 328 329 330 331
			retry++;
			msleep_interruptible(10);
			goto write_again;
		}
	}

	return err;
}

332 333 334 335 336 337
/**
 * tvp514x_write_regs() : Initializes a list of TVP5146/47 registers
 * @sd: ptr to v4l2_subdev struct
 * @reglist: list of TVP5146/47 registers and values
 *
 * Initializes a list of TVP5146/47 registers:-
338 339 340 341 342 343
 *		if token is TOK_TERM, then entire write operation terminates
 *		if token is TOK_DELAY, then a delay of 'val' msec is introduced
 *		if token is TOK_SKIP, then the register write is skipped
 *		if token is TOK_WRITE, then the register write is performed
 * Returns zero if successful, or non-zero otherwise.
 */
344
static int tvp514x_write_regs(struct v4l2_subdev *sd,
345 346 347 348 349 350 351 352 353 354 355 356 357 358
			      const struct tvp514x_reg reglist[])
{
	int err;
	const struct tvp514x_reg *next = reglist;

	for (; next->token != TOK_TERM; next++) {
		if (next->token == TOK_DELAY) {
			msleep(next->val);
			continue;
		}

		if (next->token == TOK_SKIP)
			continue;

359
		err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
360
		if (err) {
361
			v4l2_err(sd, "Write failed. Err[%d]\n", err);
362 363 364 365 366 367
			return err;
		}
	}
	return 0;
}

368
/**
369
 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
370 371
 * @sd: ptr to v4l2_subdev struct
 *
372
 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
373
 * standard detected.
374
 */
375
static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
376 377 378
{
	u8 std, std_status;

379 380
	std = tvp514x_read_reg(sd, REG_VIDEO_STD);
	if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
381
		/* use the standard status register */
382 383
		std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
	else
384 385
		/* use the standard register itself */
		std_status = std;
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

	switch (std_status & VIDEO_STD_MASK) {
	case VIDEO_STD_NTSC_MJ_BIT:
		return STD_NTSC_MJ;

	case VIDEO_STD_PAL_BDGHIN_BIT:
		return STD_PAL_BDGHIN;

	default:
		return STD_INVALID;
	}

	return STD_INVALID;
}

401
/* TVP5146/47 register dump function */
402
static void tvp514x_reg_dump(struct v4l2_subdev *sd)
403
{
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	dump_reg(sd, REG_INPUT_SEL);
	dump_reg(sd, REG_AFE_GAIN_CTRL);
	dump_reg(sd, REG_VIDEO_STD);
	dump_reg(sd, REG_OPERATION_MODE);
	dump_reg(sd, REG_COLOR_KILLER);
	dump_reg(sd, REG_LUMA_CONTROL1);
	dump_reg(sd, REG_LUMA_CONTROL2);
	dump_reg(sd, REG_LUMA_CONTROL3);
	dump_reg(sd, REG_BRIGHTNESS);
	dump_reg(sd, REG_CONTRAST);
	dump_reg(sd, REG_SATURATION);
	dump_reg(sd, REG_HUE);
	dump_reg(sd, REG_CHROMA_CONTROL1);
	dump_reg(sd, REG_CHROMA_CONTROL2);
	dump_reg(sd, REG_COMP_PR_SATURATION);
	dump_reg(sd, REG_COMP_Y_CONTRAST);
	dump_reg(sd, REG_COMP_PB_SATURATION);
	dump_reg(sd, REG_COMP_Y_BRIGHTNESS);
	dump_reg(sd, REG_AVID_START_PIXEL_LSB);
	dump_reg(sd, REG_AVID_START_PIXEL_MSB);
	dump_reg(sd, REG_AVID_STOP_PIXEL_LSB);
	dump_reg(sd, REG_AVID_STOP_PIXEL_MSB);
	dump_reg(sd, REG_HSYNC_START_PIXEL_LSB);
	dump_reg(sd, REG_HSYNC_START_PIXEL_MSB);
	dump_reg(sd, REG_HSYNC_STOP_PIXEL_LSB);
	dump_reg(sd, REG_HSYNC_STOP_PIXEL_MSB);
	dump_reg(sd, REG_VSYNC_START_LINE_LSB);
	dump_reg(sd, REG_VSYNC_START_LINE_MSB);
	dump_reg(sd, REG_VSYNC_STOP_LINE_LSB);
	dump_reg(sd, REG_VSYNC_STOP_LINE_MSB);
	dump_reg(sd, REG_VBLK_START_LINE_LSB);
	dump_reg(sd, REG_VBLK_START_LINE_MSB);
	dump_reg(sd, REG_VBLK_STOP_LINE_LSB);
	dump_reg(sd, REG_VBLK_STOP_LINE_MSB);
	dump_reg(sd, REG_SYNC_CONTROL);
	dump_reg(sd, REG_OUTPUT_FORMATTER1);
	dump_reg(sd, REG_OUTPUT_FORMATTER2);
	dump_reg(sd, REG_OUTPUT_FORMATTER3);
	dump_reg(sd, REG_OUTPUT_FORMATTER4);
	dump_reg(sd, REG_OUTPUT_FORMATTER5);
	dump_reg(sd, REG_OUTPUT_FORMATTER6);
	dump_reg(sd, REG_CLEAR_LOST_LOCK);
446 447
}

448 449 450 451 452
/**
 * tvp514x_configure() - Configure the TVP5146/47 registers
 * @sd: ptr to v4l2_subdev struct
 * @decoder: ptr to tvp514x_decoder structure
 *
453 454
 * Returns zero if successful, or non-zero otherwise.
 */
455 456
static int tvp514x_configure(struct v4l2_subdev *sd,
		struct tvp514x_decoder *decoder)
457 458 459 460 461
{
	int err;

	/* common register initialization */
	err =
462
	    tvp514x_write_regs(sd, decoder->tvp514x_regs);
463 464 465 466
	if (err)
		return err;

	if (debug)
467
		tvp514x_reg_dump(sd);
468 469 470 471

	return 0;
}

472 473 474 475 476
/**
 * tvp514x_detect() - Detect if an tvp514x is present, and if so which revision.
 * @sd: pointer to standard V4L2 sub-device structure
 * @decoder: pointer to tvp514x_decoder structure
 *
477 478 479 480 481 482
 * A device is considered to be detected if the chip ID (LSB and MSB)
 * registers match the expected values.
 * Any value of the rom version register is accepted.
 * Returns ENODEV error number if no device is detected, or zero
 * if a device is detected.
 */
483 484
static int tvp514x_detect(struct v4l2_subdev *sd,
		struct tvp514x_decoder *decoder)
485 486
{
	u8 chip_id_msb, chip_id_lsb, rom_ver;
487
	struct i2c_client *client = v4l2_get_subdevdata(sd);
488

489 490 491
	chip_id_msb = tvp514x_read_reg(sd, REG_CHIP_ID_MSB);
	chip_id_lsb = tvp514x_read_reg(sd, REG_CHIP_ID_LSB);
	rom_ver = tvp514x_read_reg(sd, REG_ROM_VERSION);
492

493
	v4l2_dbg(1, debug, sd,
494 495 496 497 498 499 500 501
		 "chip id detected msb:0x%x lsb:0x%x rom version:0x%x\n",
		 chip_id_msb, chip_id_lsb, rom_ver);
	if ((chip_id_msb != TVP514X_CHIP_ID_MSB)
		|| ((chip_id_lsb != TVP5146_CHIP_ID_LSB)
		&& (chip_id_lsb != TVP5147_CHIP_ID_LSB))) {
		/* We didn't read the values we expected, so this must not be
		 * an TVP5146/47.
		 */
502 503
		v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
				chip_id_msb, chip_id_lsb);
504 505 506 507 508
		return -ENODEV;
	}

	decoder->ver = rom_ver;

509 510 511
	v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
			client->name, decoder->ver,
			client->addr << 1, client->adapter->name);
512 513 514
	return 0;
}

515 516
/**
 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
517
 * @sd: pointer to standard V4L2 sub-device structure
518 519 520
 * @std_id: standard V4L2 std_id ioctl enum
 *
 * Returns the current standard detected by TVP5146/47. If no active input is
521
 * detected then *std_id is set to 0 and the function returns 0.
522
 */
523
static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
524
{
525
	struct tvp514x_decoder *decoder = to_decoder(sd);
526 527 528 529 530 531 532
	enum tvp514x_std current_std;
	enum tvp514x_input input_sel;
	u8 sync_lock_status, lock_mask;

	if (std_id == NULL)
		return -EINVAL;

533 534 535 536
	*std_id = V4L2_STD_UNKNOWN;

	/* query the current standard */
	current_std = tvp514x_query_current_std(sd);
537
	if (current_std == STD_INVALID)
538
		return 0;
539

540
	input_sel = decoder->input;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

	switch (input_sel) {
	case INPUT_CVBS_VI1A:
	case INPUT_CVBS_VI1B:
	case INPUT_CVBS_VI1C:
	case INPUT_CVBS_VI2A:
	case INPUT_CVBS_VI2B:
	case INPUT_CVBS_VI2C:
	case INPUT_CVBS_VI3A:
	case INPUT_CVBS_VI3B:
	case INPUT_CVBS_VI3C:
	case INPUT_CVBS_VI4A:
		lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
			STATUS_HORZ_SYNC_LOCK_BIT |
			STATUS_VIRT_SYNC_LOCK_BIT;
		break;

	case INPUT_SVIDEO_VI2A_VI1A:
	case INPUT_SVIDEO_VI2B_VI1B:
	case INPUT_SVIDEO_VI2C_VI1C:
	case INPUT_SVIDEO_VI2A_VI3A:
	case INPUT_SVIDEO_VI2B_VI3B:
	case INPUT_SVIDEO_VI2C_VI3C:
	case INPUT_SVIDEO_VI4A_VI1A:
	case INPUT_SVIDEO_VI4A_VI1B:
	case INPUT_SVIDEO_VI4A_VI1C:
	case INPUT_SVIDEO_VI4A_VI3A:
	case INPUT_SVIDEO_VI4A_VI3B:
	case INPUT_SVIDEO_VI4A_VI3C:
		lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
			STATUS_VIRT_SYNC_LOCK_BIT;
		break;
		/*Need to add other interfaces*/
	default:
		return -EINVAL;
	}
	/* check whether signal is locked */
578
	sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
579
	if (lock_mask != (sync_lock_status & lock_mask))
580
		return 0;	/* No input detected */
581 582 583

	*std_id = decoder->std_list[current_std].standard.id;

584
	v4l2_dbg(1, debug, sd, "Current STD: %s\n",
585 586 587 588
			decoder->std_list[current_std].standard.name);
	return 0;
}

589 590
/**
 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
591
 * @sd: pointer to standard V4L2 sub-device structure
592 593 594 595 596
 * @std_id: standard V4L2 v4l2_std_id ioctl enum
 *
 * If std_id is supported, sets the requested standard. Otherwise, returns
 * -EINVAL
 */
597
static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
598
{
599
	struct tvp514x_decoder *decoder = to_decoder(sd);
600 601 602
	int err, i;

	for (i = 0; i < decoder->num_stds; i++)
603
		if (std_id & decoder->std_list[i].standard.id)
604 605 606 607 608
			break;

	if ((i == decoder->num_stds) || (i == STD_INVALID))
		return -EINVAL;

609
	err = tvp514x_write_reg(sd, REG_VIDEO_STD,
610 611 612 613 614
				decoder->std_list[i].video_std);
	if (err)
		return err;

	decoder->current_std = i;
615 616
	decoder->tvp514x_regs[REG_VIDEO_STD].val =
		decoder->std_list[i].video_std;
617

618
	v4l2_dbg(1, debug, sd, "Standard set to: %s",
619 620 621 622
			decoder->std_list[i].standard.name);
	return 0;
}

623 624
/**
 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
625
 * @sd: pointer to standard V4L2 sub-device structure
626 627 628
 * @input: input selector for routing the signal
 * @output: output selector for routing the signal
 * @config: config value. Not used
629 630 631 632 633
 *
 * If index is valid, selects the requested input. Otherwise, returns -EINVAL if
 * the input is not supported or there is no active signal present in the
 * selected input.
 */
634 635
static int tvp514x_s_routing(struct v4l2_subdev *sd,
				u32 input, u32 output, u32 config)
636
{
637
	struct tvp514x_decoder *decoder = to_decoder(sd);
638 639 640 641 642 643
	int err;
	enum tvp514x_input input_sel;
	enum tvp514x_output output_sel;
	u8 sync_lock_status, lock_mask;
	int try_count = LOCK_RETRY_COUNT;

644 645
	if ((input >= INPUT_INVALID) ||
			(output >= OUTPUT_INVALID))
646 647
		/* Index out of bound */
		return -EINVAL;
648

649 650 651 652 653 654 655 656 657 658 659
	/*
	 * For the sequence streamon -> streamoff and again s_input
	 * it fails to lock the signal, since streamoff puts TVP514x
	 * into power off state which leads to failure in sub-sequent s_input.
	 *
	 * So power up the TVP514x device here, since it is important to lock
	 * the signal at this stage.
	 */
	if (!decoder->streaming)
		tvp514x_s_stream(sd, 1);

660 661
	input_sel = input;
	output_sel = output;
662

663
	err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
664 665 666
	if (err)
		return err;

667
	output_sel |= tvp514x_read_reg(sd,
668
			REG_OUTPUT_FORMATTER1) & 0x7;
669
	err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
670 671 672 673
			output_sel);
	if (err)
		return err;

674 675
	decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
	decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
676 677 678 679

	/* Clear status */
	msleep(LOCK_RETRY_DELAY);
	err =
680
	    tvp514x_write_reg(sd, REG_CLEAR_LOST_LOCK, 0x01);
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	if (err)
		return err;

	switch (input_sel) {
	case INPUT_CVBS_VI1A:
	case INPUT_CVBS_VI1B:
	case INPUT_CVBS_VI1C:
	case INPUT_CVBS_VI2A:
	case INPUT_CVBS_VI2B:
	case INPUT_CVBS_VI2C:
	case INPUT_CVBS_VI3A:
	case INPUT_CVBS_VI3B:
	case INPUT_CVBS_VI3C:
	case INPUT_CVBS_VI4A:
		lock_mask = STATUS_CLR_SUBCAR_LOCK_BIT |
			STATUS_HORZ_SYNC_LOCK_BIT |
			STATUS_VIRT_SYNC_LOCK_BIT;
		break;

	case INPUT_SVIDEO_VI2A_VI1A:
	case INPUT_SVIDEO_VI2B_VI1B:
	case INPUT_SVIDEO_VI2C_VI1C:
	case INPUT_SVIDEO_VI2A_VI3A:
	case INPUT_SVIDEO_VI2B_VI3B:
	case INPUT_SVIDEO_VI2C_VI3C:
	case INPUT_SVIDEO_VI4A_VI1A:
	case INPUT_SVIDEO_VI4A_VI1B:
	case INPUT_SVIDEO_VI4A_VI1C:
	case INPUT_SVIDEO_VI4A_VI3A:
	case INPUT_SVIDEO_VI4A_VI3B:
	case INPUT_SVIDEO_VI4A_VI3C:
		lock_mask = STATUS_HORZ_SYNC_LOCK_BIT |
			STATUS_VIRT_SYNC_LOCK_BIT;
		break;
715
	/* Need to add other interfaces*/
716 717 718 719 720 721 722 723
	default:
		return -EINVAL;
	}

	while (try_count-- > 0) {
		/* Allow decoder to sync up with new input */
		msleep(LOCK_RETRY_DELAY);

724
		sync_lock_status = tvp514x_read_reg(sd,
725 726
				REG_STATUS1);
		if (lock_mask == (sync_lock_status & lock_mask))
727 728
			/* Input detected */
			break;
729 730
	}

731
	if (try_count < 0)
732 733
		return -EINVAL;

734 735
	decoder->input = input;
	decoder->output = output;
736

737
	v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
738 739 740 741

	return 0;
}

742 743
/**
 * tvp514x_queryctrl() - V4L2 decoder interface handler for queryctrl
744
 * @sd: pointer to standard V4L2 sub-device structure
745 746 747 748 749 750
 * @qctrl: standard V4L2 v4l2_queryctrl structure
 *
 * If the requested control is supported, returns the control information.
 * Otherwise, returns -EINVAL if the control is not supported.
 */
static int
751
tvp514x_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qctrl)
752 753 754 755 756 757 758 759
{
	int err = -EINVAL;

	if (qctrl == NULL)
		return err;

	switch (qctrl->id) {
	case V4L2_CID_BRIGHTNESS:
760
		/* Brightness supported is (0-255), */
761
		err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
762 763 764
		break;
	case V4L2_CID_CONTRAST:
	case V4L2_CID_SATURATION:
765 766
		/**
		 * Saturation and Contrast supported is -
767 768 769 770 771 772 773 774 775 776 777 778
		 *	Contrast: 0 - 255 (Default - 128)
		 *	Saturation: 0 - 255 (Default - 128)
		 */
		err = v4l2_ctrl_query_fill(qctrl, 0, 255, 1, 128);
		break;
	case V4L2_CID_HUE:
		/* Hue Supported is -
		 *	Hue - -180 - +180 (Default - 0, Step - +180)
		 */
		err = v4l2_ctrl_query_fill(qctrl, -180, 180, 180, 0);
		break;
	case V4L2_CID_AUTOGAIN:
779
		/**
780 781 782 783
		 * Auto Gain supported is -
		 * 	0 - 1 (Default - 1)
		 */
		err = v4l2_ctrl_query_fill(qctrl, 0, 1, 1, 1);
784 785
		break;
	default:
786
		v4l2_err(sd, "invalid control id %d\n", qctrl->id);
787 788 789
		return err;
	}

790 791
	v4l2_dbg(1, debug, sd, "Query Control:%s: Min - %d, Max - %d, Def - %d",
			qctrl->name, qctrl->minimum, qctrl->maximum,
792 793 794 795 796
			qctrl->default_value);

	return err;
}

797 798
/**
 * tvp514x_g_ctrl() - V4L2 decoder interface handler for g_ctrl
799
 * @sd: pointer to standard V4L2 sub-device structure
800 801 802 803 804 805 806
 * @ctrl: pointer to v4l2_control structure
 *
 * If the requested control is supported, returns the control's current
 * value from the decoder. Otherwise, returns -EINVAL if the control is not
 * supported.
 */
static int
807
tvp514x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
808
{
809
	struct tvp514x_decoder *decoder = to_decoder(sd);
810 811 812 813 814 815

	if (ctrl == NULL)
		return -EINVAL;

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
816
		ctrl->value = decoder->tvp514x_regs[REG_BRIGHTNESS].val;
817 818
		break;
	case V4L2_CID_CONTRAST:
819
		ctrl->value = decoder->tvp514x_regs[REG_CONTRAST].val;
820 821
		break;
	case V4L2_CID_SATURATION:
822
		ctrl->value = decoder->tvp514x_regs[REG_SATURATION].val;
823 824
		break;
	case V4L2_CID_HUE:
825
		ctrl->value = decoder->tvp514x_regs[REG_HUE].val;
826 827 828 829 830 831 832 833 834
		if (ctrl->value == 0x7F)
			ctrl->value = 180;
		else if (ctrl->value == 0x80)
			ctrl->value = -180;
		else
			ctrl->value = 0;

		break;
	case V4L2_CID_AUTOGAIN:
835
		ctrl->value = decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val;
836 837 838 839 840 841 842
		if ((ctrl->value & 0x3) == 3)
			ctrl->value = 1;
		else
			ctrl->value = 0;

		break;
	default:
843
		v4l2_err(sd, "invalid control id %d\n", ctrl->id);
844 845 846
		return -EINVAL;
	}

847
	v4l2_dbg(1, debug, sd, "Get Control: ID - %d - %d",
848 849 850 851
			ctrl->id, ctrl->value);
	return 0;
}

852 853
/**
 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
854
 * @sd: pointer to standard V4L2 sub-device structure
855 856 857 858 859 860
 * @ctrl: pointer to v4l2_control structure
 *
 * If the requested control is supported, sets the control's current
 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
 */
static int
861
tvp514x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
862
{
863
	struct tvp514x_decoder *decoder = to_decoder(sd);
864 865 866 867 868
	int err = -EINVAL, value;

	if (ctrl == NULL)
		return err;

869
	value = ctrl->value;
870 871 872 873

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
		if (ctrl->value < 0 || ctrl->value > 255) {
874
			v4l2_err(sd, "invalid brightness setting %d\n",
875 876 877
					ctrl->value);
			return -ERANGE;
		}
878
		err = tvp514x_write_reg(sd, REG_BRIGHTNESS,
879 880 881
				value);
		if (err)
			return err;
882

883
		decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
884 885 886
		break;
	case V4L2_CID_CONTRAST:
		if (ctrl->value < 0 || ctrl->value > 255) {
887
			v4l2_err(sd, "invalid contrast setting %d\n",
888 889 890
					ctrl->value);
			return -ERANGE;
		}
891
		err = tvp514x_write_reg(sd, REG_CONTRAST, value);
892 893
		if (err)
			return err;
894

895
		decoder->tvp514x_regs[REG_CONTRAST].val = value;
896 897 898
		break;
	case V4L2_CID_SATURATION:
		if (ctrl->value < 0 || ctrl->value > 255) {
899
			v4l2_err(sd, "invalid saturation setting %d\n",
900 901 902
					ctrl->value);
			return -ERANGE;
		}
903
		err = tvp514x_write_reg(sd, REG_SATURATION, value);
904 905
		if (err)
			return err;
906

907
		decoder->tvp514x_regs[REG_SATURATION].val = value;
908 909 910 911 912 913 914 915 916
		break;
	case V4L2_CID_HUE:
		if (value == 180)
			value = 0x7F;
		else if (value == -180)
			value = 0x80;
		else if (value == 0)
			value = 0;
		else {
917
			v4l2_err(sd, "invalid hue setting %d\n", ctrl->value);
918 919
			return -ERANGE;
		}
920
		err = tvp514x_write_reg(sd, REG_HUE, value);
921 922
		if (err)
			return err;
923

924
		decoder->tvp514x_regs[REG_HUE].val = value;
925 926 927 928 929 930 931
		break;
	case V4L2_CID_AUTOGAIN:
		if (value == 1)
			value = 0x0F;
		else if (value == 0)
			value = 0x0C;
		else {
932
			v4l2_err(sd, "invalid auto gain setting %d\n",
933 934 935
					ctrl->value);
			return -ERANGE;
		}
936
		err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value);
937 938
		if (err)
			return err;
939

940
		decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
941 942
		break;
	default:
943
		v4l2_err(sd, "invalid control id %d\n", ctrl->id);
944 945 946
		return err;
	}

947
	v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d",
948 949 950 951 952
			ctrl->id, ctrl->value);

	return err;
}

953 954
/**
 * tvp514x_enum_fmt_cap() - V4L2 decoder interface handler for enum_fmt
955
 * @sd: pointer to standard V4L2 sub-device structure
956 957 958 959 960
 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
 *
 * Implement the VIDIOC_ENUM_FMT ioctl to enumerate supported formats
 */
static int
961
tvp514x_enum_fmt_cap(struct v4l2_subdev *sd, struct v4l2_fmtdesc *fmt)
962
{
963
	struct tvp514x_decoder *decoder = to_decoder(sd);
964 965 966 967 968 969 970
	int index;

	if (fmt == NULL)
		return -EINVAL;

	index = fmt->index;
	if ((index >= decoder->num_fmts) || (index < 0))
971 972
		/* Index out of bound */
		return -EINVAL;
973 974

	if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
975 976
		/* only capture is supported */
		return -EINVAL;
977 978 979 980

	memcpy(fmt, &decoder->fmt_list[index],
		sizeof(struct v4l2_fmtdesc));

981
	v4l2_dbg(1, debug, sd, "Current FMT: index - %d (%s)",
982 983 984 985 986
			decoder->fmt_list[index].index,
			decoder->fmt_list[index].description);
	return 0;
}

987 988
/**
 * tvp514x_try_fmt_cap() - V4L2 decoder interface handler for try_fmt
989
 * @sd: pointer to standard V4L2 sub-device structure
990 991 992 993 994 995 996
 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
 *
 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
 * ioctl is used to negotiate the image capture size and pixel format
 * without actually making it take effect.
 */
static int
997
tvp514x_try_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
998
{
999
	struct tvp514x_decoder *decoder = to_decoder(sd);
1000 1001 1002 1003 1004 1005 1006 1007
	int ifmt;
	struct v4l2_pix_format *pix;
	enum tvp514x_std current_std;

	if (f == NULL)
		return -EINVAL;

	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1008
		/* only capture is supported */
1009 1010 1011 1012 1013
		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

	pix = &f->fmt.pix;

	/* Calculate height and width based on current standard */
1014
	current_std = decoder->current_std;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

	pix->width = decoder->std_list[current_std].width;
	pix->height = decoder->std_list[current_std].height;

	for (ifmt = 0; ifmt < decoder->num_fmts; ifmt++) {
		if (pix->pixelformat ==
			decoder->fmt_list[ifmt].pixelformat)
			break;
	}
	if (ifmt == decoder->num_fmts)
1025 1026
		/* None of the format matched, select default */
		ifmt = 0;
1027 1028 1029 1030 1031 1032 1033 1034
	pix->pixelformat = decoder->fmt_list[ifmt].pixelformat;

	pix->field = V4L2_FIELD_INTERLACED;
	pix->bytesperline = pix->width * 2;
	pix->sizeimage = pix->bytesperline * pix->height;
	pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
	pix->priv = 0;

1035
	v4l2_dbg(1, debug, sd, "Try FMT: pixelformat - %s, bytesperline - %d"
1036 1037 1038 1039 1040 1041
			"Width - %d, Height - %d",
			decoder->fmt_list[ifmt].description, pix->bytesperline,
			pix->width, pix->height);
	return 0;
}

1042 1043
/**
 * tvp514x_s_fmt_cap() - V4L2 decoder interface handler for s_fmt
1044
 * @sd: pointer to standard V4L2 sub-device structure
1045 1046 1047 1048 1049 1050 1051
 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
 *
 * If the requested format is supported, configures the HW to use that
 * format, returns error code if format not supported or HW can't be
 * correctly configured.
 */
static int
1052
tvp514x_s_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1053
{
1054
	struct tvp514x_decoder *decoder = to_decoder(sd);
1055 1056 1057 1058 1059 1060 1061
	struct v4l2_pix_format *pix;
	int rval;

	if (f == NULL)
		return -EINVAL;

	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1062 1063
		/* only capture is supported */
		return -EINVAL;
1064 1065

	pix = &f->fmt.pix;
1066
	rval = tvp514x_try_fmt_cap(sd, f);
1067 1068 1069 1070 1071 1072 1073 1074
	if (rval)
		return rval;

		decoder->pix = *pix;

	return rval;
}

1075 1076
/**
 * tvp514x_g_fmt_cap() - V4L2 decoder interface handler for tvp514x_g_fmt_cap
1077
 * @sd: pointer to standard V4L2 sub-device structure
1078 1079 1080 1081 1082 1083
 * @f: pointer to standard V4L2 v4l2_format structure
 *
 * Returns the decoder's current pixel format in the v4l2_format
 * parameter.
 */
static int
1084
tvp514x_g_fmt_cap(struct v4l2_subdev *sd, struct v4l2_format *f)
1085
{
1086
	struct tvp514x_decoder *decoder = to_decoder(sd);
1087 1088 1089 1090 1091

	if (f == NULL)
		return -EINVAL;

	if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1092 1093
		/* only capture is supported */
		return -EINVAL;
1094 1095 1096

	f->fmt.pix = decoder->pix;

1097
	v4l2_dbg(1, debug, sd, "Current FMT: bytesperline - %d"
1098 1099 1100 1101 1102 1103
			"Width - %d, Height - %d",
			decoder->pix.bytesperline,
			decoder->pix.width, decoder->pix.height);
	return 0;
}

1104 1105
/**
 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
1106
 * @sd: pointer to standard V4L2 sub-device structure
1107 1108 1109 1110 1111
 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
 *
 * Returns the decoder's video CAPTURE parameters.
 */
static int
1112
tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1113
{
1114
	struct tvp514x_decoder *decoder = to_decoder(sd);
1115 1116 1117 1118 1119 1120 1121
	struct v4l2_captureparm *cparm;
	enum tvp514x_std current_std;

	if (a == NULL)
		return -EINVAL;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1122 1123
		/* only capture is supported */
		return -EINVAL;
1124 1125

	/* get the current standard */
1126
	current_std = decoder->current_std;
1127 1128 1129 1130 1131 1132 1133 1134 1135

	cparm = &a->parm.capture;
	cparm->capability = V4L2_CAP_TIMEPERFRAME;
	cparm->timeperframe =
		decoder->std_list[current_std].standard.frameperiod;

	return 0;
}

1136 1137
/**
 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
1138
 * @sd: pointer to standard V4L2 sub-device structure
1139 1140 1141 1142 1143 1144
 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
 *
 * Configures the decoder to use the input parameters, if possible. If
 * not possible, returns the appropriate error code.
 */
static int
1145
tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
1146
{
1147
	struct tvp514x_decoder *decoder = to_decoder(sd);
1148 1149 1150 1151 1152 1153 1154
	struct v4l2_fract *timeperframe;
	enum tvp514x_std current_std;

	if (a == NULL)
		return -EINVAL;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1155 1156
		/* only capture is supported */
		return -EINVAL;
1157 1158 1159 1160

	timeperframe = &a->parm.capture.timeperframe;

	/* get the current standard */
1161
	current_std = decoder->current_std;
1162 1163 1164 1165 1166 1167 1168

	*timeperframe =
	    decoder->std_list[current_std].standard.frameperiod;

	return 0;
}

1169 1170
/**
 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
1171 1172
 * @sd: pointer to standard V4L2 sub-device structure
 * @enable: streaming enable or disable
1173
 *
1174
 * Sets streaming to enable or disable, if possible.
1175
 */
1176
static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
1177 1178
{
	int err = 0;
1179 1180
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct tvp514x_decoder *decoder = to_decoder(sd);
1181

1182 1183
	if (decoder->streaming == enable)
		return 0;
1184

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	switch (enable) {
	case 0:
	{
		/* Power Down Sequence */
		err = tvp514x_write_reg(sd, REG_OPERATION_MODE, 0x01);
		if (err) {
			v4l2_err(sd, "Unable to turn off decoder\n");
			return err;
		}
		decoder->streaming = enable;
1195
		break;
1196 1197 1198 1199 1200
	}
	case 1:
	{
		struct tvp514x_reg *int_seq = (struct tvp514x_reg *)
				client->driver->id_table->driver_data;
1201

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		/* Power Up Sequence */
		err = tvp514x_write_regs(sd, int_seq);
		if (err) {
			v4l2_err(sd, "Unable to turn on decoder\n");
			return err;
		}
		/* Detect if not already detected */
		err = tvp514x_detect(sd, decoder);
		if (err) {
			v4l2_err(sd, "Unable to detect decoder\n");
			return err;
1213
		}
1214 1215 1216 1217 1218 1219
		err = tvp514x_configure(sd, decoder);
		if (err) {
			v4l2_err(sd, "Unable to configure decoder\n");
			return err;
		}
		decoder->streaming = enable;
1220
		break;
1221
	}
1222 1223 1224 1225 1226 1227 1228 1229
	default:
		err = -ENODEV;
		break;
	}

	return err;
}

1230 1231 1232 1233 1234 1235
static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
	.queryctrl = tvp514x_queryctrl,
	.g_ctrl = tvp514x_g_ctrl,
	.s_ctrl = tvp514x_s_ctrl,
	.s_std = tvp514x_s_std,
};
1236

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
	.s_routing = tvp514x_s_routing,
	.querystd = tvp514x_querystd,
	.enum_fmt = tvp514x_enum_fmt_cap,
	.g_fmt = tvp514x_g_fmt_cap,
	.try_fmt = tvp514x_try_fmt_cap,
	.s_fmt = tvp514x_s_fmt_cap,
	.g_parm = tvp514x_g_parm,
	.s_parm = tvp514x_s_parm,
	.s_stream = tvp514x_s_stream,
};
1248

1249 1250 1251
static const struct v4l2_subdev_ops tvp514x_ops = {
	.core = &tvp514x_core_ops,
	.video = &tvp514x_video_ops,
1252 1253 1254
};

static struct tvp514x_decoder tvp514x_dev = {
1255
	.streaming = 0,
1256 1257 1258 1259

	.fmt_list = tvp514x_fmt_list,
	.num_fmts = ARRAY_SIZE(tvp514x_fmt_list),

1260 1261
	.pix = {
		/* Default to NTSC 8-bit YUV 422 */
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
		.width = NTSC_NUM_ACTIVE_PIXELS,
		.height = NTSC_NUM_ACTIVE_LINES,
		.pixelformat = V4L2_PIX_FMT_UYVY,
		.field = V4L2_FIELD_INTERLACED,
		.bytesperline = NTSC_NUM_ACTIVE_PIXELS * 2,
		.sizeimage =
		NTSC_NUM_ACTIVE_PIXELS * 2 * NTSC_NUM_ACTIVE_LINES,
		.colorspace = V4L2_COLORSPACE_SMPTE170M,
		},

	.current_std = STD_NTSC_MJ,
	.std_list = tvp514x_std_list,
	.num_stds = ARRAY_SIZE(tvp514x_std_list),
1275

1276 1277
};

1278 1279
/**
 * tvp514x_probe() - decoder driver i2c probe handler
1280
 * @client: i2c driver client device structure
1281
 * @id: i2c driver id table
1282 1283 1284 1285 1286 1287 1288
 *
 * Register decoder as an i2c client device and V4L2
 * device.
 */
static int
tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
1289
	struct tvp514x_decoder *decoder;
1290
	struct v4l2_subdev *sd;
1291 1292 1293 1294 1295

	/* Check if the adapter supports the needed features */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

1296 1297 1298 1299 1300
	if (!client->dev.platform_data) {
		v4l2_err(client, "No platform data!!\n");
		return -ENODEV;
	}

1301 1302 1303 1304
	decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
	if (!decoder)
		return -ENOMEM;

1305
	/* Initialize the tvp514x_decoder with default configuration */
1306
	*decoder = tvp514x_dev;
1307
	/* Copy default register configuration */
1308 1309
	memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
			sizeof(tvp514x_reg_list_default));
1310

1311
	/* Copy board specific information here */
1312 1313
	decoder->pdata = client->dev.platform_data;

1314
	/**
1315 1316 1317 1318
	 * Fetch platform specific data, and configure the
	 * tvp514x_reg_list[] accordingly. Since this is one
	 * time configuration, no need to preserve.
	 */
1319
	decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1320
		(decoder->pdata->clk_polarity << 1);
1321
	decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1322 1323 1324 1325 1326
		((decoder->pdata->hs_polarity << 2) |
		 (decoder->pdata->vs_polarity << 3));
	/* Set default standard to auto */
	decoder->tvp514x_regs[REG_VIDEO_STD].val =
		VIDEO_STD_AUTO_SWITCH_BIT;
1327 1328

	/* Register with V4L2 layer as slave device */
1329 1330 1331 1332 1333
	sd = &decoder->sd;
	v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);

	v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);

1334
	return 0;
1335

1336 1337
}

1338 1339
/**
 * tvp514x_remove() - decoder driver i2c remove handler
1340 1341 1342 1343 1344
 * @client: i2c driver client device structure
 *
 * Unregister decoder as an i2c client device and V4L2
 * device. Complement of tvp514x_probe().
 */
1345
static int tvp514x_remove(struct i2c_client *client)
1346
{
1347 1348
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct tvp514x_decoder *decoder = to_decoder(sd);
1349

1350
	v4l2_device_unregister_subdev(sd);
1351
	kfree(decoder);
1352 1353
	return 0;
}
1354
/* TVP5146 Init/Power on Sequence */
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
static const struct tvp514x_reg tvp5146_init_reg_seq[] = {
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
	{TOK_WRITE, REG_OPERATION_MODE, 0x01},
	{TOK_WRITE, REG_OPERATION_MODE, 0x00},
1367
	{TOK_TERM, 0, 0},
1368
};
1369

1370
/* TVP5147 Init/Power on Sequence */
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
static const struct tvp514x_reg tvp5147_init_reg_seq[] =	{
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x02},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0x80},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x01},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x16},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xA0},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x16},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS1, 0x60},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS2, 0x00},
	{TOK_WRITE, REG_VBUS_ADDRESS_ACCESS3, 0xB0},
	{TOK_WRITE, REG_VBUS_DATA_ACCESS_NO_VBUS_ADDR_INCR, 0x00},
	{TOK_WRITE, REG_OPERATION_MODE, 0x01},
	{TOK_WRITE, REG_OPERATION_MODE, 0x00},
1390
	{TOK_TERM, 0, 0},
1391
};
1392

1393
/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1394 1395 1396
static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
	{TOK_WRITE, REG_OPERATION_MODE, 0x01},
	{TOK_WRITE, REG_OPERATION_MODE, 0x00},
1397
	{TOK_TERM, 0, 0},
1398
};
1399

1400
/**
1401 1402 1403 1404 1405 1406
 * I2C Device Table -
 *
 * name - Name of the actual device/chip.
 * driver_data - Driver data
 */
static const struct i2c_device_id tvp514x_id[] = {
1407 1408 1409 1410
	{"tvp5146", (unsigned long)tvp5146_init_reg_seq},
	{"tvp5146m2", (unsigned long)tvp514xm_init_reg_seq},
	{"tvp5147", (unsigned long)tvp5147_init_reg_seq},
	{"tvp5147m1", (unsigned long)tvp514xm_init_reg_seq},
1411 1412 1413 1414 1415
	{},
};

MODULE_DEVICE_TABLE(i2c, tvp514x_id);

1416
static struct i2c_driver tvp514x_driver = {
1417
	.driver = {
1418 1419 1420
		.owner = THIS_MODULE,
		.name = TVP514X_MODULE_NAME,
	},
1421
	.probe = tvp514x_probe,
1422
	.remove = tvp514x_remove,
1423 1424 1425 1426 1427
	.id_table = tvp514x_id,
};

static int __init tvp514x_init(void)
{
1428
	return i2c_add_driver(&tvp514x_driver);
1429 1430
}

1431
static void __exit tvp514x_exit(void)
1432
{
1433
	i2c_del_driver(&tvp514x_driver);
1434 1435 1436
}

module_init(tvp514x_init);
1437
module_exit(tvp514x_exit);