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