]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/saa7110.c
dff06895171bd7b830a2f24f0195c40b815e7d05
[linux-2.6-omap-h63xx.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 <linux/wait.h>
34 #include <asm/io.h>
35 #include <asm/uaccess.h>
36
37 MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
38 MODULE_AUTHOR("Pauline Middelink");
39 MODULE_LICENSE("GPL");
40
41 #include <linux/i2c.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_param(debug, int, 0);
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 #define SAA7110_NR_REG          0x35
64
65 struct saa7110 {
66         u8 reg[SAA7110_NR_REG];
67
68         int norm;
69         int input;
70         int enable;
71         int bright;
72         int contrast;
73         int hue;
74         int sat;
75
76         wait_queue_head_t wq;
77 };
78
79 /* ----------------------------------------------------------------------- */
80 /* I2C support functions                                                   */
81 /* ----------------------------------------------------------------------- */
82
83 static int
84 saa7110_write (struct i2c_client *client,
85                u8                 reg,
86                u8                 value)
87 {
88         struct saa7110 *decoder = i2c_get_clientdata(client);
89
90         decoder->reg[reg] = value;
91         return i2c_smbus_write_byte_data(client, reg, value);
92 }
93
94 static int
95 saa7110_write_block (struct i2c_client *client,
96                      const u8          *data,
97                      unsigned int       len)
98 {
99         int ret = -1;
100         u8 reg = *data;         /* first register to write to */
101
102         /* Sanity check */
103         if (reg + (len - 1) > SAA7110_NR_REG)
104                 return ret;
105
106         /* the saa7110 has an autoincrement function, use it if
107          * the adapter understands raw I2C */
108         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
109                 struct saa7110 *decoder = i2c_get_clientdata(client);
110                 struct i2c_msg msg;
111
112                 msg.len = len;
113                 msg.buf = (char *) data;
114                 msg.addr = client->addr;
115                 msg.flags = 0;
116                 ret = i2c_transfer(client->adapter, &msg, 1);
117
118                 /* Cache the written data */
119                 memcpy(decoder->reg + reg, data + 1, len - 1);
120         } else {
121                 for (++data, --len; len; len--) {
122                         if ((ret = saa7110_write(client, reg++,
123                                                  *data++)) < 0)
124                                 break;
125                 }
126         }
127
128         return ret;
129 }
130
131 static inline int
132 saa7110_read (struct i2c_client *client)
133 {
134         return i2c_smbus_read_byte(client);
135 }
136
137 /* ----------------------------------------------------------------------- */
138 /* SAA7110 functions                                                       */
139 /* ----------------------------------------------------------------------- */
140
141 #define FRESP_06H_COMPST 0x03   //0x13
142 #define FRESP_06H_SVIDEO 0x83   //0xC0
143
144
145 static int
146 saa7110_selmux (struct i2c_client *client,
147                 int                chan)
148 {
149         static const unsigned char modes[9][8] = {
150                 /* mode 0 */
151                 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
152                               0x44, 0x75, 0x16},
153                 /* mode 1 */
154                 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
155                               0x44, 0x75, 0x16},
156                 /* mode 2 */
157                 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
158                               0x60, 0xB5, 0x05},
159                 /* mode 3 */
160                 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
161                               0x60, 0xB5, 0x05},
162                 /* mode 4 */
163                 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
164                               0x60, 0xB5, 0x03},
165                 /* mode 5 */
166                 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
167                               0x60, 0xB5, 0x03},
168                 /* mode 6 */
169                 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
170                               0x44, 0x75, 0x12},
171                 /* mode 7 */
172                 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
173                               0x60, 0xB5, 0x14},
174                 /* mode 8 */
175                 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
176                               0x44, 0x75, 0x21}
177         };
178         struct saa7110 *decoder = i2c_get_clientdata(client);
179         const unsigned char *ptr = modes[chan];
180
181         saa7110_write(client, 0x06, ptr[0]);    /* Luminance control    */
182         saa7110_write(client, 0x20, ptr[1]);    /* Analog Control #1    */
183         saa7110_write(client, 0x21, ptr[2]);    /* Analog Control #2    */
184         saa7110_write(client, 0x22, ptr[3]);    /* Mixer Control #1     */
185         saa7110_write(client, 0x2C, ptr[4]);    /* Mixer Control #2     */
186         saa7110_write(client, 0x30, ptr[5]);    /* ADCs gain control    */
187         saa7110_write(client, 0x31, ptr[6]);    /* Mixer Control #3     */
188         saa7110_write(client, 0x21, ptr[7]);    /* Analog Control #2    */
189         decoder->input = chan;
190
191         return 0;
192 }
193
194 static const unsigned char initseq[1 + SAA7110_NR_REG] = {
195         0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
196         /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
197         /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
198         /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199         /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
200         /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
201         /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
202 };
203
204 static int
205 determine_norm (struct i2c_client *client)
206 {
207         DEFINE_WAIT(wait);
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         prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
215         schedule_timeout(HZ/4);
216         finish_wait(&decoder->wq, &wait);
217         status = saa7110_read(client);
218         if (status & 0x40) {
219                 dprintk(1, KERN_INFO "%s: status=0x%02x (no signal)\n",
220                         I2C_NAME(client), status);
221                 return decoder->norm;   // no change
222         }
223         if ((status & 3) == 0) {
224                 saa7110_write(client, 0x06, 0x83);
225                 if (status & 0x20) {
226                         dprintk(1,
227                                 KERN_INFO
228                                 "%s: status=0x%02x (NTSC/no color)\n",
229                                 I2C_NAME(client), status);
230                         //saa7110_write(client,0x2E,0x81);
231                         return VIDEO_MODE_NTSC;
232                 }
233                 dprintk(1, KERN_INFO "%s: status=0x%02x (PAL/no color)\n",
234                         I2C_NAME(client), status);
235                 //saa7110_write(client,0x2E,0x9A);
236                 return VIDEO_MODE_PAL;
237         }
238         //saa7110_write(client,0x06,0x03);
239         if (status & 0x20) {    /* 60Hz */
240                 dprintk(1, KERN_INFO "%s: status=0x%02x (NTSC)\n",
241                         I2C_NAME(client), status);
242                 saa7110_write(client, 0x0D, 0x86);
243                 saa7110_write(client, 0x0F, 0x50);
244                 saa7110_write(client, 0x11, 0x2C);
245                 //saa7110_write(client,0x2E,0x81);
246                 return VIDEO_MODE_NTSC;
247         }
248
249         /* 50Hz -> PAL/SECAM */
250         saa7110_write(client, 0x0D, 0x86);
251         saa7110_write(client, 0x0F, 0x10);
252         saa7110_write(client, 0x11, 0x59);
253         //saa7110_write(client,0x2E,0x9A);
254
255         prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
256         schedule_timeout(HZ/4);
257         finish_wait(&decoder->wq, &wait);
258
259         status = saa7110_read(client);
260         if ((status & 0x03) == 0x01) {
261                 dprintk(1, KERN_INFO "%s: status=0x%02x (SECAM)\n",
262                         I2C_NAME(client), status);
263                 saa7110_write(client, 0x0D, 0x87);
264                 return VIDEO_MODE_SECAM;
265         }
266         dprintk(1, KERN_INFO "%s: status=0x%02x (PAL)\n", I2C_NAME(client),
267                 status);
268         return VIDEO_MODE_PAL;
269 }
270
271 static int
272 saa7110_command (struct i2c_client *client,
273                  unsigned int       cmd,
274                  void              *arg)
275 {
276         struct saa7110 *decoder = i2c_get_clientdata(client);
277         int v;
278
279         switch (cmd) {
280         case 0:
281                 //saa7110_write_block(client, initseq, sizeof(initseq));
282                 break;
283
284         case DECODER_GET_CAPABILITIES:
285         {
286                 struct video_decoder_capability *dc = arg;
287
288                 dc->flags =
289                     VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
290                     VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
291                 dc->inputs = SAA7110_MAX_INPUT;
292                 dc->outputs = SAA7110_MAX_OUTPUT;
293         }
294                 break;
295
296         case DECODER_GET_STATUS:
297         {
298                 int status;
299                 int res = 0;
300
301                 status = saa7110_read(client);
302                 dprintk(1, KERN_INFO "%s: status=0x%02x norm=%d\n",
303                         I2C_NAME(client), status, decoder->norm);
304                 if (!(status & 0x40))
305                         res |= DECODER_STATUS_GOOD;
306                 if (status & 0x03)
307                         res |= DECODER_STATUS_COLOR;
308
309                 switch (decoder->norm) {
310                 case VIDEO_MODE_NTSC:
311                         res |= DECODER_STATUS_NTSC;
312                         break;
313                 case VIDEO_MODE_PAL:
314                         res |= DECODER_STATUS_PAL;
315                         break;
316                 case VIDEO_MODE_SECAM:
317                         res |= DECODER_STATUS_SECAM;
318                         break;
319                 }
320                 *(int *) arg = res;
321         }
322                 break;
323
324         case DECODER_SET_NORM:
325                 v = *(int *) arg;
326                 if (decoder->norm != v) {
327                         decoder->norm = v;
328                         //saa7110_write(client, 0x06, 0x03);
329                         switch (v) {
330                         case VIDEO_MODE_NTSC:
331                                 saa7110_write(client, 0x0D, 0x86);
332                                 saa7110_write(client, 0x0F, 0x50);
333                                 saa7110_write(client, 0x11, 0x2C);
334                                 //saa7110_write(client, 0x2E, 0x81);
335                                 dprintk(1,
336                                         KERN_INFO "%s: switched to NTSC\n",
337                                         I2C_NAME(client));
338                                 break;
339                         case VIDEO_MODE_PAL:
340                                 saa7110_write(client, 0x0D, 0x86);
341                                 saa7110_write(client, 0x0F, 0x10);
342                                 saa7110_write(client, 0x11, 0x59);
343                                 //saa7110_write(client, 0x2E, 0x9A);
344                                 dprintk(1,
345                                         KERN_INFO "%s: switched to PAL\n",
346                                         I2C_NAME(client));
347                                 break;
348                         case VIDEO_MODE_SECAM:
349                                 saa7110_write(client, 0x0D, 0x87);
350                                 saa7110_write(client, 0x0F, 0x10);
351                                 saa7110_write(client, 0x11, 0x59);
352                                 //saa7110_write(client, 0x2E, 0x9A);
353                                 dprintk(1,
354                                         KERN_INFO
355                                         "%s: switched to SECAM\n",
356                                         I2C_NAME(client));
357                                 break;
358                         case VIDEO_MODE_AUTO:
359                                 dprintk(1,
360                                         KERN_INFO
361                                         "%s: TV standard detection...\n",
362                                         I2C_NAME(client));
363                                 decoder->norm = determine_norm(client);
364                                 *(int *) arg = decoder->norm;
365                                 break;
366                         default:
367                                 return -EPERM;
368                         }
369                 }
370                 break;
371
372         case DECODER_SET_INPUT:
373                 v = *(int *) arg;
374                 if (v < 0 || v > SAA7110_MAX_INPUT) {
375                         dprintk(1,
376                                 KERN_INFO "%s: input=%d not available\n",
377                                 I2C_NAME(client), v);
378                         return -EINVAL;
379                 }
380                 if (decoder->input != v) {
381                         saa7110_selmux(client, v);
382                         dprintk(1, KERN_INFO "%s: switched to input=%d\n",
383                                 I2C_NAME(client), v);
384                 }
385                 break;
386
387         case DECODER_SET_OUTPUT:
388                 v = *(int *) arg;
389                 /* not much choice of outputs */
390                 if (v != 0)
391                         return -EINVAL;
392                 break;
393
394         case DECODER_ENABLE_OUTPUT:
395                 v = *(int *) arg;
396                 if (decoder->enable != v) {
397                         decoder->enable = v;
398                         saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
399                         dprintk(1, KERN_INFO "%s: YUV %s\n", I2C_NAME(client),
400                                 v ? "on" : "off");
401                 }
402                 break;
403
404         case DECODER_SET_PICTURE:
405         {
406                 struct video_picture *pic = arg;
407
408                 if (decoder->bright != pic->brightness) {
409                         /* We want 0 to 255 we get 0-65535 */
410                         decoder->bright = pic->brightness;
411                         saa7110_write(client, 0x19, decoder->bright >> 8);
412                 }
413                 if (decoder->contrast != pic->contrast) {
414                         /* We want 0 to 127 we get 0-65535 */
415                         decoder->contrast = pic->contrast;
416                         saa7110_write(client, 0x13,
417                                       decoder->contrast >> 9);
418                 }
419                 if (decoder->sat != pic->colour) {
420                         /* We want 0 to 127 we get 0-65535 */
421                         decoder->sat = pic->colour;
422                         saa7110_write(client, 0x12, decoder->sat >> 9);
423                 }
424                 if (decoder->hue != pic->hue) {
425                         /* We want -128 to 127 we get 0-65535 */
426                         decoder->hue = pic->hue;
427                         saa7110_write(client, 0x07,
428                                       (decoder->hue >> 8) - 128);
429                 }
430         }
431                 break;
432
433         case DECODER_DUMP:
434                 for (v = 0; v < 0x34; v += 16) {
435                         int j;
436                         dprintk(1, KERN_INFO "%s: %03x\n", I2C_NAME(client),
437                                 v);
438                         for (j = 0; j < 16; j++) {
439                                 dprintk(1, KERN_INFO " %02x",
440                                         decoder->reg[v + j]);
441                         }
442                         dprintk(1, KERN_INFO "\n");
443                 }
444                 break;
445
446         default:
447                 dprintk(1, KERN_INFO "unknown saa7110_command??(%d)\n",
448                         cmd);
449                 return -EINVAL;
450         }
451         return 0;
452 }
453
454 /* ----------------------------------------------------------------------- */
455
456 /*
457  * Generic i2c probe
458  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
459  */
460 static unsigned short normal_i2c[] = {
461         I2C_SAA7110 >> 1,
462         (I2C_SAA7110 >> 1) + 1,
463         I2C_CLIENT_END
464 };
465
466 static unsigned short ignore = I2C_CLIENT_END;
467                                                                                 
468 static struct i2c_client_address_data addr_data = {
469         .normal_i2c             = normal_i2c,
470         .probe                  = &ignore,
471         .ignore                 = &ignore,
472 };
473
474 static struct i2c_driver i2c_driver_saa7110;
475
476 static int
477 saa7110_detect_client (struct i2c_adapter *adapter,
478                        int                 address,
479                        int                 kind)
480 {
481         struct i2c_client *client;
482         struct saa7110 *decoder;
483         int rv;
484
485         dprintk(1,
486                 KERN_INFO
487                 "saa7110.c: detecting saa7110 client on address 0x%x\n",
488                 address << 1);
489
490         /* Check if the adapter supports the needed features */
491         if (!i2c_check_functionality
492             (adapter,
493              I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
494                 return 0;
495
496         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
497         if (client == 0)
498                 return -ENOMEM;
499         client->addr = address;
500         client->adapter = adapter;
501         client->driver = &i2c_driver_saa7110;
502         strlcpy(I2C_NAME(client), "saa7110", sizeof(I2C_NAME(client)));
503
504         decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
505         if (decoder == 0) {
506                 kfree(client);
507                 return -ENOMEM;
508         }
509         decoder->norm = VIDEO_MODE_PAL;
510         decoder->input = 0;
511         decoder->enable = 1;
512         decoder->bright = 32768;
513         decoder->contrast = 32768;
514         decoder->hue = 32768;
515         decoder->sat = 32768;
516         init_waitqueue_head(&decoder->wq);
517         i2c_set_clientdata(client, decoder);
518
519         rv = i2c_attach_client(client);
520         if (rv) {
521                 kfree(client);
522                 kfree(decoder);
523                 return rv;
524         }
525
526         rv = saa7110_write_block(client, initseq, sizeof(initseq));
527         if (rv < 0)
528                 dprintk(1, KERN_ERR "%s_attach: init status %d\n",
529                         I2C_NAME(client), rv);
530         else {
531                 int ver, status;
532                 saa7110_write(client, 0x21, 0x10);
533                 saa7110_write(client, 0x0e, 0x18);
534                 saa7110_write(client, 0x0D, 0x04);
535                 ver = saa7110_read(client);
536                 saa7110_write(client, 0x0D, 0x06);
537                 //mdelay(150);
538                 status = saa7110_read(client);
539                 dprintk(1,
540                         KERN_INFO
541                         "%s_attach: SAA7110A version %x at 0x%02x, status=0x%02x\n",
542                         I2C_NAME(client), ver, client->addr << 1, status);
543                 saa7110_write(client, 0x0D, 0x86);
544                 saa7110_write(client, 0x0F, 0x10);
545                 saa7110_write(client, 0x11, 0x59);
546                 //saa7110_write(client, 0x2E, 0x9A);
547         }
548
549         //saa7110_selmux(client,0);
550         //determine_norm(client);
551         /* setup and implicit mode 0 select has been performed */
552
553         return 0;
554 }
555
556 static int
557 saa7110_attach_adapter (struct i2c_adapter *adapter)
558 {
559         dprintk(1,
560                 KERN_INFO
561                 "saa7110.c: starting probe for adapter %s (0x%x)\n",
562                 I2C_NAME(adapter), adapter->id);
563         return i2c_probe(adapter, &addr_data, &saa7110_detect_client);
564 }
565
566 static int
567 saa7110_detach_client (struct i2c_client *client)
568 {
569         struct saa7110 *decoder = i2c_get_clientdata(client);
570         int err;
571
572         err = i2c_detach_client(client);
573         if (err) {
574                 return err;
575         }
576
577         kfree(decoder);
578         kfree(client);
579
580         return 0;
581 }
582
583 /* ----------------------------------------------------------------------- */
584
585 static struct i2c_driver i2c_driver_saa7110 = {
586         .driver = {
587                 .name = "saa7110",
588         },
589
590         .id = I2C_DRIVERID_SAA7110,
591
592         .attach_adapter = saa7110_attach_adapter,
593         .detach_client = saa7110_detach_client,
594         .command = saa7110_command,
595 };
596
597 static int __init
598 saa7110_init (void)
599 {
600         return i2c_add_driver(&i2c_driver_saa7110);
601 }
602
603 static void __exit
604 saa7110_exit (void)
605 {
606         i2c_del_driver(&i2c_driver_saa7110);
607 }
608
609 module_init(saa7110_init);
610 module_exit(saa7110_exit);