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.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/types.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/i2c.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-ctrls.h>
28 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
29 MODULE_AUTHOR("Laurent Pinchart");
30 MODULE_LICENSE("GPL");
33 module_param(debug
, int, 0);
34 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
37 #define VPX_TIMEOUT_COUNT 10
39 /* ----------------------------------------------------------------------- */
42 struct v4l2_subdev sd
;
43 struct v4l2_ctrl_handler hdl
;
44 unsigned char reg
[255];
51 static inline struct vpx3220
*to_vpx3220(struct v4l2_subdev
*sd
)
53 return container_of(sd
, struct vpx3220
, sd
);
56 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
58 return &container_of(ctrl
->handler
, struct vpx3220
, hdl
)->sd
;
61 static char *inputs
[] = { "internal", "composite", "svideo" };
63 /* ----------------------------------------------------------------------- */
65 static inline int vpx3220_write(struct v4l2_subdev
*sd
, u8 reg
, u8 value
)
67 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
68 struct vpx3220
*decoder
= i2c_get_clientdata(client
);
70 decoder
->reg
[reg
] = value
;
71 return i2c_smbus_write_byte_data(client
, reg
, value
);
74 static inline int vpx3220_read(struct v4l2_subdev
*sd
, u8 reg
)
76 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
78 return i2c_smbus_read_byte_data(client
, reg
);
81 static int vpx3220_fp_status(struct v4l2_subdev
*sd
)
86 for (i
= 0; i
< VPX_TIMEOUT_COUNT
; i
++) {
87 status
= vpx3220_read(sd
, 0x29);
101 static int vpx3220_fp_write(struct v4l2_subdev
*sd
, u8 fpaddr
, u16 data
)
103 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
105 /* Write the 16-bit address to the FPWR register */
106 if (i2c_smbus_write_word_data(client
, 0x27, swab16(fpaddr
)) == -1) {
107 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
111 if (vpx3220_fp_status(sd
) < 0)
114 /* Write the 16-bit data to the FPDAT register */
115 if (i2c_smbus_write_word_data(client
, 0x28, swab16(data
)) == -1) {
116 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
123 static int vpx3220_fp_read(struct v4l2_subdev
*sd
, u16 fpaddr
)
125 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
128 /* Write the 16-bit address to the FPRD register */
129 if (i2c_smbus_write_word_data(client
, 0x26, swab16(fpaddr
)) == -1) {
130 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
134 if (vpx3220_fp_status(sd
) < 0)
137 /* Read the 16-bit data from the FPDAT register */
138 data
= i2c_smbus_read_word_data(client
, 0x28);
140 v4l2_dbg(1, debug
, sd
, "%s: failed\n", __func__
);
147 static int vpx3220_write_block(struct v4l2_subdev
*sd
, const u8
*data
, unsigned int len
)
154 ret
= vpx3220_write(sd
, reg
, *data
++);
163 static int vpx3220_write_fp_block(struct v4l2_subdev
*sd
,
164 const u16
*data
, unsigned int len
)
171 ret
|= vpx3220_fp_write(sd
, reg
, *data
++);
178 /* ---------------------------------------------------------------------- */
180 static const unsigned short init_ntsc
[] = {
181 0x1c, 0x00, /* NTSC tint angle */
182 0x88, 17, /* Window 1 vertical */
183 0x89, 240, /* Vertical lines in */
184 0x8a, 240, /* Vertical lines out */
185 0x8b, 000, /* Horizontal begin */
186 0x8c, 640, /* Horizontal length */
187 0x8d, 640, /* Number of pixels */
188 0x8f, 0xc00, /* Disable window 2 */
189 0xf0, 0x73, /* 13.5 MHz transport, Forced
190 * mode, latch windows */
191 0xf2, 0x13, /* NTSC M, composite input */
192 0xe7, 0x1e1, /* Enable vertical standard
193 * locking @ 240 lines */
196 static const unsigned short init_pal
[] = {
197 0x88, 23, /* Window 1 vertical begin */
198 0x89, 288, /* Vertical lines in (16 lines
199 * skipped by the VFE) */
200 0x8a, 288, /* Vertical lines out (16 lines
201 * skipped by the VFE) */
202 0x8b, 16, /* Horizontal begin */
203 0x8c, 768, /* Horizontal length */
204 0x8d, 784, /* Number of pixels
205 * Must be >= Horizontal begin + Horizontal length */
206 0x8f, 0xc00, /* Disable window 2 */
207 0xf0, 0x77, /* 13.5 MHz transport, Forced
208 * mode, latch windows */
209 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
210 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
213 static const unsigned short init_secam
[] = {
214 0x88, 23, /* Window 1 vertical begin */
215 0x89, 288, /* Vertical lines in (16 lines
216 * skipped by the VFE) */
217 0x8a, 288, /* Vertical lines out (16 lines
218 * skipped by the VFE) */
219 0x8b, 16, /* Horizontal begin */
220 0x8c, 768, /* Horizontal length */
221 0x8d, 784, /* Number of pixels
222 * Must be >= Horizontal begin + Horizontal length */
223 0x8f, 0xc00, /* Disable window 2 */
224 0xf0, 0x77, /* 13.5 MHz transport, Forced
225 * mode, latch windows */
226 0xf2, 0x3d5, /* SECAM, composite input */
227 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
230 static const unsigned char init_common
[] = {
231 0xf2, 0x00, /* Disable all outputs */
232 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
234 0xd8, 0xa8, /* HREF/VREF active high, VREF
235 * pulse = 2, Odd/Even flag */
236 0x20, 0x03, /* IF compensation 0dB/oct */
237 0xe0, 0xff, /* Open up all comparators */
243 0xe6, 0x00, /* Brightness set to 0 */
244 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
245 * 10 to 8 2-bit error diffusion */
246 0xe8, 0xf8, /* YUV422, CbCr binary offset,
248 0xea, 0x18, /* LLC2 connected, output FIFO
249 * reset with VACTintern */
250 0xf0, 0x8a, /* Half full level to 10, bus
251 * shuffler [7:0, 23:16, 15:8] */
252 0xf1, 0x18, /* Single clock, sync mode, no
253 * FE delay, no HLEN counter */
254 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
256 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
257 * ALPHA strength to 4 */
260 static const unsigned short init_fp
[] = {
262 0xa0, 2070, /* ACC reference */
270 0x4b, 0x298, /* PLL gain */
274 static int vpx3220_init(struct v4l2_subdev
*sd
, u32 val
)
276 struct vpx3220
*decoder
= to_vpx3220(sd
);
278 vpx3220_write_block(sd
, init_common
, sizeof(init_common
));
279 vpx3220_write_fp_block(sd
, init_fp
, sizeof(init_fp
) >> 1);
280 if (decoder
->norm
& V4L2_STD_NTSC
)
281 vpx3220_write_fp_block(sd
, init_ntsc
, sizeof(init_ntsc
) >> 1);
282 else if (decoder
->norm
& V4L2_STD_PAL
)
283 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
284 else if (decoder
->norm
& V4L2_STD_SECAM
)
285 vpx3220_write_fp_block(sd
, init_secam
, sizeof(init_secam
) >> 1);
287 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
291 static int vpx3220_status(struct v4l2_subdev
*sd
, u32
*pstatus
, v4l2_std_id
*pstd
)
293 int res
= V4L2_IN_ST_NO_SIGNAL
, status
;
294 v4l2_std_id std
= pstd
? *pstd
: V4L2_STD_ALL
;
296 status
= vpx3220_fp_read(sd
, 0x0f3);
298 v4l2_dbg(1, debug
, sd
, "status: 0x%04x\n", status
);
303 if ((status
& 0x20) == 0) {
306 switch (status
& 0x18) {
315 std
&= V4L2_STD_SECAM
;
321 std
&= V4L2_STD_NTSC
;
325 std
= V4L2_STD_UNKNOWN
;
334 static int vpx3220_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
336 v4l2_dbg(1, debug
, sd
, "querystd\n");
337 return vpx3220_status(sd
, NULL
, std
);
340 static int vpx3220_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
342 v4l2_dbg(1, debug
, sd
, "g_input_status\n");
343 return vpx3220_status(sd
, status
, NULL
);
346 static int vpx3220_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
348 struct vpx3220
*decoder
= to_vpx3220(sd
);
351 /* Here we back up the input selection because it gets
352 overwritten when we fill the registers with the
354 temp_input
= vpx3220_fp_read(sd
, 0xf2);
356 v4l2_dbg(1, debug
, sd
, "s_std %llx\n", (unsigned long long)std
);
357 if (std
& V4L2_STD_NTSC
) {
358 vpx3220_write_fp_block(sd
, init_ntsc
, sizeof(init_ntsc
) >> 1);
359 v4l2_dbg(1, debug
, sd
, "norm switched to NTSC\n");
360 } else if (std
& V4L2_STD_PAL
) {
361 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
362 v4l2_dbg(1, debug
, sd
, "norm switched to PAL\n");
363 } else if (std
& V4L2_STD_SECAM
) {
364 vpx3220_write_fp_block(sd
, init_secam
, sizeof(init_secam
) >> 1);
365 v4l2_dbg(1, debug
, sd
, "norm switched to SECAM\n");
372 /* And here we set the backed up video input again */
373 vpx3220_fp_write(sd
, 0xf2, temp_input
| 0x0010);
378 static int vpx3220_s_routing(struct v4l2_subdev
*sd
,
379 u32 input
, u32 output
, u32 config
)
383 /* RJ: input = 0: ST8 (PCTV) input
384 input = 1: COMPOSITE input
385 input = 2: SVHS input */
387 const int input_vals
[3][2] = {
396 v4l2_dbg(1, debug
, sd
, "input switched to %s\n", inputs
[input
]);
398 vpx3220_write(sd
, 0x33, input_vals
[input
][0]);
400 data
= vpx3220_fp_read(sd
, 0xf2) & ~(0x0020);
403 /* 0x0010 is required to latch the setting */
404 vpx3220_fp_write(sd
, 0xf2,
405 data
| (input_vals
[input
][1] << 5) | 0x0010);
411 static int vpx3220_s_stream(struct v4l2_subdev
*sd
, int enable
)
413 v4l2_dbg(1, debug
, sd
, "s_stream %s\n", enable
? "on" : "off");
415 vpx3220_write(sd
, 0xf2, (enable
? 0x1b : 0x00));
419 static int vpx3220_s_ctrl(struct v4l2_ctrl
*ctrl
)
421 struct v4l2_subdev
*sd
= to_sd(ctrl
);
424 case V4L2_CID_BRIGHTNESS
:
425 vpx3220_write(sd
, 0xe6, ctrl
->val
);
427 case V4L2_CID_CONTRAST
:
428 /* Bit 7 and 8 is for noise shaping */
429 vpx3220_write(sd
, 0xe7, ctrl
->val
+ 192);
431 case V4L2_CID_SATURATION
:
432 vpx3220_fp_write(sd
, 0xa0, ctrl
->val
);
435 vpx3220_fp_write(sd
, 0x1c, ctrl
->val
);
441 /* ----------------------------------------------------------------------- */
443 static const struct v4l2_ctrl_ops vpx3220_ctrl_ops
= {
444 .s_ctrl
= vpx3220_s_ctrl
,
447 static const struct v4l2_subdev_core_ops vpx3220_core_ops
= {
448 .init
= vpx3220_init
,
451 static const struct v4l2_subdev_video_ops vpx3220_video_ops
= {
452 .s_std
= vpx3220_s_std
,
453 .s_routing
= vpx3220_s_routing
,
454 .s_stream
= vpx3220_s_stream
,
455 .querystd
= vpx3220_querystd
,
456 .g_input_status
= vpx3220_g_input_status
,
459 static const struct v4l2_subdev_ops vpx3220_ops
= {
460 .core
= &vpx3220_core_ops
,
461 .video
= &vpx3220_video_ops
,
464 /* -----------------------------------------------------------------------
465 * Client management code
468 static int vpx3220_probe(struct i2c_client
*client
,
469 const struct i2c_device_id
*id
)
471 struct vpx3220
*decoder
;
472 struct v4l2_subdev
*sd
;
473 const char *name
= NULL
;
477 /* Check if the adapter supports the needed features */
478 if (!i2c_check_functionality(client
->adapter
,
479 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
))
482 decoder
= devm_kzalloc(&client
->dev
, sizeof(*decoder
), GFP_KERNEL
);
486 v4l2_i2c_subdev_init(sd
, client
, &vpx3220_ops
);
487 decoder
->norm
= V4L2_STD_PAL
;
490 v4l2_ctrl_handler_init(&decoder
->hdl
, 4);
491 v4l2_ctrl_new_std(&decoder
->hdl
, &vpx3220_ctrl_ops
,
492 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
493 v4l2_ctrl_new_std(&decoder
->hdl
, &vpx3220_ctrl_ops
,
494 V4L2_CID_CONTRAST
, 0, 63, 1, 32);
495 v4l2_ctrl_new_std(&decoder
->hdl
, &vpx3220_ctrl_ops
,
496 V4L2_CID_SATURATION
, 0, 4095, 1, 2048);
497 v4l2_ctrl_new_std(&decoder
->hdl
, &vpx3220_ctrl_ops
,
498 V4L2_CID_HUE
, -512, 511, 1, 0);
499 sd
->ctrl_handler
= &decoder
->hdl
;
500 if (decoder
->hdl
.error
) {
501 int err
= decoder
->hdl
.error
;
503 v4l2_ctrl_handler_free(&decoder
->hdl
);
506 v4l2_ctrl_handler_setup(&decoder
->hdl
);
508 ver
= i2c_smbus_read_byte_data(client
, 0x00);
509 pn
= (i2c_smbus_read_byte_data(client
, 0x02) << 8) +
510 i2c_smbus_read_byte_data(client
, 0x01);
525 v4l2_info(sd
, "%s found @ 0x%x (%s)\n", name
,
526 client
->addr
<< 1, client
->adapter
->name
);
528 v4l2_info(sd
, "chip (%02x:%04x) found @ 0x%x (%s)\n",
529 ver
, pn
, client
->addr
<< 1, client
->adapter
->name
);
531 vpx3220_write_block(sd
, init_common
, sizeof(init_common
));
532 vpx3220_write_fp_block(sd
, init_fp
, sizeof(init_fp
) >> 1);
534 vpx3220_write_fp_block(sd
, init_pal
, sizeof(init_pal
) >> 1);
538 static int vpx3220_remove(struct i2c_client
*client
)
540 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
541 struct vpx3220
*decoder
= to_vpx3220(sd
);
543 v4l2_device_unregister_subdev(sd
);
544 v4l2_ctrl_handler_free(&decoder
->hdl
);
549 static const struct i2c_device_id vpx3220_id
[] = {
555 MODULE_DEVICE_TABLE(i2c
, vpx3220_id
);
557 static struct i2c_driver vpx3220_driver
= {
561 .probe
= vpx3220_probe
,
562 .remove
= vpx3220_remove
,
563 .id_table
= vpx3220_id
,
566 module_i2c_driver(vpx3220_driver
);