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