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