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