ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / saa7110.c
1 /*
2  * saa7110 - Philips SAA7110(A) video decoder driver
3  *
4  * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
5  *
6  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
7  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8  *    - some corrections for Pinnacle Systems Inc. DC10plus card.
9  *
10  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35
36 MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
37 MODULE_AUTHOR("Pauline Middelink");
38 MODULE_LICENSE("GPL");
39
40 #include <linux/i2c.h>
41 #include <linux/i2c-dev.h>
42
43 #define I2C_NAME(s) (s)->name
44
45 #include <linux/videodev.h>
46 #include <linux/video_decoder.h>
47
48 static int debug = 0;
49 MODULE_PARM(debug, "i");
50 MODULE_PARM_DESC(debug, "Debug level (0-1)");
51
52 #define dprintk(num, format, args...) \
53         do { \
54                 if (debug >= num) \
55                         printk(format, ##args); \
56         } while (0)
57
58 #define SAA7110_MAX_INPUT       9       /* 6 CVBS, 3 SVHS */
59 #define SAA7110_MAX_OUTPUT      0       /* its a decoder only */
60
61 #define I2C_SAA7110             0x9C    /* or 0x9E */
62
63 struct saa7110 {
64         unsigned char reg[54];
65
66         int norm;
67         int input;
68         int enable;
69         int bright;
70         int contrast;
71         int hue;
72         int sat;
73
74         wait_queue_head_t wq;
75 };
76
77 /* ----------------------------------------------------------------------- */
78 /* I2C support functions                                                   */
79 /* ----------------------------------------------------------------------- */
80
81 static int
82 saa7110_write (struct i2c_client *client,
83                u8                 reg,
84                u8                 value)
85 {
86         struct saa7110 *decoder = i2c_get_clientdata(client);
87
88         decoder->reg[reg] = value;
89         return i2c_smbus_write_byte_data(client, reg, value);
90 }
91
92 static int
93 saa7110_write_block (struct i2c_client *client,
94                      const u8          *data,
95                      unsigned int       len)
96 {
97         int ret = -1;
98         u8 reg = *data++;
99
100         len--;
101
102         /* the saa7110 has an autoincrement function, use it if
103          * the adapter understands raw I2C */
104         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
105                 struct saa7110 *decoder = i2c_get_clientdata(client);
106                 struct i2c_msg msg;
107                 u8 block_data[54];
108
109                 msg.len = 0;
110                 msg.buf = (char *) block_data;
111                 msg.addr = client->addr;
112                 msg.flags = client->flags;
113                 while (len >= 1) {
114                         msg.len = 0;
115                         block_data[msg.len++] = reg;
116                         while (len-- >= 1 && msg.len < 54)
117                                 block_data[msg.len++] =
118                                     decoder->reg[reg++] = *data++;
119                         ret = i2c_transfer(client->adapter, &msg, 1);
120                 }
121         } else {
122                 while (len-- >= 1) {
123                         if ((ret = saa7110_write(client, reg++,
124                                                  *data++)) < 0)
125                                 break;
126                 }
127         }
128
129         return ret;
130 }
131
132 static inline int
133 saa7110_read (struct i2c_client *client)
134 {
135         return i2c_smbus_read_byte(client);
136 }
137
138 /* ----------------------------------------------------------------------- */
139 /* SAA7110 functions                                                       */
140 /* ----------------------------------------------------------------------- */
141
142 #define FRESP_06H_COMPST 0x03   //0x13
143 #define FRESP_06H_SVIDEO 0x83   //0xC0
144
145
146 static int
147 saa7110_selmux (struct i2c_client *client,
148                 int                chan)
149 {
150         static const unsigned char modes[9][8] = {
151                 /* mode 0 */
152                 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
153                               0x44, 0x75, 0x16},
154                 /* mode 1 */
155                 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
156                               0x44, 0x75, 0x16},
157                 /* mode 2 */
158                 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
159                               0x60, 0xB5, 0x05},
160                 /* mode 3 */
161                 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
162                               0x60, 0xB5, 0x05},
163                 /* mode 4 */
164                 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
165                               0x60, 0xB5, 0x03},
166                 /* mode 5 */
167                 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
168                               0x60, 0xB5, 0x03},
169                 /* mode 6 */
170                 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
171                               0x44, 0x75, 0x12},
172                 /* mode 7 */
173                 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
174                               0x60, 0xB5, 0x14},
175                 /* mode 8 */
176                 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
177                               0x44, 0x75, 0x21}
178         };
179         struct saa7110 *decoder = i2c_get_clientdata(client);
180         const unsigned char *ptr = modes[chan];
181
182         saa7110_write(client, 0x06, ptr[0]);    /* Luminance control    */
183         saa7110_write(client, 0x20, ptr[1]);    /* Analog Control #1    */
184         saa7110_write(client, 0x21, ptr[2]);    /* Analog Control #2    */
185         saa7110_write(client, 0x22, ptr[3]);    /* Mixer Control #1     */
186         saa7110_write(client, 0x2C, ptr[4]);    /* Mixer Control #2     */
187         saa7110_write(client, 0x30, ptr[5]);    /* ADCs gain control    */
188         saa7110_write(client, 0x31, ptr[6]);    /* Mixer Control #3     */
189         saa7110_write(client, 0x21, ptr[7]);    /* Analog Control #2    */
190         decoder->input = chan;
191
192         return 0;
193 }
194
195 static const unsigned char initseq[] = {
196         0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
197         /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
198         /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
199         /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200         /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
201         /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
202         /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
203 };
204
205 static int
206 determine_norm (struct i2c_client *client)
207 {
208         struct saa7110 *decoder = i2c_get_clientdata(client);
209         int status;
210
211         /* mode changed, start automatic detection */
212         saa7110_write_block(client, initseq, sizeof(initseq));
213         saa7110_selmux(client, decoder->input);
214         sleep_on_timeout(&decoder->wq, HZ / 4);
215         status = saa7110_read(client);
216         if (status & 0x40) {
217                 dprintk(1, KERN_INFO "%s: status=0x%02x (no signal)\n",
218                         I2C_NAME(client), status);
219                 return decoder->norm;   // no change
220         }
221         if ((status & 3) == 0) {
222                 saa7110_write(client, 0x06, 0x83);
223                 if (status & 0x20) {
224                         dprintk(1,
225                                 KERN_INFO
226                                 "%s: status=0x%02x (NTSC/no color)\n",
227                                 I2C_NAME(client), status);
228                         //saa7110_write(client,0x2E,0x81);
229                         return VIDEO_MODE_NTSC;
230                 }
231                 dprintk(1, KERN_INFO "%s: status=0x%02x (PAL/no color)\n",
232                         I2C_NAME(client), status);
233                 //saa7110_write(client,0x2E,0x9A);
234                 return VIDEO_MODE_PAL;
235         }
236         //saa7110_write(client,0x06,0x03);
237         if (status & 0x20) {    /* 60Hz */
238                 dprintk(1, KERN_INFO "%s: status=0x%02x (NTSC)\n",
239                         I2C_NAME(client), status);
240                 saa7110_write(client, 0x0D, 0x86);
241                 saa7110_write(client, 0x0F, 0x50);
242                 saa7110_write(client, 0x11, 0x2C);
243                 //saa7110_write(client,0x2E,0x81);
244                 return VIDEO_MODE_NTSC;
245         }
246
247         /* 50Hz -> PAL/SECAM */
248         saa7110_write(client, 0x0D, 0x86);
249         saa7110_write(client, 0x0F, 0x10);
250         saa7110_write(client, 0x11, 0x59);
251         //saa7110_write(client,0x2E,0x9A);
252
253         sleep_on_timeout(&decoder->wq, HZ / 4);
254
255         status = saa7110_read(client);
256         if ((status & 0x03) == 0x01) {
257                 dprintk(1, KERN_INFO "%s: status=0x%02x (SECAM)\n",
258                         I2C_NAME(client), status);
259                 saa7110_write(client, 0x0D, 0x87);
260                 return VIDEO_MODE_SECAM;
261         }
262         dprintk(1, KERN_INFO "%s: status=0x%02x (PAL)\n", I2C_NAME(client),
263                 status);
264         return VIDEO_MODE_PAL;
265 }
266
267 static int
268 saa7110_command (struct i2c_client *client,
269                  unsigned int       cmd,
270                  void              *arg)
271 {
272         struct saa7110 *decoder = i2c_get_clientdata(client);
273         int v;
274
275         switch (cmd) {
276         case 0:
277                 //saa7110_write_block(client, initseq, sizeof(initseq));
278                 break;
279
280         case DECODER_GET_CAPABILITIES:
281         {
282                 struct video_decoder_capability *dc = arg;
283
284                 dc->flags =
285                     VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
286                     VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
287                 dc->inputs = SAA7110_MAX_INPUT;
288                 dc->outputs = SAA7110_MAX_OUTPUT;
289         }
290                 break;
291
292         case DECODER_GET_STATUS:
293         {
294                 int status;
295                 int res = 0;
296
297                 status = saa7110_read(client);
298                 dprintk(1, KERN_INFO "%s: status=0x%02x norm=%d\n",
299                         I2C_NAME(client), status, decoder->norm);
300                 if (!(status & 0x40))
301                         res |= DECODER_STATUS_GOOD;
302                 if (status & 0x03)
303                         res |= DECODER_STATUS_COLOR;
304
305                 switch (decoder->norm) {
306                 case VIDEO_MODE_NTSC:
307                         res |= DECODER_STATUS_NTSC;
308                         break;
309                 case VIDEO_MODE_PAL:
310                         res |= DECODER_STATUS_PAL;
311                         break;
312                 case VIDEO_MODE_SECAM:
313                         res |= DECODER_STATUS_SECAM;
314                         break;
315                 }
316                 *(int *) arg = res;
317         }
318                 break;
319
320         case DECODER_SET_NORM:
321                 v = *(int *) arg;
322                 if (decoder->norm != v) {
323                         decoder->norm = v;
324                         //saa7110_write(client, 0x06, 0x03);
325                         switch (v) {
326                         case VIDEO_MODE_NTSC:
327                                 saa7110_write(client, 0x0D, 0x86);
328                                 saa7110_write(client, 0x0F, 0x50);
329                                 saa7110_write(client, 0x11, 0x2C);
330                                 //saa7110_write(client, 0x2E, 0x81);
331                                 dprintk(1,
332                                         KERN_INFO "%s: switched to NTSC\n",
333                                         I2C_NAME(client));
334                                 break;
335                         case VIDEO_MODE_PAL:
336                                 saa7110_write(client, 0x0D, 0x86);
337                                 saa7110_write(client, 0x0F, 0x10);
338                                 saa7110_write(client, 0x11, 0x59);
339                                 //saa7110_write(client, 0x2E, 0x9A);
340                                 dprintk(1,
341                                         KERN_INFO "%s: switched to PAL\n",
342                                         I2C_NAME(client));
343                                 break;
344                         case VIDEO_MODE_SECAM:
345                                 saa7110_write(client, 0x0D, 0x87);
346                                 saa7110_write(client, 0x0F, 0x10);
347                                 saa7110_write(client, 0x11, 0x59);
348                                 //saa7110_write(client, 0x2E, 0x9A);
349                                 dprintk(1,
350                                         KERN_INFO
351                                         "%s: switched to SECAM\n",
352                                         I2C_NAME(client));
353                                 break;
354                         case VIDEO_MODE_AUTO:
355                                 dprintk(1,
356                                         KERN_INFO
357                                         "%s: TV standard detection...\n",
358                                         I2C_NAME(client));
359                                 decoder->norm = determine_norm(client);
360                                 *(int *) arg = decoder->norm;
361                                 break;
362                         default:
363                                 return -EPERM;
364                         }
365                 }
366                 break;
367
368         case DECODER_SET_INPUT:
369                 v = *(int *) arg;
370                 if (v < 0 || v > SAA7110_MAX_INPUT) {
371                         dprintk(1,
372                                 KERN_INFO "%s: input=%d not available\n",
373                                 I2C_NAME(client), v);
374                         return -EINVAL;
375                 }
376                 if (decoder->input != v) {
377                         saa7110_selmux(client, v);
378                         dprintk(1, KERN_INFO "%s: switched to input=%d\n",
379                                 I2C_NAME(client), v);
380                 }
381                 break;
382
383         case DECODER_SET_OUTPUT:
384                 v = *(int *) arg;
385                 /* not much choice of outputs */
386                 if (v != 0)
387                         return -EINVAL;
388                 break;
389
390         case DECODER_ENABLE_OUTPUT:
391                 v = *(int *) arg;
392                 if (decoder->enable != v) {
393                         decoder->enable = v;
394                         saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
395                         dprintk(1, KERN_INFO "%s: YUV %s\n", I2C_NAME(client),
396                                 v ? "on" : "off");
397                 }
398                 break;
399
400         case DECODER_SET_PICTURE:
401         {
402                 struct video_picture *pic = arg;
403
404                 if (decoder->bright != pic->brightness) {
405                         /* We want 0 to 255 we get 0-65535 */
406                         decoder->bright = pic->brightness;
407                         saa7110_write(client, 0x19, decoder->bright >> 8);
408                 }
409                 if (decoder->contrast != pic->contrast) {
410                         /* We want 0 to 127 we get 0-65535 */
411                         decoder->contrast = pic->contrast;
412                         saa7110_write(client, 0x13,
413                                       decoder->contrast >> 9);
414                 }
415                 if (decoder->sat != pic->colour) {
416                         /* We want 0 to 127 we get 0-65535 */
417                         decoder->sat = pic->colour;
418                         saa7110_write(client, 0x12, decoder->sat >> 9);
419                 }
420                 if (decoder->hue != pic->hue) {
421                         /* We want -128 to 127 we get 0-65535 */
422                         decoder->hue = pic->hue;
423                         saa7110_write(client, 0x07,
424                                       (decoder->hue >> 8) - 128);
425                 }
426         }
427                 break;
428
429         case DECODER_DUMP:
430                 for (v = 0; v < 0x34; v += 16) {
431                         int j;
432                         dprintk(1, KERN_INFO "%s: %03x\n", I2C_NAME(client),
433                                 v);
434                         for (j = 0; j < 16; j++) {
435                                 dprintk(1, KERN_INFO " %02x",
436                                         decoder->reg[v + j]);
437                         }
438                         dprintk(1, KERN_INFO "\n");
439                 }
440                 break;
441
442         default:
443                 dprintk(1, KERN_INFO "unknown saa7110_command??(%d)\n",
444                         cmd);
445                 return -EINVAL;
446         }
447         return 0;
448 }
449
450 /* ----------------------------------------------------------------------- */
451
452 /*
453  * Generic i2c probe
454  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
455  */
456 static unsigned short normal_i2c[] = {
457         I2C_SAA7110 >> 1,
458         (I2C_SAA7110 >> 1) + 1,
459         I2C_CLIENT_END
460 };
461 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
462
463 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
464 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
465 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
466 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
467 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
468                                                                                 
469 static struct i2c_client_address_data addr_data = {
470         .normal_i2c             = normal_i2c,
471         .normal_i2c_range       = normal_i2c_range,
472         .probe                  = probe,
473         .probe_range            = probe_range,
474         .ignore                 = ignore,
475         .ignore_range           = ignore_range,
476         .force                  = force
477 };
478
479 static int saa7110_i2c_id = 0;
480 static struct i2c_driver i2c_driver_saa7110;
481
482 static int
483 saa7110_detect_client (struct i2c_adapter *adapter,
484                        int                 address,
485                        int                 kind)
486 {
487         struct i2c_client *client;
488         struct saa7110 *decoder;
489         int rv;
490
491         dprintk(1,
492                 KERN_INFO
493                 "saa7110.c: detecting saa7110 client on address 0x%x\n",
494                 address << 1);
495
496         /* Check if the adapter supports the needed features */
497         if (!i2c_check_functionality
498             (adapter,
499              I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
500                 return 0;
501
502         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
503         if (client == 0)
504                 return -ENOMEM;
505         memset(client, 0, sizeof(struct i2c_client));
506         client->addr = address;
507         client->adapter = adapter;
508         client->driver = &i2c_driver_saa7110;
509         client->flags = I2C_CLIENT_ALLOW_USE;
510         client->id = saa7110_i2c_id++;
511         snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
512                 "saa7110[%d]", client->id);
513
514         decoder = kmalloc(sizeof(struct saa7110), GFP_KERNEL);
515         if (decoder == 0) {
516                 kfree(client);
517                 return -ENOMEM;
518         }
519         memset(decoder, 0, sizeof(struct saa7110));
520         decoder->norm = VIDEO_MODE_PAL;
521         decoder->input = 0;
522         decoder->enable = 1;
523         decoder->bright = 32768;
524         decoder->contrast = 32768;
525         decoder->hue = 32768;
526         decoder->sat = 32768;
527         init_waitqueue_head(&decoder->wq);
528         i2c_set_clientdata(client, decoder);
529
530         rv = i2c_attach_client(client);
531         if (rv) {
532                 kfree(client);
533                 kfree(decoder);
534                 return rv;
535         }
536
537         rv = saa7110_write_block(client, initseq, sizeof(initseq));
538         if (rv < 0)
539                 dprintk(1, KERN_ERR "%s_attach: init status %d\n",
540                         I2C_NAME(client), rv);
541         else {
542                 int ver, status;
543                 saa7110_write(client, 0x21, 0x10);
544                 saa7110_write(client, 0x0e, 0x18);
545                 saa7110_write(client, 0x0D, 0x04);
546                 ver = saa7110_read(client);
547                 saa7110_write(client, 0x0D, 0x06);
548                 //mdelay(150);
549                 status = saa7110_read(client);
550                 dprintk(1,
551                         KERN_INFO
552                         "%s_attach: SAA7110A version %x at 0x%02x, status=0x%02x\n",
553                         I2C_NAME(client), ver, client->addr << 1, status);
554                 saa7110_write(client, 0x0D, 0x86);
555                 saa7110_write(client, 0x0F, 0x10);
556                 saa7110_write(client, 0x11, 0x59);
557                 //saa7110_write(client, 0x2E, 0x9A);
558         }
559
560         //saa7110_selmux(client,0);
561         //determine_norm(client);
562         /* setup and implicit mode 0 select has been performed */
563
564         return 0;
565 }
566
567 static int
568 saa7110_attach_adapter (struct i2c_adapter *adapter)
569 {
570         dprintk(1,
571                 KERN_INFO
572                 "saa7110.c: starting probe for adapter %s (0x%x)\n",
573                 I2C_NAME(adapter), adapter->id);
574         return i2c_probe(adapter, &addr_data, &saa7110_detect_client);
575 }
576
577 static int
578 saa7110_detach_client (struct i2c_client *client)
579 {
580         struct saa7110 *decoder = i2c_get_clientdata(client);
581         int err;
582
583         err = i2c_detach_client(client);
584         if (err) {
585                 return err;
586         }
587
588         kfree(decoder);
589         kfree(client);
590
591         return 0;
592 }
593
594 /* ----------------------------------------------------------------------- */
595
596 static struct i2c_driver i2c_driver_saa7110 = {
597         .owner = THIS_MODULE,
598         .name = "saa7110",
599
600         .id = I2C_DRIVERID_SAA7110,
601         .flags = I2C_DF_NOTIFY,
602
603         .attach_adapter = saa7110_attach_adapter,
604         .detach_client = saa7110_detach_client,
605         .command = saa7110_command,
606 };
607
608 static int __init
609 saa7110_init (void)
610 {
611         return i2c_add_driver(&i2c_driver_saa7110);
612 }
613
614 static void __exit
615 saa7110_exit (void)
616 {
617         i2c_del_driver(&i2c_driver_saa7110);
618 }
619
620 module_init(saa7110_init);
621 module_exit(saa7110_exit);