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