• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/media/video/
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 <linux/i2c.h>
9#include <linux/slab.h>
10#include <linux/videodev2.h>
11#include <linux/delay.h>
12#include <media/v4l2-device.h>
13#include <media/tvp5150.h>
14#include <media/v4l2-i2c-drv.h>
15#include <media/v4l2-chip-ident.h>
16
17#include "tvp5150_reg.h"
18
19MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
20MODULE_AUTHOR("Mauro Carvalho Chehab");
21MODULE_LICENSE("GPL");
22
23
24static int debug;
25module_param(debug, int, 0);
26MODULE_PARM_DESC(debug, "Debug level (0-2)");
27
28/* supported controls */
29static struct v4l2_queryctrl tvp5150_qctrl[] = {
30	{
31		.id = V4L2_CID_BRIGHTNESS,
32		.type = V4L2_CTRL_TYPE_INTEGER,
33		.name = "Brightness",
34		.minimum = 0,
35		.maximum = 255,
36		.step = 1,
37		.default_value = 128,
38		.flags = 0,
39	}, {
40		.id = V4L2_CID_CONTRAST,
41		.type = V4L2_CTRL_TYPE_INTEGER,
42		.name = "Contrast",
43		.minimum = 0,
44		.maximum = 255,
45		.step = 0x1,
46		.default_value = 128,
47		.flags = 0,
48	}, {
49		 .id = V4L2_CID_SATURATION,
50		 .type = V4L2_CTRL_TYPE_INTEGER,
51		 .name = "Saturation",
52		 .minimum = 0,
53		 .maximum = 255,
54		 .step = 0x1,
55		 .default_value = 128,
56		 .flags = 0,
57	}, {
58		.id = V4L2_CID_HUE,
59		.type = V4L2_CTRL_TYPE_INTEGER,
60		.name = "Hue",
61		.minimum = -128,
62		.maximum = 127,
63		.step = 0x1,
64		.default_value = 0,
65		.flags = 0,
66	}
67};
68
69struct tvp5150 {
70	struct v4l2_subdev sd;
71
72	v4l2_std_id norm;	/* Current set standard */
73	u32 input;
74	u32 output;
75	int enable;
76	int bright;
77	int contrast;
78	int hue;
79	int sat;
80};
81
82static inline struct tvp5150 *to_tvp5150(struct v4l2_subdev *sd)
83{
84	return container_of(sd, struct tvp5150, sd);
85}
86
87static int tvp5150_read(struct v4l2_subdev *sd, unsigned char addr)
88{
89	struct i2c_client *c = v4l2_get_subdevdata(sd);
90	unsigned char buffer[1];
91	int rc;
92
93	buffer[0] = addr;
94	if (1 != (rc = i2c_master_send(c, buffer, 1)))
95		v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 1)\n", rc);
96
97	msleep(10);
98
99	if (1 != (rc = i2c_master_recv(c, buffer, 1)))
100		v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 1)\n", rc);
101
102	v4l2_dbg(2, debug, sd, "tvp5150: read 0x%02x = 0x%02x\n", addr, buffer[0]);
103
104	return (buffer[0]);
105}
106
107static inline void tvp5150_write(struct v4l2_subdev *sd, unsigned char addr,
108				 unsigned char value)
109{
110	struct i2c_client *c = v4l2_get_subdevdata(sd);
111	unsigned char buffer[2];
112	int rc;
113
114	buffer[0] = addr;
115	buffer[1] = value;
116	v4l2_dbg(2, debug, sd, "tvp5150: writing 0x%02x 0x%02x\n", buffer[0], buffer[1]);
117	if (2 != (rc = i2c_master_send(c, buffer, 2)))
118		v4l2_dbg(0, debug, sd, "i2c i/o error: rc == %d (should be 2)\n", rc);
119}
120
121static void dump_reg_range(struct v4l2_subdev *sd, char *s, u8 init,
122				const u8 end, int max_line)
123{
124	int i = 0;
125
126	while (init != (u8)(end + 1)) {
127		if ((i % max_line) == 0) {
128			if (i > 0)
129				printk("\n");
130			printk("tvp5150: %s reg 0x%02x = ", s, init);
131		}
132		printk("%02x ", tvp5150_read(sd, init));
133
134		init++;
135		i++;
136	}
137	printk("\n");
138}
139
140static int tvp5150_log_status(struct v4l2_subdev *sd)
141{
142	printk("tvp5150: Video input source selection #1 = 0x%02x\n",
143			tvp5150_read(sd, TVP5150_VD_IN_SRC_SEL_1));
144	printk("tvp5150: Analog channel controls = 0x%02x\n",
145			tvp5150_read(sd, TVP5150_ANAL_CHL_CTL));
146	printk("tvp5150: Operation mode controls = 0x%02x\n",
147			tvp5150_read(sd, TVP5150_OP_MODE_CTL));
148	printk("tvp5150: Miscellaneous controls = 0x%02x\n",
149			tvp5150_read(sd, TVP5150_MISC_CTL));
150	printk("tvp5150: Autoswitch mask= 0x%02x\n",
151			tvp5150_read(sd, TVP5150_AUTOSW_MSK));
152	printk("tvp5150: Color killer threshold control = 0x%02x\n",
153			tvp5150_read(sd, TVP5150_COLOR_KIL_THSH_CTL));
154	printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
155			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_1),
156			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_2),
157			tvp5150_read(sd, TVP5150_LUMA_PROC_CTL_3));
158	printk("tvp5150: Brightness control = 0x%02x\n",
159			tvp5150_read(sd, TVP5150_BRIGHT_CTL));
160	printk("tvp5150: Color saturation control = 0x%02x\n",
161			tvp5150_read(sd, TVP5150_SATURATION_CTL));
162	printk("tvp5150: Hue control = 0x%02x\n",
163			tvp5150_read(sd, TVP5150_HUE_CTL));
164	printk("tvp5150: Contrast control = 0x%02x\n",
165			tvp5150_read(sd, TVP5150_CONTRAST_CTL));
166	printk("tvp5150: Outputs and data rates select = 0x%02x\n",
167			tvp5150_read(sd, TVP5150_DATA_RATE_SEL));
168	printk("tvp5150: Configuration shared pins = 0x%02x\n",
169			tvp5150_read(sd, TVP5150_CONF_SHARED_PIN));
170	printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
171			tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_MSB),
172			tvp5150_read(sd, TVP5150_ACT_VD_CROP_ST_LSB));
173	printk("tvp5150: Active video cropping stop  = 0x%02x%02x\n",
174			tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_MSB),
175			tvp5150_read(sd, TVP5150_ACT_VD_CROP_STP_LSB));
176	printk("tvp5150: Genlock/RTC = 0x%02x\n",
177			tvp5150_read(sd, TVP5150_GENLOCK));
178	printk("tvp5150: Horizontal sync start = 0x%02x\n",
179			tvp5150_read(sd, TVP5150_HORIZ_SYNC_START));
180	printk("tvp5150: Vertical blanking start = 0x%02x\n",
181			tvp5150_read(sd, TVP5150_VERT_BLANKING_START));
182	printk("tvp5150: Vertical blanking stop = 0x%02x\n",
183			tvp5150_read(sd, TVP5150_VERT_BLANKING_STOP));
184	printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
185			tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_1),
186			tvp5150_read(sd, TVP5150_CHROMA_PROC_CTL_2));
187	printk("tvp5150: Interrupt reset register B = 0x%02x\n",
188			tvp5150_read(sd, TVP5150_INT_RESET_REG_B));
189	printk("tvp5150: Interrupt enable register B = 0x%02x\n",
190			tvp5150_read(sd, TVP5150_INT_ENABLE_REG_B));
191	printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
192			tvp5150_read(sd, TVP5150_INTT_CONFIG_REG_B));
193	printk("tvp5150: Video standard = 0x%02x\n",
194			tvp5150_read(sd, TVP5150_VIDEO_STD));
195	printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
196			tvp5150_read(sd, TVP5150_CB_GAIN_FACT),
197			tvp5150_read(sd, TVP5150_CR_GAIN_FACTOR));
198	printk("tvp5150: Macrovision on counter = 0x%02x\n",
199			tvp5150_read(sd, TVP5150_MACROVISION_ON_CTR));
200	printk("tvp5150: Macrovision off counter = 0x%02x\n",
201			tvp5150_read(sd, TVP5150_MACROVISION_OFF_CTR));
202	printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
203			(tvp5150_read(sd, TVP5150_REV_SELECT) & 1) ? 3 : 4);
204	printk("tvp5150: Device ID = %02x%02x\n",
205			tvp5150_read(sd, TVP5150_MSB_DEV_ID),
206			tvp5150_read(sd, TVP5150_LSB_DEV_ID));
207	printk("tvp5150: ROM version = (hex) %02x.%02x\n",
208			tvp5150_read(sd, TVP5150_ROM_MAJOR_VER),
209			tvp5150_read(sd, TVP5150_ROM_MINOR_VER));
210	printk("tvp5150: Vertical line count = 0x%02x%02x\n",
211			tvp5150_read(sd, TVP5150_VERT_LN_COUNT_MSB),
212			tvp5150_read(sd, TVP5150_VERT_LN_COUNT_LSB));
213	printk("tvp5150: Interrupt status register B = 0x%02x\n",
214			tvp5150_read(sd, TVP5150_INT_STATUS_REG_B));
215	printk("tvp5150: Interrupt active register B = 0x%02x\n",
216			tvp5150_read(sd, TVP5150_INT_ACTIVE_REG_B));
217	printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
218			tvp5150_read(sd, TVP5150_STATUS_REG_1),
219			tvp5150_read(sd, TVP5150_STATUS_REG_2),
220			tvp5150_read(sd, TVP5150_STATUS_REG_3),
221			tvp5150_read(sd, TVP5150_STATUS_REG_4),
222			tvp5150_read(sd, TVP5150_STATUS_REG_5));
223
224	dump_reg_range(sd, "Teletext filter 1",   TVP5150_TELETEXT_FIL1_INI,
225			TVP5150_TELETEXT_FIL1_END, 8);
226	dump_reg_range(sd, "Teletext filter 2",   TVP5150_TELETEXT_FIL2_INI,
227			TVP5150_TELETEXT_FIL2_END, 8);
228
229	printk("tvp5150: Teletext filter enable = 0x%02x\n",
230			tvp5150_read(sd, TVP5150_TELETEXT_FIL_ENA));
231	printk("tvp5150: Interrupt status register A = 0x%02x\n",
232			tvp5150_read(sd, TVP5150_INT_STATUS_REG_A));
233	printk("tvp5150: Interrupt enable register A = 0x%02x\n",
234			tvp5150_read(sd, TVP5150_INT_ENABLE_REG_A));
235	printk("tvp5150: Interrupt configuration = 0x%02x\n",
236			tvp5150_read(sd, TVP5150_INT_CONF));
237	printk("tvp5150: VDP status register = 0x%02x\n",
238			tvp5150_read(sd, TVP5150_VDP_STATUS_REG));
239	printk("tvp5150: FIFO word count = 0x%02x\n",
240			tvp5150_read(sd, TVP5150_FIFO_WORD_COUNT));
241	printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
242			tvp5150_read(sd, TVP5150_FIFO_INT_THRESHOLD));
243	printk("tvp5150: FIFO reset = 0x%02x\n",
244			tvp5150_read(sd, TVP5150_FIFO_RESET));
245	printk("tvp5150: Line number interrupt = 0x%02x\n",
246			tvp5150_read(sd, TVP5150_LINE_NUMBER_INT));
247	printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
248			tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_HIGH),
249			tvp5150_read(sd, TVP5150_PIX_ALIGN_REG_LOW));
250	printk("tvp5150: FIFO output control = 0x%02x\n",
251			tvp5150_read(sd, TVP5150_FIFO_OUT_CTRL));
252	printk("tvp5150: Full field enable = 0x%02x\n",
253			tvp5150_read(sd, TVP5150_FULL_FIELD_ENA));
254	printk("tvp5150: Full field mode register = 0x%02x\n",
255			tvp5150_read(sd, TVP5150_FULL_FIELD_MODE_REG));
256
257	dump_reg_range(sd, "CC   data",   TVP5150_CC_DATA_INI,
258			TVP5150_CC_DATA_END, 8);
259
260	dump_reg_range(sd, "WSS  data",   TVP5150_WSS_DATA_INI,
261			TVP5150_WSS_DATA_END, 8);
262
263	dump_reg_range(sd, "VPS  data",   TVP5150_VPS_DATA_INI,
264			TVP5150_VPS_DATA_END, 8);
265
266	dump_reg_range(sd, "VITC data",   TVP5150_VITC_DATA_INI,
267			TVP5150_VITC_DATA_END, 10);
268
269	dump_reg_range(sd, "Line mode",   TVP5150_LINE_MODE_INI,
270			TVP5150_LINE_MODE_END, 8);
271	return 0;
272}
273
274/****************************************************************************
275			Basic functions
276 ****************************************************************************/
277
278static inline void tvp5150_selmux(struct v4l2_subdev *sd)
279{
280	int opmode=0;
281	struct tvp5150 *decoder = to_tvp5150(sd);
282	int input = 0;
283	unsigned char val;
284
285	if ((decoder->output & TVP5150_BLACK_SCREEN) || !decoder->enable)
286		input = 8;
287
288	switch (decoder->input) {
289	case TVP5150_COMPOSITE1:
290		input |= 2;
291		/* fall through */
292	case TVP5150_COMPOSITE0:
293		opmode=0x30;		/* TV Mode */
294		break;
295	case TVP5150_SVIDEO:
296	default:
297		input |= 1;
298		opmode=0;		/* Auto Mode */
299		break;
300	}
301
302	v4l2_dbg(1, debug, sd, "Selecting video route: route input=%i, output=%i "
303			"=> tvp5150 input=%i, opmode=%i\n",
304			decoder->input, decoder->output,
305			input, opmode);
306
307	tvp5150_write(sd, TVP5150_OP_MODE_CTL, opmode);
308	tvp5150_write(sd, TVP5150_VD_IN_SRC_SEL_1, input);
309
310	/* Svideo should enable YCrCb output and disable GPCL output
311	 * For Composite and TV, it should be the reverse
312	 */
313	val = tvp5150_read(sd, TVP5150_MISC_CTL);
314	if (decoder->input == TVP5150_SVIDEO)
315		val = (val & ~0x40) | 0x10;
316	else
317		val = (val & ~0x10) | 0x40;
318	tvp5150_write(sd, TVP5150_MISC_CTL, val);
319};
320
321struct i2c_reg_value {
322	unsigned char reg;
323	unsigned char value;
324};
325
326/* Default values as sugested at TVP5150AM1 datasheet */
327static const struct i2c_reg_value tvp5150_init_default[] = {
328	{ /* 0x00 */
329		TVP5150_VD_IN_SRC_SEL_1,0x00
330	},
331	{ /* 0x01 */
332		TVP5150_ANAL_CHL_CTL,0x15
333	},
334	{ /* 0x02 */
335		TVP5150_OP_MODE_CTL,0x00
336	},
337	{ /* 0x03 */
338		TVP5150_MISC_CTL,0x01
339	},
340	{ /* 0x06 */
341		TVP5150_COLOR_KIL_THSH_CTL,0x10
342	},
343	{ /* 0x07 */
344		TVP5150_LUMA_PROC_CTL_1,0x60
345	},
346	{ /* 0x08 */
347		TVP5150_LUMA_PROC_CTL_2,0x00
348	},
349	{ /* 0x09 */
350		TVP5150_BRIGHT_CTL,0x80
351	},
352	{ /* 0x0a */
353		TVP5150_SATURATION_CTL,0x80
354	},
355	{ /* 0x0b */
356		TVP5150_HUE_CTL,0x00
357	},
358	{ /* 0x0c */
359		TVP5150_CONTRAST_CTL,0x80
360	},
361	{ /* 0x0d */
362		TVP5150_DATA_RATE_SEL,0x47
363	},
364	{ /* 0x0e */
365		TVP5150_LUMA_PROC_CTL_3,0x00
366	},
367	{ /* 0x0f */
368		TVP5150_CONF_SHARED_PIN,0x08
369	},
370	{ /* 0x11 */
371		TVP5150_ACT_VD_CROP_ST_MSB,0x00
372	},
373	{ /* 0x12 */
374		TVP5150_ACT_VD_CROP_ST_LSB,0x00
375	},
376	{ /* 0x13 */
377		TVP5150_ACT_VD_CROP_STP_MSB,0x00
378	},
379	{ /* 0x14 */
380		TVP5150_ACT_VD_CROP_STP_LSB,0x00
381	},
382	{ /* 0x15 */
383		TVP5150_GENLOCK,0x01
384	},
385	{ /* 0x16 */
386		TVP5150_HORIZ_SYNC_START,0x80
387	},
388	{ /* 0x18 */
389		TVP5150_VERT_BLANKING_START,0x00
390	},
391	{ /* 0x19 */
392		TVP5150_VERT_BLANKING_STOP,0x00
393	},
394	{ /* 0x1a */
395		TVP5150_CHROMA_PROC_CTL_1,0x0c
396	},
397	{ /* 0x1b */
398		TVP5150_CHROMA_PROC_CTL_2,0x14
399	},
400	{ /* 0x1c */
401		TVP5150_INT_RESET_REG_B,0x00
402	},
403	{ /* 0x1d */
404		TVP5150_INT_ENABLE_REG_B,0x00
405	},
406	{ /* 0x1e */
407		TVP5150_INTT_CONFIG_REG_B,0x00
408	},
409	{ /* 0x28 */
410		TVP5150_VIDEO_STD,0x00
411	},
412	{ /* 0x2e */
413		TVP5150_MACROVISION_ON_CTR,0x0f
414	},
415	{ /* 0x2f */
416		TVP5150_MACROVISION_OFF_CTR,0x01
417	},
418	{ /* 0xbb */
419		TVP5150_TELETEXT_FIL_ENA,0x00
420	},
421	{ /* 0xc0 */
422		TVP5150_INT_STATUS_REG_A,0x00
423	},
424	{ /* 0xc1 */
425		TVP5150_INT_ENABLE_REG_A,0x00
426	},
427	{ /* 0xc2 */
428		TVP5150_INT_CONF,0x04
429	},
430	{ /* 0xc8 */
431		TVP5150_FIFO_INT_THRESHOLD,0x80
432	},
433	{ /* 0xc9 */
434		TVP5150_FIFO_RESET,0x00
435	},
436	{ /* 0xca */
437		TVP5150_LINE_NUMBER_INT,0x00
438	},
439	{ /* 0xcb */
440		TVP5150_PIX_ALIGN_REG_LOW,0x4e
441	},
442	{ /* 0xcc */
443		TVP5150_PIX_ALIGN_REG_HIGH,0x00
444	},
445	{ /* 0xcd */
446		TVP5150_FIFO_OUT_CTRL,0x01
447	},
448	{ /* 0xcf */
449		TVP5150_FULL_FIELD_ENA,0x00
450	},
451	{ /* 0xd0 */
452		TVP5150_LINE_MODE_INI,0x00
453	},
454	{ /* 0xfc */
455		TVP5150_FULL_FIELD_MODE_REG,0x7f
456	},
457	{ /* end of data */
458		0xff,0xff
459	}
460};
461
462/* Default values as sugested at TVP5150AM1 datasheet */
463static const struct i2c_reg_value tvp5150_init_enable[] = {
464	{
465		TVP5150_CONF_SHARED_PIN, 2
466	},{	/* Automatic offset and AGC enabled */
467		TVP5150_ANAL_CHL_CTL, 0x15
468	},{	/* Activate YCrCb output 0x9 or 0xd ? */
469		TVP5150_MISC_CTL, 0x6f
470	},{	/* Activates video std autodetection for all standards */
471		TVP5150_AUTOSW_MSK, 0x0
472	},{	/* Default format: 0x47. For 4:2:2: 0x40 */
473		TVP5150_DATA_RATE_SEL, 0x47
474	},{
475		TVP5150_CHROMA_PROC_CTL_1, 0x0c
476	},{
477		TVP5150_CHROMA_PROC_CTL_2, 0x54
478	},{	/* Non documented, but initialized on WinTV USB2 */
479		0x27, 0x20
480	},{
481		0xff,0xff
482	}
483};
484
485struct tvp5150_vbi_type {
486	unsigned int vbi_type;
487	unsigned int ini_line;
488	unsigned int end_line;
489	unsigned int by_field :1;
490};
491
492struct i2c_vbi_ram_value {
493	u16 reg;
494	struct tvp5150_vbi_type type;
495	unsigned char values[16];
496};
497
498/* This struct have the values for each supported VBI Standard
499 * by
500 tvp5150_vbi_types should follow the same order as vbi_ram_default
501 * value 0 means rom position 0x10, value 1 means rom position 0x30
502 * and so on. There are 16 possible locations from 0 to 15.
503 */
504
505static struct i2c_vbi_ram_value vbi_ram_default[] =
506{
507	{0x030, /* Teletext, PAL, WST System B */
508		{V4L2_SLICED_TELETEXT_B,6,22,1},
509		{ 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
510		  0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
511	},
512	{0x0f0, /* Closed Caption, NTSC */
513		{V4L2_SLICED_CAPTION_525,21,21,1},
514		{ 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
515		  0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
516	},
517	{0x110, /* Wide Screen Signal, PAL/SECAM */
518		{V4L2_SLICED_WSS_625,23,23,1},
519		{ 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
520		  0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
521	},
522	{0x190, /* Video Program System (VPS), PAL */
523		{V4L2_SLICED_VPS,16,16,0},
524		{ 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
525		  0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
526	},
527	/* 0x1d0 User programmable */
528
529	/* End of struct */
530	{ (u16)-1 }
531};
532
533static int tvp5150_write_inittab(struct v4l2_subdev *sd,
534				const struct i2c_reg_value *regs)
535{
536	while (regs->reg != 0xff) {
537		tvp5150_write(sd, regs->reg, regs->value);
538		regs++;
539	}
540	return 0;
541}
542
543static int tvp5150_vdp_init(struct v4l2_subdev *sd,
544				const struct i2c_vbi_ram_value *regs)
545{
546	unsigned int i;
547
548	/* Disable Full Field */
549	tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
550
551	/* Before programming, Line mode should be at 0xff */
552	for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
553		tvp5150_write(sd, i, 0xff);
554
555	/* Load Ram Table */
556	while (regs->reg != (u16)-1) {
557		tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_HIGH, regs->reg >> 8);
558		tvp5150_write(sd, TVP5150_CONF_RAM_ADDR_LOW, regs->reg);
559
560		for (i = 0; i < 16; i++)
561			tvp5150_write(sd, TVP5150_VDP_CONF_RAM_DATA, regs->values[i]);
562
563		regs++;
564	}
565	return 0;
566}
567
568/* Fills VBI capabilities based on i2c_vbi_ram_value struct */
569static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev *sd,
570				struct v4l2_sliced_vbi_cap *cap)
571{
572	const struct i2c_vbi_ram_value *regs = vbi_ram_default;
573	int line;
574
575	v4l2_dbg(1, debug, sd, "g_sliced_vbi_cap\n");
576	memset(cap, 0, sizeof *cap);
577
578	while (regs->reg != (u16)-1 ) {
579		for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
580			cap->service_lines[0][line] |= regs->type.vbi_type;
581		}
582		cap->service_set |= regs->type.vbi_type;
583
584		regs++;
585	}
586	return 0;
587}
588
589/* Set vbi processing
590 * type - one of tvp5150_vbi_types
591 * line - line to gather data
592 * fields: bit 0 field1, bit 1, field2
593 * flags (default=0xf0) is a bitmask, were set means:
594 *	bit 7: enable filtering null bytes on CC
595 *	bit 6: send data also to FIFO
596 *	bit 5: don't allow data with errors on FIFO
597 *	bit 4: enable ECC when possible
598 * pix_align = pix alignment:
599 *	LSB = field1
600 *	MSB = field2
601 */
602static int tvp5150_set_vbi(struct v4l2_subdev *sd,
603			const struct i2c_vbi_ram_value *regs,
604			unsigned int type,u8 flags, int line,
605			const int fields)
606{
607	struct tvp5150 *decoder = to_tvp5150(sd);
608	v4l2_std_id std = decoder->norm;
609	u8 reg;
610	int pos=0;
611
612	if (std == V4L2_STD_ALL) {
613		v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
614		return 0;
615	} else if (std & V4L2_STD_625_50) {
616		/* Don't follow NTSC Line number convension */
617		line += 3;
618	}
619
620	if (line<6||line>27)
621		return 0;
622
623	while (regs->reg != (u16)-1 ) {
624		if ((type & regs->type.vbi_type) &&
625		    (line>=regs->type.ini_line) &&
626		    (line<=regs->type.end_line)) {
627			type=regs->type.vbi_type;
628			break;
629		}
630
631		regs++;
632		pos++;
633	}
634	if (regs->reg == (u16)-1)
635		return 0;
636
637	type=pos | (flags & 0xf0);
638	reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
639
640	if (fields&1) {
641		tvp5150_write(sd, reg, type);
642	}
643
644	if (fields&2) {
645		tvp5150_write(sd, reg+1, type);
646	}
647
648	return type;
649}
650
651static int tvp5150_get_vbi(struct v4l2_subdev *sd,
652			const struct i2c_vbi_ram_value *regs, int line)
653{
654	struct tvp5150 *decoder = to_tvp5150(sd);
655	v4l2_std_id std = decoder->norm;
656	u8 reg;
657	int pos, type = 0;
658
659	if (std == V4L2_STD_ALL) {
660		v4l2_err(sd, "VBI can't be configured without knowing number of lines\n");
661		return 0;
662	} else if (std & V4L2_STD_625_50) {
663		/* Don't follow NTSC Line number convension */
664		line += 3;
665	}
666
667	if (line < 6 || line > 27)
668		return 0;
669
670	reg = ((line - 6) << 1) + TVP5150_LINE_MODE_INI;
671
672	pos = tvp5150_read(sd, reg) & 0x0f;
673	if (pos < 0x0f)
674		type = regs[pos].type.vbi_type;
675
676	pos = tvp5150_read(sd, reg + 1) & 0x0f;
677	if (pos < 0x0f)
678		type |= regs[pos].type.vbi_type;
679
680	return type;
681}
682
683static int tvp5150_set_std(struct v4l2_subdev *sd, v4l2_std_id std)
684{
685	struct tvp5150 *decoder = to_tvp5150(sd);
686	int fmt = 0;
687
688	decoder->norm = std;
689
690	/* First tests should be against specific std */
691
692	if (std == V4L2_STD_ALL) {
693		fmt = 0;	/* Autodetect mode */
694	} else if (std & V4L2_STD_NTSC_443) {
695		fmt = 0xa;
696	} else if (std & V4L2_STD_PAL_M) {
697		fmt = 0x6;
698	} else if (std & (V4L2_STD_PAL_N | V4L2_STD_PAL_Nc)) {
699		fmt = 0x8;
700	} else {
701		/* Then, test against generic ones */
702		if (std & V4L2_STD_NTSC)
703			fmt = 0x2;
704		else if (std & V4L2_STD_PAL)
705			fmt = 0x4;
706		else if (std & V4L2_STD_SECAM)
707			fmt = 0xc;
708	}
709
710	v4l2_dbg(1, debug, sd, "Set video std register to %d.\n", fmt);
711	tvp5150_write(sd, TVP5150_VIDEO_STD, fmt);
712	return 0;
713}
714
715static int tvp5150_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
716{
717	struct tvp5150 *decoder = to_tvp5150(sd);
718
719	if (decoder->norm == std)
720		return 0;
721
722	return tvp5150_set_std(sd, std);
723}
724
725static int tvp5150_reset(struct v4l2_subdev *sd, u32 val)
726{
727	struct tvp5150 *decoder = to_tvp5150(sd);
728	u8 msb_id, lsb_id, msb_rom, lsb_rom;
729
730	msb_id = tvp5150_read(sd, TVP5150_MSB_DEV_ID);
731	lsb_id = tvp5150_read(sd, TVP5150_LSB_DEV_ID);
732	msb_rom = tvp5150_read(sd, TVP5150_ROM_MAJOR_VER);
733	lsb_rom = tvp5150_read(sd, TVP5150_ROM_MINOR_VER);
734
735	if (msb_rom == 4 && lsb_rom == 0) { /* Is TVP5150AM1 */
736		v4l2_info(sd, "tvp%02x%02xam1 detected.\n", msb_id, lsb_id);
737
738		/* ITU-T BT.656.4 timing */
739		tvp5150_write(sd, TVP5150_REV_SELECT, 0);
740	} else {
741		if (msb_rom == 3 || lsb_rom == 0x21) { /* Is TVP5150A */
742			v4l2_info(sd, "tvp%02x%02xa detected.\n", msb_id, lsb_id);
743		} else {
744			v4l2_info(sd, "*** unknown tvp%02x%02x chip detected.\n",
745					msb_id, lsb_id);
746			v4l2_info(sd, "*** Rom ver is %d.%d\n", msb_rom, lsb_rom);
747		}
748	}
749
750	/* Initializes TVP5150 to its default values */
751	tvp5150_write_inittab(sd, tvp5150_init_default);
752
753	/* Initializes VDP registers */
754	tvp5150_vdp_init(sd, vbi_ram_default);
755
756	/* Selects decoder input */
757	tvp5150_selmux(sd);
758
759	/* Initializes TVP5150 to stream enabled values */
760	tvp5150_write_inittab(sd, tvp5150_init_enable);
761
762	/* Initialize image preferences */
763	tvp5150_write(sd, TVP5150_BRIGHT_CTL, decoder->bright);
764	tvp5150_write(sd, TVP5150_CONTRAST_CTL, decoder->contrast);
765	tvp5150_write(sd, TVP5150_SATURATION_CTL, decoder->contrast);
766	tvp5150_write(sd, TVP5150_HUE_CTL, decoder->hue);
767
768	tvp5150_set_std(sd, decoder->norm);
769	return 0;
770};
771
772static int tvp5150_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
773{
774	v4l2_dbg(1, debug, sd, "g_ctrl called\n");
775
776	switch (ctrl->id) {
777	case V4L2_CID_BRIGHTNESS:
778		ctrl->value = tvp5150_read(sd, TVP5150_BRIGHT_CTL);
779		return 0;
780	case V4L2_CID_CONTRAST:
781		ctrl->value = tvp5150_read(sd, TVP5150_CONTRAST_CTL);
782		return 0;
783	case V4L2_CID_SATURATION:
784		ctrl->value = tvp5150_read(sd, TVP5150_SATURATION_CTL);
785		return 0;
786	case V4L2_CID_HUE:
787		ctrl->value = tvp5150_read(sd, TVP5150_HUE_CTL);
788		return 0;
789	}
790	return -EINVAL;
791}
792
793static int tvp5150_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
794{
795	u8 i, n;
796	n = ARRAY_SIZE(tvp5150_qctrl);
797
798	for (i = 0; i < n; i++) {
799		if (ctrl->id != tvp5150_qctrl[i].id)
800			continue;
801		if (ctrl->value < tvp5150_qctrl[i].minimum ||
802		    ctrl->value > tvp5150_qctrl[i].maximum)
803			return -ERANGE;
804		v4l2_dbg(1, debug, sd, "s_ctrl: id=%d, value=%d\n",
805					ctrl->id, ctrl->value);
806		break;
807	}
808
809	switch (ctrl->id) {
810	case V4L2_CID_BRIGHTNESS:
811		tvp5150_write(sd, TVP5150_BRIGHT_CTL, ctrl->value);
812		return 0;
813	case V4L2_CID_CONTRAST:
814		tvp5150_write(sd, TVP5150_CONTRAST_CTL, ctrl->value);
815		return 0;
816	case V4L2_CID_SATURATION:
817		tvp5150_write(sd, TVP5150_SATURATION_CTL, ctrl->value);
818		return 0;
819	case V4L2_CID_HUE:
820		tvp5150_write(sd, TVP5150_HUE_CTL, ctrl->value);
821		return 0;
822	}
823	return -EINVAL;
824}
825
826/****************************************************************************
827			I2C Command
828 ****************************************************************************/
829
830static int tvp5150_s_routing(struct v4l2_subdev *sd,
831			     u32 input, u32 output, u32 config)
832{
833	struct tvp5150 *decoder = to_tvp5150(sd);
834
835	decoder->input = input;
836	decoder->output = output;
837	tvp5150_selmux(sd);
838	return 0;
839}
840
841static int tvp5150_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
842{
843	/* this is for capturing 36 raw vbi lines
844	   if there's a way to cut off the beginning 2 vbi lines
845	   with the tvp5150 then the vbi line count could be lowered
846	   to 17 lines/field again, although I couldn't find a register
847	   which could do that cropping */
848	if (fmt->sample_format == V4L2_PIX_FMT_GREY)
849		tvp5150_write(sd, TVP5150_LUMA_PROC_CTL_1, 0x70);
850	if (fmt->count[0] == 18 && fmt->count[1] == 18) {
851		tvp5150_write(sd, TVP5150_VERT_BLANKING_START, 0x00);
852		tvp5150_write(sd, TVP5150_VERT_BLANKING_STOP, 0x01);
853	}
854	return 0;
855}
856
857static int tvp5150_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
858{
859	int i;
860
861	if (svbi->service_set != 0) {
862		for (i = 0; i <= 23; i++) {
863			svbi->service_lines[1][i] = 0;
864			svbi->service_lines[0][i] =
865				tvp5150_set_vbi(sd, vbi_ram_default,
866				       svbi->service_lines[0][i], 0xf0, i, 3);
867		}
868		/* Enables FIFO */
869		tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 1);
870	} else {
871		/* Disables FIFO*/
872		tvp5150_write(sd, TVP5150_FIFO_OUT_CTRL, 0);
873
874		/* Disable Full Field */
875		tvp5150_write(sd, TVP5150_FULL_FIELD_ENA, 0);
876
877		/* Disable Line modes */
878		for (i = TVP5150_LINE_MODE_INI; i <= TVP5150_LINE_MODE_END; i++)
879			tvp5150_write(sd, i, 0xff);
880	}
881	return 0;
882}
883
884static int tvp5150_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
885{
886	int i, mask = 0;
887
888	memset(svbi, 0, sizeof(*svbi));
889
890	for (i = 0; i <= 23; i++) {
891		svbi->service_lines[0][i] =
892			tvp5150_get_vbi(sd, vbi_ram_default, i);
893		mask |= svbi->service_lines[0][i];
894	}
895	svbi->service_set = mask;
896	return 0;
897}
898
899static int tvp5150_g_chip_ident(struct v4l2_subdev *sd,
900				struct v4l2_dbg_chip_ident *chip)
901{
902	int rev;
903	struct i2c_client *client = v4l2_get_subdevdata(sd);
904
905	rev = tvp5150_read(sd, TVP5150_ROM_MAJOR_VER) << 8 |
906	      tvp5150_read(sd, TVP5150_ROM_MINOR_VER);
907
908	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_TVP5150,
909					  rev);
910}
911
912
913#ifdef CONFIG_VIDEO_ADV_DEBUG
914static int tvp5150_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
915{
916	struct i2c_client *client = v4l2_get_subdevdata(sd);
917
918	if (!v4l2_chip_match_i2c_client(client, &reg->match))
919		return -EINVAL;
920	if (!capable(CAP_SYS_ADMIN))
921		return -EPERM;
922	reg->val = tvp5150_read(sd, reg->reg & 0xff);
923	reg->size = 1;
924	return 0;
925}
926
927static int tvp5150_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
928{
929	struct i2c_client *client = v4l2_get_subdevdata(sd);
930
931	if (!v4l2_chip_match_i2c_client(client, &reg->match))
932		return -EINVAL;
933	if (!capable(CAP_SYS_ADMIN))
934		return -EPERM;
935	tvp5150_write(sd, reg->reg & 0xff, reg->val & 0xff);
936	return 0;
937}
938#endif
939
940static int tvp5150_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
941{
942	int status = tvp5150_read(sd, 0x88);
943
944	vt->signal = ((status & 0x04) && (status & 0x02)) ? 0xffff : 0x0;
945	return 0;
946}
947
948static int tvp5150_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
949{
950	int i;
951
952	v4l2_dbg(1, debug, sd, "queryctrl called\n");
953
954	for (i = 0; i < ARRAY_SIZE(tvp5150_qctrl); i++)
955		if (qc->id && qc->id == tvp5150_qctrl[i].id) {
956			memcpy(qc, &(tvp5150_qctrl[i]),
957			       sizeof(*qc));
958			return 0;
959		}
960
961	return -EINVAL;
962}
963
964/* ----------------------------------------------------------------------- */
965
966static const struct v4l2_subdev_core_ops tvp5150_core_ops = {
967	.log_status = tvp5150_log_status,
968	.g_ctrl = tvp5150_g_ctrl,
969	.s_ctrl = tvp5150_s_ctrl,
970	.queryctrl = tvp5150_queryctrl,
971	.s_std = tvp5150_s_std,
972	.reset = tvp5150_reset,
973	.g_chip_ident = tvp5150_g_chip_ident,
974#ifdef CONFIG_VIDEO_ADV_DEBUG
975	.g_register = tvp5150_g_register,
976	.s_register = tvp5150_s_register,
977#endif
978};
979
980static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops = {
981	.g_tuner = tvp5150_g_tuner,
982};
983
984static const struct v4l2_subdev_video_ops tvp5150_video_ops = {
985	.s_routing = tvp5150_s_routing,
986};
987
988static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops = {
989	.g_sliced_vbi_cap = tvp5150_g_sliced_vbi_cap,
990	.g_sliced_fmt = tvp5150_g_sliced_fmt,
991	.s_sliced_fmt = tvp5150_s_sliced_fmt,
992	.s_raw_fmt = tvp5150_s_raw_fmt,
993};
994
995static const struct v4l2_subdev_ops tvp5150_ops = {
996	.core = &tvp5150_core_ops,
997	.tuner = &tvp5150_tuner_ops,
998	.video = &tvp5150_video_ops,
999	.vbi = &tvp5150_vbi_ops,
1000};
1001
1002
1003/****************************************************************************
1004			I2C Client & Driver
1005 ****************************************************************************/
1006
1007static int tvp5150_probe(struct i2c_client *c,
1008			 const struct i2c_device_id *id)
1009{
1010	struct tvp5150 *core;
1011	struct v4l2_subdev *sd;
1012
1013	/* Check if the adapter supports the needed features */
1014	if (!i2c_check_functionality(c->adapter,
1015	     I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1016		return -EIO;
1017
1018	core = kzalloc(sizeof(struct tvp5150), GFP_KERNEL);
1019	if (!core) {
1020		return -ENOMEM;
1021	}
1022	sd = &core->sd;
1023	v4l2_i2c_subdev_init(sd, c, &tvp5150_ops);
1024	v4l_info(c, "chip found @ 0x%02x (%s)\n",
1025		 c->addr << 1, c->adapter->name);
1026
1027	core->norm = V4L2_STD_ALL;	/* Default is autodetect */
1028	core->input = TVP5150_COMPOSITE1;
1029	core->enable = 1;
1030	core->bright = 128;
1031	core->contrast = 128;
1032	core->hue = 0;
1033	core->sat = 128;
1034
1035	if (debug > 1)
1036		tvp5150_log_status(sd);
1037	return 0;
1038}
1039
1040static int tvp5150_remove(struct i2c_client *c)
1041{
1042	struct v4l2_subdev *sd = i2c_get_clientdata(c);
1043
1044	v4l2_dbg(1, debug, sd,
1045		"tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1046		c->addr << 1);
1047
1048	v4l2_device_unregister_subdev(sd);
1049	kfree(to_tvp5150(sd));
1050	return 0;
1051}
1052
1053/* ----------------------------------------------------------------------- */
1054
1055static const struct i2c_device_id tvp5150_id[] = {
1056	{ "tvp5150", 0 },
1057	{ }
1058};
1059MODULE_DEVICE_TABLE(i2c, tvp5150_id);
1060
1061static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1062	.name = "tvp5150",
1063	.probe = tvp5150_probe,
1064	.remove = tvp5150_remove,
1065	.id_table = tvp5150_id,
1066};
1067