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 <media/v4l2-common.h>
28 #include <media/v4l2-i2c-drv-legacy.h>
29 #include <linux/videodev.h>
30 #include <linux/video_decoder.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)");
40 #define VPX_TIMEOUT_COUNT 10
42 /* ----------------------------------------------------------------------- */
45 unsigned char reg[255];
56 static char *inputs[] = { "internal", "composite", "svideo" };
58 /* ----------------------------------------------------------------------- */
60 static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
62 struct vpx3220 *decoder = i2c_get_clientdata(client);
64 decoder->reg[reg] = value;
65 return i2c_smbus_write_byte_data(client, reg, value);
68 static inline int vpx3220_read(struct i2c_client *client, u8 reg)
70 return i2c_smbus_read_byte_data(client, reg);
73 static int vpx3220_fp_status(struct i2c_client *client)
78 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
79 status = vpx3220_read(client, 0x29);
93 static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
95 /* Write the 16-bit address to the FPWR register */
96 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
97 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
101 if (vpx3220_fp_status(client) < 0)
104 /* Write the 16-bit data to the FPDAT register */
105 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
106 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
113 static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
117 /* Write the 16-bit address to the FPRD register */
118 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
119 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
123 if (vpx3220_fp_status(client) < 0)
126 /* Read the 16-bit data from the FPDAT register */
127 data = i2c_smbus_read_word_data(client, 0x28);
129 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
136 static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
143 ret = vpx3220_write(client, reg, *data++);
152 static int vpx3220_write_fp_block(struct i2c_client *client,
153 const u16 *data, unsigned int len)
160 ret |= vpx3220_fp_write(client, reg, *data++);
167 /* ---------------------------------------------------------------------- */
169 static const unsigned short init_ntsc[] = {
170 0x1c, 0x00, /* NTSC tint angle */
171 0x88, 17, /* Window 1 vertical */
172 0x89, 240, /* Vertical lines in */
173 0x8a, 240, /* Vertical lines out */
174 0x8b, 000, /* Horizontal begin */
175 0x8c, 640, /* Horizontal length */
176 0x8d, 640, /* Number of pixels */
177 0x8f, 0xc00, /* Disable window 2 */
178 0xf0, 0x73, /* 13.5 MHz transport, Forced
179 * mode, latch windows */
180 0xf2, 0x13, /* NTSC M, composite input */
181 0xe7, 0x1e1, /* Enable vertical standard
182 * locking @ 240 lines */
185 static const unsigned short init_pal[] = {
186 0x88, 23, /* Window 1 vertical begin */
187 0x89, 288, /* Vertical lines in (16 lines
188 * skipped by the VFE) */
189 0x8a, 288, /* Vertical lines out (16 lines
190 * skipped by the VFE) */
191 0x8b, 16, /* Horizontal begin */
192 0x8c, 768, /* Horizontal length */
193 0x8d, 784, /* Number of pixels
194 * Must be >= Horizontal begin + Horizontal length */
195 0x8f, 0xc00, /* Disable window 2 */
196 0xf0, 0x77, /* 13.5 MHz transport, Forced
197 * mode, latch windows */
198 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
199 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
202 static const unsigned short init_secam[] = {
203 0x88, 23, /* Window 1 vertical begin */
204 0x89, 288, /* Vertical lines in (16 lines
205 * skipped by the VFE) */
206 0x8a, 288, /* Vertical lines out (16 lines
207 * skipped by the VFE) */
208 0x8b, 16, /* Horizontal begin */
209 0x8c, 768, /* Horizontal length */
210 0x8d, 784, /* Number of pixels
211 * Must be >= Horizontal begin + Horizontal length */
212 0x8f, 0xc00, /* Disable window 2 */
213 0xf0, 0x77, /* 13.5 MHz transport, Forced
214 * mode, latch windows */
215 0xf2, 0x3d5, /* SECAM, composite input */
216 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
219 static const unsigned char init_common[] = {
220 0xf2, 0x00, /* Disable all outputs */
221 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
223 0xd8, 0xa8, /* HREF/VREF active high, VREF
224 * pulse = 2, Odd/Even flag */
225 0x20, 0x03, /* IF compensation 0dB/oct */
226 0xe0, 0xff, /* Open up all comparators */
232 0xe6, 0x00, /* Brightness set to 0 */
233 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
234 * 10 to 8 2-bit error diffusion */
235 0xe8, 0xf8, /* YUV422, CbCr binary offset,
237 0xea, 0x18, /* LLC2 connected, output FIFO
238 * reset with VACTintern */
239 0xf0, 0x8a, /* Half full level to 10, bus
240 * shuffler [7:0, 23:16, 15:8] */
241 0xf1, 0x18, /* Single clock, sync mode, no
242 * FE delay, no HLEN counter */
243 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
245 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
246 * ALPHA strength to 4 */
249 static const unsigned short init_fp[] = {
251 0xa0, 2070, /* ACC reference */
259 0x4b, 0x298, /* PLL gain */
262 static void vpx3220_dump_i2c(struct i2c_client *client)
264 int len = sizeof(init_common);
265 const unsigned char *data = init_common;
268 v4l_dbg(1, debug, client, "i2c reg 0x%02x data 0x%02x\n",
269 *data, vpx3220_read(client, *data));
275 static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
277 struct vpx3220 *decoder = i2c_get_clientdata(client);
282 vpx3220_write_block(client, init_common,
283 sizeof(init_common));
284 vpx3220_write_fp_block(client, init_fp,
285 sizeof(init_fp) >> 1);
286 switch (decoder->norm) {
287 case VIDEO_MODE_NTSC:
288 vpx3220_write_fp_block(client, init_ntsc,
289 sizeof(init_ntsc) >> 1);
293 vpx3220_write_fp_block(client, init_pal,
294 sizeof(init_pal) >> 1);
296 case VIDEO_MODE_SECAM:
297 vpx3220_write_fp_block(client, init_secam,
298 sizeof(init_secam) >> 1);
301 vpx3220_write_fp_block(client, init_pal,
302 sizeof(init_pal) >> 1);
310 vpx3220_dump_i2c(client);
314 case DECODER_GET_CAPABILITIES:
316 struct video_decoder_capability *cap = arg;
318 v4l_dbg(1, debug, client, "DECODER_GET_CAPABILITIES\n");
320 cap->flags = VIDEO_DECODER_PAL |
322 VIDEO_DECODER_SECAM |
330 case DECODER_GET_STATUS:
334 v4l_dbg(1, debug, client, "DECODER_GET_STATUS\n");
336 status = vpx3220_fp_read(client, 0x0f3);
338 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
343 if ((status & 0x20) == 0) {
344 res |= DECODER_STATUS_GOOD | DECODER_STATUS_COLOR;
346 switch (status & 0x18) {
351 res |= DECODER_STATUS_PAL;
355 res |= DECODER_STATUS_SECAM;
361 res |= DECODER_STATUS_NTSC;
370 case DECODER_SET_NORM:
372 int *iarg = arg, data;
375 /* Here we back up the input selection because it gets
376 overwritten when we fill the registers with the
377 choosen video norm */
378 temp_input = vpx3220_fp_read(client, 0xf2);
380 v4l_dbg(1, debug, client, "DECODER_SET_NORM %d\n", *iarg);
382 case VIDEO_MODE_NTSC:
383 vpx3220_write_fp_block(client, init_ntsc,
384 sizeof(init_ntsc) >> 1);
385 v4l_dbg(1, debug, client, "norm switched to NTSC\n");
389 vpx3220_write_fp_block(client, init_pal,
390 sizeof(init_pal) >> 1);
391 v4l_dbg(1, debug, client, "norm switched to PAL\n");
394 case VIDEO_MODE_SECAM:
395 vpx3220_write_fp_block(client, init_secam,
396 sizeof(init_secam) >> 1);
397 v4l_dbg(1, debug, client, "norm switched to SECAM\n");
400 case VIDEO_MODE_AUTO:
401 /* FIXME This is only preliminary support */
402 data = vpx3220_fp_read(client, 0xf2) & 0x20;
403 vpx3220_fp_write(client, 0xf2, 0x00c0 | data);
404 v4l_dbg(1, debug, client, "norm switched to AUTO\n");
410 decoder->norm = *iarg;
412 /* And here we set the backed up video input again */
413 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
418 case DECODER_SET_INPUT:
420 int *iarg = arg, data;
422 /* RJ: *iarg = 0: ST8 (PCTV) input
423 *iarg = 1: COMPOSITE input
424 *iarg = 2: SVHS input */
426 const int input[3][2] = {
432 if (*iarg < 0 || *iarg > 2)
435 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[*iarg]);
437 vpx3220_write(client, 0x33, input[*iarg][0]);
439 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
442 /* 0x0010 is required to latch the setting */
443 vpx3220_fp_write(client, 0xf2,
444 data | (input[*iarg][1] << 5) | 0x0010);
450 case DECODER_SET_OUTPUT:
454 /* not much choice of outputs */
461 case DECODER_ENABLE_OUTPUT:
465 v4l_dbg(1, debug, client, "DECODER_ENABLE_OUTPUT %d\n", *iarg);
467 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
471 case DECODER_SET_PICTURE:
473 struct video_picture *pic = arg;
475 if (decoder->bright != pic->brightness) {
476 /* We want -128 to 128 we get 0-65535 */
477 decoder->bright = pic->brightness;
478 vpx3220_write(client, 0xe6,
479 (decoder->bright - 32768) >> 8);
481 if (decoder->contrast != pic->contrast) {
482 /* We want 0 to 64 we get 0-65535 */
483 /* Bit 7 and 8 is for noise shaping */
484 decoder->contrast = pic->contrast;
485 vpx3220_write(client, 0xe7,
486 (decoder->contrast >> 10) + 192);
488 if (decoder->sat != pic->colour) {
489 /* We want 0 to 4096 we get 0-65535 */
490 decoder->sat = pic->colour;
491 vpx3220_fp_write(client, 0xa0,
494 if (decoder->hue != pic->hue) {
495 /* We want -512 to 512 we get 0-65535 */
496 decoder->hue = pic->hue;
497 vpx3220_fp_write(client, 0x1c,
498 ((decoder->hue - 32768) >> 6) & 0xFFF);
510 static int vpx3220_init_client(struct i2c_client *client)
512 vpx3220_write_block(client, init_common, sizeof(init_common));
513 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
515 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
520 /* -----------------------------------------------------------------------
521 * Client management code
524 static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
528 static int vpx3220_probe(struct i2c_client *client,
529 const struct i2c_device_id *id)
531 struct vpx3220 *decoder;
532 const char *name = NULL;
536 /* Check if the adapter supports the needed features */
537 if (!i2c_check_functionality(client->adapter,
538 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
541 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
544 decoder->norm = VIDEO_MODE_PAL;
547 decoder->bright = 32768;
548 decoder->contrast = 32768;
549 decoder->hue = 32768;
550 decoder->sat = 32768;
551 i2c_set_clientdata(client, decoder);
553 ver = i2c_smbus_read_byte_data(client, 0x00);
554 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
555 i2c_smbus_read_byte_data(client, 0x01);
570 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
571 client->addr << 1, client->adapter->name);
573 v4l_info(client, "chip (%02x:%04x) found @ 0x%x (%s)\n",
574 ver, pn, client->addr << 1, client->adapter->name);
576 vpx3220_init_client(client);
580 static int vpx3220_remove(struct i2c_client *client)
582 kfree(i2c_get_clientdata(client));
586 static const struct i2c_device_id vpx3220_id[] = {
592 MODULE_DEVICE_TABLE(i2c, vpx3220_id);
594 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
596 .driverid = I2C_DRIVERID_VPX3220,
597 .command = vpx3220_command,
598 .probe = vpx3220_probe,
599 .remove = vpx3220_remove,
600 .id_table = vpx3220_id,