xref: /openbmc/linux/drivers/media/i2c/tvp5150.c (revision b802fb99ae964681d1754428f67970911e0476e9)
1 /*
2  * tvp5150 - Texas Instruments TVP5150A/AM1 video decoder driver
3  *
4  * Copyright (c) 2005,2006 Mauro Carvalho Chehab (mchehab@infradead.org)
5  * This code is placed under the terms of the GNU General Public License v2
6  */
7 
8 #include <dt-bindings/media/tvp5150.h>
9 #include <linux/i2c.h>
10 #include <linux/slab.h>
11 #include <linux/videodev2.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/module.h>
15 #include <media/v4l2-async.h>
16 #include <media/v4l2-device.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-of.h>
19 #include <media/v4l2-mc.h>
20 
21 #include "tvp5150_reg.h"
22 
23 #define TVP5150_H_MAX		720U
24 #define TVP5150_V_MAX_525_60	480U
25 #define TVP5150_V_MAX_OTHERS	576U
26 #define TVP5150_MAX_CROP_LEFT	511
27 #define TVP5150_MAX_CROP_TOP	127
28 #define TVP5150_CROP_SHIFT	2
29 
30 MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
31 MODULE_AUTHOR("Mauro Carvalho Chehab");
32 MODULE_LICENSE("GPL");
33 
34 
35 static int debug;
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Debug level (0-2)");
38 
39 struct tvp5150 {
40 	struct v4l2_subdev sd;
41 #ifdef CONFIG_MEDIA_CONTROLLER
42 	struct media_pad pads[DEMOD_NUM_PADS];
43 #endif
44 	struct v4l2_ctrl_handler hdl;
45 	struct v4l2_rect rect;
46 
47 	v4l2_std_id norm;	/* Current set standard */
48 	u32 input;
49 	u32 output;
50 	int enable;
51 
52 	u16 dev_id;
53 	u16 rom_ver;
54 
55 	enum v4l2_mbus_type mbus_type;
56 };
57 
58 static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
59 {
60 	return container_of(sd, struct tvp5150, sd);
61 }
62 
63 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
64 {
65 	return &container_of(ctrl->handler, struct tvp5150, hdl)->sd;
66 }
67 
68 static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
69 {
70 	struct i2c_client *c = v4l2_get_subdevdata(sd);
71 	int rc;
72 
73 	rc = i2c_smbus_read_byte_data(c, addr);
74 	if (rc < 0) {
75 		v4l2_err(sd, "i2c i/o error: rc == %d\n", rc);
76 		return rc;
77 	}
78 
79 	v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, rc);
80 
81 	return rc;
82 }
83 
84 static inline void tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
85 				 unsigned char value)
86 {
87 	struct i2c_client *c = v4l2_get_subdevdata(sd);
88 	int rc;
89 
90 	v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", addr, value);
91 	rc = i2c_smbus_write_byte_data(c, addr, value);
92 	if (rc < 0)
93 		v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d\n", rc);
94 }
95 
96 static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
97 				const u8 end, int max_line)
98 {
99 	int i = 0;
100 
101 	while (init != (u8)(end + 1)) {
102 		if ((i % max_line) == 0) {
103 			if (i > 0)
104 				printk("\n");
105 			printk("tvp5150: %s reg 0x%02x = ", s, init);
106 		}
107 		printk("%02x ", tvp5150_read(sd, init));
108 
109 		init++;
110 		i++;
111 	}
112 	printk("\n");
113 }
114 
115 static int tvp5150_log_status(struct v4l2_subdev *sd)
116 {
117 	printk("tvp5150: Video input source selection #1 = 0x%02x\n",
118 			tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
119 	printk("tvp5150: Analog channel controls = 0x%02x\n",
120 			tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
121 	printk("tvp5150: Operation mode controls = 0x%02x\n",
122 			tvp5150_read(sd, TVP5150_OP_MODE_CTL));
123 	printk("tvp5150: Miscellaneous controls = 0x%02x\n",
124 			tvp5150_read(sd, TVP5150_MISC_CTL));
125 	printk("tvp5150: Autoswitch mask= 0x%02x\n",
126 			tvp5150_read(sd, TVP5150_AUTOSW_MSK));
127 	printk("tvp5150: Color killer threshold control = 0x%02x\n",
128 			tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
129 	printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
130 			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
131 			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
132 			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
133 	printk("tvp5150: Brightness control = 0x%02x\n",
134 			tvp5150_read(sd, TVP5150_BRIGHT_CTL));
135 	printk("tvp5150: Color saturation control = 0x%02x\n",
136 			tvp5150_read(sd, TVP5150_SATURATION_CTL));
137 	printk("tvp5150: Hue control = 0x%02x\n",
138 			tvp5150_read(sd, TVP5150_HUE_CTL));
139 	printk("tvp5150: Contrast control = 0x%02x\n",
140 			tvp5150_read(sd, TVP5150_CONTRAST_CTL));
141 	printk("tvp5150: Outputs and data rates select = 0x%02x\n",
142 			tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
143 	printk("tvp5150: Configuration shared pins = 0x%02x\n",
144 			tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
145 	printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
146 			tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
147 			tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
148 	printk("tvp5150: Active video cropping stop  = 0x%02x%02x\n",
149 			tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
150 			tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
151 	printk("tvp5150: Genlock/RTC = 0x%02x\n",
152 			tvp5150_read(sd, TVP5150_GENLOCK));
153 	printk("tvp5150: Horizontal sync start = 0x%02x\n",
154 			tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
155 	printk("tvp5150: Vertical blanking start = 0x%02x\n",
156 			tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
157 	printk("tvp5150: Vertical blanking stop = 0x%02x\n",
158 			tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
159 	printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
160 			tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
161 			tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
162 	printk("tvp5150: Interrupt reset register B = 0x%02x\n",
163 			tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
164 	printk("tvp5150: Interrupt enable register B = 0x%02x\n",
165 			tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
166 	printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
167 			tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
168 	printk("tvp5150: Video standard = 0x%02x\n",
169 			tvp5150_read(sd, TVP5150_VIDEO_STD));
170 	printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
171 			tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
172 			tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
173 	printk("tvp5150: Macrovision on counter = 0x%02x\n",
174 			tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
175 	printk("tvp5150: Macrovision off counter = 0x%02x\n",
176 			tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
177 	printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
178 			(tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
179 	printk("tvp5150: Device ID = %02x%02x\n",
180 			tvp5150_read(sd, TVP5150_MSB_DEV_ID),
181 			tvp5150_read(sd, TVP5150_LSB_DEV_ID));
182 	printk("tvp5150: ROM version = (hex) %02x.%02x\n",
183 			tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
184 			tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
185 	printk("tvp5150: Vertical line count = 0x%02x%02x\n",
186 			tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
187 			tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
188 	printk("tvp5150: Interrupt status register B = 0x%02x\n",
189 			tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
190 	printk("tvp5150: Interrupt active register B = 0x%02x\n",
191 			tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
192 	printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
193 			tvp5150_read(sd, TVP5150_STATUS_REG_1),
194 			tvp5150_read(sd, TVP5150_STATUS_REG_2),
195 			tvp5150_read(sd, TVP5150_STATUS_REG_3),
196 			tvp5150_read(sd, TVP5150_STATUS_REG_4),
197 			tvp5150_read(sd, TVP5150_STATUS_REG_5));
198 
199 	dump_reg_range(sd, "Teletext filter 1",   TVP5150_TELETEXT_FIL1_INI,
200 			TVP5150_TELETEXT_FIL1_END, 8);
201 	dump_reg_range(sd, "Teletext filter 2",   TVP5150_TELETEXT_FIL2_INI,
202 			TVP5150_TELETEXT_FIL2_END, 8);
203 
204 	printk("tvp5150: Teletext filter enable = 0x%02x\n",
205 			tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
206 	printk("tvp5150: Interrupt status register A = 0x%02x\n",
207 			tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
208 	printk("tvp5150: Interrupt enable register A = 0x%02x\n",
209 			tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
210 	printk("tvp5150: Interrupt configuration = 0x%02x\n",
211 			tvp5150_read(sd, TVP5150_INT_CONF));
212 	printk("tvp5150: VDP status register = 0x%02x\n",
213 			tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
214 	printk("tvp5150: FIFO word count = 0x%02x\n",
215 			tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
216 	printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
217 			tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
218 	printk("tvp5150: FIFO reset = 0x%02x\n",
219 			tvp5150_read(sd, TVP5150_FIFO_RESET));
220 	printk("tvp5150: Line number interrupt = 0x%02x\n",
221 			tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
222 	printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
223 			tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
224 			tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
225 	printk("tvp5150: FIFO output control = 0x%02x\n",
226 			tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
227 	printk("tvp5150: Full field enable = 0x%02x\n",
228 			tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
229 	printk("tvp5150: Full field mode register = 0x%02x\n",
230 			tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
231 
232 	dump_reg_range(sd, "CC   data",   TVP5150_CC_DATA_INI,
233 			TVP5150_CC_DATA_END, 8);
234 
235 	dump_reg_range(sd, "WSS  data",   TVP5150_WSS_DATA_INI,
236 			TVP5150_WSS_DATA_END, 8);
237 
238 	dump_reg_range(sd, "VPS  data",   TVP5150_VPS_DATA_INI,
239 			TVP5150_VPS_DATA_END, 8);
240 
241 	dump_reg_range(sd, "VITC data",   TVP5150_VITC_DATA_INI,
242 			TVP5150_VITC_DATA_END, 10);
243 
244 	dump_reg_range(sd, "Line mode",   TVP5150_LINE_MODE_INI,
245 			TVP5150_LINE_MODE_END, 8);
246 	return 0;
247 }
248 
249 /****************************************************************************
250 			Basic functions
251  ****************************************************************************/
252 
253 static inline void tvp5150_selmux(struct v4l2_subdev *sd)
254 {
255 	int opmode = 0;
256 	struct tvp5150 *decoder = to_tvp5150(sd);
257 	int input = 0;
258 	int val;
259 
260 	if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
261 		input = 8;
262 
263 	switch (decoder->input) {
264 	case TVP5150_COMPOSITE1:
265 		input |= 2;
266 		/* fall through */
267 	case TVP5150_COMPOSITE0:
268 		break;
269 	case TVP5150_SVIDEO:
270 	default:
271 		input |= 1;
272 		break;
273 	}
274 
275 	v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
276 			"=> tvp5150 input=%i, opmode=%i\n",
277 			decoder->input, decoder->output,
278 			input, opmode);
279 
280 	tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
281 	tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
282 
283 	/* Svideo should enable YCrCb output and disable GPCL output
284 	 * For Composite and TV, it should be the reverse
285 	 */
286 	val = tvp5150_read(sd, TVP5150_MISC_CTL);
287 	if (val < 0) {
288 		v4l2_err(sd, "%s: failed with error = %d\n", __func__, val);
289 		return;
290 	}
291 
292 	if (decoder->input == TVP5150_SVIDEO)
293 		val = (val & ~0x40) | 0x10;
294 	else
295 		val = (val & ~0x10) | 0x40;
296 	tvp5150_write(sd, TVP5150_MISC_CTL, val);
297 };
298 
299 struct i2c_reg_value {
300 	unsigned char reg;
301 	unsigned char value;
302 };
303 
304 /* Default values as sugested at TVP5150AM1 datasheet */
305 static const struct i2c_reg_value tvp5150_init_default[] = {
306 	{ /* 0x00 */
307 		TVP5150_VD_IN_SRC_SEL_1,0x00
308 	},
309 	{ /* 0x01 */
310 		TVP5150_ANAL_CHL_CTL,0x15
311 	},
312 	{ /* 0x02 */
313 		TVP5150_OP_MODE_CTL,0x00
314 	},
315 	{ /* 0x03 */
316 		TVP5150_MISC_CTL,0x01
317 	},
318 	{ /* 0x06 */
319 		TVP5150_COLOR_KIL_THSH_CTL,0x10
320 	},
321 	{ /* 0x07 */
322 		TVP5150_LUMA_PROC_CTL_1,0x60
323 	},
324 	{ /* 0x08 */
325 		TVP5150_LUMA_PROC_CTL_2,0x00
326 	},
327 	{ /* 0x09 */
328 		TVP5150_BRIGHT_CTL,0x80
329 	},
330 	{ /* 0x0a */
331 		TVP5150_SATURATION_CTL,0x80
332 	},
333 	{ /* 0x0b */
334 		TVP5150_HUE_CTL,0x00
335 	},
336 	{ /* 0x0c */
337 		TVP5150_CONTRAST_CTL,0x80
338 	},
339 	{ /* 0x0d */
340 		TVP5150_DATA_RATE_SEL,0x47
341 	},
342 	{ /* 0x0e */
343 		TVP5150_LUMA_PROC_CTL_3,0x00
344 	},
345 	{ /* 0x0f */
346 		TVP5150_CONF_SHARED_PIN,0x08
347 	},
348 	{ /* 0x11 */
349 		TVP5150_ACT_VD_CROP_ST_MSB,0x00
350 	},
351 	{ /* 0x12 */
352 		TVP5150_ACT_VD_CROP_ST_LSB,0x00
353 	},
354 	{ /* 0x13 */
355 		TVP5150_ACT_VD_CROP_STP_MSB,0x00
356 	},
357 	{ /* 0x14 */
358 		TVP5150_ACT_VD_CROP_STP_LSB,0x00
359 	},
360 	{ /* 0x15 */
361 		TVP5150_GENLOCK,0x01
362 	},
363 	{ /* 0x16 */
364 		TVP5150_HORIZ_SYNC_START,0x80
365 	},
366 	{ /* 0x18 */
367 		TVP5150_VERT_BLANKING_START,0x00
368 	},
369 	{ /* 0x19 */
370 		TVP5150_VERT_BLANKING_STOP,0x00
371 	},
372 	{ /* 0x1a */
373 		TVP5150_CHROMA_PROC_CTL_1,0x0c
374 	},
375 	{ /* 0x1b */
376 		TVP5150_CHROMA_PROC_CTL_2,0x14
377 	},
378 	{ /* 0x1c */
379 		TVP5150_INT_RESET_REG_B,0x00
380 	},
381 	{ /* 0x1d */
382 		TVP5150_INT_ENABLE_REG_B,0x00
383 	},
384 	{ /* 0x1e */
385 		TVP5150_INTT_CONFIG_REG_B,0x00
386 	},
387 	{ /* 0x28 */
388 		TVP5150_VIDEO_STD,0x00
389 	},
390 	{ /* 0x2e */
391 		TVP5150_MACROVISION_ON_CTR,0x0f
392 	},
393 	{ /* 0x2f */
394 		TVP5150_MACROVISION_OFF_CTR,0x01
395 	},
396 	{ /* 0xbb */
397 		TVP5150_TELETEXT_FIL_ENA,0x00
398 	},
399 	{ /* 0xc0 */
400 		TVP5150_INT_STATUS_REG_A,0x00
401 	},
402 	{ /* 0xc1 */
403 		TVP5150_INT_ENABLE_REG_A,0x00
404 	},
405 	{ /* 0xc2 */
406 		TVP5150_INT_CONF,0x04
407 	},
408 	{ /* 0xc8 */
409 		TVP5150_FIFO_INT_THRESHOLD,0x80
410 	},
411 	{ /* 0xc9 */
412 		TVP5150_FIFO_RESET,0x00
413 	},
414 	{ /* 0xca */
415 		TVP5150_LINE_NUMBER_INT,0x00
416 	},
417 	{ /* 0xcb */
418 		TVP5150_PIX_ALIGN_REG_LOW,0x4e
419 	},
420 	{ /* 0xcc */
421 		TVP5150_PIX_ALIGN_REG_HIGH,0x00
422 	},
423 	{ /* 0xcd */
424 		TVP5150_FIFO_OUT_CTRL,0x01
425 	},
426 	{ /* 0xcf */
427 		TVP5150_FULL_FIELD_ENA,0x00
428 	},
429 	{ /* 0xd0 */
430 		TVP5150_LINE_MODE_INI,0x00
431 	},
432 	{ /* 0xfc */
433 		TVP5150_FULL_FIELD_MODE_REG,0x7f
434 	},
435 	{ /* end of data */
436 		0xff,0xff
437 	}
438 };
439 
440 /* Default values as sugested at TVP5150AM1 datasheet */
441 static const struct i2c_reg_value tvp5150_init_enable[] = {
442 	{
443 		TVP5150_CONF_SHARED_PIN, 2
444 	},{	/* Automatic offset and AGC enabled */
445 		TVP5150_ANAL_CHL_CTL, 0x15
446 	},{	/* Activate YCrCb output 0x9 or 0xd ? */
447 		TVP5150_MISC_CTL, 0x6f
448 	},{	/* Activates video std autodetection for all standards */
449 		TVP5150_AUTOSW_MSK, 0x0
450 	},{	/* Default format: 0x47. For 4:2:2: 0x40 */
451 		TVP5150_DATA_RATE_SEL, 0x47
452 	},{
453 		TVP5150_CHROMA_PROC_CTL_1, 0x0c
454 	},{
455 		TVP5150_CHROMA_PROC_CTL_2, 0x54
456 	},{	/* Non documented, but initialized on WinTV USB2 */
457 		0x27, 0x20
458 	},{
459 		0xff,0xff
460 	}
461 };
462 
463 struct tvp5150_vbi_type {
464 	unsigned int vbi_type;
465 	unsigned int ini_line;
466 	unsigned int end_line;
467 	unsigned int by_field :1;
468 };
469 
470 struct i2c_vbi_ram_value {
471 	u16 reg;
472 	struct tvp5150_vbi_type type;
473 	unsigned char values[16];
474 };
475 
476 /* This struct have the values for each supported VBI Standard
477  * by
478  tvp5150_vbi_types should follow the same order as vbi_ram_default
479  * value 0 means rom position 0x10, value 1 means rom position 0x30
480  * and so on. There are 16 possible locations from 0 to 15.
481  */
482 
483 static struct i2c_vbi_ram_value vbi_ram_default[] =
484 {
485 	/* FIXME: Current api doesn't handle all VBI types, those not
486 	   yet supported are placed under #if 0 */
487 #if 0
488 	{0x010, /* Teletext, SECAM, WST System A */
489 		{V4L2_SLICED_TELETEXT_SECAM,6,23,1},
490 		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
491 		  0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
492 	},
493 #endif
494 	{0x030, /* Teletext, PAL, WST System B */
495 		{V4L2_SLICED_TELETEXT_B,6,22,1},
496 		{ 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
497 		  0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
498 	},
499 #if 0
500 	{0x050, /* Teletext, PAL, WST System C */
501 		{V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
502 		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
503 		  0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
504 	},
505 	{0x070, /* Teletext, NTSC, WST System B */
506 		{V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
507 		{ 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
508 		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
509 	},
510 	{0x090, /* Tetetext, NTSC NABTS System C */
511 		{V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
512 		{ 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
513 		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
514 	},
515 	{0x0b0, /* Teletext, NTSC-J, NABTS System D */
516 		{V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
517 		{ 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
518 		  0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
519 	},
520 	{0x0d0, /* Closed Caption, PAL/SECAM */
521 		{V4L2_SLICED_CAPTION_625,22,22,1},
522 		{ 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
523 		  0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
524 	},
525 #endif
526 	{0x0f0, /* Closed Caption, NTSC */
527 		{V4L2_SLICED_CAPTION_525,21,21,1},
528 		{ 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
529 		  0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
530 	},
531 	{0x110, /* Wide Screen Signal, PAL/SECAM */
532 		{V4L2_SLICED_WSS_625,23,23,1},
533 		{ 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
534 		  0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
535 	},
536 #if 0
537 	{0x130, /* Wide Screen Signal, NTSC C */
538 		{V4L2_SLICED_WSS_525,20,20,1},
539 		{ 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
540 		  0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
541 	},
542 	{0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
543 		{V4l2_SLICED_VITC_625,6,22,0},
544 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
545 		  0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
546 	},
547 	{0x170, /* Vertical Interval Timecode (VITC), NTSC */
548 		{V4l2_SLICED_VITC_525,10,20,0},
549 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
550 		  0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
551 	},
552 #endif
553 	{0x190, /* Video Program System (VPS), PAL */
554 		{V4L2_SLICED_VPS,16,16,0},
555 		{ 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
556 		  0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
557 	},
558 	/* 0x1d0 User programmable */
559 
560 	/* End of struct */
561 	{ (u16)-1 }
562 };
563 
564 static int tvp5150_write_inittab(struct v4l2_subdev *sd,
565 				const struct i2c_reg_value *regs)
566 {
567 	while (regs->reg != 0xff) {
568 		tvp5150_write(sd, regs->reg, regs->value);
569 		regs++;
570 	}
571 	return 0;
572 }
573 
574 static int tvp5150_vdp_init(struct v4l2_subdev *sd,
575 				const struct i2c_vbi_ram_value *regs)
576 {
577 	unsigned int i;
578 
579 	/* Disable Full Field */
580 	tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
581 
582 	/* Before programming, Line mode should be at 0xff */
583 	for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
584 		tvp5150_write(sd, i, 0xff);
585 
586 	/* Load Ram Table */
587 	while (regs->reg != (u16)-1) {
588 		tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
589 		tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
590 
591 		for (i = 0; i < 16; i++)
592 			tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
593 
594 		regs++;
595 	}
596 	return 0;
597 }
598 
599 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
600 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
601 				struct v4l2_sliced_vbi_cap *cap)
602 {
603 	const struct i2c_vbi_ram_value *regs = vbi_ram_default;
604 	int line;
605 
606 	v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
607 	memset(cap, 0, sizeof *cap);
608 
609 	while (regs->reg != (u16)-1 ) {
610 		for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
611 			cap->service_lines[0][line] |= regs->type.vbi_type;
612 		}
613 		cap->service_set |= regs->type.vbi_type;
614 
615 		regs++;
616 	}
617 	return 0;
618 }
619 
620 /* Set vbi processing
621  * type - one of tvp5150_vbi_types
622  * line - line to gather data
623  * fields: bit 0 field1, bit 1, field2
624  * flags (default=0xf0) is a bitmask, were set means:
625  *	bit 7: enable filtering null bytes on CC
626  *	bit 6: send data also to FIFO
627  *	bit 5: don't allow data with errors on FIFO
628  *	bit 4: enable ECC when possible
629  * pix_align = pix alignment:
630  *	LSB = field1
631  *	MSB = field2
632  */
633 static int tvp5150_set_vbi(struct v4l2_subdev *sd,
634 			const struct i2c_vbi_ram_value *regs,
635 			unsigned int type,u8 flags, int line,
636 			const int fields)
637 {
638 	struct tvp5150 *decoder = to_tvp5150(sd);
639 	v4l2_std_id std = decoder->norm;
640 	u8 reg;
641 	int pos=0;
642 
643 	if (std == V4L2_STD_ALL) {
644 		v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
645 		return 0;
646 	} else if (std & V4L2_STD_625_50) {
647 		/* Don't follow NTSC Line number convension */
648 		line += 3;
649 	}
650 
651 	if (line<6||line>27)
652 		return 0;
653 
654 	while (regs->reg != (u16)-1 ) {
655 		if ((type & regs->type.vbi_type) &&
656 		    (line>=regs->type.ini_line) &&
657 		    (line<=regs->type.end_line)) {
658 			type=regs->type.vbi_type;
659 			break;
660 		}
661 
662 		regs++;
663 		pos++;
664 	}
665 	if (regs->reg == (u16)-1)
666 		return 0;
667 
668 	type=pos | (flags & 0xf0);
669 	reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
670 
671 	if (fields&1) {
672 		tvp5150_write(sd, reg, type);
673 	}
674 
675 	if (fields&2) {
676 		tvp5150_write(sd, reg+1, type);
677 	}
678 
679 	return type;
680 }
681 
682 static int tvp5150_get_vbi(struct v4l2_subdev *sd,
683 			const struct i2c_vbi_ram_value *regs, int line)
684 {
685 	struct tvp5150 *decoder = to_tvp5150(sd);
686 	v4l2_std_id std = decoder->norm;
687 	u8 reg;
688 	int pos, type = 0;
689 	int i, ret = 0;
690 
691 	if (std == V4L2_STD_ALL) {
692 		v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
693 		return 0;
694 	} else if (std & V4L2_STD_625_50) {
695 		/* Don't follow NTSC Line number convension */
696 		line += 3;
697 	}
698 
699 	if (line < 6 || line > 27)
700 		return 0;
701 
702 	reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
703 
704 	for (i = 0; i <= 1; i++) {
705 		ret = tvp5150_read(sd, reg + i);
706 		if (ret < 0) {
707 			v4l2_err(sd, "%s: failed with error = %d\n",
708 				 __func__, ret);
709 			return 0;
710 		}
711 		pos = ret & 0x0f;
712 		if (pos < 0x0f)
713 			type |= regs[pos].type.vbi_type;
714 	}
715 
716 	return type;
717 }
718 
719 static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
720 {
721 	struct tvp5150 *decoder = to_tvp5150(sd);
722 	int fmt = 0;
723 
724 	decoder->norm = std;
725 
726 	/* First tests should be against specific std */
727 
728 	if (std == V4L2_STD_NTSC_443) {
729 		fmt = VIDEO_STD_NTSC_4_43_BIT;
730 	} else if (std == V4L2_STD_PAL_M) {
731 		fmt = VIDEO_STD_PAL_M_BIT;
732 	} else if (std == V4L2_STD_PAL_N || std == V4L2_STD_PAL_Nc) {
733 		fmt = VIDEO_STD_PAL_COMBINATION_N_BIT;
734 	} else {
735 		/* Then, test against generic ones */
736 		if (std & V4L2_STD_NTSC)
737 			fmt = VIDEO_STD_NTSC_MJ_BIT;
738 		else if (std & V4L2_STD_PAL)
739 			fmt = VIDEO_STD_PAL_BDGHIN_BIT;
740 		else if (std & V4L2_STD_SECAM)
741 			fmt = VIDEO_STD_SECAM_BIT;
742 	}
743 
744 	v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
745 	tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
746 	return 0;
747 }
748 
749 static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
750 {
751 	struct tvp5150 *decoder = to_tvp5150(sd);
752 
753 	if (decoder->norm == std)
754 		return 0;
755 
756 	/* Change cropping height limits */
757 	if (std & V4L2_STD_525_60)
758 		decoder->rect.height = TVP5150_V_MAX_525_60;
759 	else
760 		decoder->rect.height = TVP5150_V_MAX_OTHERS;
761 
762 
763 	return tvp5150_set_std(sd, std);
764 }
765 
766 static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
767 {
768 	struct tvp5150 *decoder = to_tvp5150(sd);
769 
770 	/* Initializes TVP5150 to its default values */
771 	tvp5150_write_inittab(sd, tvp5150_init_default);
772 
773 	/* Initializes VDP registers */
774 	tvp5150_vdp_init(sd, vbi_ram_default);
775 
776 	/* Selects decoder input */
777 	tvp5150_selmux(sd);
778 
779 	/* Initializes TVP5150 to stream enabled values */
780 	tvp5150_write_inittab(sd, tvp5150_init_enable);
781 
782 	/* Initialize image preferences */
783 	v4l2_ctrl_handler_setup(&decoder->hdl);
784 
785 	tvp5150_set_std(sd, decoder->norm);
786 
787 	if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
788 		tvp5150_write(sd, TVP5150_DATA_RATE_SEL, 0x40);
789 
790 	return 0;
791 };
792 
793 static int tvp5150_s_ctrl(struct v4l2_ctrl *ctrl)
794 {
795 	struct v4l2_subdev *sd = to_sd(ctrl);
796 
797 	switch (ctrl->id) {
798 	case V4L2_CID_BRIGHTNESS:
799 		tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->val);
800 		return 0;
801 	case V4L2_CID_CONTRAST:
802 		tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->val);
803 		return 0;
804 	case V4L2_CID_SATURATION:
805 		tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->val);
806 		return 0;
807 	case V4L2_CID_HUE:
808 		tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->val);
809 		return 0;
810 	}
811 	return -EINVAL;
812 }
813 
814 static v4l2_std_id tvp5150_read_std(struct v4l2_subdev *sd)
815 {
816 	int val = tvp5150_read(sd, TVP5150_STATUS_REG_5);
817 
818 	switch (val & 0x0F) {
819 	case 0x01:
820 		return V4L2_STD_NTSC;
821 	case 0x03:
822 		return V4L2_STD_PAL;
823 	case 0x05:
824 		return V4L2_STD_PAL_M;
825 	case 0x07:
826 		return V4L2_STD_PAL_N | V4L2_STD_PAL_Nc;
827 	case 0x09:
828 		return V4L2_STD_NTSC_443;
829 	case 0xb:
830 		return V4L2_STD_SECAM;
831 	default:
832 		return V4L2_STD_UNKNOWN;
833 	}
834 }
835 
836 static int tvp5150_fill_fmt(struct v4l2_subdev *sd,
837 		struct v4l2_subdev_pad_config *cfg,
838 		struct v4l2_subdev_format *format)
839 {
840 	struct v4l2_mbus_framefmt *f;
841 	struct tvp5150 *decoder = to_tvp5150(sd);
842 
843 	if (!format || format->pad)
844 		return -EINVAL;
845 
846 	f = &format->format;
847 
848 	tvp5150_reset(sd, 0);
849 
850 	f->width = decoder->rect.width;
851 	f->height = decoder->rect.height / 2;
852 
853 	f->code = MEDIA_BUS_FMT_UYVY8_2X8;
854 	f->field = V4L2_FIELD_ALTERNATE;
855 	f->colorspace = V4L2_COLORSPACE_SMPTE170M;
856 
857 	v4l2_dbg(1, debug, sd, "width = %d, height = %d\n", f->width,
858 			f->height);
859 	return 0;
860 }
861 
862 static int tvp5150_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
863 {
864 	struct v4l2_rect rect = a->c;
865 	struct tvp5150 *decoder = to_tvp5150(sd);
866 	v4l2_std_id std;
867 	unsigned int hmax;
868 
869 	v4l2_dbg(1, debug, sd, "%s left=%d, top=%d, width=%d, height=%d\n",
870 		__func__, rect.left, rect.top, rect.width, rect.height);
871 
872 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 		return -EINVAL;
874 
875 	/* tvp5150 has some special limits */
876 	rect.left = clamp(rect.left, 0, TVP5150_MAX_CROP_LEFT);
877 	rect.width = clamp_t(unsigned int, rect.width,
878 			     TVP5150_H_MAX - TVP5150_MAX_CROP_LEFT - rect.left,
879 			     TVP5150_H_MAX - rect.left);
880 	rect.top = clamp(rect.top, 0, TVP5150_MAX_CROP_TOP);
881 
882 	/* Calculate height based on current standard */
883 	if (decoder->norm == V4L2_STD_ALL)
884 		std = tvp5150_read_std(sd);
885 	else
886 		std = decoder->norm;
887 
888 	if (std & V4L2_STD_525_60)
889 		hmax = TVP5150_V_MAX_525_60;
890 	else
891 		hmax = TVP5150_V_MAX_OTHERS;
892 
893 	rect.height = clamp_t(unsigned int, rect.height,
894 			      hmax - TVP5150_MAX_CROP_TOP - rect.top,
895 			      hmax - rect.top);
896 
897 	tvp5150_write(sd, TVP5150_VERT_BLANKING_START, rect.top);
898 	tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP,
899 		      rect.top + rect.height - hmax);
900 	tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_MSB,
901 		      rect.left >> TVP5150_CROP_SHIFT);
902 	tvp5150_write(sd, TVP5150_ACT_VD_CROP_ST_LSB,
903 		      rect.left | (1 << TVP5150_CROP_SHIFT));
904 	tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_MSB,
905 		      (rect.left + rect.width - TVP5150_MAX_CROP_LEFT) >>
906 		      TVP5150_CROP_SHIFT);
907 	tvp5150_write(sd, TVP5150_ACT_VD_CROP_STP_LSB,
908 		      rect.left + rect.width - TVP5150_MAX_CROP_LEFT);
909 
910 	decoder->rect = rect;
911 
912 	return 0;
913 }
914 
915 static int tvp5150_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
916 {
917 	struct tvp5150 *decoder = to_tvp5150(sd);
918 
919 	a->c	= decoder->rect;
920 	a->type	= V4L2_BUF_TYPE_VIDEO_CAPTURE;
921 
922 	return 0;
923 }
924 
925 static int tvp5150_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
926 {
927 	struct tvp5150 *decoder = to_tvp5150(sd);
928 	v4l2_std_id std;
929 
930 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
931 		return -EINVAL;
932 
933 	a->bounds.left			= 0;
934 	a->bounds.top			= 0;
935 	a->bounds.width			= TVP5150_H_MAX;
936 
937 	/* Calculate height based on current standard */
938 	if (decoder->norm == V4L2_STD_ALL)
939 		std = tvp5150_read_std(sd);
940 	else
941 		std = decoder->norm;
942 
943 	if (std & V4L2_STD_525_60)
944 		a->bounds.height = TVP5150_V_MAX_525_60;
945 	else
946 		a->bounds.height = TVP5150_V_MAX_OTHERS;
947 
948 	a->defrect			= a->bounds;
949 	a->pixelaspect.numerator	= 1;
950 	a->pixelaspect.denominator	= 1;
951 
952 	return 0;
953 }
954 
955 static int tvp5150_g_mbus_config(struct v4l2_subdev *sd,
956 				 struct v4l2_mbus_config *cfg)
957 {
958 	struct tvp5150 *decoder = to_tvp5150(sd);
959 
960 	cfg->type = decoder->mbus_type;
961 	cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING
962 		   | V4L2_MBUS_FIELD_EVEN_LOW | V4L2_MBUS_DATA_ACTIVE_HIGH;
963 
964 	return 0;
965 }
966 
967 /****************************************************************************
968 			V4L2 subdev pad ops
969  ****************************************************************************/
970 static int tvp5150_enum_mbus_code(struct v4l2_subdev *sd,
971 		struct v4l2_subdev_pad_config *cfg,
972 		struct v4l2_subdev_mbus_code_enum *code)
973 {
974 	if (code->pad || code->index)
975 		return -EINVAL;
976 
977 	code->code = MEDIA_BUS_FMT_UYVY8_2X8;
978 	return 0;
979 }
980 
981 static int tvp5150_enum_frame_size(struct v4l2_subdev *sd,
982 				   struct v4l2_subdev_pad_config *cfg,
983 				   struct v4l2_subdev_frame_size_enum *fse)
984 {
985 	struct tvp5150 *decoder = to_tvp5150(sd);
986 
987 	if (fse->index >= 8 || fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
988 		return -EINVAL;
989 
990 	fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
991 	fse->min_width = decoder->rect.width;
992 	fse->max_width = decoder->rect.width;
993 	fse->min_height = decoder->rect.height / 2;
994 	fse->max_height = decoder->rect.height / 2;
995 
996 	return 0;
997 }
998 
999 /****************************************************************************
1000 			I2C Command
1001  ****************************************************************************/
1002 
1003 static int tvp5150_s_stream(struct v4l2_subdev *sd, int enable)
1004 {
1005 	struct tvp5150 *decoder = to_tvp5150(sd);
1006 	/* Output format: 8-bit ITU-R BT.656 with embedded syncs */
1007 	int val = 0x09;
1008 
1009 	/* Output format: 8-bit 4:2:2 YUV with discrete sync */
1010 	if (decoder->mbus_type == V4L2_MBUS_PARALLEL)
1011 		val = 0x0d;
1012 
1013 	/* Initializes TVP5150 to its default values */
1014 	/* # set PCLK (27MHz) */
1015 	tvp5150_write(sd, TVP5150_CONF_SHARED_PIN, 0x00);
1016 
1017 	if (enable)
1018 		tvp5150_write(sd, TVP5150_MISC_CTL, val);
1019 	else
1020 		tvp5150_write(sd, TVP5150_MISC_CTL, 0x00);
1021 
1022 	return 0;
1023 }
1024 
1025 static int tvp5150_s_routing(struct v4l2_subdev *sd,
1026 			     u32 input, u32 output, u32 config)
1027 {
1028 	struct tvp5150 *decoder = to_tvp5150(sd);
1029 
1030 	decoder->input = input;
1031 	decoder->output = output;
1032 	tvp5150_selmux(sd);
1033 	return 0;
1034 }
1035 
1036 static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
1037 {
1038 	/* this is for capturing 36 raw vbi lines
1039 	   if there's a way to cut off the beginning 2 vbi lines
1040 	   with the tvp5150 then the vbi line count could be lowered
1041 	   to 17 lines/field again, although I couldn't find a register
1042 	   which could do that cropping */
1043 	if (fmt->sample_format == V4L2_PIX_FMT_GREY)
1044 		tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
1045 	if (fmt->count[0] == 18 && fmt->count[1] == 18) {
1046 		tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
1047 		tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
1048 	}
1049 	return 0;
1050 }
1051 
1052 static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1053 {
1054 	int i;
1055 
1056 	if (svbi->service_set != 0) {
1057 		for (i = 0; i <= 23; i++) {
1058 			svbi->service_lines[1][i] = 0;
1059 			svbi->service_lines[0][i] =
1060 				tvp5150_set_vbi(sd, vbi_ram_default,
1061 				       svbi->service_lines[0][i], 0xf0, i, 3);
1062 		}
1063 		/* Enables FIFO */
1064 		tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
1065 	} else {
1066 		/* Disables FIFO*/
1067 		tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
1068 
1069 		/* Disable Full Field */
1070 		tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
1071 
1072 		/* Disable Line modes */
1073 		for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
1074 			tvp5150_write(sd, i, 0xff);
1075 	}
1076 	return 0;
1077 }
1078 
1079 static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
1080 {
1081 	int i, mask = 0;
1082 
1083 	memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
1084 
1085 	for (i = 0; i <= 23; i++) {
1086 		svbi->service_lines[0][i] =
1087 			tvp5150_get_vbi(sd, vbi_ram_default, i);
1088 		mask |= svbi->service_lines[0][i];
1089 	}
1090 	svbi->service_set = mask;
1091 	return 0;
1092 }
1093 
1094 #ifdef CONFIG_VIDEO_ADV_DEBUG
1095 static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1096 {
1097 	int res;
1098 
1099 	res = tvp5150_read(sd, reg->reg & 0xff);
1100 	if (res < 0) {
1101 		v4l2_err(sd, "%s: failed with error = %d\n", __func__, res);
1102 		return res;
1103 	}
1104 
1105 	reg->val = res;
1106 	reg->size = 1;
1107 	return 0;
1108 }
1109 
1110 static int tvp5150_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
1111 {
1112 	tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
1113 	return 0;
1114 }
1115 #endif
1116 
1117 static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1118 {
1119 	int status = tvp5150_read(sd, 0x88);
1120 
1121 	vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
1122 	return 0;
1123 }
1124 
1125 /* ----------------------------------------------------------------------- */
1126 
1127 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops = {
1128 	.s_ctrl = tvp5150_s_ctrl,
1129 };
1130 
1131 static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
1132 	.log_status = tvp5150_log_status,
1133 	.reset = tvp5150_reset,
1134 #ifdef CONFIG_VIDEO_ADV_DEBUG
1135 	.g_register = tvp5150_g_register,
1136 	.s_register = tvp5150_s_register,
1137 #endif
1138 };
1139 
1140 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
1141 	.g_tuner = tvp5150_g_tuner,
1142 };
1143 
1144 static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
1145 	.s_std = tvp5150_s_std,
1146 	.s_stream = tvp5150_s_stream,
1147 	.s_routing = tvp5150_s_routing,
1148 	.s_crop = tvp5150_s_crop,
1149 	.g_crop = tvp5150_g_crop,
1150 	.cropcap = tvp5150_cropcap,
1151 	.g_mbus_config = tvp5150_g_mbus_config,
1152 };
1153 
1154 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
1155 	.g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
1156 	.g_sliced_fmt = tvp5150_g_sliced_fmt,
1157 	.s_sliced_fmt = tvp5150_s_sliced_fmt,
1158 	.s_raw_fmt = tvp5150_s_raw_fmt,
1159 };
1160 
1161 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops = {
1162 	.enum_mbus_code = tvp5150_enum_mbus_code,
1163 	.enum_frame_size = tvp5150_enum_frame_size,
1164 	.set_fmt = tvp5150_fill_fmt,
1165 	.get_fmt = tvp5150_fill_fmt,
1166 };
1167 
1168 static const struct v4l2_subdev_ops tvp5150_ops = {
1169 	.core = &tvp5150_core_ops,
1170 	.tuner = &tvp5150_tuner_ops,
1171 	.video = &tvp5150_video_ops,
1172 	.vbi = &tvp5150_vbi_ops,
1173 	.pad = &tvp5150_pad_ops,
1174 };
1175 
1176 
1177 /****************************************************************************
1178 			I2C Client & Driver
1179  ****************************************************************************/
1180 
1181 static int tvp5150_detect_version(struct tvp5150 *core)
1182 {
1183 	struct v4l2_subdev *sd = &core->sd;
1184 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1185 	unsigned int i;
1186 	u8 regs[4];
1187 	int res;
1188 
1189 	/*
1190 	 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1191 	 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1192 	 */
1193 	for (i = 0; i < 4; i++) {
1194 		res = tvp5150_read(sd, TVP5150_MSB_DEV_ID + i);
1195 		if (res < 0)
1196 			return res;
1197 		regs[i] = res;
1198 	}
1199 
1200 	core->dev_id = (regs[0] << 8) | regs[1];
1201 	core->rom_ver = (regs[2] << 8) | regs[3];
1202 
1203 	v4l2_info(sd, "tvp%04x (%u.%u) chip found @ 0x%02x (%s)\n",
1204 		  core->dev_id, regs[2], regs[3], c->addr << 1,
1205 		  c->adapter->name);
1206 
1207 	if (core->dev_id == 0x5150 && core->rom_ver == 0x0321) {
1208 		v4l2_info(sd, "tvp5150a detected.\n");
1209 	} else if (core->dev_id == 0x5150 && core->rom_ver == 0x0400) {
1210 		v4l2_info(sd, "tvp5150am1 detected.\n");
1211 
1212 		/* ITU-T BT.656.4 timing */
1213 		tvp5150_write(sd, TVP5150_REV_SELECT, 0);
1214 	} else if (core->dev_id == 0x5151 && core->rom_ver == 0x0100) {
1215 		v4l2_info(sd, "tvp5151 detected.\n");
1216 	} else {
1217 		v4l2_info(sd, "*** unknown tvp%04x chip detected.\n",
1218 			  core->dev_id);
1219 	}
1220 
1221 	return 0;
1222 }
1223 
1224 static int tvp5150_init(struct i2c_client *c)
1225 {
1226 	struct gpio_desc *pdn_gpio;
1227 	struct gpio_desc *reset_gpio;
1228 
1229 	pdn_gpio = devm_gpiod_get_optional(&c->dev, "pdn", GPIOD_OUT_HIGH);
1230 	if (IS_ERR(pdn_gpio))
1231 		return PTR_ERR(pdn_gpio);
1232 
1233 	if (pdn_gpio) {
1234 		gpiod_set_value_cansleep(pdn_gpio, 0);
1235 		/* Delay time between power supplies active and reset */
1236 		msleep(20);
1237 	}
1238 
1239 	reset_gpio = devm_gpiod_get_optional(&c->dev, "reset", GPIOD_OUT_HIGH);
1240 	if (IS_ERR(reset_gpio))
1241 		return PTR_ERR(reset_gpio);
1242 
1243 	if (reset_gpio) {
1244 		/* RESETB pulse duration */
1245 		ndelay(500);
1246 		gpiod_set_value_cansleep(reset_gpio, 0);
1247 		/* Delay time between end of reset to I2C active */
1248 		usleep_range(200, 250);
1249 	}
1250 
1251 	return 0;
1252 }
1253 
1254 static int tvp5150_parse_dt(struct tvp5150 *decoder, struct device_node *np)
1255 {
1256 	struct v4l2_of_endpoint bus_cfg;
1257 	struct device_node *ep;
1258 	unsigned int flags;
1259 	int ret = 0;
1260 
1261 	ep = of_graph_get_next_endpoint(np, NULL);
1262 	if (!ep)
1263 		return -EINVAL;
1264 
1265 	ret = v4l2_of_parse_endpoint(ep, &bus_cfg);
1266 	if (ret)
1267 		goto err;
1268 
1269 	flags = bus_cfg.bus.parallel.flags;
1270 
1271 	if (bus_cfg.bus_type == V4L2_MBUS_PARALLEL &&
1272 	    !(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH &&
1273 	      flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH &&
1274 	      flags & V4L2_MBUS_FIELD_EVEN_LOW)) {
1275 		ret = -EINVAL;
1276 		goto err;
1277 	}
1278 
1279 	decoder->mbus_type = bus_cfg.bus_type;
1280 
1281 err:
1282 	of_node_put(ep);
1283 	return ret;
1284 }
1285 
1286 static int tvp5150_probe(struct i2c_client *c,
1287 			 const struct i2c_device_id *id)
1288 {
1289 	struct tvp5150 *core;
1290 	struct v4l2_subdev *sd;
1291 	struct device_node *np = c->dev.of_node;
1292 	int res;
1293 
1294 	/* Check if the adapter supports the needed features */
1295 	if (!i2c_check_functionality(c->adapter,
1296 	     I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1297 		return -EIO;
1298 
1299 	res = tvp5150_init(c);
1300 	if (res)
1301 		return res;
1302 
1303 	core = devm_kzalloc(&c->dev, sizeof(*core), GFP_KERNEL);
1304 	if (!core)
1305 		return -ENOMEM;
1306 
1307 	sd = &core->sd;
1308 
1309 	if (IS_ENABLED(CONFIG_OF) && np) {
1310 		res = tvp5150_parse_dt(core, np);
1311 		if (res) {
1312 			v4l2_err(sd, "DT parsing error: %d\n", res);
1313 			return res;
1314 		}
1315 	} else {
1316 		/* Default to BT.656 embedded sync */
1317 		core->mbus_type = V4L2_MBUS_BT656;
1318 	}
1319 
1320 	v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
1321 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1322 
1323 #if defined(CONFIG_MEDIA_CONTROLLER)
1324 	core->pads[DEMOD_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
1325 	core->pads[DEMOD_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
1326 	core->pads[DEMOD_PAD_VBI_OUT].flags = MEDIA_PAD_FL_SOURCE;
1327 
1328 	sd->entity.function = MEDIA_ENT_F_ATV_DECODER;
1329 
1330 	res = media_entity_pads_init(&sd->entity, DEMOD_NUM_PADS, core->pads);
1331 	if (res < 0)
1332 		return res;
1333 #endif
1334 
1335 	res = tvp5150_detect_version(core);
1336 	if (res < 0)
1337 		return res;
1338 
1339 	core->norm = V4L2_STD_ALL;	/* Default is autodetect */
1340 	core->input = TVP5150_COMPOSITE1;
1341 	core->enable = 1;
1342 
1343 	v4l2_ctrl_handler_init(&core->hdl, 5);
1344 	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1345 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1346 	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1347 			V4L2_CID_CONTRAST, 0, 255, 1, 128);
1348 	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1349 			V4L2_CID_SATURATION, 0, 255, 1, 128);
1350 	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1351 			V4L2_CID_HUE, -128, 127, 1, 0);
1352 	v4l2_ctrl_new_std(&core->hdl, &tvp5150_ctrl_ops,
1353 			V4L2_CID_PIXEL_RATE, 27000000,
1354 			27000000, 1, 27000000);
1355 	sd->ctrl_handler = &core->hdl;
1356 	if (core->hdl.error) {
1357 		res = core->hdl.error;
1358 		goto err;
1359 	}
1360 	v4l2_ctrl_handler_setup(&core->hdl);
1361 
1362 	/* Default is no cropping */
1363 	core->rect.top = 0;
1364 	if (tvp5150_read_std(sd) & V4L2_STD_525_60)
1365 		core->rect.height = TVP5150_V_MAX_525_60;
1366 	else
1367 		core->rect.height = TVP5150_V_MAX_OTHERS;
1368 	core->rect.left = 0;
1369 	core->rect.width = TVP5150_H_MAX;
1370 
1371 	res = v4l2_async_register_subdev(sd);
1372 	if (res < 0)
1373 		goto err;
1374 
1375 	if (debug > 1)
1376 		tvp5150_log_status(sd);
1377 	return 0;
1378 
1379 err:
1380 	v4l2_ctrl_handler_free(&core->hdl);
1381 	return res;
1382 }
1383 
1384 static int tvp5150_remove(struct i2c_client *c)
1385 {
1386 	struct v4l2_subdev *sd = i2c_get_clientdata(c);
1387 	struct tvp5150 *decoder = to_tvp5150(sd);
1388 
1389 	v4l2_dbg(1, debug, sd,
1390 		"tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1391 		c->addr << 1);
1392 
1393 	v4l2_async_unregister_subdev(sd);
1394 	v4l2_ctrl_handler_free(&decoder->hdl);
1395 	return 0;
1396 }
1397 
1398 /* ----------------------------------------------------------------------- */
1399 
1400 static const struct i2c_device_id tvp5150_id[] = {
1401 	{ "tvp5150", 0 },
1402 	{ }
1403 };
1404 MODULE_DEVICE_TABLE(i2c, tvp5150_id);
1405 
1406 #if IS_ENABLED(CONFIG_OF)
1407 static const struct of_device_id tvp5150_of_match[] = {
1408 	{ .compatible = "ti,tvp5150", },
1409 	{ /* sentinel */ },
1410 };
1411 MODULE_DEVICE_TABLE(of, tvp5150_of_match);
1412 #endif
1413 
1414 static struct i2c_driver tvp5150_driver = {
1415 	.driver = {
1416 		.of_match_table = of_match_ptr(tvp5150_of_match),
1417 		.name	= "tvp5150",
1418 	},
1419 	.probe		= tvp5150_probe,
1420 	.remove		= tvp5150_remove,
1421 	.id_table	= tvp5150_id,
1422 };
1423 
1424 module_i2c_driver(tvp5150_driver);
1425