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