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