Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / media / video / vpx3220.c
blob40b205b91481e495812c727da02baee30017834e
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>
34 #define I2C_NAME(x) (x)->name
36 #include <linux/videodev.h>
37 #include <linux/video_decoder.h>
39 #define I2C_VPX3220 0x86
40 #define VPX3220_DEBUG KERN_DEBUG "vpx3220: "
42 static int debug = 0;
43 module_param(debug, int, 0);
44 MODULE_PARM_DESC(debug, "Debug level (0-1)");
46 #define dprintk(num, format, args...) \
47 do { \
48 if (debug >= num) \
49 printk(format, ##args); \
50 } while (0)
52 #define VPX_TIMEOUT_COUNT 10
54 /* ----------------------------------------------------------------------- */
56 struct vpx3220 {
57 unsigned char reg[255];
59 int norm;
60 int input;
61 int enable;
62 int bright;
63 int contrast;
64 int hue;
65 int sat;
68 static char *inputs[] = { "internal", "composite", "svideo" };
70 /* ----------------------------------------------------------------------- */
71 static inline int
72 vpx3220_write (struct i2c_client *client,
73 u8 reg,
74 u8 value)
76 struct vpx3220 *decoder = i2c_get_clientdata(client);
78 decoder->reg[reg] = value;
79 return i2c_smbus_write_byte_data(client, reg, value);
82 static inline int
83 vpx3220_read (struct i2c_client *client,
84 u8 reg)
86 return i2c_smbus_read_byte_data(client, reg);
89 static int
90 vpx3220_fp_status (struct i2c_client *client)
92 unsigned char status;
93 unsigned int i;
95 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
96 status = vpx3220_read(client, 0x29);
98 if (!(status & 4))
99 return 0;
101 udelay(10);
103 if (need_resched())
104 cond_resched();
107 return -1;
110 static int
111 vpx3220_fp_write (struct i2c_client *client,
112 u8 fpaddr,
113 u16 data)
115 /* Write the 16-bit address to the FPWR register */
116 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
117 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
118 return -1;
121 if (vpx3220_fp_status(client) < 0)
122 return -1;
124 /* Write the 16-bit data to the FPDAT register */
125 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
126 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
127 return -1;
130 return 0;
133 static u16
134 vpx3220_fp_read (struct i2c_client *client,
135 u16 fpaddr)
137 s16 data;
139 /* Write the 16-bit address to the FPRD register */
140 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
141 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
142 return -1;
145 if (vpx3220_fp_status(client) < 0)
146 return -1;
148 /* Read the 16-bit data from the FPDAT register */
149 data = i2c_smbus_read_word_data(client, 0x28);
150 if (data == -1) {
151 dprintk(1, VPX3220_DEBUG "%s: failed\n", __func__);
152 return -1;
155 return swab16(data);
158 static int
159 vpx3220_write_block (struct i2c_client *client,
160 const u8 *data,
161 unsigned int len)
163 u8 reg;
164 int ret = -1;
166 while (len >= 2) {
167 reg = *data++;
168 if ((ret =
169 vpx3220_write(client, reg, *data++)) < 0)
170 break;
171 len -= 2;
174 return ret;
177 static int
178 vpx3220_write_fp_block (struct i2c_client *client,
179 const u16 *data,
180 unsigned int len)
182 u8 reg;
183 int ret = 0;
185 while (len > 1) {
186 reg = *data++;
187 ret |= vpx3220_fp_write(client, reg, *data++);
188 len -= 2;
191 return ret;
194 /* ---------------------------------------------------------------------- */
196 static const unsigned short init_ntsc[] = {
197 0x1c, 0x00, /* NTSC tint angle */
198 0x88, 17, /* Window 1 vertical */
199 0x89, 240, /* Vertical lines in */
200 0x8a, 240, /* Vertical lines out */
201 0x8b, 000, /* Horizontal begin */
202 0x8c, 640, /* Horizontal length */
203 0x8d, 640, /* Number of pixels */
204 0x8f, 0xc00, /* Disable window 2 */
205 0xf0, 0x73, /* 13.5 MHz transport, Forced
206 * mode, latch windows */
207 0xf2, 0x13, /* NTSC M, composite input */
208 0xe7, 0x1e1, /* Enable vertical standard
209 * locking @ 240 lines */
212 static const unsigned short init_pal[] = {
213 0x88, 23, /* Window 1 vertical begin */
214 0x89, 288, /* Vertical lines in (16 lines
215 * skipped by the VFE) */
216 0x8a, 288, /* Vertical lines out (16 lines
217 * skipped by the VFE) */
218 0x8b, 16, /* Horizontal begin */
219 0x8c, 768, /* Horizontal length */
220 0x8d, 784, /* Number of pixels
221 * Must be >= Horizontal begin + Horizontal length */
222 0x8f, 0xc00, /* Disable window 2 */
223 0xf0, 0x77, /* 13.5 MHz transport, Forced
224 * mode, latch windows */
225 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
226 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
229 static const unsigned short init_secam[] = {
230 0x88, 23, /* Window 1 vertical begin */
231 0x89, 288, /* Vertical lines in (16 lines
232 * skipped by the VFE) */
233 0x8a, 288, /* Vertical lines out (16 lines
234 * skipped by the VFE) */
235 0x8b, 16, /* Horizontal begin */
236 0x8c, 768, /* Horizontal length */
237 0x8d, 784, /* Number of pixels
238 * Must be >= Horizontal begin + Horizontal length */
239 0x8f, 0xc00, /* Disable window 2 */
240 0xf0, 0x77, /* 13.5 MHz transport, Forced
241 * mode, latch windows */
242 0xf2, 0x3d5, /* SECAM, composite input */
243 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
246 static const unsigned char init_common[] = {
247 0xf2, 0x00, /* Disable all outputs */
248 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
249 * (clamp off) */
250 0xd8, 0xa8, /* HREF/VREF active high, VREF
251 * pulse = 2, Odd/Even flag */
252 0x20, 0x03, /* IF compensation 0dB/oct */
253 0xe0, 0xff, /* Open up all comparators */
254 0xe1, 0x00,
255 0xe2, 0x7f,
256 0xe3, 0x80,
257 0xe4, 0x7f,
258 0xe5, 0x80,
259 0xe6, 0x00, /* Brightness set to 0 */
260 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
261 * 10 to 8 2-bit error diffusion */
262 0xe8, 0xf8, /* YUV422, CbCr binary offset,
263 * ... (p.32) */
264 0xea, 0x18, /* LLC2 connected, output FIFO
265 * reset with VACTintern */
266 0xf0, 0x8a, /* Half full level to 10, bus
267 * shuffler [7:0, 23:16, 15:8] */
268 0xf1, 0x18, /* Single clock, sync mode, no
269 * FE delay, no HLEN counter */
270 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
271 * strength to 2 */
272 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
273 * ALPHA strength to 4 */
276 static const unsigned short init_fp[] = {
277 0x59, 0,
278 0xa0, 2070, /* ACC reference */
279 0xa3, 0,
280 0xa4, 0,
281 0xa8, 30,
282 0xb2, 768,
283 0xbe, 27,
284 0x58, 0,
285 0x26, 0,
286 0x4b, 0x298, /* PLL gain */
289 static void
290 vpx3220_dump_i2c (struct i2c_client *client)
292 int len = sizeof(init_common);
293 const unsigned char *data = init_common;
295 while (len > 1) {
296 dprintk(1,
297 KERN_DEBUG "vpx3216b i2c reg 0x%02x data 0x%02x\n",
298 *data, vpx3220_read(client, *data));
299 data += 2;
300 len -= 2;
304 static int
305 vpx3220_command (struct i2c_client *client,
306 unsigned int cmd,
307 void *arg)
309 struct vpx3220 *decoder = i2c_get_clientdata(client);
311 switch (cmd) {
312 case 0:
314 vpx3220_write_block(client, init_common,
315 sizeof(init_common));
316 vpx3220_write_fp_block(client, init_fp,
317 sizeof(init_fp) >> 1);
318 switch (decoder->norm) {
320 case VIDEO_MODE_NTSC:
321 vpx3220_write_fp_block(client, init_ntsc,
322 sizeof(init_ntsc) >> 1);
323 break;
325 case VIDEO_MODE_PAL:
326 vpx3220_write_fp_block(client, init_pal,
327 sizeof(init_pal) >> 1);
328 break;
329 case VIDEO_MODE_SECAM:
330 vpx3220_write_fp_block(client, init_secam,
331 sizeof(init_secam) >> 1);
332 break;
333 default:
334 vpx3220_write_fp_block(client, init_pal,
335 sizeof(init_pal) >> 1);
336 break;
339 break;
341 case DECODER_DUMP:
343 vpx3220_dump_i2c(client);
345 break;
347 case DECODER_GET_CAPABILITIES:
349 struct video_decoder_capability *cap = arg;
351 dprintk(1, KERN_DEBUG "%s: DECODER_GET_CAPABILITIES\n",
352 I2C_NAME(client));
354 cap->flags = VIDEO_DECODER_PAL |
355 VIDEO_DECODER_NTSC |
356 VIDEO_DECODER_SECAM |
357 VIDEO_DECODER_AUTO |
358 VIDEO_DECODER_CCIR;
359 cap->inputs = 3;
360 cap->outputs = 1;
362 break;
364 case DECODER_GET_STATUS:
366 int res = 0, status;
368 dprintk(1, KERN_INFO "%s: DECODER_GET_STATUS\n",
369 I2C_NAME(client));
371 status = vpx3220_fp_read(client, 0x0f3);
373 dprintk(1, KERN_INFO "%s: status: 0x%04x\n", I2C_NAME(client),
374 status);
376 if (status < 0)
377 return status;
379 if ((status & 0x20) == 0) {
380 res |= DECODER_STATUS_GOOD | DECODER_STATUS_COLOR;
382 switch (status & 0x18) {
384 case 0x00:
385 case 0x10:
386 case 0x14:
387 case 0x18:
388 res |= DECODER_STATUS_PAL;
389 break;
391 case 0x08:
392 res |= DECODER_STATUS_SECAM;
393 break;
395 case 0x04:
396 case 0x0c:
397 case 0x1c:
398 res |= DECODER_STATUS_NTSC;
399 break;
403 *(int *) arg = res;
405 break;
407 case DECODER_SET_NORM:
409 int *iarg = arg, data;
410 int temp_input;
412 /* Here we back up the input selection because it gets
413 overwritten when we fill the registers with the
414 choosen video norm */
415 temp_input = vpx3220_fp_read(client, 0xf2);
417 dprintk(1, KERN_DEBUG "%s: DECODER_SET_NORM %d\n",
418 I2C_NAME(client), *iarg);
419 switch (*iarg) {
421 case VIDEO_MODE_NTSC:
422 vpx3220_write_fp_block(client, init_ntsc,
423 sizeof(init_ntsc) >> 1);
424 dprintk(1, KERN_INFO "%s: norm switched to NTSC\n",
425 I2C_NAME(client));
426 break;
428 case VIDEO_MODE_PAL:
429 vpx3220_write_fp_block(client, init_pal,
430 sizeof(init_pal) >> 1);
431 dprintk(1, KERN_INFO "%s: norm switched to PAL\n",
432 I2C_NAME(client));
433 break;
435 case VIDEO_MODE_SECAM:
436 vpx3220_write_fp_block(client, init_secam,
437 sizeof(init_secam) >> 1);
438 dprintk(1, KERN_INFO "%s: norm switched to SECAM\n",
439 I2C_NAME(client));
440 break;
442 case VIDEO_MODE_AUTO:
443 /* FIXME This is only preliminary support */
444 data = vpx3220_fp_read(client, 0xf2) & 0x20;
445 vpx3220_fp_write(client, 0xf2, 0x00c0 | data);
446 dprintk(1, KERN_INFO "%s: norm switched to Auto\n",
447 I2C_NAME(client));
448 break;
450 default:
451 return -EINVAL;
454 decoder->norm = *iarg;
456 /* And here we set the backed up video input again */
457 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
458 udelay(10);
460 break;
462 case DECODER_SET_INPUT:
464 int *iarg = arg, data;
466 /* RJ: *iarg = 0: ST8 (PCTV) input
467 *iarg = 1: COMPOSITE input
468 *iarg = 2: SVHS input */
470 const int input[3][2] = {
471 {0x0c, 0},
472 {0x0d, 0},
473 {0x0e, 1}
476 if (*iarg < 0 || *iarg > 2)
477 return -EINVAL;
479 dprintk(1, KERN_INFO "%s: input switched to %s\n",
480 I2C_NAME(client), inputs[*iarg]);
482 vpx3220_write(client, 0x33, input[*iarg][0]);
484 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
485 if (data < 0)
486 return data;
487 /* 0x0010 is required to latch the setting */
488 vpx3220_fp_write(client, 0xf2,
489 data | (input[*iarg][1] << 5) | 0x0010);
491 udelay(10);
493 break;
495 case DECODER_SET_OUTPUT:
497 int *iarg = arg;
499 /* not much choice of outputs */
500 if (*iarg != 0) {
501 return -EINVAL;
504 break;
506 case DECODER_ENABLE_OUTPUT:
508 int *iarg = arg;
510 dprintk(1, KERN_DEBUG "%s: DECODER_ENABLE_OUTPUT %d\n",
511 I2C_NAME(client), *iarg);
513 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
515 break;
517 case DECODER_SET_PICTURE:
519 struct video_picture *pic = arg;
521 if (decoder->bright != pic->brightness) {
522 /* We want -128 to 128 we get 0-65535 */
523 decoder->bright = pic->brightness;
524 vpx3220_write(client, 0xe6,
525 (decoder->bright - 32768) >> 8);
527 if (decoder->contrast != pic->contrast) {
528 /* We want 0 to 64 we get 0-65535 */
529 /* Bit 7 and 8 is for noise shaping */
530 decoder->contrast = pic->contrast;
531 vpx3220_write(client, 0xe7,
532 (decoder->contrast >> 10) + 192);
534 if (decoder->sat != pic->colour) {
535 /* We want 0 to 4096 we get 0-65535 */
536 decoder->sat = pic->colour;
537 vpx3220_fp_write(client, 0xa0,
538 decoder->sat >> 4);
540 if (decoder->hue != pic->hue) {
541 /* We want -512 to 512 we get 0-65535 */
542 decoder->hue = pic->hue;
543 vpx3220_fp_write(client, 0x1c,
544 ((decoder->hue - 32768) >> 6) & 0xFFF);
547 break;
549 default:
550 return -EINVAL;
553 return 0;
556 static int
557 vpx3220_init_client (struct i2c_client *client)
559 vpx3220_write_block(client, init_common, sizeof(init_common));
560 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
561 /* Default to PAL */
562 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
564 return 0;
567 /* -----------------------------------------------------------------------
568 * Client managment code
572 * Generic i2c probe
573 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
575 static unsigned short normal_i2c[] =
576 { I2C_VPX3220 >> 1, (I2C_VPX3220 >> 1) + 4,
577 I2C_CLIENT_END
580 static unsigned short ignore = I2C_CLIENT_END;
582 static struct i2c_client_address_data addr_data = {
583 .normal_i2c = normal_i2c,
584 .probe = &ignore,
585 .ignore = &ignore,
588 static struct i2c_driver vpx3220_i2c_driver;
590 static int
591 vpx3220_detach_client (struct i2c_client *client)
593 struct vpx3220 *decoder = i2c_get_clientdata(client);
594 int err;
596 err = i2c_detach_client(client);
597 if (err) {
598 return err;
601 kfree(decoder);
602 kfree(client);
604 return 0;
607 static int
608 vpx3220_detect_client (struct i2c_adapter *adapter,
609 int address,
610 int kind)
612 int err;
613 struct i2c_client *client;
614 struct vpx3220 *decoder;
616 dprintk(1, VPX3220_DEBUG "%s\n", __func__);
618 /* Check if the adapter supports the needed features */
619 if (!i2c_check_functionality
620 (adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
621 return 0;
623 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
624 if (client == NULL) {
625 return -ENOMEM;
628 client->addr = address;
629 client->adapter = adapter;
630 client->driver = &vpx3220_i2c_driver;
632 /* Check for manufacture ID and part number */
633 if (kind < 0) {
634 u8 id;
635 u16 pn;
637 id = vpx3220_read(client, 0x00);
638 if (id != 0xec) {
639 dprintk(1,
640 KERN_INFO
641 "vpx3220_attach: Wrong manufacturer ID (0x%02x)\n",
642 id);
643 kfree(client);
644 return 0;
647 pn = (vpx3220_read(client, 0x02) << 8) +
648 vpx3220_read(client, 0x01);
649 switch (pn) {
650 case 0x4680:
651 strlcpy(I2C_NAME(client), "vpx3220a",
652 sizeof(I2C_NAME(client)));
653 break;
654 case 0x4260:
655 strlcpy(I2C_NAME(client), "vpx3216b",
656 sizeof(I2C_NAME(client)));
657 break;
658 case 0x4280:
659 strlcpy(I2C_NAME(client), "vpx3214c",
660 sizeof(I2C_NAME(client)));
661 break;
662 default:
663 dprintk(1,
664 KERN_INFO
665 "%s: Wrong part number (0x%04x)\n",
666 __func__, pn);
667 kfree(client);
668 return 0;
670 } else {
671 strlcpy(I2C_NAME(client), "forced vpx32xx",
672 sizeof(I2C_NAME(client)));
675 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
676 if (decoder == NULL) {
677 kfree(client);
678 return -ENOMEM;
680 decoder->norm = VIDEO_MODE_PAL;
681 decoder->input = 0;
682 decoder->enable = 1;
683 decoder->bright = 32768;
684 decoder->contrast = 32768;
685 decoder->hue = 32768;
686 decoder->sat = 32768;
687 i2c_set_clientdata(client, decoder);
689 err = i2c_attach_client(client);
690 if (err) {
691 kfree(client);
692 kfree(decoder);
693 return err;
696 dprintk(1, KERN_INFO "%s: vpx32xx client found at address 0x%02x\n",
697 I2C_NAME(client), client->addr << 1);
699 vpx3220_init_client(client);
701 return 0;
704 static int
705 vpx3220_attach_adapter (struct i2c_adapter *adapter)
707 int ret;
709 ret = i2c_probe(adapter, &addr_data, &vpx3220_detect_client);
710 dprintk(1, VPX3220_DEBUG "%s: i2c_probe returned %d\n",
711 __func__, ret);
712 return ret;
715 /* -----------------------------------------------------------------------
716 * Driver initialization and cleanup code
719 static struct i2c_driver vpx3220_i2c_driver = {
720 .driver = {
721 .name = "vpx3220",
724 .id = I2C_DRIVERID_VPX3220,
726 .attach_adapter = vpx3220_attach_adapter,
727 .detach_client = vpx3220_detach_client,
728 .command = vpx3220_command,
731 static int __init
732 vpx3220_init (void)
734 return i2c_add_driver(&vpx3220_i2c_driver);
737 static void __exit
738 vpx3220_cleanup (void)
740 i2c_del_driver(&vpx3220_i2c_driver);
743 module_init(vpx3220_init);
744 module_exit(vpx3220_cleanup);
746 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video encoder driver");
747 MODULE_AUTHOR("Laurent Pinchart");
748 MODULE_LICENSE("GPL");