tvp514x.c 35.4 KB
Newer Older
1
/*
2
 * drivers/media/i2c/tvp514x.c
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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>
15
 *     Prabhakar Lad <prabhakar.lad@ti.com>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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>
33
#include <linux/slab.h>
34 35
#include <linux/delay.h>
#include <linux/videodev2.h>
36
#include <linux/module.h>
37
#include <linux/v4l2-mediabus.h>
38
#include <linux/of.h>
39

40
#include <media/v4l2-async.h>
41 42
#include <media/v4l2-device.h>
#include <media/v4l2-common.h>
43
#include <media/v4l2-mediabus.h>
44
#include <media/v4l2-of.h>
45
#include <media/v4l2-ctrls.h>
46
#include <media/tvp514x.h>
47
#include <media/media-entity.h>
48 49 50 51 52 53 54 55 56

#include "tvp514x_regs.h"

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

/* Debug functions */
57
static bool debug;
58 59 60
module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "Debug level (0-1)");

61 62 63
MODULE_AUTHOR("Texas Instruments");
MODULE_DESCRIPTION("TVP514X linux decoder driver");
MODULE_LICENSE("GPL");
64

65
/* enum tvp514x_std - enum for supported standards */
66 67 68 69 70 71
enum tvp514x_std {
	STD_NTSC_MJ = 0,
	STD_PAL_BDGHIN,
	STD_INVALID
};

72
/**
73 74 75 76 77 78 79 80 81 82 83 84 85
 * 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;
};

86
static struct tvp514x_reg tvp514x_reg_list_default[0x40];
87 88

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

	int ver;
112
	int streaming;
113

114 115 116 117
	struct v4l2_pix_format pix;
	int num_fmts;
	const struct v4l2_fmtdesc *fmt_list;

118 119
	enum tvp514x_std current_std;
	int num_stds;
120
	const struct tvp514x_std_info *std_list;
121
	/* Input and Output Routing parameters */
122 123
	u32 input;
	u32 output;
124 125 126 127

	/* mc related members */
	struct media_pad pad;
	struct v4l2_mbus_framefmt format;
128 129

	struct tvp514x_reg *int_seq;
130 131 132
};

/* TVP514x default register values */
133
static struct tvp514x_reg tvp514x_reg_list_default[] = {
134 135
	/* Composite selected */
	{TOK_WRITE, REG_INPUT_SEL, 0x05},
136
	{TOK_WRITE, REG_AFE_GAIN_CTRL, 0x0F},
137 138
	/* Auto mode */
	{TOK_WRITE, REG_VIDEO_STD, 0x00},
139 140 141 142 143 144 145 146 147 148 149 150
	{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},
151 152
	/* Reserved */
	{TOK_SKIP, 0x0F, 0x00},
153 154 155
	{TOK_WRITE, REG_COMP_PR_SATURATION, 0x80},
	{TOK_WRITE, REG_COMP_Y_CONTRAST, 0x80},
	{TOK_WRITE, REG_COMP_PB_SATURATION, 0x80},
156 157
	/* Reserved */
	{TOK_SKIP, 0x13, 0x00},
158
	{TOK_WRITE, REG_COMP_Y_BRIGHTNESS, 0x80},
159 160 161 162
	/* Reserved */
	{TOK_SKIP, 0x15, 0x00},
	/* NTSC timing */
	{TOK_SKIP, REG_AVID_START_PIXEL_LSB, 0x55},
163 164 165
	{TOK_SKIP, REG_AVID_START_PIXEL_MSB, 0x00},
	{TOK_SKIP, REG_AVID_STOP_PIXEL_LSB, 0x25},
	{TOK_SKIP, REG_AVID_STOP_PIXEL_MSB, 0x03},
166 167
	/* NTSC timing */
	{TOK_SKIP, REG_HSYNC_START_PIXEL_LSB, 0x00},
168 169 170
	{TOK_SKIP, REG_HSYNC_START_PIXEL_MSB, 0x00},
	{TOK_SKIP, REG_HSYNC_STOP_PIXEL_LSB, 0x40},
	{TOK_SKIP, REG_HSYNC_STOP_PIXEL_MSB, 0x00},
171 172
	/* NTSC timing */
	{TOK_SKIP, REG_VSYNC_START_LINE_LSB, 0x04},
173 174 175
	{TOK_SKIP, REG_VSYNC_START_LINE_MSB, 0x00},
	{TOK_SKIP, REG_VSYNC_STOP_LINE_LSB, 0x07},
	{TOK_SKIP, REG_VSYNC_STOP_LINE_MSB, 0x00},
176 177
	/* NTSC timing */
	{TOK_SKIP, REG_VBLK_START_LINE_LSB, 0x01},
178 179 180
	{TOK_SKIP, REG_VBLK_START_LINE_MSB, 0x00},
	{TOK_SKIP, REG_VBLK_STOP_LINE_LSB, 0x15},
	{TOK_SKIP, REG_VBLK_STOP_LINE_MSB, 0x00},
181 182 183 184
	/* Reserved */
	{TOK_SKIP, 0x26, 0x00},
	/* Reserved */
	{TOK_SKIP, 0x27, 0x00},
185
	{TOK_SKIP, REG_FAST_SWTICH_CONTROL, 0xCC},
186 187
	/* Reserved */
	{TOK_SKIP, 0x29, 0x00},
188
	{TOK_SKIP, REG_FAST_SWTICH_SCART_DELAY, 0x00},
189 190
	/* Reserved */
	{TOK_SKIP, 0x2B, 0x00},
191 192 193
	{TOK_SKIP, REG_SCART_DELAY, 0x00},
	{TOK_SKIP, REG_CTI_DELAY, 0x00},
	{TOK_SKIP, REG_CTI_CONTROL, 0x00},
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	/* 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},
210 211
	{TOK_WRITE, REG_OUTPUT_FORMATTER5, 0xFF},
	{TOK_WRITE, REG_OUTPUT_FORMATTER6, 0xFF},
212 213
	/* Clear status */
	{TOK_WRITE, REG_CLEAR_LOST_LOCK, 0x01},
214 215 216
	{TOK_TERM, 0, 0},
};

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/**
 * List of image formats supported by TVP5146/47 decoder
 * 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,
	},
};

232
/**
233 234 235 236 237
 * Supported standards -
 *
 * Currently supports two standards only, need to add support for rest of the
 * modes, like SECAM, etc...
 */
238
static const struct tvp514x_std_info tvp514x_std_list[] = {
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	/* 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 */
};
267 268 269 270 271 272 273


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

274 275 276 277 278
static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
{
	return &container_of(ctrl->handler, struct tvp514x_decoder, hdl)->sd;
}

279

280 281 282 283 284
/**
 * tvp514x_read_reg() - Read a value from a register in an TVP5146/47.
 * @sd: ptr to v4l2_subdev struct
 * @reg: TVP5146/47 register address
 *
285 286
 * Returns value read if successful, or non-zero (-1) otherwise.
 */
287
static int tvp514x_read_reg(struct v4l2_subdev *sd, u8 reg)
288
{
289 290 291
	int err, retry = 0;
	struct i2c_client *client = v4l2_get_subdevdata(sd);

292 293 294
read_again:

	err = i2c_smbus_read_byte_data(client, reg);
295
	if (err < 0) {
296
		if (retry <= I2C_RETRY_COUNT) {
297
			v4l2_warn(sd, "Read: retry ... %d\n", retry);
298 299 300 301 302 303 304 305 306
			retry++;
			msleep_interruptible(10);
			goto read_again;
		}
	}

	return err;
}

307 308 309 310 311
/**
 * dump_reg() - dump the register content of TVP5146/47.
 * @sd: ptr to v4l2_subdev struct
 * @reg: TVP5146/47 register address
 */
312 313 314 315 316 317 318 319
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);
}

320 321 322 323 324 325
/**
 * 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
 *
326 327 328
 * Write a value to a register in an TVP5146/47 decoder device.
 * Returns zero if successful, or non-zero otherwise.
 */
329
static int tvp514x_write_reg(struct v4l2_subdev *sd, u8 reg, u8 val)
330
{
331 332 333
	int err, retry = 0;
	struct i2c_client *client = v4l2_get_subdevdata(sd);

334 335 336 337 338
write_again:

	err = i2c_smbus_write_byte_data(client, reg, val);
	if (err) {
		if (retry <= I2C_RETRY_COUNT) {
339
			v4l2_warn(sd, "Write: retry ... %d\n", retry);
340 341 342 343 344 345 346 347 348
			retry++;
			msleep_interruptible(10);
			goto write_again;
		}
	}

	return err;
}

349 350 351 352 353 354
/**
 * 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:-
355 356 357 358 359 360
 *		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.
 */
361
static int tvp514x_write_regs(struct v4l2_subdev *sd,
362 363 364 365 366 367 368 369 370 371 372 373 374 375
			      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;

376
		err = tvp514x_write_reg(sd, next->reg, (u8) next->val);
377
		if (err) {
378
			v4l2_err(sd, "Write failed. Err[%d]\n", err);
379 380 381 382 383 384
			return err;
		}
	}
	return 0;
}

385
/**
386
 * tvp514x_query_current_std() : Query the current standard detected by TVP5146/47
387 388
 * @sd: ptr to v4l2_subdev struct
 *
389
 * Returns the current standard detected by TVP5146/47, STD_INVALID if there is no
390
 * standard detected.
391
 */
392
static enum tvp514x_std tvp514x_query_current_std(struct v4l2_subdev *sd)
393 394 395
{
	u8 std, std_status;

396 397
	std = tvp514x_read_reg(sd, REG_VIDEO_STD);
	if ((std & VIDEO_STD_MASK) == VIDEO_STD_AUTO_SWITCH_BIT)
398
		/* use the standard status register */
399 400
		std_status = tvp514x_read_reg(sd, REG_VIDEO_STD_STATUS);
	else
401 402
		/* use the standard register itself */
		std_status = std;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

	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;
}

418
/* TVP5146/47 register dump function */
419
static void tvp514x_reg_dump(struct v4l2_subdev *sd)
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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	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);
463 464
}

465 466 467 468 469
/**
 * tvp514x_configure() - Configure the TVP5146/47 registers
 * @sd: ptr to v4l2_subdev struct
 * @decoder: ptr to tvp514x_decoder structure
 *
470 471
 * Returns zero if successful, or non-zero otherwise.
 */
472 473
static int tvp514x_configure(struct v4l2_subdev *sd,
		struct tvp514x_decoder *decoder)
474 475 476 477 478
{
	int err;

	/* common register initialization */
	err =
479
	    tvp514x_write_regs(sd, decoder->tvp514x_regs);
480 481 482 483
	if (err)
		return err;

	if (debug)
484
		tvp514x_reg_dump(sd);
485 486 487 488

	return 0;
}

489 490 491 492 493
/**
 * 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
 *
494 495 496 497 498 499
 * 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.
 */
500 501
static int tvp514x_detect(struct v4l2_subdev *sd,
		struct tvp514x_decoder *decoder)
502 503
{
	u8 chip_id_msb, chip_id_lsb, rom_ver;
504
	struct i2c_client *client = v4l2_get_subdevdata(sd);
505

506 507 508
	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);
509

510
	v4l2_dbg(1, debug, sd,
511 512 513 514 515 516 517 518
		 "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.
		 */
519 520
		v4l2_err(sd, "chip id mismatch msb:0x%x lsb:0x%x\n",
				chip_id_msb, chip_id_lsb);
521 522 523 524 525
		return -ENODEV;
	}

	decoder->ver = rom_ver;

526 527 528
	v4l2_info(sd, "%s (Version - 0x%.2x) found at 0x%x (%s)\n",
			client->name, decoder->ver,
			client->addr << 1, client->adapter->name);
529 530 531
	return 0;
}

532 533
/**
 * tvp514x_querystd() - V4L2 decoder interface handler for querystd
534
 * @sd: pointer to standard V4L2 sub-device structure
535 536 537
 * @std_id: standard V4L2 std_id ioctl enum
 *
 * Returns the current standard detected by TVP5146/47. If no active input is
538
 * detected then *std_id is set to 0 and the function returns 0.
539
 */
540
static int tvp514x_querystd(struct v4l2_subdev *sd, v4l2_std_id *std_id)
541
{
542
	struct tvp514x_decoder *decoder = to_decoder(sd);
543 544 545 546 547 548 549
	enum tvp514x_std current_std;
	enum tvp514x_input input_sel;
	u8 sync_lock_status, lock_mask;

	if (std_id == NULL)
		return -EINVAL;

550 551 552 553 554 555
	/* To query the standard the TVP514x must power on the ADCs. */
	if (!decoder->streaming) {
		tvp514x_s_stream(sd, 1);
		msleep(LOCK_RETRY_DELAY);
	}

556 557
	/* query the current standard */
	current_std = tvp514x_query_current_std(sd);
H
Hans Verkuil 已提交
558 559
	if (current_std == STD_INVALID) {
		*std_id = V4L2_STD_UNKNOWN;
560
		return 0;
H
Hans Verkuil 已提交
561
	}
562

563
	input_sel = decoder->input;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

	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 */
601
	sync_lock_status = tvp514x_read_reg(sd, REG_STATUS1);
H
Hans Verkuil 已提交
602 603
	if (lock_mask != (sync_lock_status & lock_mask)) {
		*std_id = V4L2_STD_UNKNOWN;
604
		return 0;	/* No input detected */
H
Hans Verkuil 已提交
605
	}
606

H
Hans Verkuil 已提交
607
	*std_id &= decoder->std_list[current_std].standard.id;
608

609
	v4l2_dbg(1, debug, sd, "Current STD: %s\n",
610 611 612 613
			decoder->std_list[current_std].standard.name);
	return 0;
}

614 615
/**
 * tvp514x_s_std() - V4L2 decoder interface handler for s_std
616
 * @sd: pointer to standard V4L2 sub-device structure
617 618 619 620 621
 * @std_id: standard V4L2 v4l2_std_id ioctl enum
 *
 * If std_id is supported, sets the requested standard. Otherwise, returns
 * -EINVAL
 */
622
static int tvp514x_s_std(struct v4l2_subdev *sd, v4l2_std_id std_id)
623
{
624
	struct tvp514x_decoder *decoder = to_decoder(sd);
625 626 627
	int err, i;

	for (i = 0; i < decoder->num_stds; i++)
628
		if (std_id & decoder->std_list[i].standard.id)
629 630 631 632 633
			break;

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

634
	err = tvp514x_write_reg(sd, REG_VIDEO_STD,
635 636 637 638 639
				decoder->std_list[i].video_std);
	if (err)
		return err;

	decoder->current_std = i;
640 641
	decoder->tvp514x_regs[REG_VIDEO_STD].val =
		decoder->std_list[i].video_std;
642

643
	v4l2_dbg(1, debug, sd, "Standard set to: %s\n",
644 645 646 647
			decoder->std_list[i].standard.name);
	return 0;
}

648 649
/**
 * tvp514x_s_routing() - V4L2 decoder interface handler for s_routing
650
 * @sd: pointer to standard V4L2 sub-device structure
651 652 653
 * @input: input selector for routing the signal
 * @output: output selector for routing the signal
 * @config: config value. Not used
654 655 656 657 658
 *
 * 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.
 */
659 660
static int tvp514x_s_routing(struct v4l2_subdev *sd,
				u32 input, u32 output, u32 config)
661
{
662
	struct tvp514x_decoder *decoder = to_decoder(sd);
663 664 665 666
	int err;
	enum tvp514x_input input_sel;
	enum tvp514x_output output_sel;

667 668
	if ((input >= INPUT_INVALID) ||
			(output >= OUTPUT_INVALID))
669 670
		/* Index out of bound */
		return -EINVAL;
671

672 673
	input_sel = input;
	output_sel = output;
674

675
	err = tvp514x_write_reg(sd, REG_INPUT_SEL, input_sel);
676 677 678
	if (err)
		return err;

679
	output_sel |= tvp514x_read_reg(sd,
680
			REG_OUTPUT_FORMATTER1) & 0x7;
681
	err = tvp514x_write_reg(sd, REG_OUTPUT_FORMATTER1,
682 683 684 685
			output_sel);
	if (err)
		return err;

686 687
	decoder->tvp514x_regs[REG_INPUT_SEL].val = input_sel;
	decoder->tvp514x_regs[REG_OUTPUT_FORMATTER1].val = output_sel;
688 689
	decoder->input = input;
	decoder->output = output;
690

691
	v4l2_dbg(1, debug, sd, "Input set to: %d\n", input_sel);
692 693 694 695

	return 0;
}

696 697
/**
 * tvp514x_s_ctrl() - V4L2 decoder interface handler for s_ctrl
698
 * @ctrl: pointer to v4l2_ctrl structure
699 700 701 702
 *
 * If the requested control is supported, sets the control's current
 * value in HW. Otherwise, returns -EINVAL if the control is not supported.
 */
703
static int tvp514x_s_ctrl(struct v4l2_ctrl *ctrl)
704
{
705
	struct v4l2_subdev *sd = to_sd(ctrl);
706
	struct tvp514x_decoder *decoder = to_decoder(sd);
707 708
	int err = -EINVAL, value;

709
	value = ctrl->val;
710 711 712

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
713 714 715
		err = tvp514x_write_reg(sd, REG_BRIGHTNESS, value);
		if (!err)
			decoder->tvp514x_regs[REG_BRIGHTNESS].val = value;
716 717
		break;
	case V4L2_CID_CONTRAST:
718
		err = tvp514x_write_reg(sd, REG_CONTRAST, value);
719 720
		if (!err)
			decoder->tvp514x_regs[REG_CONTRAST].val = value;
721 722
		break;
	case V4L2_CID_SATURATION:
723
		err = tvp514x_write_reg(sd, REG_SATURATION, value);
724 725
		if (!err)
			decoder->tvp514x_regs[REG_SATURATION].val = value;
726 727 728 729 730 731
		break;
	case V4L2_CID_HUE:
		if (value == 180)
			value = 0x7F;
		else if (value == -180)
			value = 0x80;
732
		err = tvp514x_write_reg(sd, REG_HUE, value);
733 734
		if (!err)
			decoder->tvp514x_regs[REG_HUE].val = value;
735 736
		break;
	case V4L2_CID_AUTOGAIN:
737 738 739
		err = tvp514x_write_reg(sd, REG_AFE_GAIN_CTRL, value ? 0x0f : 0x0c);
		if (!err)
			decoder->tvp514x_regs[REG_AFE_GAIN_CTRL].val = value;
740 741 742
		break;
	}

743
	v4l2_dbg(1, debug, sd, "Set Control: ID - %d - %d\n",
744
			ctrl->id, ctrl->val);
745 746 747
	return err;
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
/**
 * tvp514x_enum_mbus_fmt() - V4L2 decoder interface handler for enum_mbus_fmt
 * @sd: pointer to standard V4L2 sub-device structure
 * @index: index of pixelcode to retrieve
 * @code: receives the pixelcode
 *
 * Enumerates supported mediabus formats
 */
static int
tvp514x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
					enum v4l2_mbus_pixelcode *code)
{
	if (index)
		return -EINVAL;

	*code = V4L2_MBUS_FMT_YUYV10_2X10;
	return 0;
}

/**
768
 * tvp514x_mbus_fmt() - V4L2 decoder interface handler for try/s/g_mbus_fmt
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
 * @sd: pointer to standard V4L2 sub-device structure
 * @f: pointer to the mediabus format structure
 *
 * Negotiates the image capture size and mediabus format.
 */
static int
tvp514x_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *f)
{
	struct tvp514x_decoder *decoder = to_decoder(sd);
	enum tvp514x_std current_std;

	if (f == NULL)
		return -EINVAL;

	/* Calculate height and width based on current standard */
	current_std = decoder->current_std;

786
	f->code = V4L2_MBUS_FMT_YUYV8_2X8;
787 788 789 790 791 792 793 794 795
	f->width = decoder->std_list[current_std].width;
	f->height = decoder->std_list[current_std].height;
	f->field = V4L2_FIELD_INTERLACED;
	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
	v4l2_dbg(1, debug, sd, "MBUS_FMT: Width - %d, Height - %d\n",
			f->width, f->height);
	return 0;
}

796 797
/**
 * tvp514x_g_parm() - V4L2 decoder interface handler for g_parm
798
 * @sd: pointer to standard V4L2 sub-device structure
799 800 801 802 803
 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
 *
 * Returns the decoder's video CAPTURE parameters.
 */
static int
804
tvp514x_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
805
{
806
	struct tvp514x_decoder *decoder = to_decoder(sd);
807 808 809 810 811 812 813
	struct v4l2_captureparm *cparm;
	enum tvp514x_std current_std;

	if (a == NULL)
		return -EINVAL;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
814 815
		/* only capture is supported */
		return -EINVAL;
816 817

	/* get the current standard */
818
	current_std = decoder->current_std;
819 820 821 822 823 824 825 826 827

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

	return 0;
}

828 829
/**
 * tvp514x_s_parm() - V4L2 decoder interface handler for s_parm
830
 * @sd: pointer to standard V4L2 sub-device structure
831 832 833 834 835 836
 * @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
837
tvp514x_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *a)
838
{
839
	struct tvp514x_decoder *decoder = to_decoder(sd);
840 841 842 843 844 845 846
	struct v4l2_fract *timeperframe;
	enum tvp514x_std current_std;

	if (a == NULL)
		return -EINVAL;

	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
847 848
		/* only capture is supported */
		return -EINVAL;
849 850 851 852

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

	/* get the current standard */
853
	current_std = decoder->current_std;
854 855 856 857 858 859 860

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

	return 0;
}

861 862
/**
 * tvp514x_s_stream() - V4L2 decoder i/f handler for s_stream
863 864
 * @sd: pointer to standard V4L2 sub-device structure
 * @enable: streaming enable or disable
865
 *
866
 * Sets streaming to enable or disable, if possible.
867
 */
868
static int tvp514x_s_stream(struct v4l2_subdev *sd, int enable)
869 870
{
	int err = 0;
871
	struct tvp514x_decoder *decoder = to_decoder(sd);
872

873 874
	if (decoder->streaming == enable)
		return 0;
875

876 877 878 879 880 881 882 883 884 885
	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;
886
		break;
887 888 889 890
	}
	case 1:
	{
		/* Power Up Sequence */
891
		err = tvp514x_write_regs(sd, decoder->int_seq);
892 893 894 895 896 897 898 899 900
		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;
901
		}
902 903 904 905 906 907
		err = tvp514x_configure(sd, decoder);
		if (err) {
			v4l2_err(sd, "Unable to configure decoder\n");
			return err;
		}
		decoder->streaming = enable;
908
		break;
909
	}
910 911 912 913 914 915 916 917
	default:
		err = -ENODEV;
		break;
	}

	return err;
}

918
static const struct v4l2_ctrl_ops tvp514x_ctrl_ops = {
919
	.s_ctrl = tvp514x_s_ctrl,
920 921
};

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
/**
 * tvp514x_enum_mbus_code() - V4L2 decoder interface handler for enum_mbus_code
 * @sd: pointer to standard V4L2 sub-device structure
 * @fh: file handle
 * @code: pointer to v4l2_subdev_mbus_code_enum structure
 *
 * Enumertaes mbus codes supported
 */
static int tvp514x_enum_mbus_code(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_mbus_code_enum *code)
{
	u32 pad = code->pad;
	u32 index = code->index;

	memset(code, 0, sizeof(*code));
	code->index = index;
	code->pad = pad;

	if (index != 0)
		return -EINVAL;

	code->code = V4L2_MBUS_FMT_YUYV8_2X8;

	return 0;
}

/**
 * tvp514x_get_pad_format() - V4L2 decoder interface handler for get pad format
 * @sd: pointer to standard V4L2 sub-device structure
 * @fh: file handle
 * @format: pointer to v4l2_subdev_format structure
 *
 * Retrieves pad format which is active or tried based on requirement
 */
static int tvp514x_get_pad_format(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_format *format)
{
	struct tvp514x_decoder *decoder = to_decoder(sd);
	__u32 which = format->which;

	if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		format->format = decoder->format;
		return 0;
	}

	format->format.code = V4L2_MBUS_FMT_YUYV8_2X8;
	format->format.width = tvp514x_std_list[decoder->current_std].width;
	format->format.height = tvp514x_std_list[decoder->current_std].height;
	format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
	format->format.field = V4L2_FIELD_INTERLACED;

	return 0;
}

/**
 * tvp514x_set_pad_format() - V4L2 decoder interface handler for set pad format
 * @sd: pointer to standard V4L2 sub-device structure
 * @fh: file handle
 * @format: pointer to v4l2_subdev_format structure
 *
 * Set pad format for the output pad
 */
static int tvp514x_set_pad_format(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_format *fmt)
{
	struct tvp514x_decoder *decoder = to_decoder(sd);

	if (fmt->format.field != V4L2_FIELD_INTERLACED ||
	    fmt->format.code != V4L2_MBUS_FMT_YUYV8_2X8 ||
	    fmt->format.colorspace != V4L2_COLORSPACE_SMPTE170M ||
	    fmt->format.width != tvp514x_std_list[decoder->current_std].width ||
	    fmt->format.height != tvp514x_std_list[decoder->current_std].height)
		return -EINVAL;

	decoder->format = fmt->format;

	return 0;
}

1004 1005 1006 1007 1008 1009 1010 1011
static const struct v4l2_subdev_core_ops tvp514x_core_ops = {
	.g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
	.try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
	.s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
	.g_ctrl = v4l2_subdev_g_ctrl,
	.s_ctrl = v4l2_subdev_s_ctrl,
	.queryctrl = v4l2_subdev_queryctrl,
	.querymenu = v4l2_subdev_querymenu,
1012 1013
	.s_std = tvp514x_s_std,
};
1014

1015 1016 1017
static const struct v4l2_subdev_video_ops tvp514x_video_ops = {
	.s_routing = tvp514x_s_routing,
	.querystd = tvp514x_querystd,
1018 1019 1020 1021
	.enum_mbus_fmt = tvp514x_enum_mbus_fmt,
	.g_mbus_fmt = tvp514x_mbus_fmt,
	.try_mbus_fmt = tvp514x_mbus_fmt,
	.s_mbus_fmt = tvp514x_mbus_fmt,
1022 1023 1024 1025
	.g_parm = tvp514x_g_parm,
	.s_parm = tvp514x_s_parm,
	.s_stream = tvp514x_s_stream,
};
1026

1027 1028 1029 1030 1031 1032
static const struct v4l2_subdev_pad_ops tvp514x_pad_ops = {
	.enum_mbus_code = tvp514x_enum_mbus_code,
	.get_fmt = tvp514x_get_pad_format,
	.set_fmt = tvp514x_set_pad_format,
};

1033 1034 1035
static const struct v4l2_subdev_ops tvp514x_ops = {
	.core = &tvp514x_core_ops,
	.video = &tvp514x_video_ops,
1036
	.pad = &tvp514x_pad_ops,
1037 1038 1039
};

static struct tvp514x_decoder tvp514x_dev = {
1040
	.streaming = 0,
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	.fmt_list = tvp514x_fmt_list,
	.num_fmts = ARRAY_SIZE(tvp514x_fmt_list),
	.pix = {
		/* Default to NTSC 8-bit YUV 422 */
		.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,
		},
1054 1055 1056
	.current_std = STD_NTSC_MJ,
	.std_list = tvp514x_std_list,
	.num_stds = ARRAY_SIZE(tvp514x_std_list),
1057

1058 1059
};

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static struct tvp514x_platform_data *
tvp514x_get_pdata(struct i2c_client *client)
{
	struct tvp514x_platform_data *pdata;
	struct v4l2_of_endpoint bus_cfg;
	struct device_node *endpoint;
	unsigned int flags;

	if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
		return client->dev.platform_data;

	endpoint = v4l2_of_get_next_endpoint(client->dev.of_node, NULL);
	if (!endpoint)
		return NULL;

	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		goto done;

	v4l2_of_parse_endpoint(endpoint, &bus_cfg);
	flags = bus_cfg.bus.parallel.flags;

	if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
		pdata->hs_polarity = 1;

	if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
		pdata->vs_polarity = 1;

	if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
		pdata->clk_polarity = 1;

done:
	of_node_put(endpoint);
	return pdata;
}

1096 1097
/**
 * tvp514x_probe() - decoder driver i2c probe handler
1098
 * @client: i2c driver client device structure
1099
 * @id: i2c driver id table
1100 1101 1102 1103 1104 1105 1106
 *
 * Register decoder as an i2c client device and V4L2
 * device.
 */
static int
tvp514x_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
1107
	struct tvp514x_platform_data *pdata = tvp514x_get_pdata(client);
1108
	struct tvp514x_decoder *decoder;
1109
	struct v4l2_subdev *sd;
1110
	int ret;
1111

1112 1113 1114 1115 1116
	if (pdata == NULL) {
		dev_err(&client->dev, "No platform data\n");
		return -EINVAL;
	}

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

1121
	decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
1122 1123 1124
	if (!decoder)
		return -ENOMEM;

1125
	/* Initialize the tvp514x_decoder with default configuration */
1126
	*decoder = tvp514x_dev;
1127
	/* Copy default register configuration */
1128 1129
	memcpy(decoder->tvp514x_regs, tvp514x_reg_list_default,
			sizeof(tvp514x_reg_list_default));
1130

1131 1132
	decoder->int_seq = (struct tvp514x_reg *)id->driver_data;

1133
	/* Copy board specific information here */
1134
	decoder->pdata = pdata;
1135

1136
	/**
1137 1138 1139 1140
	 * Fetch platform specific data, and configure the
	 * tvp514x_reg_list[] accordingly. Since this is one
	 * time configuration, no need to preserve.
	 */
1141
	decoder->tvp514x_regs[REG_OUTPUT_FORMATTER2].val |=
1142
		(decoder->pdata->clk_polarity << 1);
1143
	decoder->tvp514x_regs[REG_SYNC_CONTROL].val |=
1144 1145 1146 1147 1148
		((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;
1149 1150

	/* Register with V4L2 layer as slave device */
1151 1152
	sd = &decoder->sd;
	v4l2_i2c_subdev_init(sd, client, &tvp514x_ops);
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

#if defined(CONFIG_MEDIA_CONTROLLER)
	decoder->pad.flags = MEDIA_PAD_FL_SOURCE;
	decoder->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
	decoder->sd.entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;

	ret = media_entity_init(&decoder->sd.entity, 1, &decoder->pad, 0);
	if (ret < 0) {
		v4l2_err(sd, "%s decoder driver failed to register !!\n",
			 sd->name);
		return ret;
	}
#endif
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	v4l2_ctrl_handler_init(&decoder->hdl, 5);
	v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
		V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
	v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
		V4L2_CID_CONTRAST, 0, 255, 1, 128);
	v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
		V4L2_CID_SATURATION, 0, 255, 1, 128);
	v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
		V4L2_CID_HUE, -180, 180, 180, 0);
	v4l2_ctrl_new_std(&decoder->hdl, &tvp514x_ctrl_ops,
		V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
	sd->ctrl_handler = &decoder->hdl;
	if (decoder->hdl.error) {
1179
		ret = decoder->hdl.error;
1180
		goto done;
1181 1182 1183
	}
	v4l2_ctrl_handler_setup(&decoder->hdl);

1184 1185 1186
	ret = v4l2_async_register_subdev(&decoder->sd);
	if (!ret)
		v4l2_info(sd, "%s decoder driver registered !!\n", sd->name);
1187

1188 1189 1190 1191 1192 1193 1194 1195
done:
	if (ret < 0) {
		v4l2_ctrl_handler_free(&decoder->hdl);
#if defined(CONFIG_MEDIA_CONTROLLER)
		media_entity_cleanup(&decoder->sd.entity);
#endif
	}
	return ret;
1196 1197
}

1198 1199
/**
 * tvp514x_remove() - decoder driver i2c remove handler
1200 1201 1202 1203 1204
 * @client: i2c driver client device structure
 *
 * Unregister decoder as an i2c client device and V4L2
 * device. Complement of tvp514x_probe().
 */
1205
static int tvp514x_remove(struct i2c_client *client)
1206
{
1207 1208
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct tvp514x_decoder *decoder = to_decoder(sd);
1209

1210
	v4l2_async_unregister_subdev(&decoder->sd);
1211
	v4l2_device_unregister_subdev(sd);
1212 1213 1214
#if defined(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&decoder->sd.entity);
#endif
1215
	v4l2_ctrl_handler_free(&decoder->hdl);
1216 1217
	return 0;
}
1218
/* TVP5146 Init/Power on Sequence */
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
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},
1231
	{TOK_TERM, 0, 0},
1232
};
1233

1234
/* TVP5147 Init/Power on Sequence */
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
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},
1254
	{TOK_TERM, 0, 0},
1255
};
1256

1257
/* TVP5146M2/TVP5147M1 Init/Power on Sequence */
1258 1259 1260
static const struct tvp514x_reg tvp514xm_init_reg_seq[] = {
	{TOK_WRITE, REG_OPERATION_MODE, 0x01},
	{TOK_WRITE, REG_OPERATION_MODE, 0x00},
1261
	{TOK_TERM, 0, 0},
1262
};
1263

1264
/**
1265 1266 1267 1268 1269 1270
 * I2C Device Table -
 *
 * name - Name of the actual device/chip.
 * driver_data - Driver data
 */
static const struct i2c_device_id tvp514x_id[] = {
1271 1272 1273 1274
	{"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},
1275 1276 1277 1278 1279
	{},
};

MODULE_DEVICE_TABLE(i2c, tvp514x_id);

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id tvp514x_of_match[] = {
	{ .compatible = "ti,tvp5146", },
	{ .compatible = "ti,tvp5146m2", },
	{ .compatible = "ti,tvp5147", },
	{ .compatible = "ti,tvp5147m1", },
	{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, tvp514x_of_match);
#endif

1291
static struct i2c_driver tvp514x_driver = {
1292
	.driver = {
1293
		.of_match_table = of_match_ptr(tvp514x_of_match),
1294 1295 1296
		.owner = THIS_MODULE,
		.name = TVP514X_MODULE_NAME,
	},
1297
	.probe = tvp514x_probe,
1298
	.remove = tvp514x_remove,
1299 1300 1301
	.id_table = tvp514x_id,
};

1302
module_i2c_driver(tvp514x_driver);