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