2 * vpx3220a, vpx3216b & vpx3214c video decoder driver version 0.0.1
4 * Copyright (C) 2001 Laurent Pinchart <lpinchart@freegates.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/types.h>
25 #include <asm/uaccess.h>
26 #include <linux/i2c.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-chip-ident.h>
30 #include <media/v4l2-i2c-drv.h>
32 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
33 MODULE_AUTHOR("Laurent Pinchart");
34 MODULE_LICENSE("GPL");
37 module_param(debug
, int, 0);
38 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
41 #define VPX_TIMEOUT_COUNT 10
43 /* ----------------------------------------------------------------------- */
46 struct v4l2_subdev sd
;
47 unsigned char reg
[255];
59 static inline struct vpx3220
*to_vpx3220(struct v4l2_subdev
*sd
)
61 return container_of(sd
, struct vpx3220
, sd
);
64 static char *inputs
[] = { "internal", "composite", "svideo" };
66 /* ----------------------------------------------------------------------- */
68 static inline int vpx3220_write(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
70 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
71 struct vpx3220
*decoder
= i2c_get_clientdata(client
);
73 decoder
->reg
[reg
] = value
;
74 return i2c_smbus_write_byte_data(client
, reg
, value
);
77 static inline int vpx3220_read(struct v4l2_subdev
*sd
, u8 reg
)
79 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
81 return i2c_smbus_read_byte_data(client
, reg
);
84 static int vpx3220_fp_status(struct v4l2_subdev
*sd
)
89 for (i
= 0; i
< VPX_TIMEOUT_COUNT
; i
++) {
90 status
= vpx3220_read(sd
, 0x29);
104 static int vpx3220_fp_write(struct v4l2_subdev
*sd
, u8 fpaddr
, u16 data
)
106 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
108 /* Write the 16-bit address to the FPWR register */
109 if (i2c_smbus_write_word_data(client
, 0x27, swab16(fpaddr
)) == -1) {
110 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
114 if (vpx3220_fp_status(sd
) < 0)
117 /* Write the 16-bit data to the FPDAT register */
118 if (i2c_smbus_write_word_data(client
, 0x28, swab16(data
)) == -1) {
119 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
126 static u16
vpx3220_fp_read(struct v4l2_subdev
*sd
, u16 fpaddr
)
128 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
131 /* Write the 16-bit address to the FPRD register */
132 if (i2c_smbus_write_word_data(client
, 0x26, swab16(fpaddr
)) == -1) {
133 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
137 if (vpx3220_fp_status(sd
) < 0)
140 /* Read the 16-bit data from the FPDAT register */
141 data
= i2c_smbus_read_word_data(client
, 0x28);
143 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
150 static int vpx3220_write_block(struct v4l2_subdev
*sd
, const u8
*data
, unsigned int len
)
157 ret
= vpx3220_write(sd
, reg
, *data
++);
166 static int vpx3220_write_fp_block(struct v4l2_subdev
*sd
,
167 const u16
*data
, unsigned int len
)
174 ret
|= vpx3220_fp_write(sd
, reg
, *data
++);
181 /* ---------------------------------------------------------------------- */
183 static const unsigned short init_ntsc
[] = {
184 0x1c, 0x00, /* NTSC tint angle */
185 0x88, 17, /* Window 1 vertical */
186 0x89, 240, /* Vertical lines in */
187 0x8a, 240, /* Vertical lines out */
188 0x8b, 000, /* Horizontal begin */
189 0x8c, 640, /* Horizontal length */
190 0x8d, 640, /* Number of pixels */
191 0x8f, 0xc00, /* Disable window 2 */
192 0xf0, 0x73, /* 13.5 MHz transport, Forced
193 * mode, latch windows */
194 0xf2, 0x13, /* NTSC M, composite input */
195 0xe7, 0x1e1, /* Enable vertical standard
196 * locking @ 240 lines */
199 static const unsigned short init_pal
[] = {
200 0x88, 23, /* Window 1 vertical begin */
201 0x89, 288, /* Vertical lines in (16 lines
202 * skipped by the VFE) */
203 0x8a, 288, /* Vertical lines out (16 lines
204 * skipped by the VFE) */
205 0x8b, 16, /* Horizontal begin */
206 0x8c, 768, /* Horizontal length */
207 0x8d, 784, /* Number of pixels
208 * Must be >= Horizontal begin + Horizontal length */
209 0x8f, 0xc00, /* Disable window 2 */
210 0xf0, 0x77, /* 13.5 MHz transport, Forced
211 * mode, latch windows */
212 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
213 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
216 static const unsigned short init_secam
[] = {
217 0x88, 23, /* Window 1 vertical begin */
218 0x89, 288, /* Vertical lines in (16 lines
219 * skipped by the VFE) */
220 0x8a, 288, /* Vertical lines out (16 lines
221 * skipped by the VFE) */
222 0x8b, 16, /* Horizontal begin */
223 0x8c, 768, /* Horizontal length */
224 0x8d, 784, /* Number of pixels
225 * Must be >= Horizontal begin + Horizontal length */
226 0x8f, 0xc00, /* Disable window 2 */
227 0xf0, 0x77, /* 13.5 MHz transport, Forced
228 * mode, latch windows */
229 0xf2, 0x3d5, /* SECAM, composite input */
230 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
233 static const unsigned char init_common
[] = {
234 0xf2, 0x00, /* Disable all outputs */
235 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
237 0xd8, 0xa8, /* HREF/VREF active high, VREF
238 * pulse = 2, Odd/Even flag */
239 0x20, 0x03, /* IF compensation 0dB/oct */
240 0xe0, 0xff, /* Open up all comparators */
246 0xe6, 0x00, /* Brightness set to 0 */
247 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
248 * 10 to 8 2-bit error diffusion */
249 0xe8, 0xf8, /* YUV422, CbCr binary offset,
251 0xea, 0x18, /* LLC2 connected, output FIFO
252 * reset with VACTintern */
253 0xf0, 0x8a, /* Half full level to 10, bus
254 * shuffler [7:0, 23:16, 15:8] */
255 0xf1, 0x18, /* Single clock, sync mode, no
256 * FE delay, no HLEN counter */
257 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
259 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
260 * ALPHA strength to 4 */
263 static const unsigned short init_fp
[] = {
265 0xa0, 2070, /* ACC reference */
273 0x4b, 0x298, /* PLL gain */
277 static int vpx3220_init(struct v4l2_subdev
*sd
, u32 val
)
279 struct vpx3220
*decoder
= to_vpx3220(sd
);
281 vpx3220_write_block(sd
, init_common
, sizeof(init_common
));
282 vpx3220_write_fp_block(sd
, init_fp
, sizeof(init_fp
) >> 1);
283 if (decoder
->norm
& V4L2_STD_NTSC
)
284 vpx3220_write_fp_block(sd
, init_ntsc
, sizeof(init_ntsc
) >> 1);
285 else if (decoder
->norm
& V4L2_STD_PAL
)
286 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
287 else if (decoder
->norm
& V4L2_STD_SECAM
)
288 vpx3220_write_fp_block(sd
, init_secam
, sizeof(init_secam
) >> 1);
290 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
294 static int vpx3220_status(struct v4l2_subdev
*sd
, u32
*pstatus
, v4l2_std_id
*pstd
)
296 int res
= V4L2_IN_ST_NO_SIGNAL
, status
;
299 status
= vpx3220_fp_read(sd
, 0x0f3);
301 v4l2_dbg(1, debug
, sd
, "status: 0x%04x\n", status
);
306 if ((status
& 0x20) == 0) {
309 switch (status
& 0x18) {
318 std
= V4L2_STD_SECAM
;
335 static int vpx3220_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
337 v4l2_dbg(1, debug
, sd
, "querystd\n");
338 return vpx3220_status(sd
, NULL
, std
);
341 static int vpx3220_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
343 v4l2_dbg(1, debug
, sd
, "g_input_status\n");
344 return vpx3220_status(sd
, status
, NULL
);
347 static int vpx3220_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
349 struct vpx3220
*decoder
= to_vpx3220(sd
);
352 /* Here we back up the input selection because it gets
353 overwritten when we fill the registers with the
354 choosen video norm */
355 temp_input
= vpx3220_fp_read(sd
, 0xf2);
357 v4l2_dbg(1, debug
, sd
, "s_std %llx\n", (unsigned long long)std
);
358 if (std
& V4L2_STD_NTSC
) {
359 vpx3220_write_fp_block(sd
, init_ntsc
, sizeof(init_ntsc
) >> 1);
360 v4l2_dbg(1, debug
, sd
, "norm switched to NTSC\n");
361 } else if (std
& V4L2_STD_PAL
) {
362 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
363 v4l2_dbg(1, debug
, sd
, "norm switched to PAL\n");
364 } else if (std
& V4L2_STD_SECAM
) {
365 vpx3220_write_fp_block(sd
, init_secam
, sizeof(init_secam
) >> 1);
366 v4l2_dbg(1, debug
, sd
, "norm switched to SECAM\n");
373 /* And here we set the backed up video input again */
374 vpx3220_fp_write(sd
, 0xf2, temp_input
| 0x0010);
379 static int vpx3220_s_routing(struct v4l2_subdev
*sd
,
380 u32 input
, u32 output
, u32 config
)
384 /* RJ: input = 0: ST8 (PCTV) input
385 input = 1: COMPOSITE input
386 input = 2: SVHS input */
388 const int input_vals
[3][2] = {
394 if (input
< 0 || input
> 2)
397 v4l2_dbg(1, debug
, sd
, "input switched to %s\n", inputs
[input
]);
399 vpx3220_write(sd
, 0x33, input_vals
[input
][0]);
401 data
= vpx3220_fp_read(sd
, 0xf2) & ~(0x0020);
404 /* 0x0010 is required to latch the setting */
405 vpx3220_fp_write(sd
, 0xf2,
406 data
| (input_vals
[input
][1] << 5) | 0x0010);
412 static int vpx3220_s_stream(struct v4l2_subdev
*sd
, int enable
)
414 v4l2_dbg(1, debug
, sd
, "s_stream %s\n", enable
? "on" : "off");
416 vpx3220_write(sd
, 0xf2, (enable
? 0x1b : 0x00));
420 static int vpx3220_queryctrl(struct v4l2_subdev
*sd
, struct v4l2_queryctrl
*qc
)
423 case V4L2_CID_BRIGHTNESS
:
424 v4l2_ctrl_query_fill(qc
, -128, 127, 1, 0);
427 case V4L2_CID_CONTRAST
:
428 v4l2_ctrl_query_fill(qc
, 0, 63, 1, 32);
431 case V4L2_CID_SATURATION
:
432 v4l2_ctrl_query_fill(qc
, 0, 4095, 1, 2048);
436 v4l2_ctrl_query_fill(qc
, -512, 511, 1, 0);
445 static int vpx3220_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
447 struct vpx3220
*decoder
= to_vpx3220(sd
);
450 case V4L2_CID_BRIGHTNESS
:
451 ctrl
->value
= decoder
->bright
;
453 case V4L2_CID_CONTRAST
:
454 ctrl
->value
= decoder
->contrast
;
456 case V4L2_CID_SATURATION
:
457 ctrl
->value
= decoder
->sat
;
460 ctrl
->value
= decoder
->hue
;
468 static int vpx3220_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
470 struct vpx3220
*decoder
= to_vpx3220(sd
);
473 case V4L2_CID_BRIGHTNESS
:
474 if (decoder
->bright
!= ctrl
->value
) {
475 decoder
->bright
= ctrl
->value
;
476 vpx3220_write(sd
, 0xe6, decoder
->bright
);
479 case V4L2_CID_CONTRAST
:
480 if (decoder
->contrast
!= ctrl
->value
) {
481 /* Bit 7 and 8 is for noise shaping */
482 decoder
->contrast
= ctrl
->value
;
483 vpx3220_write(sd
, 0xe7, decoder
->contrast
+ 192);
486 case V4L2_CID_SATURATION
:
487 if (decoder
->sat
!= ctrl
->value
) {
488 decoder
->sat
= ctrl
->value
;
489 vpx3220_fp_write(sd
, 0xa0, decoder
->sat
);
493 if (decoder
->hue
!= ctrl
->value
) {
494 decoder
->hue
= ctrl
->value
;
495 vpx3220_fp_write(sd
, 0x1c, decoder
->hue
);
504 static int vpx3220_g_chip_ident(struct v4l2_subdev
*sd
, struct v4l2_dbg_chip_ident
*chip
)
506 struct vpx3220
*decoder
= to_vpx3220(sd
);
507 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
509 return v4l2_chip_ident_i2c_client(client
, chip
, decoder
->ident
, 0);
512 /* ----------------------------------------------------------------------- */
514 static const struct v4l2_subdev_core_ops vpx3220_core_ops
= {
515 .g_chip_ident
= vpx3220_g_chip_ident
,
516 .init
= vpx3220_init
,
517 .g_ctrl
= vpx3220_g_ctrl
,
518 .s_ctrl
= vpx3220_s_ctrl
,
519 .queryctrl
= vpx3220_queryctrl
,
520 .s_std
= vpx3220_s_std
,
523 static const struct v4l2_subdev_video_ops vpx3220_video_ops
= {
524 .s_routing
= vpx3220_s_routing
,
525 .s_stream
= vpx3220_s_stream
,
526 .querystd
= vpx3220_querystd
,
527 .g_input_status
= vpx3220_g_input_status
,
530 static const struct v4l2_subdev_ops vpx3220_ops
= {
531 .core
= &vpx3220_core_ops
,
532 .video
= &vpx3220_video_ops
,
535 /* -----------------------------------------------------------------------
536 * Client management code
539 static int vpx3220_probe(struct i2c_client
*client
,
540 const struct i2c_device_id
*id
)
542 struct vpx3220
*decoder
;
543 struct v4l2_subdev
*sd
;
544 const char *name
= NULL
;
548 /* Check if the adapter supports the needed features */
549 if (!i2c_check_functionality(client
->adapter
,
550 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
553 decoder
= kzalloc(sizeof(struct vpx3220
), GFP_KERNEL
);
557 v4l2_i2c_subdev_init(sd
, client
, &vpx3220_ops
);
558 decoder
->norm
= V4L2_STD_PAL
;
561 decoder
->bright
= 32768;
562 decoder
->contrast
= 32768;
563 decoder
->hue
= 32768;
564 decoder
->sat
= 32768;
566 ver
= i2c_smbus_read_byte_data(client
, 0x00);
567 pn
= (i2c_smbus_read_byte_data(client
, 0x02) << 8) +
568 i2c_smbus_read_byte_data(client
, 0x01);
569 decoder
->ident
= V4L2_IDENT_VPX3220A
;
577 decoder
->ident
= V4L2_IDENT_VPX3216B
;
581 decoder
->ident
= V4L2_IDENT_VPX3214C
;
586 v4l2_info(sd
, "%s found @ 0x%x (%s)\n", name
,
587 client
->addr
<< 1, client
->adapter
->name
);
589 v4l2_info(sd
, "chip (%02x:%04x) found @ 0x%x (%s)\n",
590 ver
, pn
, client
->addr
<< 1, client
->adapter
->name
);
592 vpx3220_write_block(sd
, init_common
, sizeof(init_common
));
593 vpx3220_write_fp_block(sd
, init_fp
, sizeof(init_fp
) >> 1);
595 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
599 static int vpx3220_remove(struct i2c_client
*client
)
601 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
603 v4l2_device_unregister_subdev(sd
);
604 kfree(to_vpx3220(sd
));
608 static const struct i2c_device_id vpx3220_id
[] = {
614 MODULE_DEVICE_TABLE(i2c
, vpx3220_id
);
616 static struct v4l2_i2c_driver_data v4l2_i2c_data
= {
618 .probe
= vpx3220_probe
,
619 .remove
= vpx3220_remove
,
620 .id_table
= vpx3220_id
,