Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[cascardo/linux.git] / drivers / media / video / saa7111.c
1 /*
2  * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
3  *
4  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5  *
6  * Slight changes for video timing and attachment output by
7  * Wolfgang Scherr <scherr@net4you.net>
8  *
9  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
11  *
12  * Changes by Michael Hunold <michael@mihu.de>
13  *    - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/fs.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/slab.h>
38 #include <linux/mm.h>
39 #include <linux/signal.h>
40 #include <asm/io.h>
41 #include <asm/pgtable.h>
42 #include <asm/page.h>
43 #include <linux/types.h>
44
45 #include <linux/videodev.h>
46 #include <asm/uaccess.h>
47
48 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
49 MODULE_AUTHOR("Dave Perks");
50 MODULE_LICENSE("GPL");
51
52 #include <linux/i2c.h>
53
54 #define I2C_NAME(s) (s)->name
55
56 #include <linux/video_decoder.h>
57
58 static int debug = 0;
59 module_param(debug, int, 0644);
60 MODULE_PARM_DESC(debug, "Debug level (0-1)");
61
62 #define dprintk(num, format, args...) \
63         do { \
64                 if (debug >= num) \
65                         printk(format, ##args); \
66         } while (0)
67
68 /* ----------------------------------------------------------------------- */
69
70 #define SAA7111_NR_REG          0x18
71
72 struct saa7111 {
73         unsigned char reg[SAA7111_NR_REG];
74
75         int norm;
76         int input;
77         int enable;
78         int bright;
79         int contrast;
80         int hue;
81         int sat;
82 };
83
84 #define   I2C_SAA7111        0x48
85
86 /* ----------------------------------------------------------------------- */
87
88 static inline int
89 saa7111_write (struct i2c_client *client,
90                u8                 reg,
91                u8                 value)
92 {
93         struct saa7111 *decoder = i2c_get_clientdata(client);
94
95         decoder->reg[reg] = value;
96         return i2c_smbus_write_byte_data(client, reg, value);
97 }
98
99 static int
100 saa7111_write_block (struct i2c_client *client,
101                      const u8          *data,
102                      unsigned int       len)
103 {
104         int ret = -1;
105         u8 reg;
106
107         /* the saa7111 has an autoincrement function, use it if
108          * the adapter understands raw I2C */
109         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
110                 /* do raw I2C, not smbus compatible */
111                 struct saa7111 *decoder = i2c_get_clientdata(client);
112                 u8 block_data[32];
113                 int block_len;
114
115                 while (len >= 2) {
116                         block_len = 0;
117                         block_data[block_len++] = reg = data[0];
118                         do {
119                                 block_data[block_len++] =
120                                     decoder->reg[reg++] = data[1];
121                                 len -= 2;
122                                 data += 2;
123                         } while (len >= 2 && data[0] == reg &&
124                                  block_len < 32);
125                         if ((ret = i2c_master_send(client, block_data,
126                                                    block_len)) < 0)
127                                 break;
128                 }
129         } else {
130                 /* do some slow I2C emulation kind of thing */
131                 while (len >= 2) {
132                         reg = *data++;
133                         if ((ret = saa7111_write(client, reg,
134                                                  *data++)) < 0)
135                                 break;
136                         len -= 2;
137                 }
138         }
139
140         return ret;
141 }
142
143 static int
144 saa7111_init_decoder (struct i2c_client *client,
145               struct video_decoder_init *init)
146 {
147         return saa7111_write_block(client, init->data, init->len);
148 }
149
150 static inline int
151 saa7111_read (struct i2c_client *client,
152               u8                 reg)
153 {
154         return i2c_smbus_read_byte_data(client, reg);
155 }
156
157 /* ----------------------------------------------------------------------- */
158
159 static const unsigned char saa7111_i2c_init[] = {
160         0x00, 0x00,             /* 00 - ID byte */
161         0x01, 0x00,             /* 01 - reserved */
162
163         /*front end */
164         0x02, 0xd0,             /* 02 - FUSE=3, GUDL=2, MODE=0 */
165         0x03, 0x23,             /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
166                                  * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
167         0x04, 0x00,             /* 04 - GAI1=256 */
168         0x05, 0x00,             /* 05 - GAI2=256 */
169
170         /* decoder */
171         0x06, 0xf3,             /* 06 - HSB at  13(50Hz) /  17(60Hz)
172                                  * pixels after end of last line */
173         /*0x07, 0x13,     * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
174                                  * after end of last line */
175         0x07, 0xe8,             /* 07 - HSS seems to be needed to
176                                  * work with NTSC, too */
177         0x08, 0xc8,             /* 08 - AUFD=1, FSEL=1, EXFIL=0,
178                                  * VTRC=1, HPLL=0, VNOI=0 */
179         0x09, 0x01,             /* 09 - BYPS=0, PREF=0, BPSS=0,
180                                  * VBLB=0, UPTCV=0, APER=1 */
181         0x0a, 0x80,             /* 0a - BRIG=128 */
182         0x0b, 0x47,             /* 0b - CONT=1.109 */
183         0x0c, 0x40,             /* 0c - SATN=1.0 */
184         0x0d, 0x00,             /* 0d - HUE=0 */
185         0x0e, 0x01,             /* 0e - CDTO=0, CSTD=0, DCCF=0,
186                                  * FCTC=0, CHBW=1 */
187         0x0f, 0x00,             /* 0f - reserved */
188         0x10, 0x48,             /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
189         0x11, 0x1c,             /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
190                                  * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
191         0x12, 0x00,             /* 12 - output control 2 */
192         0x13, 0x00,             /* 13 - output control 3 */
193         0x14, 0x00,             /* 14 - reserved */
194         0x15, 0x00,             /* 15 - VBI */
195         0x16, 0x00,             /* 16 - VBI */
196         0x17, 0x00,             /* 17 - VBI */
197 };
198
199 static int
200 saa7111_command (struct i2c_client *client,
201                  unsigned int       cmd,
202                  void              *arg)
203 {
204         struct saa7111 *decoder = i2c_get_clientdata(client);
205
206         switch (cmd) {
207
208         case 0:
209                 break;
210         case DECODER_INIT:
211         {
212                 struct video_decoder_init *init = arg;
213                 if (NULL != init)
214                         return saa7111_init_decoder(client, init);
215                 else {
216                         struct video_decoder_init vdi;
217                         vdi.data = saa7111_i2c_init;
218                         vdi.len = sizeof(saa7111_i2c_init);
219                         return saa7111_init_decoder(client, &vdi);
220                 }
221         }
222
223         case DECODER_DUMP:
224         {
225                 int i;
226
227                 for (i = 0; i < SAA7111_NR_REG; i += 16) {
228                         int j;
229
230                         printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
231                         for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
232                                 printk(" %02x",
233                                        saa7111_read(client, i + j));
234                         }
235                         printk("\n");
236                 }
237         }
238                 break;
239
240         case DECODER_GET_CAPABILITIES:
241         {
242                 struct video_decoder_capability *cap = arg;
243
244                 cap->flags = VIDEO_DECODER_PAL |
245                              VIDEO_DECODER_NTSC |
246                              VIDEO_DECODER_SECAM |
247                              VIDEO_DECODER_AUTO |
248                              VIDEO_DECODER_CCIR;
249                 cap->inputs = 8;
250                 cap->outputs = 1;
251         }
252                 break;
253
254         case DECODER_GET_STATUS:
255         {
256                 int *iarg = arg;
257                 int status;
258                 int res;
259
260                 status = saa7111_read(client, 0x1f);
261                 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
262                         status);
263                 res = 0;
264                 if ((status & (1 << 6)) == 0) {
265                         res |= DECODER_STATUS_GOOD;
266                 }
267                 switch (decoder->norm) {
268                 case VIDEO_MODE_NTSC:
269                         res |= DECODER_STATUS_NTSC;
270                         break;
271                 case VIDEO_MODE_PAL:
272                         res |= DECODER_STATUS_PAL;
273                         break;
274                 case VIDEO_MODE_SECAM:
275                         res |= DECODER_STATUS_SECAM;
276                         break;
277                 default:
278                 case VIDEO_MODE_AUTO:
279                         if ((status & (1 << 5)) != 0) {
280                                 res |= DECODER_STATUS_NTSC;
281                         } else {
282                                 res |= DECODER_STATUS_PAL;
283                         }
284                         break;
285                 }
286                 if ((status & (1 << 0)) != 0) {
287                         res |= DECODER_STATUS_COLOR;
288                 }
289                 *iarg = res;
290         }
291                 break;
292
293         case DECODER_SET_GPIO:
294         {
295                 int *iarg = arg;
296                 if (0 != *iarg) {
297                         saa7111_write(client, 0x11,
298                                 (decoder->reg[0x11] | 0x80));
299                 } else {
300                         saa7111_write(client, 0x11,
301                                 (decoder->reg[0x11] & 0x7f));
302                 }
303                 break;
304         }
305
306         case DECODER_SET_VBI_BYPASS:
307         {
308                 int *iarg = arg;
309                 if (0 != *iarg) {
310                         saa7111_write(client, 0x13,
311                                 (decoder->reg[0x13] & 0xf0) | 0x0a);
312                 } else {
313                         saa7111_write(client, 0x13,
314                                 (decoder->reg[0x13] & 0xf0));
315                 }
316                 break;
317         }
318
319         case DECODER_SET_NORM:
320         {
321                 int *iarg = arg;
322
323                 switch (*iarg) {
324
325                 case VIDEO_MODE_NTSC:
326                         saa7111_write(client, 0x08,
327                                       (decoder->reg[0x08] & 0x3f) | 0x40);
328                         saa7111_write(client, 0x0e,
329                                       (decoder->reg[0x0e] & 0x8f));
330                         break;
331
332                 case VIDEO_MODE_PAL:
333                         saa7111_write(client, 0x08,
334                                       (decoder->reg[0x08] & 0x3f) | 0x00);
335                         saa7111_write(client, 0x0e,
336                                       (decoder->reg[0x0e] & 0x8f));
337                         break;
338
339                 case VIDEO_MODE_SECAM:
340                         saa7111_write(client, 0x08,
341                                       (decoder->reg[0x08] & 0x3f) | 0x00);
342                         saa7111_write(client, 0x0e,
343                                       (decoder->reg[0x0e] & 0x8f) | 0x50);
344                         break;
345
346                 case VIDEO_MODE_AUTO:
347                         saa7111_write(client, 0x08,
348                                       (decoder->reg[0x08] & 0x3f) | 0x80);
349                         saa7111_write(client, 0x0e,
350                                       (decoder->reg[0x0e] & 0x8f));
351                         break;
352
353                 default:
354                         return -EINVAL;
355
356                 }
357                 decoder->norm = *iarg;
358         }
359                 break;
360
361         case DECODER_SET_INPUT:
362         {
363                 int *iarg = arg;
364
365                 if (*iarg < 0 || *iarg > 7) {
366                         return -EINVAL;
367                 }
368
369                 if (decoder->input != *iarg) {
370                         decoder->input = *iarg;
371                         /* select mode */
372                         saa7111_write(client, 0x02,
373                                       (decoder->
374                                        reg[0x02] & 0xf8) | decoder->input);
375                         /* bypass chrominance trap for modes 4..7 */
376                         saa7111_write(client, 0x09,
377                                       (decoder->
378                                        reg[0x09] & 0x7f) | ((decoder->
379                                                              input >
380                                                              3) ? 0x80 :
381                                                             0));
382                 }
383         }
384                 break;
385
386         case DECODER_SET_OUTPUT:
387         {
388                 int *iarg = arg;
389
390                 /* not much choice of outputs */
391                 if (*iarg != 0) {
392                         return -EINVAL;
393                 }
394         }
395                 break;
396
397         case DECODER_ENABLE_OUTPUT:
398         {
399                 int *iarg = arg;
400                 int enable = (*iarg != 0);
401
402                 if (decoder->enable != enable) {
403                         decoder->enable = enable;
404
405                         /* RJ: If output should be disabled (for
406                          * playing videos), we also need a open PLL.
407                          * The input is set to 0 (where no input
408                          * source is connected), although this
409                          * is not necessary.
410                          *
411                          * If output should be enabled, we have to
412                          * reverse the above.
413                          */
414
415                         if (decoder->enable) {
416                                 saa7111_write(client, 0x02,
417                                               (decoder->
418                                                reg[0x02] & 0xf8) |
419                                               decoder->input);
420                                 saa7111_write(client, 0x08,
421                                               (decoder->reg[0x08] & 0xfb));
422                                 saa7111_write(client, 0x11,
423                                               (decoder->
424                                                reg[0x11] & 0xf3) | 0x0c);
425                         } else {
426                                 saa7111_write(client, 0x02,
427                                               (decoder->reg[0x02] & 0xf8));
428                                 saa7111_write(client, 0x08,
429                                               (decoder->
430                                                reg[0x08] & 0xfb) | 0x04);
431                                 saa7111_write(client, 0x11,
432                                               (decoder->reg[0x11] & 0xf3));
433                         }
434                 }
435         }
436                 break;
437
438         case DECODER_SET_PICTURE:
439         {
440                 struct video_picture *pic = arg;
441
442                 if (decoder->bright != pic->brightness) {
443                         /* We want 0 to 255 we get 0-65535 */
444                         decoder->bright = pic->brightness;
445                         saa7111_write(client, 0x0a, decoder->bright >> 8);
446                 }
447                 if (decoder->contrast != pic->contrast) {
448                         /* We want 0 to 127 we get 0-65535 */
449                         decoder->contrast = pic->contrast;
450                         saa7111_write(client, 0x0b,
451                                       decoder->contrast >> 9);
452                 }
453                 if (decoder->sat != pic->colour) {
454                         /* We want 0 to 127 we get 0-65535 */
455                         decoder->sat = pic->colour;
456                         saa7111_write(client, 0x0c, decoder->sat >> 9);
457                 }
458                 if (decoder->hue != pic->hue) {
459                         /* We want -128 to 127 we get 0-65535 */
460                         decoder->hue = pic->hue;
461                         saa7111_write(client, 0x0d,
462                                       (decoder->hue - 32768) >> 8);
463                 }
464         }
465                 break;
466
467         default:
468                 return -EINVAL;
469         }
470
471         return 0;
472 }
473
474 /* ----------------------------------------------------------------------- */
475
476 /*
477  * Generic i2c probe
478  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
479  */
480 static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
481
482 static unsigned short ignore = I2C_CLIENT_END;
483
484 static struct i2c_client_address_data addr_data = {
485         .normal_i2c             = normal_i2c,
486         .probe                  = &ignore,
487         .ignore                 = &ignore,
488 };
489
490 static struct i2c_driver i2c_driver_saa7111;
491
492 static int
493 saa7111_detect_client (struct i2c_adapter *adapter,
494                        int                 address,
495                        int                 kind)
496 {
497         int i;
498         struct i2c_client *client;
499         struct saa7111 *decoder;
500         struct video_decoder_init vdi;
501
502         dprintk(1,
503                 KERN_INFO
504                 "saa7111.c: detecting saa7111 client on address 0x%x\n",
505                 address << 1);
506
507         /* Check if the adapter supports the needed features */
508         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
509                 return 0;
510
511         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
512         if (client == 0)
513                 return -ENOMEM;
514         client->addr = address;
515         client->adapter = adapter;
516         client->driver = &i2c_driver_saa7111;
517         strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
518
519         decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
520         if (decoder == NULL) {
521                 kfree(client);
522                 return -ENOMEM;
523         }
524         decoder->norm = VIDEO_MODE_NTSC;
525         decoder->input = 0;
526         decoder->enable = 1;
527         decoder->bright = 32768;
528         decoder->contrast = 32768;
529         decoder->hue = 32768;
530         decoder->sat = 32768;
531         i2c_set_clientdata(client, decoder);
532
533         i = i2c_attach_client(client);
534         if (i) {
535                 kfree(client);
536                 kfree(decoder);
537                 return i;
538         }
539
540         vdi.data = saa7111_i2c_init;
541         vdi.len = sizeof(saa7111_i2c_init);
542         i = saa7111_init_decoder(client, &vdi);
543         if (i < 0) {
544                 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
545                         I2C_NAME(client), i);
546         } else {
547                 dprintk(1,
548                         KERN_INFO
549                         "%s_attach: chip version %x at address 0x%x\n",
550                         I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
551                         client->addr << 1);
552         }
553
554         return 0;
555 }
556
557 static int
558 saa7111_attach_adapter (struct i2c_adapter *adapter)
559 {
560         dprintk(1,
561                 KERN_INFO
562                 "saa7111.c: starting probe for adapter %s (0x%x)\n",
563                 I2C_NAME(adapter), adapter->id);
564         return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
565 }
566
567 static int
568 saa7111_detach_client (struct i2c_client *client)
569 {
570         struct saa7111 *decoder = i2c_get_clientdata(client);
571         int err;
572
573         err = i2c_detach_client(client);
574         if (err) {
575                 return err;
576         }
577
578         kfree(decoder);
579         kfree(client);
580
581         return 0;
582 }
583
584 /* ----------------------------------------------------------------------- */
585
586 static struct i2c_driver i2c_driver_saa7111 = {
587         .driver = {
588                 .name = "saa7111",
589         },
590
591         .id = I2C_DRIVERID_SAA7111A,
592
593         .attach_adapter = saa7111_attach_adapter,
594         .detach_client = saa7111_detach_client,
595         .command = saa7111_command,
596 };
597
598 static int __init
599 saa7111_init (void)
600 {
601         return i2c_add_driver(&i2c_driver_saa7111);
602 }
603
604 static void __exit
605 saa7111_exit (void)
606 {
607         i2c_del_driver(&i2c_driver_saa7111);
608 }
609
610 module_init(saa7111_init);
611 module_exit(saa7111_exit);