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