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/slab.h>
10 #include <linux/videodev2.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <media/v4l2-async.h>
14 #include <media/v4l2-device.h>
15 #include <media/tvp5150.h>
16 #include <media/v4l2-ctrls.h>
18 #include "tvp5150_reg.h"
20 #define TVP5150_H_MAX 720U
21 #define TVP5150_V_MAX_525_60 480U
22 #define TVP5150_V_MAX_OTHERS 576U
23 #define TVP5150_MAX_CROP_LEFT 511
24 #define TVP5150_MAX_CROP_TOP 127
25 #define TVP5150_CROP_SHIFT 2
27 MODULE_DESCRIPTION("Texas Instruments TVP5150A video decoder driver");
28 MODULE_AUTHOR("Mauro Carvalho Chehab");
29 MODULE_LICENSE("GPL");
33 module_param(debug
, int, 0644);
34 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
37 struct v4l2_subdev sd
;
38 struct v4l2_ctrl_handler hdl
;
39 struct v4l2_rect rect
;
41 v4l2_std_id norm
; /* Current set standard */
47 static inline struct tvp5150
*to_tvp5150(struct v4l2_subdev
*sd
)
49 return container_of(sd
, struct tvp5150
, sd
);
52 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
54 return &container_of(ctrl
->handler
, struct tvp5150
, hdl
)->sd
;
57 static int tvp5150_read(struct v4l2_subdev
*sd
, unsigned char addr
)
59 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
62 rc
= i2c_smbus_read_byte_data(c
, addr
);
64 v4l2_err(sd
, "i2c i/o error: rc == %d\n", rc
);
68 v4l2_dbg(2, debug
, sd
, "tvp5150: read 0x%02x = 0x%02x\n", addr
, rc
);
73 static inline void tvp5150_write(struct v4l2_subdev
*sd
, unsigned char addr
,
76 struct i2c_client
*c
= v4l2_get_subdevdata(sd
);
79 v4l2_dbg(2, debug
, sd
, "tvp5150: writing 0x%02x 0x%02x\n", addr
, value
);
80 rc
= i2c_smbus_write_byte_data(c
, addr
, value
);
82 v4l2_dbg(0, debug
, sd
, "i2c i/o error: rc == %d\n", rc
);
85 static void dump_reg_range(struct v4l2_subdev
*sd
, char *s
, u8 init
,
86 const u8 end
, int max_line
)
90 while (init
!= (u8
)(end
+ 1)) {
91 if ((i
% max_line
) == 0) {
94 printk("tvp5150: %s reg 0x%02x = ", s
, init
);
96 printk("%02x ", tvp5150_read(sd
, init
));
104 static int tvp5150_log_status(struct v4l2_subdev
*sd
)
106 printk("tvp5150: Video input source selection #1 = 0x%02x\n",
107 tvp5150_read(sd
, TVP5150_VD_IN_SRC_SEL_1
));
108 printk("tvp5150: Analog channel controls = 0x%02x\n",
109 tvp5150_read(sd
, TVP5150_ANAL_CHL_CTL
));
110 printk("tvp5150: Operation mode controls = 0x%02x\n",
111 tvp5150_read(sd
, TVP5150_OP_MODE_CTL
));
112 printk("tvp5150: Miscellaneous controls = 0x%02x\n",
113 tvp5150_read(sd
, TVP5150_MISC_CTL
));
114 printk("tvp5150: Autoswitch mask= 0x%02x\n",
115 tvp5150_read(sd
, TVP5150_AUTOSW_MSK
));
116 printk("tvp5150: Color killer threshold control = 0x%02x\n",
117 tvp5150_read(sd
, TVP5150_COLOR_KIL_THSH_CTL
));
118 printk("tvp5150: Luminance processing controls #1 #2 and #3 = %02x %02x %02x\n",
119 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_1
),
120 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_2
),
121 tvp5150_read(sd
, TVP5150_LUMA_PROC_CTL_3
));
122 printk("tvp5150: Brightness control = 0x%02x\n",
123 tvp5150_read(sd
, TVP5150_BRIGHT_CTL
));
124 printk("tvp5150: Color saturation control = 0x%02x\n",
125 tvp5150_read(sd
, TVP5150_SATURATION_CTL
));
126 printk("tvp5150: Hue control = 0x%02x\n",
127 tvp5150_read(sd
, TVP5150_HUE_CTL
));
128 printk("tvp5150: Contrast control = 0x%02x\n",
129 tvp5150_read(sd
, TVP5150_CONTRAST_CTL
));
130 printk("tvp5150: Outputs and data rates select = 0x%02x\n",
131 tvp5150_read(sd
, TVP5150_DATA_RATE_SEL
));
132 printk("tvp5150: Configuration shared pins = 0x%02x\n",
133 tvp5150_read(sd
, TVP5150_CONF_SHARED_PIN
));
134 printk("tvp5150: Active video cropping start = 0x%02x%02x\n",
135 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_ST_MSB
),
136 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_ST_LSB
));
137 printk("tvp5150: Active video cropping stop = 0x%02x%02x\n",
138 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_STP_MSB
),
139 tvp5150_read(sd
, TVP5150_ACT_VD_CROP_STP_LSB
));
140 printk("tvp5150: Genlock/RTC = 0x%02x\n",
141 tvp5150_read(sd
, TVP5150_GENLOCK
));
142 printk("tvp5150: Horizontal sync start = 0x%02x\n",
143 tvp5150_read(sd
, TVP5150_HORIZ_SYNC_START
));
144 printk("tvp5150: Vertical blanking start = 0x%02x\n",
145 tvp5150_read(sd
, TVP5150_VERT_BLANKING_START
));
146 printk("tvp5150: Vertical blanking stop = 0x%02x\n",
147 tvp5150_read(sd
, TVP5150_VERT_BLANKING_STOP
));
148 printk("tvp5150: Chrominance processing control #1 and #2 = %02x %02x\n",
149 tvp5150_read(sd
, TVP5150_CHROMA_PROC_CTL_1
),
150 tvp5150_read(sd
, TVP5150_CHROMA_PROC_CTL_2
));
151 printk("tvp5150: Interrupt reset register B = 0x%02x\n",
152 tvp5150_read(sd
, TVP5150_INT_RESET_REG_B
));
153 printk("tvp5150: Interrupt enable register B = 0x%02x\n",
154 tvp5150_read(sd
, TVP5150_INT_ENABLE_REG_B
));
155 printk("tvp5150: Interrupt configuration register B = 0x%02x\n",
156 tvp5150_read(sd
, TVP5150_INTT_CONFIG_REG_B
));
157 printk("tvp5150: Video standard = 0x%02x\n",
158 tvp5150_read(sd
, TVP5150_VIDEO_STD
));
159 printk("tvp5150: Chroma gain factor: Cb=0x%02x Cr=0x%02x\n",
160 tvp5150_read(sd
, TVP5150_CB_GAIN_FACT
),
161 tvp5150_read(sd
, TVP5150_CR_GAIN_FACTOR
));
162 printk("tvp5150: Macrovision on counter = 0x%02x\n",
163 tvp5150_read(sd
, TVP5150_MACROVISION_ON_CTR
));
164 printk("tvp5150: Macrovision off counter = 0x%02x\n",
165 tvp5150_read(sd
, TVP5150_MACROVISION_OFF_CTR
));
166 printk("tvp5150: ITU-R BT.656.%d timing(TVP5150AM1 only)\n",
167 (tvp5150_read(sd
, TVP5150_REV_SELECT
) & 1) ? 3 : 4);
168 printk("tvp5150: Device ID = %02x%02x\n",
169 tvp5150_read(sd
, TVP5150_MSB_DEV_ID
),
170 tvp5150_read(sd
, TVP5150_LSB_DEV_ID
));
171 printk("tvp5150: ROM version = (hex) %02x.%02x\n",
172 tvp5150_read(sd
, TVP5150_ROM_MAJOR_VER
),
173 tvp5150_read(sd
, TVP5150_ROM_MINOR_VER
));
174 printk("tvp5150: Vertical line count = 0x%02x%02x\n",
175 tvp5150_read(sd
, TVP5150_VERT_LN_COUNT_MSB
),
176 tvp5150_read(sd
, TVP5150_VERT_LN_COUNT_LSB
));
177 printk("tvp5150: Interrupt status register B = 0x%02x\n",
178 tvp5150_read(sd
, TVP5150_INT_STATUS_REG_B
));
179 printk("tvp5150: Interrupt active register B = 0x%02x\n",
180 tvp5150_read(sd
, TVP5150_INT_ACTIVE_REG_B
));
181 printk("tvp5150: Status regs #1 to #5 = %02x %02x %02x %02x %02x\n",
182 tvp5150_read(sd
, TVP5150_STATUS_REG_1
),
183 tvp5150_read(sd
, TVP5150_STATUS_REG_2
),
184 tvp5150_read(sd
, TVP5150_STATUS_REG_3
),
185 tvp5150_read(sd
, TVP5150_STATUS_REG_4
),
186 tvp5150_read(sd
, TVP5150_STATUS_REG_5
));
188 dump_reg_range(sd
, "Teletext filter 1", TVP5150_TELETEXT_FIL1_INI
,
189 TVP5150_TELETEXT_FIL1_END
, 8);
190 dump_reg_range(sd
, "Teletext filter 2", TVP5150_TELETEXT_FIL2_INI
,
191 TVP5150_TELETEXT_FIL2_END
, 8);
193 printk("tvp5150: Teletext filter enable = 0x%02x\n",
194 tvp5150_read(sd
, TVP5150_TELETEXT_FIL_ENA
));
195 printk("tvp5150: Interrupt status register A = 0x%02x\n",
196 tvp5150_read(sd
, TVP5150_INT_STATUS_REG_A
));
197 printk("tvp5150: Interrupt enable register A = 0x%02x\n",
198 tvp5150_read(sd
, TVP5150_INT_ENABLE_REG_A
));
199 printk("tvp5150: Interrupt configuration = 0x%02x\n",
200 tvp5150_read(sd
, TVP5150_INT_CONF
));
201 printk("tvp5150: VDP status register = 0x%02x\n",
202 tvp5150_read(sd
, TVP5150_VDP_STATUS_REG
));
203 printk("tvp5150: FIFO word count = 0x%02x\n",
204 tvp5150_read(sd
, TVP5150_FIFO_WORD_COUNT
));
205 printk("tvp5150: FIFO interrupt threshold = 0x%02x\n",
206 tvp5150_read(sd
, TVP5150_FIFO_INT_THRESHOLD
));
207 printk("tvp5150: FIFO reset = 0x%02x\n",
208 tvp5150_read(sd
, TVP5150_FIFO_RESET
));
209 printk("tvp5150: Line number interrupt = 0x%02x\n",
210 tvp5150_read(sd
, TVP5150_LINE_NUMBER_INT
));
211 printk("tvp5150: Pixel alignment register = 0x%02x%02x\n",
212 tvp5150_read(sd
, TVP5150_PIX_ALIGN_REG_HIGH
),
213 tvp5150_read(sd
, TVP5150_PIX_ALIGN_REG_LOW
));
214 printk("tvp5150: FIFO output control = 0x%02x\n",
215 tvp5150_read(sd
, TVP5150_FIFO_OUT_CTRL
));
216 printk("tvp5150: Full field enable = 0x%02x\n",
217 tvp5150_read(sd
, TVP5150_FULL_FIELD_ENA
));
218 printk("tvp5150: Full field mode register = 0x%02x\n",
219 tvp5150_read(sd
, TVP5150_FULL_FIELD_MODE_REG
));
221 dump_reg_range(sd
, "CC data", TVP5150_CC_DATA_INI
,
222 TVP5150_CC_DATA_END
, 8);
224 dump_reg_range(sd
, "WSS data", TVP5150_WSS_DATA_INI
,
225 TVP5150_WSS_DATA_END
, 8);
227 dump_reg_range(sd
, "VPS data", TVP5150_VPS_DATA_INI
,
228 TVP5150_VPS_DATA_END
, 8);
230 dump_reg_range(sd
, "VITC data", TVP5150_VITC_DATA_INI
,
231 TVP5150_VITC_DATA_END
, 10);
233 dump_reg_range(sd
, "Line mode", TVP5150_LINE_MODE_INI
,
234 TVP5150_LINE_MODE_END
, 8);
238 /****************************************************************************
240 ****************************************************************************/
242 static inline void tvp5150_selmux(struct v4l2_subdev
*sd
)
245 struct tvp5150
*decoder
= to_tvp5150(sd
);
249 if ((decoder
->output
& TVP5150_BLACK_SCREEN
) || !decoder
->enable
)
252 switch (decoder
->input
) {
253 case TVP5150_COMPOSITE1
:
256 case TVP5150_COMPOSITE0
:
264 v4l2_dbg(1, debug
, sd
, "Selecting video route: route input=%i, output=%i "
265 "=> tvp5150 input=%i, opmode=%i\n",
266 decoder
->input
, decoder
->output
,
269 tvp5150_write(sd
, TVP5150_OP_MODE_CTL
, opmode
);
270 tvp5150_write(sd
, TVP5150_VD_IN_SRC_SEL_1
, input
);
272 /* Svideo should enable YCrCb output and disable GPCL output
273 * For Composite and TV, it should be the reverse
275 val
= tvp5150_read(sd
, TVP5150_MISC_CTL
);
277 v4l2_err(sd
, "%s: failed with error = %d\n", __func__
, val
);
281 if (decoder
->input
== TVP5150_SVIDEO
)
282 val
= (val
& ~0x40) | 0x10;
284 val
= (val
& ~0x10) | 0x40;
285 tvp5150_write(sd
, TVP5150_MISC_CTL
, val
);
288 struct i2c_reg_value
{
293 /* Default values as sugested at TVP5150AM1 datasheet */
294 static const struct i2c_reg_value tvp5150_init_default
[] = {
296 TVP5150_VD_IN_SRC_SEL_1
,0x00
299 TVP5150_ANAL_CHL_CTL
,0x15
302 TVP5150_OP_MODE_CTL
,0x00
305 TVP5150_MISC_CTL
,0x01
308 TVP5150_COLOR_KIL_THSH_CTL
,0x10
311 TVP5150_LUMA_PROC_CTL_1
,0x60
314 TVP5150_LUMA_PROC_CTL_2
,0x00
317 TVP5150_BRIGHT_CTL
,0x80
320 TVP5150_SATURATION_CTL
,0x80
326 TVP5150_CONTRAST_CTL
,0x80
329 TVP5150_DATA_RATE_SEL
,0x47
332 TVP5150_LUMA_PROC_CTL_3
,0x00
335 TVP5150_CONF_SHARED_PIN
,0x08
338 TVP5150_ACT_VD_CROP_ST_MSB
,0x00
341 TVP5150_ACT_VD_CROP_ST_LSB
,0x00
344 TVP5150_ACT_VD_CROP_STP_MSB
,0x00
347 TVP5150_ACT_VD_CROP_STP_LSB
,0x00
353 TVP5150_HORIZ_SYNC_START
,0x80
356 TVP5150_VERT_BLANKING_START
,0x00
359 TVP5150_VERT_BLANKING_STOP
,0x00
362 TVP5150_CHROMA_PROC_CTL_1
,0x0c
365 TVP5150_CHROMA_PROC_CTL_2
,0x14
368 TVP5150_INT_RESET_REG_B
,0x00
371 TVP5150_INT_ENABLE_REG_B
,0x00
374 TVP5150_INTT_CONFIG_REG_B
,0x00
377 TVP5150_VIDEO_STD
,0x00
380 TVP5150_MACROVISION_ON_CTR
,0x0f
383 TVP5150_MACROVISION_OFF_CTR
,0x01
386 TVP5150_TELETEXT_FIL_ENA
,0x00
389 TVP5150_INT_STATUS_REG_A
,0x00
392 TVP5150_INT_ENABLE_REG_A
,0x00
395 TVP5150_INT_CONF
,0x04
398 TVP5150_FIFO_INT_THRESHOLD
,0x80
401 TVP5150_FIFO_RESET
,0x00
404 TVP5150_LINE_NUMBER_INT
,0x00
407 TVP5150_PIX_ALIGN_REG_LOW
,0x4e
410 TVP5150_PIX_ALIGN_REG_HIGH
,0x00
413 TVP5150_FIFO_OUT_CTRL
,0x01
416 TVP5150_FULL_FIELD_ENA
,0x00
419 TVP5150_LINE_MODE_INI
,0x00
422 TVP5150_FULL_FIELD_MODE_REG
,0x7f
429 /* Default values as sugested at TVP5150AM1 datasheet */
430 static const struct i2c_reg_value tvp5150_init_enable
[] = {
432 TVP5150_CONF_SHARED_PIN
, 2
433 },{ /* Automatic offset and AGC enabled */
434 TVP5150_ANAL_CHL_CTL
, 0x15
435 },{ /* Activate YCrCb output 0x9 or 0xd ? */
436 TVP5150_MISC_CTL
, 0x6f
437 },{ /* Activates video std autodetection for all standards */
438 TVP5150_AUTOSW_MSK
, 0x0
439 },{ /* Default format: 0x47. For 4:2:2: 0x40 */
440 TVP5150_DATA_RATE_SEL
, 0x47
442 TVP5150_CHROMA_PROC_CTL_1
, 0x0c
444 TVP5150_CHROMA_PROC_CTL_2
, 0x54
445 },{ /* Non documented, but initialized on WinTV USB2 */
452 struct tvp5150_vbi_type
{
453 unsigned int vbi_type
;
454 unsigned int ini_line
;
455 unsigned int end_line
;
456 unsigned int by_field
:1;
459 struct i2c_vbi_ram_value
{
461 struct tvp5150_vbi_type type
;
462 unsigned char values
[16];
465 /* This struct have the values for each supported VBI Standard
467 tvp5150_vbi_types should follow the same order as vbi_ram_default
468 * value 0 means rom position 0x10, value 1 means rom position 0x30
469 * and so on. There are 16 possible locations from 0 to 15.
472 static struct i2c_vbi_ram_value vbi_ram_default
[] =
474 /* FIXME: Current api doesn't handle all VBI types, those not
475 yet supported are placed under #if 0 */
477 {0x010, /* Teletext, SECAM, WST System A */
478 {V4L2_SLICED_TELETEXT_SECAM
,6,23,1},
479 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x26,
480 0xe6, 0xb4, 0x0e, 0x00, 0x00, 0x00, 0x10, 0x00 }
483 {0x030, /* Teletext, PAL, WST System B */
484 {V4L2_SLICED_TELETEXT_B
,6,22,1},
485 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x2b,
486 0xa6, 0x72, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00 }
489 {0x050, /* Teletext, PAL, WST System C */
490 {V4L2_SLICED_TELETEXT_PAL_C
,6,22,1},
491 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
492 0xa6, 0x98, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
494 {0x070, /* Teletext, NTSC, WST System B */
495 {V4L2_SLICED_TELETEXT_NTSC_B
,10,21,1},
496 { 0xaa, 0xaa, 0xff, 0xff, 0x27, 0x2e, 0x20, 0x23,
497 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
499 {0x090, /* Tetetext, NTSC NABTS System C */
500 {V4L2_SLICED_TELETEXT_NTSC_C
,10,21,1},
501 { 0xaa, 0xaa, 0xff, 0xff, 0xe7, 0x2e, 0x20, 0x22,
502 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x15, 0x00 }
504 {0x0b0, /* Teletext, NTSC-J, NABTS System D */
505 {V4L2_SLICED_TELETEXT_NTSC_D
,10,21,1},
506 { 0xaa, 0xaa, 0xff, 0xff, 0xa7, 0x2e, 0x20, 0x23,
507 0x69, 0x93, 0x0d, 0x00, 0x00, 0x00, 0x10, 0x00 }
509 {0x0d0, /* Closed Caption, PAL/SECAM */
510 {V4L2_SLICED_CAPTION_625
,22,22,1},
511 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
512 0xa6, 0x7b, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
515 {0x0f0, /* Closed Caption, NTSC */
516 {V4L2_SLICED_CAPTION_525
,21,21,1},
517 { 0xaa, 0x2a, 0xff, 0x3f, 0x04, 0x51, 0x6e, 0x02,
518 0x69, 0x8c, 0x09, 0x00, 0x00, 0x00, 0x27, 0x00 }
520 {0x110, /* Wide Screen Signal, PAL/SECAM */
521 {V4L2_SLICED_WSS_625
,23,23,1},
522 { 0x5b, 0x55, 0xc5, 0xff, 0x00, 0x71, 0x6e, 0x42,
523 0xa6, 0xcd, 0x0f, 0x00, 0x00, 0x00, 0x3a, 0x00 }
526 {0x130, /* Wide Screen Signal, NTSC C */
527 {V4L2_SLICED_WSS_525
,20,20,1},
528 { 0x38, 0x00, 0x3f, 0x00, 0x00, 0x71, 0x6e, 0x43,
529 0x69, 0x7c, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00 }
531 {0x150, /* Vertical Interval Timecode (VITC), PAL/SECAM */
532 {V4l2_SLICED_VITC_625
,6,22,0},
533 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
534 0xa6, 0x85, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
536 {0x170, /* Vertical Interval Timecode (VITC), NTSC */
537 {V4l2_SLICED_VITC_525
,10,20,0},
538 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x6d, 0x49,
539 0x69, 0x94, 0x08, 0x00, 0x00, 0x00, 0x4c, 0x00 }
542 {0x190, /* Video Program System (VPS), PAL */
543 {V4L2_SLICED_VPS
,16,16,0},
544 { 0xaa, 0xaa, 0xff, 0xff, 0xba, 0xce, 0x2b, 0x0d,
545 0xa6, 0xda, 0x0b, 0x00, 0x00, 0x00, 0x60, 0x00 }
547 /* 0x1d0 User programmable */
553 static int tvp5150_write_inittab(struct v4l2_subdev
*sd
,
554 const struct i2c_reg_value
*regs
)
556 while (regs
->reg
!= 0xff) {
557 tvp5150_write(sd
, regs
->reg
, regs
->value
);
563 static int tvp5150_vdp_init(struct v4l2_subdev
*sd
,
564 const struct i2c_vbi_ram_value
*regs
)
568 /* Disable Full Field */
569 tvp5150_write(sd
, TVP5150_FULL_FIELD_ENA
, 0);
571 /* Before programming, Line mode should be at 0xff */
572 for (i
= TVP5150_LINE_MODE_INI
; i
<= TVP5150_LINE_MODE_END
; i
++)
573 tvp5150_write(sd
, i
, 0xff);
576 while (regs
->reg
!= (u16
)-1) {
577 tvp5150_write(sd
, TVP5150_CONF_RAM_ADDR_HIGH
, regs
->reg
>> 8);
578 tvp5150_write(sd
, TVP5150_CONF_RAM_ADDR_LOW
, regs
->reg
);
580 for (i
= 0; i
< 16; i
++)
581 tvp5150_write(sd
, TVP5150_VDP_CONF_RAM_DATA
, regs
->values
[i
]);
588 /* Fills VBI capabilities based on i2c_vbi_ram_value struct */
589 static int tvp5150_g_sliced_vbi_cap(struct v4l2_subdev
*sd
,
590 struct v4l2_sliced_vbi_cap
*cap
)
592 const struct i2c_vbi_ram_value
*regs
= vbi_ram_default
;
595 v4l2_dbg(1, debug
, sd
, "g_sliced_vbi_cap\n");
596 memset(cap
, 0, sizeof *cap
);
598 while (regs
->reg
!= (u16
)-1 ) {
599 for (line
=regs
->type
.ini_line
;line
<=regs
->type
.end_line
;line
++) {
600 cap
->service_lines
[0][line
] |= regs
->type
.vbi_type
;
602 cap
->service_set
|= regs
->type
.vbi_type
;
609 /* Set vbi processing
610 * type - one of tvp5150_vbi_types
611 * line - line to gather data
612 * fields: bit 0 field1, bit 1, field2
613 * flags (default=0xf0) is a bitmask, were set means:
614 * bit 7: enable filtering null bytes on CC
615 * bit 6: send data also to FIFO
616 * bit 5: don't allow data with errors on FIFO
617 * bit 4: enable ECC when possible
618 * pix_align = pix alignment:
622 static int tvp5150_set_vbi(struct v4l2_subdev
*sd
,
623 const struct i2c_vbi_ram_value
*regs
,
624 unsigned int type
,u8 flags
, int line
,
627 struct tvp5150
*decoder
= to_tvp5150(sd
);
628 v4l2_std_id std
= decoder
->norm
;
632 if (std
== V4L2_STD_ALL
) {
633 v4l2_err(sd
, "VBI can't be configured without knowing number of lines\n");
635 } else if (std
& V4L2_STD_625_50
) {
636 /* Don't follow NTSC Line number convension */
643 while (regs
->reg
!= (u16
)-1 ) {
644 if ((type
& regs
->type
.vbi_type
) &&
645 (line
>=regs
->type
.ini_line
) &&
646 (line
<=regs
->type
.end_line
)) {
647 type
=regs
->type
.vbi_type
;
654 if (regs
->reg
== (u16
)-1)
657 type
=pos
| (flags
& 0xf0);
658 reg
=((line
-6)<<1)+TVP5150_LINE_MODE_INI
;
661 tvp5150_write(sd
, reg
, type
);
665 tvp5150_write(sd
, reg
+1, type
);
671 static int tvp5150_get_vbi(struct v4l2_subdev
*sd
,
672 const struct i2c_vbi_ram_value
*regs
, int line
)
674 struct tvp5150
*decoder
= to_tvp5150(sd
);
675 v4l2_std_id std
= decoder
->norm
;
680 if (std
== V4L2_STD_ALL
) {
681 v4l2_err(sd
, "VBI can't be configured without knowing number of lines\n");
683 } else if (std
& V4L2_STD_625_50
) {
684 /* Don't follow NTSC Line number convension */
688 if (line
< 6 || line
> 27)
691 reg
= ((line
- 6) << 1) + TVP5150_LINE_MODE_INI
;
693 for (i
= 0; i
<= 1; i
++) {
694 ret
= tvp5150_read(sd
, reg
+ i
);
696 v4l2_err(sd
, "%s: failed with error = %d\n",
702 type
|= regs
[pos
].type
.vbi_type
;
708 static int tvp5150_set_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
710 struct tvp5150
*decoder
= to_tvp5150(sd
);
715 /* First tests should be against specific std */
717 if (std
== V4L2_STD_NTSC_443
) {
718 fmt
= VIDEO_STD_NTSC_4_43_BIT
;
719 } else if (std
== V4L2_STD_PAL_M
) {
720 fmt
= VIDEO_STD_PAL_M_BIT
;
721 } else if (std
== V4L2_STD_PAL_N
|| std
== V4L2_STD_PAL_Nc
) {
722 fmt
= VIDEO_STD_PAL_COMBINATION_N_BIT
;
724 /* Then, test against generic ones */
725 if (std
& V4L2_STD_NTSC
)
726 fmt
= VIDEO_STD_NTSC_MJ_BIT
;
727 else if (std
& V4L2_STD_PAL
)
728 fmt
= VIDEO_STD_PAL_BDGHIN_BIT
;
729 else if (std
& V4L2_STD_SECAM
)
730 fmt
= VIDEO_STD_SECAM_BIT
;
733 v4l2_dbg(1, debug
, sd
, "Set video std register to %d.\n", fmt
);
734 tvp5150_write(sd
, TVP5150_VIDEO_STD
, fmt
);
738 static int tvp5150_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
740 struct tvp5150
*decoder
= to_tvp5150(sd
);
742 if (decoder
->norm
== std
)
745 /* Change cropping height limits */
746 if (std
& V4L2_STD_525_60
)
747 decoder
->rect
.height
= TVP5150_V_MAX_525_60
;
749 decoder
->rect
.height
= TVP5150_V_MAX_OTHERS
;
752 return tvp5150_set_std(sd
, std
);
755 static int tvp5150_reset(struct v4l2_subdev
*sd
, u32 val
)
757 struct tvp5150
*decoder
= to_tvp5150(sd
);
759 /* Initializes TVP5150 to its default values */
760 tvp5150_write_inittab(sd
, tvp5150_init_default
);
762 /* Initializes VDP registers */
763 tvp5150_vdp_init(sd
, vbi_ram_default
);
765 /* Selects decoder input */
768 /* Initializes TVP5150 to stream enabled values */
769 tvp5150_write_inittab(sd
, tvp5150_init_enable
);
771 /* Initialize image preferences */
772 v4l2_ctrl_handler_setup(&decoder
->hdl
);
774 tvp5150_set_std(sd
, decoder
->norm
);
778 static int tvp5150_s_ctrl(struct v4l2_ctrl
*ctrl
)
780 struct v4l2_subdev
*sd
= to_sd(ctrl
);
783 case V4L2_CID_BRIGHTNESS
:
784 tvp5150_write(sd
, TVP5150_BRIGHT_CTL
, ctrl
->val
);
786 case V4L2_CID_CONTRAST
:
787 tvp5150_write(sd
, TVP5150_CONTRAST_CTL
, ctrl
->val
);
789 case V4L2_CID_SATURATION
:
790 tvp5150_write(sd
, TVP5150_SATURATION_CTL
, ctrl
->val
);
793 tvp5150_write(sd
, TVP5150_HUE_CTL
, ctrl
->val
);
799 static v4l2_std_id
tvp5150_read_std(struct v4l2_subdev
*sd
)
801 int val
= tvp5150_read(sd
, TVP5150_STATUS_REG_5
);
803 switch (val
& 0x0F) {
805 return V4L2_STD_NTSC
;
809 return V4L2_STD_PAL_M
;
811 return V4L2_STD_PAL_N
| V4L2_STD_PAL_Nc
;
813 return V4L2_STD_NTSC_443
;
815 return V4L2_STD_SECAM
;
817 return V4L2_STD_UNKNOWN
;
821 static int tvp5150_enum_mbus_code(struct v4l2_subdev
*sd
,
822 struct v4l2_subdev_pad_config
*cfg
,
823 struct v4l2_subdev_mbus_code_enum
*code
)
825 if (code
->pad
|| code
->index
)
828 code
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
832 static int tvp5150_fill_fmt(struct v4l2_subdev
*sd
,
833 struct v4l2_subdev_pad_config
*cfg
,
834 struct v4l2_subdev_format
*format
)
836 struct v4l2_mbus_framefmt
*f
;
837 struct tvp5150
*decoder
= to_tvp5150(sd
);
839 if (!format
|| format
->pad
)
844 tvp5150_reset(sd
, 0);
846 f
->width
= decoder
->rect
.width
;
847 f
->height
= decoder
->rect
.height
;
849 f
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
850 f
->field
= V4L2_FIELD_SEQ_TB
;
851 f
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
853 v4l2_dbg(1, debug
, sd
, "width = %d, height = %d\n", f
->width
,
858 static int tvp5150_s_crop(struct v4l2_subdev
*sd
, const struct v4l2_crop
*a
)
860 struct v4l2_rect rect
= a
->c
;
861 struct tvp5150
*decoder
= to_tvp5150(sd
);
865 v4l2_dbg(1, debug
, sd
, "%s left=%d, top=%d, width=%d, height=%d\n",
866 __func__
, rect
.left
, rect
.top
, rect
.width
, rect
.height
);
868 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
871 /* tvp5150 has some special limits */
872 rect
.left
= clamp(rect
.left
, 0, TVP5150_MAX_CROP_LEFT
);
873 rect
.width
= clamp_t(unsigned int, rect
.width
,
874 TVP5150_H_MAX
- TVP5150_MAX_CROP_LEFT
- rect
.left
,
875 TVP5150_H_MAX
- rect
.left
);
876 rect
.top
= clamp(rect
.top
, 0, TVP5150_MAX_CROP_TOP
);
878 /* Calculate height based on current standard */
879 if (decoder
->norm
== V4L2_STD_ALL
)
880 std
= tvp5150_read_std(sd
);
884 if (std
& V4L2_STD_525_60
)
885 hmax
= TVP5150_V_MAX_525_60
;
887 hmax
= TVP5150_V_MAX_OTHERS
;
889 rect
.height
= clamp_t(unsigned int, rect
.height
,
890 hmax
- TVP5150_MAX_CROP_TOP
- rect
.top
,
893 tvp5150_write(sd
, TVP5150_VERT_BLANKING_START
, rect
.top
);
894 tvp5150_write(sd
, TVP5150_VERT_BLANKING_STOP
,
895 rect
.top
+ rect
.height
- hmax
);
896 tvp5150_write(sd
, TVP5150_ACT_VD_CROP_ST_MSB
,
897 rect
.left
>> TVP5150_CROP_SHIFT
);
898 tvp5150_write(sd
, TVP5150_ACT_VD_CROP_ST_LSB
,
899 rect
.left
| (1 << TVP5150_CROP_SHIFT
));
900 tvp5150_write(sd
, TVP5150_ACT_VD_CROP_STP_MSB
,
901 (rect
.left
+ rect
.width
- TVP5150_MAX_CROP_LEFT
) >>
903 tvp5150_write(sd
, TVP5150_ACT_VD_CROP_STP_LSB
,
904 rect
.left
+ rect
.width
- TVP5150_MAX_CROP_LEFT
);
906 decoder
->rect
= rect
;
911 static int tvp5150_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
913 struct tvp5150
*decoder
= to_tvp5150(sd
);
915 a
->c
= decoder
->rect
;
916 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
921 static int tvp5150_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
923 struct tvp5150
*decoder
= to_tvp5150(sd
);
926 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
931 a
->bounds
.width
= TVP5150_H_MAX
;
933 /* Calculate height based on current standard */
934 if (decoder
->norm
== V4L2_STD_ALL
)
935 std
= tvp5150_read_std(sd
);
939 if (std
& V4L2_STD_525_60
)
940 a
->bounds
.height
= TVP5150_V_MAX_525_60
;
942 a
->bounds
.height
= TVP5150_V_MAX_OTHERS
;
944 a
->defrect
= a
->bounds
;
945 a
->pixelaspect
.numerator
= 1;
946 a
->pixelaspect
.denominator
= 1;
951 /****************************************************************************
953 ****************************************************************************/
955 static int tvp5150_s_routing(struct v4l2_subdev
*sd
,
956 u32 input
, u32 output
, u32 config
)
958 struct tvp5150
*decoder
= to_tvp5150(sd
);
960 decoder
->input
= input
;
961 decoder
->output
= output
;
966 static int tvp5150_s_raw_fmt(struct v4l2_subdev
*sd
, struct v4l2_vbi_format
*fmt
)
968 /* this is for capturing 36 raw vbi lines
969 if there's a way to cut off the beginning 2 vbi lines
970 with the tvp5150 then the vbi line count could be lowered
971 to 17 lines/field again, although I couldn't find a register
972 which could do that cropping */
973 if (fmt
->sample_format
== V4L2_PIX_FMT_GREY
)
974 tvp5150_write(sd
, TVP5150_LUMA_PROC_CTL_1
, 0x70);
975 if (fmt
->count
[0] == 18 && fmt
->count
[1] == 18) {
976 tvp5150_write(sd
, TVP5150_VERT_BLANKING_START
, 0x00);
977 tvp5150_write(sd
, TVP5150_VERT_BLANKING_STOP
, 0x01);
982 static int tvp5150_s_sliced_fmt(struct v4l2_subdev
*sd
, struct v4l2_sliced_vbi_format
*svbi
)
986 if (svbi
->service_set
!= 0) {
987 for (i
= 0; i
<= 23; i
++) {
988 svbi
->service_lines
[1][i
] = 0;
989 svbi
->service_lines
[0][i
] =
990 tvp5150_set_vbi(sd
, vbi_ram_default
,
991 svbi
->service_lines
[0][i
], 0xf0, i
, 3);
994 tvp5150_write(sd
, TVP5150_FIFO_OUT_CTRL
, 1);
997 tvp5150_write(sd
, TVP5150_FIFO_OUT_CTRL
, 0);
999 /* Disable Full Field */
1000 tvp5150_write(sd
, TVP5150_FULL_FIELD_ENA
, 0);
1002 /* Disable Line modes */
1003 for (i
= TVP5150_LINE_MODE_INI
; i
<= TVP5150_LINE_MODE_END
; i
++)
1004 tvp5150_write(sd
, i
, 0xff);
1009 static int tvp5150_g_sliced_fmt(struct v4l2_subdev
*sd
, struct v4l2_sliced_vbi_format
*svbi
)
1013 memset(svbi
->service_lines
, 0, sizeof(svbi
->service_lines
));
1015 for (i
= 0; i
<= 23; i
++) {
1016 svbi
->service_lines
[0][i
] =
1017 tvp5150_get_vbi(sd
, vbi_ram_default
, i
);
1018 mask
|= svbi
->service_lines
[0][i
];
1020 svbi
->service_set
= mask
;
1024 #ifdef CONFIG_VIDEO_ADV_DEBUG
1025 static int tvp5150_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
1029 res
= tvp5150_read(sd
, reg
->reg
& 0xff);
1031 v4l2_err(sd
, "%s: failed with error = %d\n", __func__
, res
);
1040 static int tvp5150_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
1042 tvp5150_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xff);
1047 static int tvp5150_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1049 int status
= tvp5150_read(sd
, 0x88);
1051 vt
->signal
= ((status
& 0x04) && (status
& 0x02)) ? 0xffff : 0x0;
1055 /* ----------------------------------------------------------------------- */
1057 static const struct v4l2_ctrl_ops tvp5150_ctrl_ops
= {
1058 .s_ctrl
= tvp5150_s_ctrl
,
1061 static const struct v4l2_subdev_core_ops tvp5150_core_ops
= {
1062 .log_status
= tvp5150_log_status
,
1063 .reset
= tvp5150_reset
,
1064 #ifdef CONFIG_VIDEO_ADV_DEBUG
1065 .g_register
= tvp5150_g_register
,
1066 .s_register
= tvp5150_s_register
,
1070 static const struct v4l2_subdev_tuner_ops tvp5150_tuner_ops
= {
1071 .g_tuner
= tvp5150_g_tuner
,
1074 static const struct v4l2_subdev_video_ops tvp5150_video_ops
= {
1075 .s_std
= tvp5150_s_std
,
1076 .s_routing
= tvp5150_s_routing
,
1077 .s_crop
= tvp5150_s_crop
,
1078 .g_crop
= tvp5150_g_crop
,
1079 .cropcap
= tvp5150_cropcap
,
1082 static const struct v4l2_subdev_vbi_ops tvp5150_vbi_ops
= {
1083 .g_sliced_vbi_cap
= tvp5150_g_sliced_vbi_cap
,
1084 .g_sliced_fmt
= tvp5150_g_sliced_fmt
,
1085 .s_sliced_fmt
= tvp5150_s_sliced_fmt
,
1086 .s_raw_fmt
= tvp5150_s_raw_fmt
,
1089 static const struct v4l2_subdev_pad_ops tvp5150_pad_ops
= {
1090 .enum_mbus_code
= tvp5150_enum_mbus_code
,
1091 .set_fmt
= tvp5150_fill_fmt
,
1092 .get_fmt
= tvp5150_fill_fmt
,
1095 static const struct v4l2_subdev_ops tvp5150_ops
= {
1096 .core
= &tvp5150_core_ops
,
1097 .tuner
= &tvp5150_tuner_ops
,
1098 .video
= &tvp5150_video_ops
,
1099 .vbi
= &tvp5150_vbi_ops
,
1100 .pad
= &tvp5150_pad_ops
,
1104 /****************************************************************************
1106 ****************************************************************************/
1108 static int tvp5150_probe(struct i2c_client
*c
,
1109 const struct i2c_device_id
*id
)
1111 struct tvp5150
*core
;
1112 struct v4l2_subdev
*sd
;
1116 /* Check if the adapter supports the needed features */
1117 if (!i2c_check_functionality(c
->adapter
,
1118 I2C_FUNC_SMBUS_READ_BYTE
| I2C_FUNC_SMBUS_WRITE_BYTE_DATA
))
1121 core
= devm_kzalloc(&c
->dev
, sizeof(*core
), GFP_KERNEL
);
1125 v4l2_i2c_subdev_init(sd
, c
, &tvp5150_ops
);
1128 * Read consequent registers - TVP5150_MSB_DEV_ID, TVP5150_LSB_DEV_ID,
1129 * TVP5150_ROM_MAJOR_VER, TVP5150_ROM_MINOR_VER
1131 for (i
= 0; i
< 4; i
++) {
1132 res
= tvp5150_read(sd
, TVP5150_MSB_DEV_ID
+ i
);
1135 tvp5150_id
[i
] = res
;
1138 v4l_info(c
, "chip found @ 0x%02x (%s)\n",
1139 c
->addr
<< 1, c
->adapter
->name
);
1141 if (tvp5150_id
[2] == 4 && tvp5150_id
[3] == 0) { /* Is TVP5150AM1 */
1142 v4l2_info(sd
, "tvp%02x%02xam1 detected.\n",
1143 tvp5150_id
[0], tvp5150_id
[1]);
1145 /* ITU-T BT.656.4 timing */
1146 tvp5150_write(sd
, TVP5150_REV_SELECT
, 0);
1149 if (tvp5150_id
[2] == 3 || tvp5150_id
[3] == 0x21) {
1150 v4l2_info(sd
, "tvp%02x%02xa detected.\n",
1151 tvp5150_id
[0], tvp5150_id
[1]);
1153 v4l2_info(sd
, "*** unknown tvp%02x%02x chip detected.\n",
1154 tvp5150_id
[0], tvp5150_id
[1]);
1155 v4l2_info(sd
, "*** Rom ver is %d.%d\n",
1156 tvp5150_id
[2], tvp5150_id
[3]);
1160 core
->norm
= V4L2_STD_ALL
; /* Default is autodetect */
1161 core
->input
= TVP5150_COMPOSITE1
;
1164 v4l2_ctrl_handler_init(&core
->hdl
, 4);
1165 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1166 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
1167 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1168 V4L2_CID_CONTRAST
, 0, 255, 1, 128);
1169 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1170 V4L2_CID_SATURATION
, 0, 255, 1, 128);
1171 v4l2_ctrl_new_std(&core
->hdl
, &tvp5150_ctrl_ops
,
1172 V4L2_CID_HUE
, -128, 127, 1, 0);
1173 sd
->ctrl_handler
= &core
->hdl
;
1174 if (core
->hdl
.error
) {
1175 res
= core
->hdl
.error
;
1178 v4l2_ctrl_handler_setup(&core
->hdl
);
1180 /* Default is no cropping */
1182 if (tvp5150_read_std(sd
) & V4L2_STD_525_60
)
1183 core
->rect
.height
= TVP5150_V_MAX_525_60
;
1185 core
->rect
.height
= TVP5150_V_MAX_OTHERS
;
1186 core
->rect
.left
= 0;
1187 core
->rect
.width
= TVP5150_H_MAX
;
1189 res
= v4l2_async_register_subdev(sd
);
1194 tvp5150_log_status(sd
);
1198 v4l2_ctrl_handler_free(&core
->hdl
);
1202 static int tvp5150_remove(struct i2c_client
*c
)
1204 struct v4l2_subdev
*sd
= i2c_get_clientdata(c
);
1205 struct tvp5150
*decoder
= to_tvp5150(sd
);
1207 v4l2_dbg(1, debug
, sd
,
1208 "tvp5150.c: removing tvp5150 adapter on address 0x%x\n",
1211 v4l2_async_unregister_subdev(sd
);
1212 v4l2_ctrl_handler_free(&decoder
->hdl
);
1216 /* ----------------------------------------------------------------------- */
1218 static const struct i2c_device_id tvp5150_id
[] = {
1222 MODULE_DEVICE_TABLE(i2c
, tvp5150_id
);
1224 static struct i2c_driver tvp5150_driver
= {
1228 .probe
= tvp5150_probe
,
1229 .remove
= tvp5150_remove
,
1230 .id_table
= tvp5150_id
,
1233 module_i2c_driver(tvp5150_driver
);