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