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