Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / media / video / saa7111.c
1 /*
2  * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
3  *
4  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5  *
6  * Slight changes for video timing and attachment output by
7  * Wolfgang Scherr <scherr@net4you.net>
8  *
9  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
11  *
12  * Changes by Michael Hunold <michael@mihu.de>
13  *    - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/ioctl.h>
33 #include <asm/uaccess.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-id.h>
36 #include <linux/videodev.h>
37 #include <linux/video_decoder.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-i2c-drv-legacy.h>
40
41 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
42 MODULE_AUTHOR("Dave Perks");
43 MODULE_LICENSE("GPL");
44
45 static int debug;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "Debug level (0-1)");
48
49 /* ----------------------------------------------------------------------- */
50
51 #define SAA7111_NR_REG          0x18
52
53 struct saa7111 {
54         unsigned char reg[SAA7111_NR_REG];
55
56         int norm;
57         int input;
58         int enable;
59 };
60
61 /* ----------------------------------------------------------------------- */
62
63 static inline int saa7111_write(struct i2c_client *client, u8 reg, u8 value)
64 {
65         struct saa7111 *decoder = i2c_get_clientdata(client);
66
67         decoder->reg[reg] = value;
68         return i2c_smbus_write_byte_data(client, reg, value);
69 }
70
71 static inline void saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
72 {
73         struct saa7111 *decoder = i2c_get_clientdata(client);
74
75         if (decoder->reg[reg] != value) {
76                 decoder->reg[reg] = value;
77                 i2c_smbus_write_byte_data(client, reg, value);
78         }
79 }
80
81 static int saa7111_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
82 {
83         int ret = -1;
84         u8 reg;
85
86         /* the saa7111 has an autoincrement function, use it if
87          * the adapter understands raw I2C */
88         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
89                 /* do raw I2C, not smbus compatible */
90                 struct saa7111 *decoder = i2c_get_clientdata(client);
91                 u8 block_data[32];
92                 int block_len;
93
94                 while (len >= 2) {
95                         block_len = 0;
96                         block_data[block_len++] = reg = data[0];
97                         do {
98                                 block_data[block_len++] =
99                                     decoder->reg[reg++] = data[1];
100                                 len -= 2;
101                                 data += 2;
102                         } while (len >= 2 && data[0] == reg && block_len < 32);
103                         ret = i2c_master_send(client, block_data, block_len);
104                         if (ret < 0)
105                                 break;
106                 }
107         } else {
108                 /* do some slow I2C emulation kind of thing */
109                 while (len >= 2) {
110                         reg = *data++;
111                         ret = saa7111_write(client, reg, *data++);
112                         if (ret < 0)
113                                 break;
114                         len -= 2;
115                 }
116         }
117
118         return ret;
119 }
120
121 static int saa7111_init_decoder(struct i2c_client *client,
122                 struct video_decoder_init *init)
123 {
124         return saa7111_write_block(client, init->data, init->len);
125 }
126
127 static inline int saa7111_read(struct i2c_client *client, u8 reg)
128 {
129         return i2c_smbus_read_byte_data(client, reg);
130 }
131
132 /* ----------------------------------------------------------------------- */
133
134 static const unsigned char saa7111_i2c_init[] = {
135         0x00, 0x00,             /* 00 - ID byte */
136         0x01, 0x00,             /* 01 - reserved */
137
138         /*front end */
139         0x02, 0xd0,             /* 02 - FUSE=3, GUDL=2, MODE=0 */
140         0x03, 0x23,             /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
141                                  * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
142         0x04, 0x00,             /* 04 - GAI1=256 */
143         0x05, 0x00,             /* 05 - GAI2=256 */
144
145         /* decoder */
146         0x06, 0xf3,             /* 06 - HSB at  13(50Hz) /  17(60Hz)
147                                  * pixels after end of last line */
148         /*0x07, 0x13,     * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
149                                  * after end of last line */
150         0x07, 0xe8,             /* 07 - HSS seems to be needed to
151                                  * work with NTSC, too */
152         0x08, 0xc8,             /* 08 - AUFD=1, FSEL=1, EXFIL=0,
153                                  * VTRC=1, HPLL=0, VNOI=0 */
154         0x09, 0x01,             /* 09 - BYPS=0, PREF=0, BPSS=0,
155                                  * VBLB=0, UPTCV=0, APER=1 */
156         0x0a, 0x80,             /* 0a - BRIG=128 */
157         0x0b, 0x47,             /* 0b - CONT=1.109 */
158         0x0c, 0x40,             /* 0c - SATN=1.0 */
159         0x0d, 0x00,             /* 0d - HUE=0 */
160         0x0e, 0x01,             /* 0e - CDTO=0, CSTD=0, DCCF=0,
161                                  * FCTC=0, CHBW=1 */
162         0x0f, 0x00,             /* 0f - reserved */
163         0x10, 0x48,             /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
164         0x11, 0x1c,             /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
165                                  * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
166         0x12, 0x00,             /* 12 - output control 2 */
167         0x13, 0x00,             /* 13 - output control 3 */
168         0x14, 0x00,             /* 14 - reserved */
169         0x15, 0x00,             /* 15 - VBI */
170         0x16, 0x00,             /* 16 - VBI */
171         0x17, 0x00,             /* 17 - VBI */
172 };
173
174 static int saa7111_command(struct i2c_client *client, unsigned cmd, void *arg)
175 {
176         struct saa7111 *decoder = i2c_get_clientdata(client);
177
178         switch (cmd) {
179         case 0:
180                 break;
181         case DECODER_INIT:
182         {
183                 struct video_decoder_init *init = arg;
184                 struct video_decoder_init vdi;
185
186                 if (NULL != init)
187                         return saa7111_init_decoder(client, init);
188                 vdi.data = saa7111_i2c_init;
189                 vdi.len = sizeof(saa7111_i2c_init);
190                 return saa7111_init_decoder(client, &vdi);
191         }
192
193         case DECODER_DUMP:
194         {
195                 int i;
196
197                 for (i = 0; i < SAA7111_NR_REG; i += 16) {
198                         int j;
199
200                         v4l_info(client, "%03x", i);
201                         for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
202                                 printk(KERN_CONT " %02x",
203                                        saa7111_read(client, i + j));
204                         }
205                         printk(KERN_CONT "\n");
206                 }
207                 break;
208         }
209
210         case DECODER_GET_CAPABILITIES:
211         {
212                 struct video_decoder_capability *cap = arg;
213
214                 cap->flags = VIDEO_DECODER_PAL |
215                              VIDEO_DECODER_NTSC |
216                              VIDEO_DECODER_SECAM |
217                              VIDEO_DECODER_AUTO |
218                              VIDEO_DECODER_CCIR;
219                 cap->inputs = 8;
220                 cap->outputs = 1;
221                 break;
222         }
223
224         case DECODER_GET_STATUS:
225         {
226                 int *iarg = arg;
227                 int status;
228                 int res;
229
230                 status = saa7111_read(client, 0x1f);
231                 v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
232                 res = 0;
233                 if ((status & (1 << 6)) == 0) {
234                         res |= DECODER_STATUS_GOOD;
235                 }
236                 switch (decoder->norm) {
237                 case VIDEO_MODE_NTSC:
238                         res |= DECODER_STATUS_NTSC;
239                         break;
240                 case VIDEO_MODE_PAL:
241                         res |= DECODER_STATUS_PAL;
242                         break;
243                 case VIDEO_MODE_SECAM:
244                         res |= DECODER_STATUS_SECAM;
245                         break;
246                 default:
247                 case VIDEO_MODE_AUTO:
248                         if ((status & (1 << 5)) != 0) {
249                                 res |= DECODER_STATUS_NTSC;
250                         } else {
251                                 res |= DECODER_STATUS_PAL;
252                         }
253                         break;
254                 }
255                 if ((status & (1 << 0)) != 0) {
256                         res |= DECODER_STATUS_COLOR;
257                 }
258                 *iarg = res;
259                 break;
260         }
261
262         case DECODER_SET_GPIO:
263         {
264                 int *iarg = arg;
265                 if (0 != *iarg) {
266                         saa7111_write(client, 0x11,
267                                 (decoder->reg[0x11] | 0x80));
268                 } else {
269                         saa7111_write(client, 0x11,
270                                 (decoder->reg[0x11] & 0x7f));
271                 }
272                 break;
273         }
274
275         case DECODER_SET_VBI_BYPASS:
276         {
277                 int *iarg = arg;
278                 if (0 != *iarg) {
279                         saa7111_write(client, 0x13,
280                                 (decoder->reg[0x13] & 0xf0) | 0x0a);
281                 } else {
282                         saa7111_write(client, 0x13,
283                                 (decoder->reg[0x13] & 0xf0));
284                 }
285                 break;
286         }
287
288         case DECODER_SET_NORM:
289         {
290                 int *iarg = arg;
291
292                 switch (*iarg) {
293
294                 case VIDEO_MODE_NTSC:
295                         saa7111_write(client, 0x08,
296                                       (decoder->reg[0x08] & 0x3f) | 0x40);
297                         saa7111_write(client, 0x0e,
298                                       (decoder->reg[0x0e] & 0x8f));
299                         break;
300
301                 case VIDEO_MODE_PAL:
302                         saa7111_write(client, 0x08,
303                                       (decoder->reg[0x08] & 0x3f) | 0x00);
304                         saa7111_write(client, 0x0e,
305                                       (decoder->reg[0x0e] & 0x8f));
306                         break;
307
308                 case VIDEO_MODE_SECAM:
309                         saa7111_write(client, 0x08,
310                                       (decoder->reg[0x08] & 0x3f) | 0x00);
311                         saa7111_write(client, 0x0e,
312                                       (decoder->reg[0x0e] & 0x8f) | 0x50);
313                         break;
314
315                 case VIDEO_MODE_AUTO:
316                         saa7111_write(client, 0x08,
317                                       (decoder->reg[0x08] & 0x3f) | 0x80);
318                         saa7111_write(client, 0x0e,
319                                       (decoder->reg[0x0e] & 0x8f));
320                         break;
321
322                 default:
323                         return -EINVAL;
324
325                 }
326                 decoder->norm = *iarg;
327                 break;
328         }
329
330         case DECODER_SET_INPUT:
331         {
332                 int *iarg = arg;
333
334                 if (*iarg < 0 || *iarg > 7) {
335                         return -EINVAL;
336                 }
337
338                 if (decoder->input != *iarg) {
339                         decoder->input = *iarg;
340                         /* select mode */
341                         saa7111_write(client, 0x02,
342                                       (decoder->
343                                        reg[0x02] & 0xf8) | decoder->input);
344                         /* bypass chrominance trap for modes 4..7 */
345                         saa7111_write(client, 0x09,
346                                       (decoder->
347                                        reg[0x09] & 0x7f) | ((decoder->
348                                                              input >
349                                                              3) ? 0x80 :
350                                                             0));
351                 }
352                 break;
353         }
354
355         case DECODER_SET_OUTPUT:
356         {
357                 int *iarg = arg;
358
359                 /* not much choice of outputs */
360                 if (*iarg != 0) {
361                         return -EINVAL;
362                 }
363                 break;
364         }
365
366         case DECODER_ENABLE_OUTPUT:
367         {
368                 int *iarg = arg;
369                 int enable = (*iarg != 0);
370
371                 if (decoder->enable != enable) {
372                         decoder->enable = enable;
373
374                         /* RJ: If output should be disabled (for
375                          * playing videos), we also need a open PLL.
376                          * The input is set to 0 (where no input
377                          * source is connected), although this
378                          * is not necessary.
379                          *
380                          * If output should be enabled, we have to
381                          * reverse the above.
382                          */
383
384                         if (decoder->enable) {
385                                 saa7111_write(client, 0x02,
386                                               (decoder->
387                                                reg[0x02] & 0xf8) |
388                                               decoder->input);
389                                 saa7111_write(client, 0x08,
390                                               (decoder->reg[0x08] & 0xfb));
391                                 saa7111_write(client, 0x11,
392                                               (decoder->
393                                                reg[0x11] & 0xf3) | 0x0c);
394                         } else {
395                                 saa7111_write(client, 0x02,
396                                               (decoder->reg[0x02] & 0xf8));
397                                 saa7111_write(client, 0x08,
398                                               (decoder->
399                                                reg[0x08] & 0xfb) | 0x04);
400                                 saa7111_write(client, 0x11,
401                                               (decoder->reg[0x11] & 0xf3));
402                         }
403                 }
404                 break;
405         }
406
407         case DECODER_SET_PICTURE:
408         {
409                 struct video_picture *pic = arg;
410
411                 /* We want 0 to 255 we get 0-65535 */
412                 saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
413                 /* We want 0 to 127 we get 0-65535 */
414                 saa7111_write(client, 0x0b, pic->contrast >> 9);
415                 /* We want 0 to 127 we get 0-65535 */
416                 saa7111_write(client, 0x0c, pic->colour >> 9);
417                 /* We want -128 to 127 we get 0-65535 */
418                 saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
419                 break;
420         }
421
422         default:
423                 return -EINVAL;
424         }
425
426         return 0;
427 }
428
429 /* ----------------------------------------------------------------------- */
430
431 static unsigned short normal_i2c[] = { 0x48 >> 1, I2C_CLIENT_END };
432
433 I2C_CLIENT_INSMOD;
434
435 static int saa7111_probe(struct i2c_client *client,
436                         const struct i2c_device_id *id)
437 {
438         int i;
439         struct saa7111 *decoder;
440         struct video_decoder_init vdi;
441
442         /* Check if the adapter supports the needed features */
443         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
444                 return -ENODEV;
445
446         v4l_info(client, "chip found @ 0x%x (%s)\n",
447                         client->addr << 1, client->adapter->name);
448
449         decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
450         if (decoder == NULL) {
451                 kfree(client);
452                 return -ENOMEM;
453         }
454         decoder->norm = VIDEO_MODE_NTSC;
455         decoder->input = 0;
456         decoder->enable = 1;
457         i2c_set_clientdata(client, decoder);
458
459         vdi.data = saa7111_i2c_init;
460         vdi.len = sizeof(saa7111_i2c_init);
461         i = saa7111_init_decoder(client, &vdi);
462         if (i < 0) {
463                 v4l_dbg(1, debug, client, "init status %d\n", i);
464         } else {
465                 v4l_dbg(1, debug, client, "revision %x\n",
466                         saa7111_read(client, 0x00) >> 4);
467         }
468         return 0;
469 }
470
471 static int saa7111_remove(struct i2c_client *client)
472 {
473         kfree(i2c_get_clientdata(client));
474         return 0;
475 }
476
477 /* ----------------------------------------------------------------------- */
478
479 static const struct i2c_device_id saa7111_id[] = {
480         { "saa7111_old", 0 },   /* "saa7111" maps to the saa7115 driver */
481         { }
482 };
483 MODULE_DEVICE_TABLE(i2c, saa7111_id);
484
485 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
486         .name = "saa7111",
487         .driverid = I2C_DRIVERID_SAA7111A,
488         .command = saa7111_command,
489         .probe = saa7111_probe,
490         .remove = saa7111_remove,
491         .id_table = saa7111_id,
492 };