MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / media / video / vpx3220.c
blob935b72c3da3db7e79c6ce0019aba273af5098557
1 /*
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>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
32 #include <linux/i2c.h>
33 #include <linux/i2c-dev.h>
35 #define I2C_NAME(x) (x)->name
37 #include <linux/videodev.h>
38 #include <linux/video_decoder.h>
40 #define I2C_VPX3220 0x86
41 #define VPX3220_DEBUG KERN_DEBUG "vpx3220: "
43 static int debug = 0;
44 MODULE_PARM(debug, "i");
45 MODULE_PARM_DESC(debug, "Debug level (0-1)");
47 #define dprintk(num, format, args...) \
48 do { \
49 if (debug >= num) \
50 printk(format, ##args); \
51 } while (0)
53 #define VPX_TIMEOUT_COUNT 10
55 /* ----------------------------------------------------------------------- */
57 struct vpx3220 {
58 unsigned char reg[255];
60 int norm;
61 int input;
62 int enable;
63 int bright;
64 int contrast;
65 int hue;
66 int sat;
69 static char *inputs[] = { "internal", "composite", "svideo" };
71 /* ----------------------------------------------------------------------- */
72 static inline int
73 vpx3220_write (struct i2c_client *client,
74 u8 reg,
75 u8 value)
77 struct vpx3220 *decoder = i2c_get_clientdata(client);
79 decoder->reg[reg] = value;
80 return i2c_smbus_write_byte_data(client, reg, value);
83 static inline int
84 vpx3220_read (struct i2c_client *client,
85 u8 reg)
87 return i2c_smbus_read_byte_data(client, reg);
90 static int
91 vpx3220_fp_status (struct i2c_client *client)
93 unsigned char status;
94 unsigned int i;
96 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
97 status = vpx3220_read(client, 0x29);
99 if (!(status & 4))
100 return 0;
102 udelay(10);
104 if (need_resched())
105 cond_resched();
108 return -1;
111 static int
112 vpx3220_fp_write (struct i2c_client *client,
113 u8 fpaddr,
114 u16 data)
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__);
119 return -1;
122 if (vpx3220_fp_status(client) < 0)
123 return -1;
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__);
128 return -1;
131 return 0;
134 static u16
135 vpx3220_fp_read (struct i2c_client *client,
136 u16 fpaddr)
138 s16 data;
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__);
143 return -1;
146 if (vpx3220_fp_status(client) < 0)
147 return -1;
149 /* Read the 16-bit data from the FPDAT register */
150 data = i2c_smbus_read_word_data(client, 0x28);
151 if (data == -1) {
152 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
153 return -1;
156 return swab16(data);
159 static int
160 vpx3220_write_block (struct i2c_client *client,
161 const u8 *data,
162 unsigned int len)
164 u8 reg;
165 int ret = -1;
167 while (len >= 2) {
168 reg = *data++;
169 if ((ret =
170 vpx3220_write(client, reg, *data++)) < 0)
171 break;
172 len -= 2;
175 return ret;
178 static int
179 vpx3220_write_fp_block (struct i2c_client *client,
180 const u16 *data,
181 unsigned int len)
183 u8 reg;
184 int ret = 0;
186 while (len > 1) {
187 reg = *data++;
188 ret |= vpx3220_fp_write(client, reg, *data++);
189 len -= 2;
192 return ret;
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, 0x173, /* 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 + 16, /* Vertical lines in (16 lines
216 * skipped by the VFE) */
217 0x8a, 288 + 16, /* 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, 0x177, /* 13.5 MHz transport, Forced
225 * mode, latch windows */
226 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
227 0xe7, 0x261, /* PAL/SECAM set to 288 + 16 lines
228 * change to 0x241 for 288 lines */
231 static const unsigned short init_secam[] = {
232 0x88, 23 - 16, /* Window 1 vertical begin */
233 0x89, 288 + 16, /* Vertical lines in (16 lines
234 * skipped by the VFE) */
235 0x8a, 288 + 16, /* Vertical lines out (16 lines
236 * skipped by the VFE) */
237 0x8b, 16, /* Horizontal begin */
238 0x8c, 768, /* Horizontal length */
239 0x8d, 784, /* Number of pixels
240 * Must be >= Horizontal begin + Horizontal length */
241 0x8f, 0xc00, /* Disable window 2 */
242 0xf0, 0x177, /* 13.5 MHz transport, Forced
243 * mode, latch windows */
244 0xf2, 0x3d5, /* SECAM, composite input */
245 0xe7, 0x261, /* PAL/SECAM set to 288 + 16 lines
246 * change to 0x241 for 288 lines */
249 static const unsigned char init_common[] = {
250 0xf2, 0x00, /* Disable all outputs */
251 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
252 * (clamp off) */
253 0xd8, 0xa8, /* HREF/VREF active high, VREF
254 * pulse = 2, Odd/Even flag */
255 0x20, 0x03, /* IF compensation 0dB/oct */
256 0xe0, 0xff, /* Open up all comparators */
257 0xe1, 0x00,
258 0xe2, 0x7f,
259 0xe3, 0x80,
260 0xe4, 0x7f,
261 0xe5, 0x80,
262 0xe6, 0x00, /* Brightness set to 0 */
263 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
264 * 10 to 8 2-bit error diffusion */
265 0xe8, 0xf8, /* YUV422, CbCr binary offset,
266 * ... (p.32) */
267 0xea, 0x18, /* LLC2 connected, output FIFO
268 * reset with VACTintern */
269 0xf0, 0x8a, /* Half full level to 10, bus
270 * shuffler [7:0, 23:16, 15:8] */
271 0xf1, 0x18, /* Single clock, sync mode, no
272 * FE delay, no HLEN counter */
273 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
274 * strength to 2 */
275 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
276 * ALPHA strength to 4 */
279 static const unsigned short init_fp[] = {
280 0x59, 0,
281 0xa0, 2070, /* ACC reference */
282 0xa3, 0,
283 0xa4, 0,
284 0xa8, 30,
285 0xb2, 768,
286 0xbe, 27,
287 0x58, 0,
288 0x26, 0,
289 0x4b, 0x298, /* PLL gain */
292 static void
293 vpx3220_dump_i2c (struct i2c_client *client)
295 int len = sizeof(init_common);
296 const unsigned char *data = init_common;
298 while (len > 1) {
299 dprintk(1,
300 KERN_DEBUG "vpx3216b i2c reg 0x%02x data 0x%02x\n",
301 *data, vpx3220_read(client, *data));
302 data += 2;
303 len -= 2;
307 static int
308 vpx3220_command (struct i2c_client *client,
309 unsigned int cmd,
310 void *arg)
312 struct vpx3220 *decoder = i2c_get_clientdata(client);
314 switch (cmd) {
315 case 0:
317 vpx3220_write_block(client, init_common,
318 sizeof(init_common));
319 vpx3220_write_fp_block(client, init_fp,
320 sizeof(init_fp) >> 1);
321 switch (decoder->norm) {
323 case VIDEO_MODE_NTSC:
324 vpx3220_write_fp_block(client, init_ntsc,
325 sizeof(init_ntsc) >> 1);
326 break;
328 case VIDEO_MODE_PAL:
329 vpx3220_write_fp_block(client, init_pal,
330 sizeof(init_pal) >> 1);
331 break;
332 case VIDEO_MODE_SECAM:
333 vpx3220_write_fp_block(client, init_secam,
334 sizeof(init_secam) >> 1);
335 break;
336 default:
337 vpx3220_write_fp_block(client, init_pal,
338 sizeof(init_pal) >> 1);
339 break;
342 break;
344 case DECODER_DUMP:
346 vpx3220_dump_i2c(client);
348 break;
350 case DECODER_GET_CAPABILITIES:
352 struct video_decoder_capability *cap = arg;
354 dprintk(1, KERN_DEBUG "%s: DECODER_GET_CAPABILITIES\n",
355 I2C_NAME(client));
357 cap->flags = VIDEO_DECODER_PAL |
358 VIDEO_DECODER_NTSC |
359 VIDEO_DECODER_SECAM |
360 VIDEO_DECODER_AUTO |
361 VIDEO_DECODER_CCIR;
362 cap->inputs = 3;
363 cap->outputs = 1;
365 break;
367 case DECODER_GET_STATUS:
369 int res = 0, status;
371 dprintk(1, KERN_INFO "%s: DECODER_GET_STATUS\n",
372 I2C_NAME(client));
374 status = vpx3220_fp_read(client, 0x0f3);
376 dprintk(1, KERN_INFO "%s: status: 0x%04x\n", I2C_NAME(client),
377 status);
379 if (status < 0)
380 return status;
382 if ((status & 0x20) == 0) {
383 res |= DECODER_STATUS_GOOD | DECODER_STATUS_COLOR;
385 switch (status & 0x18) {
387 case 0x00:
388 case 0x10:
389 case 0x14:
390 case 0x18:
391 res |= DECODER_STATUS_PAL;
392 break;
394 case 0x08:
395 res |= DECODER_STATUS_SECAM;
396 break;
398 case 0x04:
399 case 0x0c:
400 case 0x1c:
401 res |= DECODER_STATUS_NTSC;
402 break;
406 *(int *) arg = res;
408 break;
410 case DECODER_SET_NORM:
412 int *iarg = arg, data;
414 dprintk(1, KERN_DEBUG "%s: DECODER_SET_NORM %d\n",
415 I2C_NAME(client), *iarg);
416 switch (*iarg) {
418 case VIDEO_MODE_NTSC:
419 vpx3220_write_fp_block(client, init_ntsc,
420 sizeof(init_ntsc) >> 1);
421 dprintk(1, KERN_INFO "%s: norm switched to NTSC\n",
422 I2C_NAME(client));
423 break;
425 case VIDEO_MODE_PAL:
426 vpx3220_write_fp_block(client, init_pal,
427 sizeof(init_pal) >> 1);
428 dprintk(1, KERN_INFO "%s: norm switched to PAL\n",
429 I2C_NAME(client));
430 break;
432 case VIDEO_MODE_SECAM:
433 vpx3220_write_fp_block(client, init_secam,
434 sizeof(init_secam) >> 1);
435 dprintk(1, KERN_INFO "%s: norm switched to SECAM\n",
436 I2C_NAME(client));
437 break;
439 case VIDEO_MODE_AUTO:
440 /* FIXME This is only preliminary support */
441 data = vpx3220_fp_read(client, 0xf2) & 0x20;
442 vpx3220_fp_write(client, 0xf2, 0x00c0 | data);
443 dprintk(1, KERN_INFO "%s: norm switched to Auto\n",
444 I2C_NAME(client));
445 break;
447 default:
448 return -EINVAL;
451 decoder->norm = *iarg;
453 break;
455 case DECODER_SET_INPUT:
457 int *iarg = arg, data;
459 /* RJ: *iarg = 0: ST8 (PCTV) input
460 *iarg = 1: COMPOSITE input
461 *iarg = 2: SVHS input */
463 const int input[3][2] = {
464 {0x0c, 0},
465 {0x0d, 0},
466 {0x0e, 1}
469 if (*iarg < 0 || *iarg > 2)
470 return -EINVAL;
472 dprintk(1, KERN_INFO "%s: input switched to %s\n",
473 I2C_NAME(client), inputs[*iarg]);
475 vpx3220_write(client, 0x33, input[*iarg][0]);
477 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
478 if (data < 0)
479 return data;
480 /* 0x0010 is required to latch the setting */
481 vpx3220_fp_write(client, 0xf2,
482 data | (input[*iarg][1] << 5) | 0x0010);
484 udelay(10);
486 break;
488 case DECODER_SET_OUTPUT:
490 int *iarg = arg;
492 /* not much choice of outputs */
493 if (*iarg != 0) {
494 return -EINVAL;
497 break;
499 case DECODER_ENABLE_OUTPUT:
501 int *iarg = arg;
503 dprintk(1, KERN_DEBUG "%s: DECODER_ENABLE_OUTPUT %d\n",
504 I2C_NAME(client), *iarg);
506 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
508 break;
510 case DECODER_SET_PICTURE:
512 struct video_picture *pic = arg;
514 if (decoder->bright != pic->brightness) {
515 /* We want -128 to 128 we get 0-65535 */
516 decoder->bright = pic->brightness;
517 vpx3220_write(client, 0xe6,
518 (decoder->bright - 32768) >> 8);
520 if (decoder->contrast != pic->contrast) {
521 /* We want 0 to 64 we get 0-65535 */
522 /* Bit 7 and 8 is for noise shaping */
523 decoder->contrast = pic->contrast;
524 vpx3220_write(client, 0xe7,
525 (decoder->contrast >> 10) + 192);
527 if (decoder->sat != pic->colour) {
528 /* We want 0 to 4096 we get 0-65535 */
529 decoder->sat = pic->colour;
530 vpx3220_fp_write(client, 0xa0,
531 decoder->sat >> 4);
533 if (decoder->hue != pic->hue) {
534 /* We want -512 to 512 we get 0-65535 */
535 decoder->hue = pic->hue;
536 vpx3220_fp_write(client, 0x1c,
537 ((decoder->hue - 32768) >> 6) & 0xFFF);
540 break;
542 default:
543 return -EINVAL;
546 return 0;
549 static int
550 vpx3220_init_client (struct i2c_client *client)
552 vpx3220_write_block(client, init_common, sizeof(init_common));
553 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
554 /* Default to PAL */
555 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
557 return 0;
560 /* -----------------------------------------------------------------------
561 * Client managment code
565 * Generic i2c probe
566 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
568 static unsigned short normal_i2c[] =
569 { I2C_VPX3220 >> 1, (I2C_VPX3220 >> 1) + 4,
570 I2C_CLIENT_END
572 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
574 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
575 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
576 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
577 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
578 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
580 static struct i2c_client_address_data addr_data = {
581 .normal_i2c = normal_i2c,
582 .normal_i2c_range = normal_i2c_range,
583 .probe = probe,
584 .probe_range = probe_range,
585 .ignore = ignore,
586 .ignore_range = ignore_range,
587 .force = force
590 static int vpx3220_i2c_id = 0;
591 static struct i2c_driver vpx3220_i2c_driver;
593 static int
594 vpx3220_detach_client (struct i2c_client *client)
596 struct vpx3220 *decoder = i2c_get_clientdata(client);
597 int err;
599 err = i2c_detach_client(client);
600 if (err) {
601 return err;
604 kfree(decoder);
605 kfree(client);
607 return 0;
610 static int
611 vpx3220_detect_client (struct i2c_adapter *adapter,
612 int address,
613 int kind)
615 int err;
616 struct i2c_client *client;
617 struct vpx3220 *decoder;
619 dprintk(1, VPX3220_DEBUG "%s\n", __func__);
621 /* Check if the adapter supports the needed features */
622 if (!i2c_check_functionality
623 (adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
624 return 0;
626 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
627 if (client == NULL) {
628 return -ENOMEM;
631 memset(client, 0, sizeof(struct i2c_client));
633 client->addr = address;
634 client->adapter = adapter;
635 client->driver = &vpx3220_i2c_driver;
636 client->flags = I2C_CLIENT_ALLOW_USE;
637 client->id = vpx3220_i2c_id++;
639 /* Check for manufacture ID and part number */
640 if (kind < 0) {
641 u8 id;
642 u16 pn;
644 id = vpx3220_read(client, 0x00);
645 if (id != 0xec) {
646 dprintk(1,
647 KERN_INFO
648 "vpx3220_attach: Wrong manufacturer ID (0x%02x)\n",
649 id);
650 kfree(client);
651 return 0;
654 pn = (vpx3220_read(client, 0x02) << 8) +
655 vpx3220_read(client, 0x01);
656 switch (pn) {
657 case 0x4680:
658 snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
659 "vpx3220a[%d]", client->id);
660 break;
661 case 0x4260:
662 snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
663 "vpx3216b[%d]", client->id);
664 break;
665 case 0x4280:
666 snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
667 "vpx3214c[%d]", client->id);
668 break;
669 default:
670 dprintk(1,
671 KERN_INFO
672 "%s: Wrong part number (0x%04x)\n",
673 __func__, pn);
674 kfree(client);
675 return 0;
677 } else {
678 snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
679 "forced vpx32xx[%d]",
680 client->id);
683 decoder = kmalloc(sizeof(struct vpx3220), GFP_KERNEL);
684 if (decoder == NULL) {
685 kfree(client);
686 return -ENOMEM;
688 memset(decoder, 0, sizeof(struct vpx3220));
689 decoder->norm = VIDEO_MODE_PAL;
690 decoder->input = 0;
691 decoder->enable = 1;
692 decoder->bright = 32768;
693 decoder->contrast = 32768;
694 decoder->hue = 32768;
695 decoder->sat = 32768;
696 i2c_set_clientdata(client, decoder);
698 err = i2c_attach_client(client);
699 if (err) {
700 kfree(client);
701 kfree(decoder);
702 return err;
705 dprintk(1, KERN_INFO "%s: vpx32xx client found at address 0x%02x\n",
706 I2C_NAME(client), client->addr << 1);
708 vpx3220_init_client(client);
710 return 0;
713 static int
714 vpx3220_attach_adapter (struct i2c_adapter *adapter)
716 int ret;
718 ret = i2c_probe(adapter, &addr_data, &vpx3220_detect_client);
719 dprintk(1, VPX3220_DEBUG "%s: i2c_probe returned %d\n",
720 __func__, ret);
721 return ret;
724 /* -----------------------------------------------------------------------
725 * Driver initialization and cleanup code
728 static struct i2c_driver vpx3220_i2c_driver = {
729 .owner = THIS_MODULE,
730 .name = "vpx3220",
732 .id = I2C_DRIVERID_VPX3220,
733 .flags = I2C_DF_NOTIFY,
735 .attach_adapter = vpx3220_attach_adapter,
736 .detach_client = vpx3220_detach_client,
737 .command = vpx3220_command,
740 static int __init
741 vpx3220_init (void)
743 return i2c_add_driver(&vpx3220_i2c_driver);
746 static void __exit
747 vpx3220_cleanup (void)
749 i2c_del_driver(&vpx3220_i2c_driver);
752 module_init(vpx3220_init);
753 module_exit(vpx3220_cleanup);
755 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video encoder driver");
756 MODULE_AUTHOR("Laurent Pinchart");
757 MODULE_LICENSE("GPL");