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 <linux/slab.h>
27 #include <linux/byteorder/swab.h>
30 #include <asm/uaccess.h>
32 #include <linux/i2c.h>
34 #define I2C_NAME(x) (x)->name
36 #include <linux/videodev.h>
37 #include <media/v4l2-common.h>
38 #include <linux/video_decoder.h>
40 #define I2C_VPX3220 0x86
41 #define VPX3220_DEBUG KERN_DEBUG "vpx3220: "
44 module_param(debug, int, 0);
45 MODULE_PARM_DESC(debug, "Debug level (0-1)");
47 #define dprintk(num, format, args...) \
50 printk(format, ##args); \
53 #define VPX_TIMEOUT_COUNT 10
55 /* ----------------------------------------------------------------------- */
58 unsigned char reg[255];
69 static char *inputs[] = { "internal", "composite", "svideo" };
71 /* ----------------------------------------------------------------------- */
73 vpx3220_write (struct i2c_client *client,
77 struct vpx3220 *decoder = i2c_get_clientdata(client);
79 decoder->reg[reg] = value;
80 return i2c_smbus_write_byte_data(client, reg, value);
84 vpx3220_read (struct i2c_client *client,
87 return i2c_smbus_read_byte_data(client, reg);
91 vpx3220_fp_status (struct i2c_client *client)
96 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
97 status = vpx3220_read(client, 0x29);
112 vpx3220_fp_write (struct i2c_client *client,
116 /* Write the 16-bit address to the FPWR register */
117 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
118 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
122 if (vpx3220_fp_status(client) < 0)
125 /* Write the 16-bit data to the FPDAT register */
126 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
127 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
135 vpx3220_fp_read (struct i2c_client *client,
140 /* Write the 16-bit address to the FPRD register */
141 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
142 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
146 if (vpx3220_fp_status(client) < 0)
149 /* Read the 16-bit data from the FPDAT register */
150 data = i2c_smbus_read_word_data(client, 0x28);
152 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
160 vpx3220_write_block (struct i2c_client *client,
170 vpx3220_write(client, reg, *data++)) < 0)
179 vpx3220_write_fp_block (struct i2c_client *client,
188 ret |= vpx3220_fp_write(client, reg, *data++);
195 /* ---------------------------------------------------------------------- */
197 static const unsigned short init_ntsc[] = {
198 0x1c, 0x00, /* NTSC tint angle */
199 0x88, 17, /* Window 1 vertical */
200 0x89, 240, /* Vertical lines in */
201 0x8a, 240, /* Vertical lines out */
202 0x8b, 000, /* Horizontal begin */
203 0x8c, 640, /* Horizontal length */
204 0x8d, 640, /* Number of pixels */
205 0x8f, 0xc00, /* Disable window 2 */
206 0xf0, 0x73, /* 13.5 MHz transport, Forced
207 * mode, latch windows */
208 0xf2, 0x13, /* NTSC M, composite input */
209 0xe7, 0x1e1, /* Enable vertical standard
210 * locking @ 240 lines */
213 static const unsigned short init_pal[] = {
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, 0x3d1, /* PAL B,G,H,I, composite input */
227 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
230 static const unsigned short init_secam[] = {
231 0x88, 23, /* Window 1 vertical begin */
232 0x89, 288, /* Vertical lines in (16 lines
233 * skipped by the VFE) */
234 0x8a, 288, /* Vertical lines out (16 lines
235 * skipped by the VFE) */
236 0x8b, 16, /* Horizontal begin */
237 0x8c, 768, /* Horizontal length */
238 0x8d, 784, /* Number of pixels
239 * Must be >= Horizontal begin + Horizontal length */
240 0x8f, 0xc00, /* Disable window 2 */
241 0xf0, 0x77, /* 13.5 MHz transport, Forced
242 * mode, latch windows */
243 0xf2, 0x3d5, /* SECAM, composite input */
244 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
247 static const unsigned char init_common[] = {
248 0xf2, 0x00, /* Disable all outputs */
249 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
251 0xd8, 0xa8, /* HREF/VREF active high, VREF
252 * pulse = 2, Odd/Even flag */
253 0x20, 0x03, /* IF compensation 0dB/oct */
254 0xe0, 0xff, /* Open up all comparators */
260 0xe6, 0x00, /* Brightness set to 0 */
261 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
262 * 10 to 8 2-bit error diffusion */
263 0xe8, 0xf8, /* YUV422, CbCr binary offset,
265 0xea, 0x18, /* LLC2 connected, output FIFO
266 * reset with VACTintern */
267 0xf0, 0x8a, /* Half full level to 10, bus
268 * shuffler [7:0, 23:16, 15:8] */
269 0xf1, 0x18, /* Single clock, sync mode, no
270 * FE delay, no HLEN counter */
271 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
273 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
274 * ALPHA strength to 4 */
277 static const unsigned short init_fp[] = {
279 0xa0, 2070, /* ACC reference */
287 0x4b, 0x298, /* PLL gain */
291 vpx3220_dump_i2c (struct i2c_client *client)
293 int len = sizeof(init_common);
294 const unsigned char *data = init_common;
298 KERN_DEBUG "vpx3216b i2c reg 0x%02x data 0x%02x\n",
299 *data, vpx3220_read(client, *data));
306 vpx3220_command (struct i2c_client *client,
310 struct vpx3220 *decoder = i2c_get_clientdata(client);
315 vpx3220_write_block(client, init_common,
316 sizeof(init_common));
317 vpx3220_write_fp_block(client, init_fp,
318 sizeof(init_fp) >> 1);
319 switch (decoder->norm) {
321 case VIDEO_MODE_NTSC:
322 vpx3220_write_fp_block(client, init_ntsc,
323 sizeof(init_ntsc) >> 1);
327 vpx3220_write_fp_block(client, init_pal,
328 sizeof(init_pal) >> 1);
330 case VIDEO_MODE_SECAM:
331 vpx3220_write_fp_block(client, init_secam,
332 sizeof(init_secam) >> 1);
335 vpx3220_write_fp_block(client, init_pal,
336 sizeof(init_pal) >> 1);
344 vpx3220_dump_i2c(client);
348 case DECODER_GET_CAPABILITIES:
350 struct video_decoder_capability *cap = arg;
352 dprintk(1, KERN_DEBUG "%s: DECODER_GET_CAPABILITIES\n",
355 cap->flags = VIDEO_DECODER_PAL |
357 VIDEO_DECODER_SECAM |
365 case DECODER_GET_STATUS:
369 dprintk(1, KERN_INFO "%s: DECODER_GET_STATUS\n",
372 status = vpx3220_fp_read(client, 0x0f3);
374 dprintk(1, KERN_INFO "%s: status: 0x%04x\n", I2C_NAME(client),
380 if ((status & 0x20) == 0) {
381 res |= DECODER_STATUS_GOOD | DECODER_STATUS_COLOR;
383 switch (status & 0x18) {
389 res |= DECODER_STATUS_PAL;
393 res |= DECODER_STATUS_SECAM;
399 res |= DECODER_STATUS_NTSC;
408 case DECODER_SET_NORM:
410 int *iarg = arg, data;
413 /* Here we back up the input selection because it gets
414 overwritten when we fill the registers with the
415 choosen video norm */
416 temp_input = vpx3220_fp_read(client, 0xf2);
418 dprintk(1, KERN_DEBUG "%s: DECODER_SET_NORM %d\n",
419 I2C_NAME(client), *iarg);
422 case VIDEO_MODE_NTSC:
423 vpx3220_write_fp_block(client, init_ntsc,
424 sizeof(init_ntsc) >> 1);
425 dprintk(1, KERN_INFO "%s: norm switched to NTSC\n",
430 vpx3220_write_fp_block(client, init_pal,
431 sizeof(init_pal) >> 1);
432 dprintk(1, KERN_INFO "%s: norm switched to PAL\n",
436 case VIDEO_MODE_SECAM:
437 vpx3220_write_fp_block(client, init_secam,
438 sizeof(init_secam) >> 1);
439 dprintk(1, KERN_INFO "%s: norm switched to SECAM\n",
443 case VIDEO_MODE_AUTO:
444 /* FIXME This is only preliminary support */
445 data = vpx3220_fp_read(client, 0xf2) & 0x20;
446 vpx3220_fp_write(client, 0xf2, 0x00c0 | data);
447 dprintk(1, KERN_INFO "%s: norm switched to Auto\n",
455 decoder->norm = *iarg;
457 /* And here we set the backed up video input again */
458 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
463 case DECODER_SET_INPUT:
465 int *iarg = arg, data;
467 /* RJ: *iarg = 0: ST8 (PCTV) input
468 *iarg = 1: COMPOSITE input
469 *iarg = 2: SVHS input */
471 const int input[3][2] = {
477 if (*iarg < 0 || *iarg > 2)
480 dprintk(1, KERN_INFO "%s: input switched to %s\n",
481 I2C_NAME(client), inputs[*iarg]);
483 vpx3220_write(client, 0x33, input[*iarg][0]);
485 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
488 /* 0x0010 is required to latch the setting */
489 vpx3220_fp_write(client, 0xf2,
490 data | (input[*iarg][1] << 5) | 0x0010);
496 case DECODER_SET_OUTPUT:
500 /* not much choice of outputs */
507 case DECODER_ENABLE_OUTPUT:
511 dprintk(1, KERN_DEBUG "%s: DECODER_ENABLE_OUTPUT %d\n",
512 I2C_NAME(client), *iarg);
514 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
518 case DECODER_SET_PICTURE:
520 struct video_picture *pic = arg;
522 if (decoder->bright != pic->brightness) {
523 /* We want -128 to 128 we get 0-65535 */
524 decoder->bright = pic->brightness;
525 vpx3220_write(client, 0xe6,
526 (decoder->bright - 32768) >> 8);
528 if (decoder->contrast != pic->contrast) {
529 /* We want 0 to 64 we get 0-65535 */
530 /* Bit 7 and 8 is for noise shaping */
531 decoder->contrast = pic->contrast;
532 vpx3220_write(client, 0xe7,
533 (decoder->contrast >> 10) + 192);
535 if (decoder->sat != pic->colour) {
536 /* We want 0 to 4096 we get 0-65535 */
537 decoder->sat = pic->colour;
538 vpx3220_fp_write(client, 0xa0,
541 if (decoder->hue != pic->hue) {
542 /* We want -512 to 512 we get 0-65535 */
543 decoder->hue = pic->hue;
544 vpx3220_fp_write(client, 0x1c,
545 ((decoder->hue - 32768) >> 6) & 0xFFF);
558 vpx3220_init_client (struct i2c_client *client)
560 vpx3220_write_block(client, init_common, sizeof(init_common));
561 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
563 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
568 /* -----------------------------------------------------------------------
569 * Client management code
574 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
576 static unsigned short normal_i2c[] =
577 { I2C_VPX3220 >> 1, (I2C_VPX3220 >> 1) + 4,
581 static unsigned short ignore = I2C_CLIENT_END;
583 static struct i2c_client_address_data addr_data = {
584 .normal_i2c = normal_i2c,
589 static struct i2c_driver vpx3220_i2c_driver;
592 vpx3220_detach_client (struct i2c_client *client)
594 struct vpx3220 *decoder = i2c_get_clientdata(client);
597 err = i2c_detach_client(client);
609 vpx3220_detect_client (struct i2c_adapter *adapter,
614 struct i2c_client *client;
615 struct vpx3220 *decoder;
617 dprintk(1, VPX3220_DEBUG "%s\n", __func__);
619 /* Check if the adapter supports the needed features */
620 if (!i2c_check_functionality
621 (adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
624 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
625 if (client == NULL) {
629 client->addr = address;
630 client->adapter = adapter;
631 client->driver = &vpx3220_i2c_driver;
633 /* Check for manufacture ID and part number */
638 id = vpx3220_read(client, 0x00);
642 "vpx3220_attach: Wrong manufacturer ID (0x%02x)\n",
648 pn = (vpx3220_read(client, 0x02) << 8) +
649 vpx3220_read(client, 0x01);
652 strlcpy(I2C_NAME(client), "vpx3220a",
653 sizeof(I2C_NAME(client)));
656 strlcpy(I2C_NAME(client), "vpx3216b",
657 sizeof(I2C_NAME(client)));
660 strlcpy(I2C_NAME(client), "vpx3214c",
661 sizeof(I2C_NAME(client)));
666 "%s: Wrong part number (0x%04x)\n",
672 strlcpy(I2C_NAME(client), "forced vpx32xx",
673 sizeof(I2C_NAME(client)));
676 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
677 if (decoder == NULL) {
681 decoder->norm = VIDEO_MODE_PAL;
684 decoder->bright = 32768;
685 decoder->contrast = 32768;
686 decoder->hue = 32768;
687 decoder->sat = 32768;
688 i2c_set_clientdata(client, decoder);
690 err = i2c_attach_client(client);
697 dprintk(1, KERN_INFO "%s: vpx32xx client found at address 0x%02x\n",
698 I2C_NAME(client), client->addr << 1);
700 vpx3220_init_client(client);
706 vpx3220_attach_adapter (struct i2c_adapter *adapter)
710 ret = i2c_probe(adapter, &addr_data, &vpx3220_detect_client);
711 dprintk(1, VPX3220_DEBUG "%s: i2c_probe returned %d\n",
716 /* -----------------------------------------------------------------------
717 * Driver initialization and cleanup code
720 static struct i2c_driver vpx3220_i2c_driver = {
725 .id = I2C_DRIVERID_VPX3220,
727 .attach_adapter = vpx3220_attach_adapter,
728 .detach_client = vpx3220_detach_client,
729 .command = vpx3220_command,
735 return i2c_add_driver(&vpx3220_i2c_driver);
739 vpx3220_cleanup (void)
741 i2c_del_driver(&vpx3220_i2c_driver);
744 module_init(vpx3220_init);
745 module_exit(vpx3220_cleanup);
747 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
748 MODULE_AUTHOR("Laurent Pinchart");
749 MODULE_LICENSE("GPL");