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