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/videodev2.h>
31 #include <linux/video_decoder.h>
33 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
34 MODULE_AUTHOR("Laurent Pinchart");
35 MODULE_LICENSE("GPL");
38 module_param(debug, int, 0);
39 MODULE_PARM_DESC(debug, "Debug level (0-1)");
41 #define VPX_TIMEOUT_COUNT 10
43 /* ----------------------------------------------------------------------- */
46 unsigned char reg[255];
57 static char *inputs[] = { "internal", "composite", "svideo" };
59 /* ----------------------------------------------------------------------- */
61 static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
63 struct vpx3220 *decoder = i2c_get_clientdata(client);
65 decoder->reg[reg] = value;
66 return i2c_smbus_write_byte_data(client, reg, value);
69 static inline int vpx3220_read(struct i2c_client *client, u8 reg)
71 return i2c_smbus_read_byte_data(client, reg);
74 static int vpx3220_fp_status(struct i2c_client *client)
79 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
80 status = vpx3220_read(client, 0x29);
94 static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
96 /* Write the 16-bit address to the FPWR register */
97 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
98 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
102 if (vpx3220_fp_status(client) < 0)
105 /* Write the 16-bit data to the FPDAT register */
106 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
107 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
114 static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
118 /* Write the 16-bit address to the FPRD register */
119 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
120 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
124 if (vpx3220_fp_status(client) < 0)
127 /* Read the 16-bit data from the FPDAT register */
128 data = i2c_smbus_read_word_data(client, 0x28);
130 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
137 static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
144 ret = vpx3220_write(client, reg, *data++);
153 static int vpx3220_write_fp_block(struct i2c_client *client,
154 const u16 *data, unsigned int len)
161 ret |= vpx3220_fp_write(client, reg, *data++);
168 /* ---------------------------------------------------------------------- */
170 static const unsigned short init_ntsc[] = {
171 0x1c, 0x00, /* NTSC tint angle */
172 0x88, 17, /* Window 1 vertical */
173 0x89, 240, /* Vertical lines in */
174 0x8a, 240, /* Vertical lines out */
175 0x8b, 000, /* Horizontal begin */
176 0x8c, 640, /* Horizontal length */
177 0x8d, 640, /* Number of pixels */
178 0x8f, 0xc00, /* Disable window 2 */
179 0xf0, 0x73, /* 13.5 MHz transport, Forced
180 * mode, latch windows */
181 0xf2, 0x13, /* NTSC M, composite input */
182 0xe7, 0x1e1, /* Enable vertical standard
183 * locking @ 240 lines */
186 static const unsigned short init_pal[] = {
187 0x88, 23, /* Window 1 vertical begin */
188 0x89, 288, /* Vertical lines in (16 lines
189 * skipped by the VFE) */
190 0x8a, 288, /* Vertical lines out (16 lines
191 * skipped by the VFE) */
192 0x8b, 16, /* Horizontal begin */
193 0x8c, 768, /* Horizontal length */
194 0x8d, 784, /* Number of pixels
195 * Must be >= Horizontal begin + Horizontal length */
196 0x8f, 0xc00, /* Disable window 2 */
197 0xf0, 0x77, /* 13.5 MHz transport, Forced
198 * mode, latch windows */
199 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
200 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
203 static const unsigned short init_secam[] = {
204 0x88, 23, /* Window 1 vertical begin */
205 0x89, 288, /* Vertical lines in (16 lines
206 * skipped by the VFE) */
207 0x8a, 288, /* Vertical lines out (16 lines
208 * skipped by the VFE) */
209 0x8b, 16, /* Horizontal begin */
210 0x8c, 768, /* Horizontal length */
211 0x8d, 784, /* Number of pixels
212 * Must be >= Horizontal begin + Horizontal length */
213 0x8f, 0xc00, /* Disable window 2 */
214 0xf0, 0x77, /* 13.5 MHz transport, Forced
215 * mode, latch windows */
216 0xf2, 0x3d5, /* SECAM, composite input */
217 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
220 static const unsigned char init_common[] = {
221 0xf2, 0x00, /* Disable all outputs */
222 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
224 0xd8, 0xa8, /* HREF/VREF active high, VREF
225 * pulse = 2, Odd/Even flag */
226 0x20, 0x03, /* IF compensation 0dB/oct */
227 0xe0, 0xff, /* Open up all comparators */
233 0xe6, 0x00, /* Brightness set to 0 */
234 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
235 * 10 to 8 2-bit error diffusion */
236 0xe8, 0xf8, /* YUV422, CbCr binary offset,
238 0xea, 0x18, /* LLC2 connected, output FIFO
239 * reset with VACTintern */
240 0xf0, 0x8a, /* Half full level to 10, bus
241 * shuffler [7:0, 23:16, 15:8] */
242 0xf1, 0x18, /* Single clock, sync mode, no
243 * FE delay, no HLEN counter */
244 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
246 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
247 * ALPHA strength to 4 */
250 static const unsigned short init_fp[] = {
252 0xa0, 2070, /* ACC reference */
260 0x4b, 0x298, /* PLL gain */
264 static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
266 struct vpx3220 *decoder = i2c_get_clientdata(client);
269 case VIDIOC_INT_INIT:
271 vpx3220_write_block(client, init_common,
272 sizeof(init_common));
273 vpx3220_write_fp_block(client, init_fp,
274 sizeof(init_fp) >> 1);
275 if (decoder->norm & V4L2_STD_NTSC) {
276 vpx3220_write_fp_block(client, init_ntsc,
277 sizeof(init_ntsc) >> 1);
278 } else if (decoder->norm & V4L2_STD_PAL) {
279 vpx3220_write_fp_block(client, init_pal,
280 sizeof(init_pal) >> 1);
281 } else if (decoder->norm & V4L2_STD_SECAM) {
282 vpx3220_write_fp_block(client, init_secam,
283 sizeof(init_secam) >> 1);
285 vpx3220_write_fp_block(client, init_pal,
286 sizeof(init_pal) >> 1);
291 case VIDIOC_QUERYSTD:
292 case VIDIOC_INT_G_INPUT_STATUS:
294 int res = V4L2_IN_ST_NO_SIGNAL, status;
297 v4l_dbg(1, debug, client, "VIDIOC_QUERYSTD/VIDIOC_INT_G_INPUT_STATUS\n");
299 status = vpx3220_fp_read(client, 0x0f3);
301 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
306 if ((status & 0x20) == 0) {
309 switch (status & 0x18) {
318 std = V4L2_STD_SECAM;
329 if (cmd == VIDIOC_QUERYSTD)
330 *(v4l2_std_id *)arg = std;
338 v4l2_std_id *iarg = arg;
341 /* Here we back up the input selection because it gets
342 overwritten when we fill the registers with the
343 choosen video norm */
344 temp_input = vpx3220_fp_read(client, 0xf2);
346 v4l_dbg(1, debug, client, "VIDIOC_S_STD %llx\n", *iarg);
347 if (*iarg & V4L2_STD_NTSC) {
348 vpx3220_write_fp_block(client, init_ntsc,
349 sizeof(init_ntsc) >> 1);
350 v4l_dbg(1, debug, client, "norm switched to NTSC\n");
351 } else if (*iarg & V4L2_STD_PAL) {
352 vpx3220_write_fp_block(client, init_pal,
353 sizeof(init_pal) >> 1);
354 v4l_dbg(1, debug, client, "norm switched to PAL\n");
355 } else if (*iarg & V4L2_STD_SECAM) {
356 vpx3220_write_fp_block(client, init_secam,
357 sizeof(init_secam) >> 1);
358 v4l_dbg(1, debug, client, "norm switched to SECAM\n");
363 decoder->norm = *iarg;
365 /* And here we set the backed up video input again */
366 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
371 case VIDIOC_INT_S_VIDEO_ROUTING:
373 struct v4l2_routing *route = arg;
376 /* RJ: *iarg = 0: ST8 (PCTV) input
377 *iarg = 1: COMPOSITE input
378 *iarg = 2: SVHS input */
380 const int input[3][2] = {
386 if (route->input < 0 || route->input > 2)
389 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[route->input]);
391 vpx3220_write(client, 0x33, input[route->input][0]);
393 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
396 /* 0x0010 is required to latch the setting */
397 vpx3220_fp_write(client, 0xf2,
398 data | (input[route->input][1] << 5) | 0x0010);
404 case VIDIOC_STREAMON:
405 case VIDIOC_STREAMOFF:
407 int on = cmd == VIDIOC_STREAMON;
408 v4l_dbg(1, debug, client, "VIDIOC_STREAM%s\n", on ? "ON" : "OFF");
410 vpx3220_write(client, 0xf2, (on ? 0x1b : 0x00));
414 case VIDIOC_QUERYCTRL:
416 struct v4l2_queryctrl *qc = arg;
419 case V4L2_CID_BRIGHTNESS:
420 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
423 case V4L2_CID_CONTRAST:
424 v4l2_ctrl_query_fill(qc, 0, 63, 1, 32);
427 case V4L2_CID_SATURATION:
428 v4l2_ctrl_query_fill(qc, 0, 4095, 1, 2048);
432 v4l2_ctrl_query_fill(qc, -512, 511, 1, 0);
443 struct v4l2_control *ctrl = arg;
446 case V4L2_CID_BRIGHTNESS:
447 ctrl->value = decoder->bright;
449 case V4L2_CID_CONTRAST:
450 ctrl->value = decoder->contrast;
452 case V4L2_CID_SATURATION:
453 ctrl->value = decoder->sat;
456 ctrl->value = decoder->hue;
466 struct v4l2_control *ctrl = arg;
469 case V4L2_CID_BRIGHTNESS:
470 if (decoder->bright != ctrl->value) {
471 decoder->bright = ctrl->value;
472 vpx3220_write(client, 0xe6, decoder->bright);
475 case V4L2_CID_CONTRAST:
476 if (decoder->contrast != ctrl->value) {
477 /* Bit 7 and 8 is for noise shaping */
478 decoder->contrast = ctrl->value;
479 vpx3220_write(client, 0xe7,
480 decoder->contrast + 192);
483 case V4L2_CID_SATURATION:
484 if (decoder->sat != ctrl->value) {
485 decoder->sat = ctrl->value;
486 vpx3220_fp_write(client, 0xa0, decoder->sat);
490 if (decoder->hue != ctrl->value) {
491 decoder->hue = ctrl->value;
492 vpx3220_fp_write(client, 0x1c, decoder->hue);
508 static int vpx3220_init_client(struct i2c_client *client)
510 vpx3220_write_block(client, init_common, sizeof(init_common));
511 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
513 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
518 /* -----------------------------------------------------------------------
519 * Client management code
522 static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
526 static int vpx3220_probe(struct i2c_client *client,
527 const struct i2c_device_id *id)
529 struct vpx3220 *decoder;
530 const char *name = NULL;
534 /* Check if the adapter supports the needed features */
535 if (!i2c_check_functionality(client->adapter,
536 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
539 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
542 decoder->norm = V4L2_STD_PAL;
545 decoder->bright = 32768;
546 decoder->contrast = 32768;
547 decoder->hue = 32768;
548 decoder->sat = 32768;
549 i2c_set_clientdata(client, decoder);
551 ver = i2c_smbus_read_byte_data(client, 0x00);
552 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
553 i2c_smbus_read_byte_data(client, 0x01);
568 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
569 client->addr << 1, client->adapter->name);
571 v4l_info(client, "chip (%02x:%04x) found @ 0x%x (%s)\n",
572 ver, pn, client->addr << 1, client->adapter->name);
574 vpx3220_init_client(client);
578 static int vpx3220_remove(struct i2c_client *client)
580 kfree(i2c_get_clientdata(client));
584 static const struct i2c_device_id vpx3220_id[] = {
590 MODULE_DEVICE_TABLE(i2c, vpx3220_id);
592 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
594 .driverid = I2C_DRIVERID_VPX3220,
595 .command = vpx3220_command,
596 .probe = vpx3220_probe,
597 .remove = vpx3220_remove,
598 .id_table = vpx3220_id,