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