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