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