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