2 * tvp5150 - Texas Instruments TVP5150A/AM1 video decoder driver
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
9 #include <linux/videodev.h>
10 #include <linux/delay.h>
11 #include <linux/video_decoder.h>
12 #include <media/v4l2-common.h>
14 #include "tvp5150_reg.h"
16 MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
17 MODULE_AUTHOR("Mauro Carvalho Chehab");
18 MODULE_LICENSE("GPL");
20 /* standard i2c insmod options */
21 static unsigned short normal_i2c[] = {
30 module_param(debug, int, 0);
31 MODULE_PARM_DESC(debug, "Debug level (0-1)");
33 #define tvp5150_err(fmt, arg...) do { \
34 printk(KERN_ERR "%s %d-%04x: " fmt, c->driver->driver.name, \
35 i2c_adapter_id(c->adapter), c->addr , ## arg); } while (0)
36 #define tvp5150_info(fmt, arg...) do { \
37 printk(KERN_INFO "%s %d-%04x: " fmt, c->driver->driver.name, \
38 i2c_adapter_id(c->adapter), c->addr , ## arg); } while (0)
39 #define tvp5150_dbg(num, fmt, arg...) \
42 printk(KERN_DEBUG "%s debug %d-%04x: " fmt,\
43 c->driver->driver.name, \
44 i2c_adapter_id(c->adapter), \
45 c->addr , ## arg); } while (0)
47 /* supported controls */
48 static struct v4l2_queryctrl tvp5150_qctrl[] = {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
59 .id = V4L2_CID_CONTRAST,
60 .type = V4L2_CTRL_TYPE_INTEGER,
65 .default_value = 0x10,
68 .id = V4L2_CID_SATURATION,
69 .type = V4L2_CTRL_TYPE_INTEGER,
74 .default_value = 0x10,
78 .type = V4L2_CTRL_TYPE_INTEGER,
83 .default_value = 0x10,
89 struct i2c_client *client;
91 v4l2_std_id norm; /* Current set standard */
100 static int tvp5150_read(struct i2c_client *c, unsigned char addr)
102 unsigned char buffer[1];
106 if (1 != (rc = i2c_master_send(c, buffer, 1)))
107 tvp5150_dbg(0, "i2c i/o error: rc == %d (should be 1)\n", rc);
111 if (1 != (rc = i2c_master_recv(c, buffer, 1)))
112 tvp5150_dbg(0, "i2c i/o error: rc == %d (should be 1)\n", rc);
114 tvp5150_dbg(2, "tvp5150: read 0x%02x = 0x%02x\n", addr, buffer[0]);
119 static inline void tvp5150_write(struct i2c_client *c, unsigned char addr,
122 unsigned char buffer[2];
127 tvp5150_dbg(2, "tvp5150: writing 0x%02x 0x%02x\n", buffer[0], buffer[1]);
128 if (2 != (rc = i2c_master_send(c, buffer, 2)))
129 tvp5150_dbg(0, "i2c i/o error: rc == %d (should be 2)\n", rc);
132 static void dump_reg_range(struct i2c_client *c, char *s, u8 init, const u8 end,int max_line)
136 while (init!=(u8)(end+1)) {
137 if ((i%max_line) == 0) {
140 printk("tvp5150: %s reg 0x%02x = ",s,init);
142 printk("%02x ",tvp5150_read(c, init));
150 static void dump_reg(struct i2c_client *c)
152 printk("tvp5150: Video input source selection #1 = 0x%02x\n",
153 tvp5150_read(c, TVP5150_VD_IN_SRC_SEL_1));
154 printk("tvp5150: Analog channel controls = 0x%02x\n",
155 tvp5150_read(c, TVP5150_ANAL_CHL_CTL));
156 printk("tvp5150: Operation mode controls = 0x%02x\n",
157 tvp5150_read(c, TVP5150_OP_MODE_CTL));
158 printk("tvp5150: Miscellaneous controls = 0x%02x\n",
159 tvp5150_read(c, TVP5150_MISC_CTL));
160 printk("tvp5150: Autoswitch mask= 0x%02x\n",
161 tvp5150_read(c, TVP5150_AUTOSW_MSK));
162 printk("tvp5150: Color killer threshold control = 0x%02x\n",
163 tvp5150_read(c, TVP5150_COLOR_KIL_THSH_CTL));
164 printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
165 tvp5150_read(c, TVP5150_LUMA_PROC_CTL_1),
166 tvp5150_read(c, TVP5150_LUMA_PROC_CTL_2),
167 tvp5150_read(c, TVP5150_LUMA_PROC_CTL_3));
168 printk("tvp5150: Brightness control = 0x%02x\n",
169 tvp5150_read(c, TVP5150_BRIGHT_CTL));
170 printk("tvp5150: Color saturation control = 0x%02x\n",
171 tvp5150_read(c, TVP5150_SATURATION_CTL));
172 printk("tvp5150: Hue control = 0x%02x\n",
173 tvp5150_read(c, TVP5150_HUE_CTL));
174 printk("tvp5150: Contrast control = 0x%02x\n",
175 tvp5150_read(c, TVP5150_CONTRAST_CTL));
176 printk("tvp5150: Outputs and data rates select = 0x%02x\n",
177 tvp5150_read(c, TVP5150_DATA_RATE_SEL));
178 printk("tvp5150: Configuration shared pins = 0x%02x\n",
179 tvp5150_read(c, TVP5150_CONF_SHARED_PIN));
180 printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
181 tvp5150_read(c, TVP5150_ACT_VD_CROP_ST_MSB),
182 tvp5150_read(c, TVP5150_ACT_VD_CROP_ST_LSB));
183 printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
184 tvp5150_read(c, TVP5150_ACT_VD_CROP_STP_MSB),
185 tvp5150_read(c, TVP5150_ACT_VD_CROP_STP_LSB));
186 printk("tvp5150: Genlock/RTC = 0x%02x\n",
187 tvp5150_read(c, TVP5150_GENLOCK));
188 printk("tvp5150: Horizontal sync start = 0x%02x\n",
189 tvp5150_read(c, TVP5150_HORIZ_SYNC_START));
190 printk("tvp5150: Vertical blanking start = 0x%02x\n",
191 tvp5150_read(c, TVP5150_VERT_BLANKING_START));
192 printk("tvp5150: Vertical blanking stop = 0x%02x\n",
193 tvp5150_read(c, TVP5150_VERT_BLANKING_STOP));
194 printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
195 tvp5150_read(c, TVP5150_CHROMA_PROC_CTL_1),
196 tvp5150_read(c, TVP5150_CHROMA_PROC_CTL_2));
197 printk("tvp5150: Interrupt reset register B = 0x%02x\n",
198 tvp5150_read(c, TVP5150_INT_RESET_REG_B));
199 printk("tvp5150: Interrupt enable register B = 0x%02x\n",
200 tvp5150_read(c, TVP5150_INT_ENABLE_REG_B));
201 printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
202 tvp5150_read(c, TVP5150_INTT_CONFIG_REG_B));
203 printk("tvp5150: Video standard = 0x%02x\n",
204 tvp5150_read(c, TVP5150_VIDEO_STD));
205 printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
206 tvp5150_read(c, TVP5150_CB_GAIN_FACT),
207 tvp5150_read(c, TVP5150_CR_GAIN_FACTOR));
208 printk("tvp5150: Macrovision on counter = 0x%02x\n",
209 tvp5150_read(c, TVP5150_MACROVISION_ON_CTR));
210 printk("tvp5150: Macrovision off counter = 0x%02x\n",
211 tvp5150_read(c, TVP5150_MACROVISION_OFF_CTR));
212 printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
213 (tvp5150_read(c, TVP5150_REV_SELECT)&1)?3:4);
214 printk("tvp5150: Device ID = %02x%02x\n",
215 tvp5150_read(c, TVP5150_MSB_DEV_ID),
216 tvp5150_read(c, TVP5150_LSB_DEV_ID));
217 printk("tvp5150: ROM version = (hex) %02x.%02x\n",
218 tvp5150_read(c, TVP5150_ROM_MAJOR_VER),
219 tvp5150_read(c, TVP5150_ROM_MINOR_VER));
220 printk("tvp5150: Vertical line count = 0x%02x%02x\n",
221 tvp5150_read(c, TVP5150_VERT_LN_COUNT_MSB),
222 tvp5150_read(c, TVP5150_VERT_LN_COUNT_LSB));
223 printk("tvp5150: Interrupt status register B = 0x%02x\n",
224 tvp5150_read(c, TVP5150_INT_STATUS_REG_B));
225 printk("tvp5150: Interrupt active register B = 0x%02x\n",
226 tvp5150_read(c, TVP5150_INT_ACTIVE_REG_B));
227 printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
228 tvp5150_read(c, TVP5150_STATUS_REG_1),
229 tvp5150_read(c, TVP5150_STATUS_REG_2),
230 tvp5150_read(c, TVP5150_STATUS_REG_3),
231 tvp5150_read(c, TVP5150_STATUS_REG_4),
232 tvp5150_read(c, TVP5150_STATUS_REG_5));
234 dump_reg_range(c,"Teletext filter 1", TVP5150_TELETEXT_FIL1_INI,
235 TVP5150_TELETEXT_FIL1_END,8);
236 dump_reg_range(c,"Teletext filter 2", TVP5150_TELETEXT_FIL2_INI,
237 TVP5150_TELETEXT_FIL2_END,8);
239 printk("tvp5150: Teletext filter enable = 0x%02x\n",
240 tvp5150_read(c, TVP5150_TELETEXT_FIL_ENA));
241 printk("tvp5150: Interrupt status register A = 0x%02x\n",
242 tvp5150_read(c, TVP5150_INT_STATUS_REG_A));
243 printk("tvp5150: Interrupt enable register A = 0x%02x\n",
244 tvp5150_read(c, TVP5150_INT_ENABLE_REG_A));
245 printk("tvp5150: Interrupt configuration = 0x%02x\n",
246 tvp5150_read(c, TVP5150_INT_CONF));
247 printk("tvp5150: VDP status register = 0x%02x\n",
248 tvp5150_read(c, TVP5150_VDP_STATUS_REG));
249 printk("tvp5150: FIFO word count = 0x%02x\n",
250 tvp5150_read(c, TVP5150_FIFO_WORD_COUNT));
251 printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
252 tvp5150_read(c, TVP5150_FIFO_INT_THRESHOLD));
253 printk("tvp5150: FIFO reset = 0x%02x\n",
254 tvp5150_read(c, TVP5150_FIFO_RESET));
255 printk("tvp5150: Line number interrupt = 0x%02x\n",
256 tvp5150_read(c, TVP5150_LINE_NUMBER_INT));
257 printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
258 tvp5150_read(c, TVP5150_PIX_ALIGN_REG_HIGH),
259 tvp5150_read(c, TVP5150_PIX_ALIGN_REG_LOW));
260 printk("tvp5150: FIFO output control = 0x%02x\n",
261 tvp5150_read(c, TVP5150_FIFO_OUT_CTRL));
262 printk("tvp5150: Full field enable = 0x%02x\n",
263 tvp5150_read(c, TVP5150_FULL_FIELD_ENA));
264 printk("tvp5150: Full field mode register = 0x%02x\n",
265 tvp5150_read(c, TVP5150_FULL_FIELD_MODE_REG));
267 dump_reg_range(c,"CC data", TVP5150_CC_DATA_INI,
268 TVP5150_CC_DATA_END,8);
270 dump_reg_range(c,"WSS data", TVP5150_WSS_DATA_INI,
271 TVP5150_WSS_DATA_END,8);
273 dump_reg_range(c,"VPS data", TVP5150_VPS_DATA_INI,
274 TVP5150_VPS_DATA_END,8);
276 dump_reg_range(c,"VITC data", TVP5150_VITC_DATA_INI,
277 TVP5150_VITC_DATA_END,10);
279 dump_reg_range(c,"Line mode", TVP5150_LINE_MODE_INI,
280 TVP5150_LINE_MODE_END,8);
283 /****************************************************************************
285 ****************************************************************************/
287 TVP5150_ANALOG_CH0 = 0,
289 TVP5150_ANALOG_CH1 = 2,
290 TVP5150_BLACK_SCREEN = 8
293 static inline void tvp5150_selmux(struct i2c_client *c,
294 enum tvp5150_input input)
298 struct tvp5150 *decoder = i2c_get_clientdata(c);
300 if (!decoder->enable)
301 input |= TVP5150_BLACK_SCREEN;
304 case TVP5150_ANALOG_CH0:
305 case TVP5150_ANALOG_CH1:
306 opmode=0x30; /* TV Mode */
309 opmode=0; /* Auto Mode */
313 tvp5150_write(c, TVP5150_OP_MODE_CTL, opmode);
314 tvp5150_write(c, TVP5150_VD_IN_SRC_SEL_1, input);
317 struct i2c_reg_value {
322 /* Default values as sugested at TVP5150AM1 datasheet */
323 static const struct i2c_reg_value tvp5150_init_default[] = {
325 TVP5150_VD_IN_SRC_SEL_1,0x00
328 TVP5150_ANAL_CHL_CTL,0x15
331 TVP5150_OP_MODE_CTL,0x00
334 TVP5150_MISC_CTL,0x01
337 TVP5150_COLOR_KIL_THSH_CTL,0x10
340 TVP5150_LUMA_PROC_CTL_1,0x60
343 TVP5150_LUMA_PROC_CTL_2,0x00
346 TVP5150_BRIGHT_CTL,0x80
349 TVP5150_SATURATION_CTL,0x80
355 TVP5150_CONTRAST_CTL,0x80
358 TVP5150_DATA_RATE_SEL,0x47
361 TVP5150_LUMA_PROC_CTL_3,0x00
364 TVP5150_CONF_SHARED_PIN,0x08
367 TVP5150_ACT_VD_CROP_ST_MSB,0x00
370 TVP5150_ACT_VD_CROP_ST_LSB,0x00
373 TVP5150_ACT_VD_CROP_STP_MSB,0x00
376 TVP5150_ACT_VD_CROP_STP_LSB,0x00
382 TVP5150_HORIZ_SYNC_START,0x80
385 TVP5150_VERT_BLANKING_START,0x00
388 TVP5150_VERT_BLANKING_STOP,0x00
391 TVP5150_CHROMA_PROC_CTL_1,0x0c
394 TVP5150_CHROMA_PROC_CTL_2,0x14
397 TVP5150_INT_RESET_REG_B,0x00
400 TVP5150_INT_ENABLE_REG_B,0x00
403 TVP5150_INTT_CONFIG_REG_B,0x00
406 TVP5150_VIDEO_STD,0x00
409 TVP5150_MACROVISION_ON_CTR,0x0f
412 TVP5150_MACROVISION_OFF_CTR,0x01
415 TVP5150_TELETEXT_FIL_ENA,0x00
418 TVP5150_INT_STATUS_REG_A,0x00
421 TVP5150_INT_ENABLE_REG_A,0x00
424 TVP5150_INT_CONF,0x04
427 TVP5150_FIFO_INT_THRESHOLD,0x80
430 TVP5150_FIFO_RESET,0x00
433 TVP5150_LINE_NUMBER_INT,0x00
436 TVP5150_PIX_ALIGN_REG_LOW,0x4e
439 TVP5150_PIX_ALIGN_REG_HIGH,0x00
442 TVP5150_FIFO_OUT_CTRL,0x01
445 TVP5150_FULL_FIELD_ENA,0x00
448 TVP5150_LINE_MODE_INI,0x00
451 TVP5150_FULL_FIELD_MODE_REG,0x7f
458 /* Default values as sugested at TVP5150AM1 datasheet */
459 static const struct i2c_reg_value tvp5150_init_enable[] = {
461 TVP5150_CONF_SHARED_PIN, 2
462 },{ /* Automatic offset and AGC enabled */
463 TVP5150_ANAL_CHL_CTL, 0x15
464 },{ /* Activate YCrCb output 0x9 or 0xd ? */
465 TVP5150_MISC_CTL, 0x6f
466 },{ /* Activates video std autodetection for all standards */
467 TVP5150_AUTOSW_MSK, 0x0
468 },{ /* Default format: 0x47. For 4:2:2: 0x40 */
469 TVP5150_DATA_RATE_SEL, 0x47
471 TVP5150_CHROMA_PROC_CTL_1, 0x0c
473 TVP5150_CHROMA_PROC_CTL_2, 0x54
474 },{ /* Non documented, but initialized on WinTV USB2 */
481 struct tvp5150_vbi_type {
482 unsigned int vbi_type;
483 unsigned int ini_line;
484 unsigned int end_line;
485 unsigned int by_field :1;
488 struct i2c_vbi_ram_value {
490 struct tvp5150_vbi_type type;
491 unsigned char values[16];
494 /* This struct have the values for each supported VBI Standard
496 tvp5150_vbi_types should follow the same order as vbi_ram_default
497 * value 0 means rom position 0x10, value 1 means rom position 0x30
498 * and so on. There are 16 possible locations from 0 to 15.
501 static struct i2c_vbi_ram_value vbi_ram_default[] =
503 {0x010, /* Teletext, SECAM, WST System A */
504 {V4L2_SLICED_TELETEXT_SECAM,6,23,1},
505 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
506 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
508 {0x030, /* Teletext, PAL, WST System B */
509 {V4L2_SLICED_TELETEXT_PAL_B,6,22,1},
510 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
511 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
513 {0x050, /* Teletext, PAL, WST System C */
514 {V4L2_SLICED_TELETEXT_PAL_C,6,22,1},
515 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
516 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
518 {0x070, /* Teletext, NTSC, WST System B */
519 {V4L2_SLICED_TELETEXT_NTSC_B,10,21,1},
520 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
521 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
523 {0x090, /* Tetetext, NTSC NABTS System C */
524 {V4L2_SLICED_TELETEXT_NTSC_C,10,21,1},
525 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
526 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
528 {0x0b0, /* Teletext, NTSC-J, NABTS System D */
529 {V4L2_SLICED_TELETEXT_NTSC_D,10,21,1},
530 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
531 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
533 {0x0d0, /* Closed Caption, PAL/SECAM */
534 {V4L2_SLICED_CAPTION_625,22,22,1},
535 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
536 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
538 {0x0f0, /* Closed Caption, NTSC */
539 {V4L2_SLICED_CAPTION_525,21,21,1},
540 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
541 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
543 {0x110, /* Wide Screen Signal, PAL/SECAM */
544 {V4L2_SLICED_WSS_625,23,23,1},
545 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
546 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
548 {0x130, /* Wide Screen Signal, NTSC C */
549 {V4L2_SLICED_WSS_525,20,20,1},
550 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
551 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
553 {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
554 {V4l2_SLICED_VITC_625,6,22,0},
555 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
556 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
558 {0x170, /* Vertical Interval Timecode (VITC), NTSC */
559 {V4l2_SLICED_VITC_525,10,20,0},
560 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
561 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
563 {0x190, /* Video Program System (VPS), PAL */
564 {V4L2_SLICED_VPS,16,16,0},
565 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
566 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
568 /* 0x1d0 User programmable */
574 static int tvp5150_write_inittab(struct i2c_client *c,
575 const struct i2c_reg_value *regs)
577 while (regs->reg != 0xff) {
578 tvp5150_write(c, regs->reg, regs->value);
584 static int tvp5150_vdp_init(struct i2c_client *c,
585 const struct i2c_vbi_ram_value *regs)
589 /* Disable Full Field */
590 tvp5150_write(c, TVP5150_FULL_FIELD_ENA, 0);
592 /* Before programming, Line mode should be at 0xff */
593 for (i=TVP5150_LINE_MODE_INI; i<=TVP5150_LINE_MODE_END; i++)
594 tvp5150_write(c, i, 0xff);
597 while (regs->reg != (u16)-1 ) {
598 tvp5150_write(c, TVP5150_CONF_RAM_ADDR_HIGH,regs->reg>>8);
599 tvp5150_write(c, TVP5150_CONF_RAM_ADDR_LOW,regs->reg);
602 tvp5150_write(c, TVP5150_VDP_CONF_RAM_DATA,regs->values[i]);
609 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
610 static void tvp5150_vbi_get_cap(const struct i2c_vbi_ram_value *regs,
611 struct v4l2_sliced_vbi_cap *cap)
615 memset(cap, 0, sizeof *cap);
617 while (regs->reg != (u16)-1 ) {
618 for (line=regs->type.ini_line;line<=regs->type.end_line;line++) {
619 cap->service_lines[0][line] |= regs->type.vbi_type;
621 cap->service_set |= regs->type.vbi_type;
627 /* Set vbi processing
628 * type - one of tvp5150_vbi_types
629 * line - line to gather data
630 * fields: bit 0 field1, bit 1, field2
631 * flags (default=0xf0) is a bitmask, were set means:
632 * bit 7: enable filtering null bytes on CC
633 * bit 6: send data also to FIFO
634 * bit 5: don't allow data with errors on FIFO
635 * bit 4: enable ECC when possible
636 * pix_align = pix alignment:
640 static int tvp5150_set_vbi(struct i2c_client *c,
641 const struct i2c_vbi_ram_value *regs,
642 unsigned int type,u8 flags, int line,
645 struct tvp5150 *decoder = i2c_get_clientdata(c);
646 v4l2_std_id std=decoder->norm;
650 if (std == V4L2_STD_ALL) {
651 tvp5150_err("VBI can't be configured without knowing number of lines\n");
653 } else if (std && V4L2_STD_625_50) {
654 /* Don't follow NTSC Line number convension */
661 while (regs->reg != (u16)-1 ) {
662 if ((type & regs->type.vbi_type) &&
663 (line>=regs->type.ini_line) &&
664 (line<=regs->type.end_line)) {
665 type=regs->type.vbi_type;
672 if (regs->reg == (u16)-1)
675 type=pos | (flags & 0xf0);
676 reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
679 tvp5150_write(c, reg, type);
683 tvp5150_write(c, reg+1, type);
689 static int tvp5150_get_vbi(struct i2c_client *c,
690 const struct i2c_vbi_ram_value *regs, int line)
692 struct tvp5150 *decoder = i2c_get_clientdata(c);
693 v4l2_std_id std=decoder->norm;
697 if (std == V4L2_STD_ALL) {
698 tvp5150_err("VBI can't be configured without knowing number of lines\n");
700 } else if (std && V4L2_STD_625_50) {
701 /* Don't follow NTSC Line number convension */
708 reg=((line-6)<<1)+TVP5150_LINE_MODE_INI;
710 pos=tvp5150_read(c, reg)&0x0f;
712 type=regs[pos].type.vbi_type;
714 pos=tvp5150_read(c, reg+1)&0x0f;
716 type|=regs[pos].type.vbi_type;
720 static int tvp5150_set_std(struct i2c_client *c, v4l2_std_id std)
722 struct tvp5150 *decoder = i2c_get_clientdata(c);
727 /* First tests should be against specific std */
729 if (std == V4L2_STD_ALL) {
730 fmt=0; /* Autodetect mode */
731 } else if (std & V4L2_STD_NTSC_443) {
733 } else if (std & V4L2_STD_PAL_M) {
735 } else if (std & (V4L2_STD_PAL_N| V4L2_STD_PAL_Nc)) {
738 /* Then, test against generic ones */
739 if (std & V4L2_STD_NTSC) {
741 } else if (std & V4L2_STD_PAL) {
743 } else if (std & V4L2_STD_SECAM) {
748 tvp5150_dbg(1,"Set video std register to %d.\n",fmt);
749 tvp5150_write(c, TVP5150_VIDEO_STD, fmt);
754 static inline void tvp5150_reset(struct i2c_client *c)
756 u8 msb_id, lsb_id, msb_rom, lsb_rom;
757 struct tvp5150 *decoder = i2c_get_clientdata(c);
759 msb_id=tvp5150_read(c,TVP5150_MSB_DEV_ID);
760 lsb_id=tvp5150_read(c,TVP5150_LSB_DEV_ID);
761 msb_rom=tvp5150_read(c,TVP5150_ROM_MAJOR_VER);
762 lsb_rom=tvp5150_read(c,TVP5150_ROM_MINOR_VER);
764 if ((msb_rom==4)&&(lsb_rom==0)) { /* Is TVP5150AM1 */
765 tvp5150_info("tvp%02x%02xam1 detected.\n",msb_id, lsb_id);
767 /* ITU-T BT.656.4 timing */
768 tvp5150_write(c,TVP5150_REV_SELECT,0);
770 if ((msb_rom==3)||(lsb_rom==0x21)) { /* Is TVP5150A */
771 tvp5150_info("tvp%02x%02xa detected.\n",msb_id, lsb_id);
773 tvp5150_info("*** unknown tvp%02x%02x chip detected.\n",msb_id,lsb_id);
774 tvp5150_info("*** Rom ver is %d.%d\n",msb_rom,lsb_rom);
778 /* Initializes TVP5150 to its default values */
779 tvp5150_write_inittab(c, tvp5150_init_default);
781 /* Initializes VDP registers */
782 tvp5150_vdp_init(c, vbi_ram_default);
784 /* Selects decoder input */
785 tvp5150_selmux(c, decoder->input);
787 /* Initializes TVP5150 to stream enabled values */
788 tvp5150_write_inittab(c, tvp5150_init_enable);
790 /* Initialize image preferences */
791 tvp5150_write(c, TVP5150_BRIGHT_CTL, decoder->bright >> 8);
792 tvp5150_write(c, TVP5150_CONTRAST_CTL, decoder->contrast >> 8);
793 tvp5150_write(c, TVP5150_SATURATION_CTL, decoder->contrast >> 8);
794 tvp5150_write(c, TVP5150_HUE_CTL, (decoder->hue - 32768) >> 8);
796 tvp5150_set_std(c, decoder->norm);
799 static int tvp5150_get_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
801 /* struct tvp5150 *decoder = i2c_get_clientdata(c); */
804 case V4L2_CID_BRIGHTNESS:
805 ctrl->value = tvp5150_read(c, TVP5150_BRIGHT_CTL);
807 case V4L2_CID_CONTRAST:
808 ctrl->value = tvp5150_read(c, TVP5150_CONTRAST_CTL);
810 case V4L2_CID_SATURATION:
811 ctrl->value = tvp5150_read(c, TVP5150_SATURATION_CTL);
814 ctrl->value = tvp5150_read(c, TVP5150_HUE_CTL);
820 static int tvp5150_set_ctrl(struct i2c_client *c, struct v4l2_control *ctrl)
822 /* struct tvp5150 *decoder = i2c_get_clientdata(c); */
825 case V4L2_CID_BRIGHTNESS:
826 tvp5150_write(c, TVP5150_BRIGHT_CTL, ctrl->value);
828 case V4L2_CID_CONTRAST:
829 tvp5150_write(c, TVP5150_CONTRAST_CTL, ctrl->value);
831 case V4L2_CID_SATURATION:
832 tvp5150_write(c, TVP5150_SATURATION_CTL, ctrl->value);
835 tvp5150_write(c, TVP5150_HUE_CTL, ctrl->value);
841 /****************************************************************************
843 ****************************************************************************/
844 static int tvp5150_command(struct i2c_client *c,
845 unsigned int cmd, void *arg)
847 struct tvp5150 *decoder = i2c_get_clientdata(c);
852 case VIDIOC_INT_RESET:
857 if (decoder->norm == *(v4l2_std_id *)arg)
859 return tvp5150_set_std(c, *(v4l2_std_id *)arg);
861 *(v4l2_std_id *)arg = decoder->norm;
864 case VIDIOC_G_SLICED_VBI_CAP:
866 struct v4l2_sliced_vbi_cap *cap = arg;
867 tvp5150_dbg(1, "VIDIOC_G_SLICED_VBI_CAP\n");
869 tvp5150_vbi_get_cap(vbi_ram_default, cap);
874 struct v4l2_format *fmt;
875 struct v4l2_sliced_vbi_format *svbi;
879 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
881 svbi = &fmt->fmt.sliced;
882 if (svbi->service_set != 0) {
883 for (i = 0; i <= 23; i++) {
884 svbi->service_lines[1][i] = 0;
886 svbi->service_lines[0][i]=tvp5150_set_vbi(c,
888 svbi->service_lines[0][i],0xf0,i,3);
891 tvp5150_write(c, TVP5150_FIFO_OUT_CTRL,1);
894 tvp5150_write(c, TVP5150_FIFO_OUT_CTRL,0);
896 /* Disable Full Field */
897 tvp5150_write(c, TVP5150_FULL_FIELD_ENA, 0);
899 /* Disable Line modes */
900 for (i=TVP5150_LINE_MODE_INI; i<=TVP5150_LINE_MODE_END; i++)
901 tvp5150_write(c, i, 0xff);
907 struct v4l2_format *fmt;
908 struct v4l2_sliced_vbi_format *svbi;
913 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
915 svbi = &fmt->fmt.sliced;
916 memset(svbi, 0, sizeof(*svbi));
918 for (i = 0; i <= 23; i++) {
919 svbi->service_lines[0][i]=tvp5150_get_vbi(c,
921 mask|=svbi->service_lines[0][i];
923 svbi->service_set=mask;
927 #ifdef CONFIG_VIDEO_ADV_DEBUG
928 case VIDIOC_INT_G_REGISTER:
930 struct v4l2_register *reg = arg;
932 if (reg->i2c_id != I2C_DRIVERID_TVP5150)
934 reg->val = tvp5150_read(c, reg->reg & 0xff);
938 case VIDIOC_INT_S_REGISTER:
940 struct v4l2_register *reg = arg;
942 if (reg->i2c_id != I2C_DRIVERID_TVP5150)
944 if (!capable(CAP_SYS_ADMIN))
946 tvp5150_write(c, reg->reg & 0xff, reg->val & 0xff);
951 case VIDIOC_LOG_STATUS:
956 case DECODER_GET_CAPABILITIES:
958 struct video_decoder_capability *cap = arg;
960 cap->flags = VIDEO_DECODER_PAL |
962 VIDEO_DECODER_SECAM |
963 VIDEO_DECODER_AUTO | VIDEO_DECODER_CCIR;
968 case DECODER_GET_STATUS:
973 status = tvp5150_read(c, 0x88);
975 res |= DECODER_STATUS_COLOR;
977 if(status&0x04 && status&0x02){
978 res |= DECODER_STATUS_GOOD;
984 case DECODER_SET_GPIO:
987 case DECODER_SET_VBI_BYPASS:
990 case DECODER_SET_NORM:
996 case VIDEO_MODE_NTSC:
1002 case VIDEO_MODE_SECAM:
1005 case VIDEO_MODE_AUTO:
1012 decoder->norm = *iarg;
1015 case DECODER_SET_INPUT:
1018 if (*iarg < 0 || *iarg > 3) {
1022 decoder->input = *iarg;
1023 tvp5150_selmux(c, decoder->input);
1027 case DECODER_SET_OUTPUT:
1031 /* not much choice of outputs */
1037 case DECODER_ENABLE_OUTPUT:
1041 decoder->enable = (*iarg != 0);
1043 tvp5150_selmux(c, decoder->input);
1047 case VIDIOC_QUERYCTRL:
1049 struct v4l2_queryctrl *qc = arg;
1052 tvp5150_dbg(1, "VIDIOC_QUERYCTRL called\n");
1054 for (i = 0; i < ARRAY_SIZE(tvp5150_qctrl); i++)
1055 if (qc->id && qc->id == tvp5150_qctrl[i].id) {
1056 memcpy(qc, &(tvp5150_qctrl[i]),
1065 struct v4l2_control *ctrl = arg;
1066 tvp5150_dbg(1, "VIDIOC_G_CTRL called\n");
1068 return tvp5150_get_ctrl(c, ctrl);
1072 struct v4l2_control *ctrl = arg;
1074 n = sizeof(tvp5150_qctrl) / sizeof(tvp5150_qctrl[0]);
1075 for (i = 0; i < n; i++)
1076 if (ctrl->id == tvp5150_qctrl[i].id) {
1078 tvp5150_qctrl[i].minimum
1080 tvp5150_qctrl[i].maximum)
1083 "VIDIOC_S_CTRL: id=%d, value=%d\n",
1084 ctrl->id, ctrl->value);
1085 return tvp5150_set_ctrl(c, ctrl);
1090 case DECODER_SET_PICTURE:
1092 struct video_picture *pic = arg;
1093 if (decoder->bright != pic->brightness) {
1094 /* We want 0 to 255 we get 0-65535 */
1095 decoder->bright = pic->brightness;
1096 tvp5150_write(c, TVP5150_BRIGHT_CTL,
1097 decoder->bright >> 8);
1099 if (decoder->contrast != pic->contrast) {
1100 /* We want 0 to 255 we get 0-65535 */
1101 decoder->contrast = pic->contrast;
1102 tvp5150_write(c, TVP5150_CONTRAST_CTL,
1103 decoder->contrast >> 8);
1105 if (decoder->sat != pic->colour) {
1106 /* We want 0 to 255 we get 0-65535 */
1107 decoder->sat = pic->colour;
1108 tvp5150_write(c, TVP5150_SATURATION_CTL,
1109 decoder->contrast >> 8);
1111 if (decoder->hue != pic->hue) {
1112 /* We want -128 to 127 we get 0-65535 */
1113 decoder->hue = pic->hue;
1114 tvp5150_write(c, TVP5150_HUE_CTL,
1115 (decoder->hue - 32768) >> 8);
1126 /****************************************************************************
1128 ****************************************************************************/
1129 static struct i2c_driver driver;
1131 static struct i2c_client client_template = {
1136 static int tvp5150_detect_client(struct i2c_adapter *adapter,
1137 int address, int kind)
1139 struct i2c_client *c;
1140 struct tvp5150 *core;
1145 "tvp5150.c: detecting tvp5150 client on address 0x%x\n",
1148 client_template.adapter = adapter;
1149 client_template.addr = address;
1151 /* Check if the adapter supports the needed features */
1152 if (!i2c_check_functionality
1154 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
1157 c = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
1160 memcpy(c, &client_template, sizeof(struct i2c_client));
1162 core = kzalloc(sizeof(struct tvp5150), GFP_KERNEL);
1167 i2c_set_clientdata(c, core);
1169 rv = i2c_attach_client(c);
1171 core->norm = V4L2_STD_ALL; /* Default is autodetect */
1174 core->bright = 32768;
1175 core->contrast = 32768;
1190 static int tvp5150_attach_adapter(struct i2c_adapter *adapter)
1194 "tvp5150.c: starting probe for adapter %s (0x%x)\n",
1195 adapter->name, adapter->id);
1196 return i2c_probe(adapter, &addr_data, &tvp5150_detect_client);
1199 static int tvp5150_detach_client(struct i2c_client *c)
1201 struct tvp5150 *decoder = i2c_get_clientdata(c);
1205 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1208 err = i2c_detach_client(c);
1219 /* ----------------------------------------------------------------------- */
1221 static struct i2c_driver driver = {
1225 .id = I2C_DRIVERID_TVP5150,
1227 .attach_adapter = tvp5150_attach_adapter,
1228 .detach_client = tvp5150_detach_client,
1230 .command = tvp5150_command,
1233 static int __init tvp5150_init(void)
1235 return i2c_add_driver(&driver);
1238 static void __exit tvp5150_exit(void)
1240 i2c_del_driver(&driver);
1243 module_init(tvp5150_init);
1244 module_exit(tvp5150_exit);