[PATCH] v4l: (966) Authorship fixes for new Modules
[linux-2.6] / drivers / media / video / saa711x.c
1 /*
2  * saa711x - Philips SAA711x video decoder driver version 0.0.1
3  *
4  * To do: Now, it handles only saa7113/7114. Should be improved to
5  * handle all Philips saa711x devices.
6  *
7  * Based on saa7113 driver from Dave Perks <dperks@ibm.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/fs.h>
29 #include <linux/kernel.h>
30 #include <linux/major.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/pci.h>
34 #include <linux/signal.h>
35 #include <asm/io.h>
36 #include <asm/pgtable.h>
37 #include <asm/page.h>
38 #include <linux/sched.h>
39 #include <asm/segment.h>
40 #include <linux/types.h>
41 #include <asm/uaccess.h>
42 #include <linux/videodev.h>
43
44 MODULE_DESCRIPTION("Philips SAA711x video decoder driver");
45 MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
46 MODULE_LICENSE("GPL");
47
48 #include <linux/i2c.h>
49 #include <linux/i2c-dev.h>
50
51 #define I2C_NAME(s) (s)->name
52
53 #include <linux/video_decoder.h>
54
55 static int debug = 0;
56 MODULE_PARM(debug, "i");
57 MODULE_PARM_DESC(debug, " Set the default Debug level.  Default: 0 (Off) - (0-1)");
58
59
60 #define dprintk(num, format, args...) \
61         do { \
62                 if (debug >= num) \
63                         printk(format , ##args); \
64         } while (0)
65
66 /* ----------------------------------------------------------------------- */
67
68 struct saa711x {
69         unsigned char reg[32];
70
71         int norm;
72         int input;
73         int enable;
74         int bright;
75         int contrast;
76         int hue;
77         int sat;
78 };
79
80 #define   I2C_SAA7113        0x4A
81 #define   I2C_SAA7114        0x42
82
83 /* ----------------------------------------------------------------------- */
84
85 static inline int
86 saa711x_write (struct i2c_client *client,
87                u8                 reg,
88                u8                 value)
89 {
90         struct saa711x *decoder = i2c_get_clientdata(client);
91
92         decoder->reg[reg] = value;
93         return i2c_smbus_write_byte_data(client, reg, value);
94 }
95
96 static int
97 saa711x_write_block (struct i2c_client *client,
98                      const u8          *data,
99                      unsigned int       len)
100 {
101         int ret = -1;
102         u8 reg;
103
104         /* the saa711x has an autoincrement function, use it if
105          * the adapter understands raw I2C */
106         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
107                 /* do raw I2C, not smbus compatible */
108                 struct saa711x *decoder = i2c_get_clientdata(client);
109                 struct i2c_msg msg;
110                 u8 block_data[32];
111
112                 msg.addr = client->addr;
113                 msg.flags = 0;
114                 while (len >= 2) {
115                         msg.buf = (char *) block_data;
116                         msg.len = 0;
117                         block_data[msg.len++] = reg = data[0];
118                         do {
119                                 block_data[msg.len++] =
120                                     decoder->reg[reg++] = data[1];
121                                 len -= 2;
122                                 data += 2;
123                         } while (len >= 2 && data[0] == reg &&
124                                  msg.len < 32);
125                         if ((ret = i2c_transfer(client->adapter,
126                                                 &msg, 1)) < 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 = saa711x_write(client, reg,
134                                                  *data++)) < 0)
135                                 break;
136                         len -= 2;
137                 }
138         }
139
140         return ret;
141 }
142
143 static int
144 saa711x_init_decoder (struct i2c_client *client,
145               struct video_decoder_init *init)
146 {
147         return saa711x_write_block(client, init->data, init->len);
148 }
149
150 static inline int
151 saa711x_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 saa711x_i2c_init[] = {
160         0x00, 0x00,     /* PH711x_CHIP_VERSION 00 - ID byte */
161         0x01, 0x08,     /* PH711x_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
162         0x02, 0xc0,     /* PH711x_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
163         0x03, 0x23,     /* PH711x_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
164         0x04, 0x00,     /* PH711x_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
165         0x05, 0x00,     /* PH711x_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
166         0x06, 0xeb,     /* PH711x_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
167         0x07, 0xe0,     /* PH711x_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
168         0x08, 0x88,     /* PH711x_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
169         0x09, 0x00,     /* PH711x_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
170         0x0a, 0x80,     /* PH711x_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
171         0x0b, 0x47,     /* PH711x_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
172         0x0c, 0x40,     /* PH711x_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
173         0x0d, 0x00,     /* PH711x_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
174         0x0e, 0x01,     /* PH711x_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
175         0x0f, 0xaa,     /* PH711x_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
176         0x10, 0x00,     /* PH711x_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
177         0x11, 0x1C,     /* PH711x_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
178         0x12, 0x01,     /* PH711x_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
179         0x13, 0x00,     /* PH711x_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
180         0x14, 0x00,     /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
181         0x15, 0x00,     /* PH711x_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
182         0x16, 0x00,     /* PH711x_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
183         0x17, 0x00,     /* PH711x_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
184 };
185
186 static int
187 saa711x_command (struct i2c_client *client,
188                  unsigned int       cmd,
189                  void              *arg)
190 {
191         struct saa711x *decoder = i2c_get_clientdata(client);
192
193         switch (cmd) {
194
195         case 0:
196         case DECODER_INIT:
197         {
198                 struct video_decoder_init *init = arg;
199                 if (NULL != init)
200                         return saa711x_init_decoder(client, init);
201                 else {
202                         struct video_decoder_init vdi;
203                         vdi.data = saa711x_i2c_init;
204                         vdi.len = sizeof(saa711x_i2c_init);
205                         return saa711x_init_decoder(client, &vdi);
206                 }
207         }
208
209         case DECODER_DUMP:
210         {
211                 int i;
212
213                 for (i = 0; i < 32; i += 16) {
214                         int j;
215
216                         printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
217                         for (j = 0; j < 16; ++j) {
218                                 printk(" %02x",
219                                        saa711x_read(client, i + j));
220                         }
221                         printk("\n");
222                 }
223         }
224                 break;
225
226         case DECODER_GET_CAPABILITIES:
227         {
228                 struct video_decoder_capability *cap = arg;
229
230                 cap->flags = VIDEO_DECODER_PAL |
231                              VIDEO_DECODER_NTSC |
232                              VIDEO_DECODER_SECAM |
233                              VIDEO_DECODER_AUTO |
234                              VIDEO_DECODER_CCIR;
235                 cap->inputs = 8;
236                 cap->outputs = 1;
237         }
238                 break;
239
240         case DECODER_GET_STATUS:
241         {
242                 int *iarg = arg;
243                 int status;
244                 int res;
245
246                 status = saa711x_read(client, 0x1f);
247                 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
248                         status);
249                 res = 0;
250                 if ((status & (1 << 6)) == 0) {
251                         res |= DECODER_STATUS_GOOD;
252                 }
253                 switch (decoder->norm) {
254                 case VIDEO_MODE_NTSC:
255                         res |= DECODER_STATUS_NTSC;
256                         break;
257                 case VIDEO_MODE_PAL:
258                         res |= DECODER_STATUS_PAL;
259                         break;
260                 case VIDEO_MODE_SECAM:
261                         res |= DECODER_STATUS_SECAM;
262                         break;
263                 default:
264                 case VIDEO_MODE_AUTO:
265                         if ((status & (1 << 5)) != 0) {
266                                 res |= DECODER_STATUS_NTSC;
267                         } else {
268                                 res |= DECODER_STATUS_PAL;
269                         }
270                         break;
271                 }
272                 if ((status & (1 << 0)) != 0) {
273                         res |= DECODER_STATUS_COLOR;
274                 }
275                 *iarg = res;
276         }
277                 break;
278
279         case DECODER_SET_GPIO:
280         {
281                 int *iarg = arg;
282                 if (0 != *iarg) {
283                         saa711x_write(client, 0x11,
284                                 (decoder->reg[0x11] | 0x80));
285                 } else {
286                         saa711x_write(client, 0x11,
287                                 (decoder->reg[0x11] & 0x7f));
288                 }
289                 break;
290         }
291
292         case DECODER_SET_VBI_BYPASS:
293         {
294                 int *iarg = arg;
295                 if (0 != *iarg) {
296                         saa711x_write(client, 0x13,
297                                 (decoder->reg[0x13] & 0xf0) | 0x0a);
298                 } else {
299                         saa711x_write(client, 0x13,
300                                 (decoder->reg[0x13] & 0xf0));
301                 }
302                 break;
303         }
304
305         case DECODER_SET_NORM:
306         {
307                 int *iarg = arg;
308
309                 switch (*iarg) {
310
311                 case VIDEO_MODE_NTSC:
312                         saa711x_write(client, 0x08,
313                                       (decoder->reg[0x08] & 0x3f) | 0x40);
314                         saa711x_write(client, 0x0e,
315                                       (decoder->reg[0x0e] & 0x8f));
316                         break;
317
318                 case VIDEO_MODE_PAL:
319                         saa711x_write(client, 0x08,
320                                       (decoder->reg[0x08] & 0x3f) | 0x00);
321                         saa711x_write(client, 0x0e,
322                                       (decoder->reg[0x0e] & 0x8f));
323                         break;
324
325                 case VIDEO_MODE_SECAM:
326                         saa711x_write(client, 0x08,
327                                       (decoder->reg[0x0e] & 0x3f) | 0x00);
328                         saa711x_write(client, 0x0e,
329                                       (decoder->reg[0x0e] & 0x8f) | 0x50);
330                         break;
331
332                 case VIDEO_MODE_AUTO:
333                         saa711x_write(client, 0x08,
334                                       (decoder->reg[0x08] & 0x3f) | 0x80);
335                         saa711x_write(client, 0x0e,
336                                       (decoder->reg[0x0e] & 0x8f));
337                         break;
338
339                 default:
340                         return -EINVAL;
341
342                 }
343                 decoder->norm = *iarg;
344         }
345                 break;
346
347         case DECODER_SET_INPUT:
348         {
349                 int *iarg = arg;
350                 if (*iarg < 0 || *iarg > 9) {
351                         return -EINVAL;
352                 }
353                 if (decoder->input != *iarg) {
354                         decoder->input = *iarg;
355                         /* select mode */
356                         saa711x_write(client, 0x02,
357                                       (decoder->reg[0x02] & 0xf0) | decoder->input);
358                         /* bypass chrominance trap for modes 4..7 */
359                         saa711x_write(client, 0x09,
360                                       (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
361                 }
362         }
363                 break;
364
365         case DECODER_SET_OUTPUT:
366         {
367                 int *iarg = arg;
368
369                 /* not much choice of outputs */
370                 if (*iarg != 0) {
371                         return -EINVAL;
372                 }
373         }
374                 break;
375
376         case DECODER_ENABLE_OUTPUT:
377         {
378                 int *iarg = arg;
379                 int enable = (*iarg != 0);
380
381                 if (decoder->enable != enable) {
382                         decoder->enable = enable;
383
384                         /* RJ: If output should be disabled (for
385                          * playing videos), we also need a open PLL.
386                          * The input is set to 0 (where no input
387                          * source is connected), although this
388                          * is not necessary.
389                          *
390                          * If output should be enabled, we have to
391                          * reverse the above.
392                          */
393
394                         if (decoder->enable) {
395                                 saa711x_write(client, 0x02,
396                                               (decoder->
397                                                reg[0x02] & 0xf8) |
398                                               decoder->input);
399                                 saa711x_write(client, 0x08,
400                                               (decoder->reg[0x08] & 0xfb));
401                                 saa711x_write(client, 0x11,
402                                               (decoder->
403                                                reg[0x11] & 0xf3) | 0x0c);
404                         } else {
405                                 saa711x_write(client, 0x02,
406                                               (decoder->reg[0x02] & 0xf8));
407                                 saa711x_write(client, 0x08,
408                                               (decoder->
409                                                reg[0x08] & 0xfb) | 0x04);
410                                 saa711x_write(client, 0x11,
411                                               (decoder->reg[0x11] & 0xf3));
412                         }
413                 }
414         }
415                 break;
416
417         case DECODER_SET_PICTURE:
418         {
419                 struct video_picture *pic = arg;
420
421                 if (decoder->bright != pic->brightness) {
422                         /* We want 0 to 255 we get 0-65535 */
423                         decoder->bright = pic->brightness;
424                         saa711x_write(client, 0x0a, decoder->bright >> 8);
425                 }
426                 if (decoder->contrast != pic->contrast) {
427                         /* We want 0 to 127 we get 0-65535 */
428                         decoder->contrast = pic->contrast;
429                         saa711x_write(client, 0x0b,
430                                       decoder->contrast >> 9);
431                 }
432                 if (decoder->sat != pic->colour) {
433                         /* We want 0 to 127 we get 0-65535 */
434                         decoder->sat = pic->colour;
435                         saa711x_write(client, 0x0c, decoder->sat >> 9);
436                 }
437                 if (decoder->hue != pic->hue) {
438                         /* We want -128 to 127 we get 0-65535 */
439                         decoder->hue = pic->hue;
440                         saa711x_write(client, 0x0d,
441                                       (decoder->hue - 32768) >> 8);
442                 }
443         }
444                 break;
445
446         default:
447                 return -EINVAL;
448         }
449
450         return 0;
451 }
452
453 /* ----------------------------------------------------------------------- */
454
455 /*
456  * Generic i2c probe
457  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
458  */
459
460 /* standard i2c insmod options */
461 static unsigned short normal_i2c[] = {
462         I2C_SAA7113>>1,         /* saa7113 */
463         I2C_SAA7114>>1,         /* saa7114 */
464         I2C_CLIENT_END
465 };
466
467 I2C_CLIENT_INSMOD;
468
469
470 static struct i2c_driver i2c_driver_saa711x;
471
472 static int
473 saa711x_detect_client (struct i2c_adapter *adapter,
474                        int                 address,
475                        int                 kind)
476 {
477         int i;
478         struct i2c_client *client;
479         struct saa711x *decoder;
480         struct video_decoder_init vdi;
481
482         dprintk(1,
483                 KERN_INFO
484                 "saa711x.c: detecting saa711x client on address 0x%x\n",
485                 address << 1);
486
487         /* Check if the adapter supports the needed features */
488         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
489                 return 0;
490
491         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
492         if (client == 0)
493                 return -ENOMEM;
494         memset(client, 0, sizeof(struct i2c_client));
495         client->addr = address;
496         client->adapter = adapter;
497         client->driver = &i2c_driver_saa711x;
498         client->flags = I2C_CLIENT_ALLOW_USE;
499         strlcpy(I2C_NAME(client), "saa711x", sizeof(I2C_NAME(client)));
500         decoder = kmalloc(sizeof(struct saa711x), GFP_KERNEL);
501         if (decoder == NULL) {
502                 kfree(client);
503                 return -ENOMEM;
504         }
505         memset(decoder, 0, sizeof(struct saa711x));
506         decoder->norm = VIDEO_MODE_NTSC;
507         decoder->input = 0;
508         decoder->enable = 1;
509         decoder->bright = 32768;
510         decoder->contrast = 32768;
511         decoder->hue = 32768;
512         decoder->sat = 32768;
513         i2c_set_clientdata(client, decoder);
514
515         i = i2c_attach_client(client);
516         if (i) {
517                 kfree(client);
518                 kfree(decoder);
519                 return i;
520         }
521
522         vdi.data = saa711x_i2c_init;
523         vdi.len = sizeof(saa711x_i2c_init);
524         i = saa711x_init_decoder(client, &vdi);
525         if (i < 0) {
526                 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
527                         I2C_NAME(client), i);
528         } else {
529                 dprintk(1,
530                         KERN_INFO
531                         "%s_attach: chip version %x at address 0x%x\n",
532                         I2C_NAME(client), saa711x_read(client, 0x00) >> 4,
533                         client->addr << 1);
534         }
535
536         return 0;
537 }
538
539 static int
540 saa711x_attach_adapter (struct i2c_adapter *adapter)
541 {
542         dprintk(1,
543                 KERN_INFO
544                 "saa711x.c: starting probe for adapter %s (0x%x)\n",
545                 I2C_NAME(adapter), adapter->id);
546         return i2c_probe(adapter, &addr_data, &saa711x_detect_client);
547 }
548
549 static int
550 saa711x_detach_client (struct i2c_client *client)
551 {
552         struct saa711x *decoder = i2c_get_clientdata(client);
553         int err;
554
555         err = i2c_detach_client(client);
556         if (err) {
557                 return err;
558         }
559
560         kfree(decoder);
561         kfree(client);
562
563         return 0;
564 }
565
566 /* ----------------------------------------------------------------------- */
567
568 static struct i2c_driver i2c_driver_saa711x = {
569         .owner = THIS_MODULE,
570         .name = "saa711x",
571
572         .id = I2C_DRIVERID_SAA711X,
573         .flags = I2C_DF_NOTIFY,
574
575         .attach_adapter = saa711x_attach_adapter,
576         .detach_client = saa711x_detach_client,
577         .command = saa711x_command,
578 };
579
580 static int __init
581 saa711x_init (void)
582 {
583         return i2c_add_driver(&i2c_driver_saa711x);
584 }
585
586 static void __exit
587 saa711x_exit (void)
588 {
589         i2c_del_driver(&i2c_driver_saa711x);
590 }
591
592 module_init(saa711x_init);
593 module_exit(saa711x_exit);