Merge master.kernel.org:/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[safe/jmp/linux-2.6] / drivers / media / video / adv7170.c
1 /* 
2  * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
3  *
4  * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
5  *
6  * Based on adv7176 driver by:    
7  *
8  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
10  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
11  *    - some corrections for Pinnacle Systems Inc. DC10plus card.
12  *
13  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
35 #include <linux/fs.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/slab.h>
39 #include <linux/mm.h>
40 #include <linux/pci.h>
41 #include <linux/signal.h>
42 #include <asm/io.h>
43 #include <asm/pgtable.h>
44 #include <asm/page.h>
45 #include <linux/sched.h>
46 #include <linux/types.h>
47
48 #include <linux/videodev.h>
49 #include <asm/uaccess.h>
50
51 MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
52 MODULE_AUTHOR("Maxim Yevtyushkin");
53 MODULE_LICENSE("GPL");
54
55 #include <linux/i2c.h>
56
57 #define I2C_NAME(x) (x)->name
58
59 #include <linux/video_encoder.h>
60
61 static int debug = 0;
62 module_param(debug, int, 0);
63 MODULE_PARM_DESC(debug, "Debug level (0-1)");
64
65 #define dprintk(num, format, args...) \
66         do { \
67                 if (debug >= num) \
68                         printk(format, ##args); \
69         } while (0)
70
71 /* ----------------------------------------------------------------------- */
72
73 struct adv7170 {
74         unsigned char reg[128];
75
76         int norm;
77         int input;
78         int enable;
79         int bright;
80         int contrast;
81         int hue;
82         int sat;
83 };
84
85 #define   I2C_ADV7170        0xd4
86 #define   I2C_ADV7171        0x54
87
88 static char adv7170_name[] = "adv7170";
89 static char adv7171_name[] = "adv7171";
90
91 static char *inputs[] = { "pass_through", "play_back" };
92 static char *norms[] = { "PAL", "NTSC" };
93
94 /* ----------------------------------------------------------------------- */
95
96 static inline int
97 adv7170_write (struct i2c_client *client,
98                u8                 reg,
99                u8                 value)
100 {
101         struct adv7170 *encoder = i2c_get_clientdata(client);
102
103         encoder->reg[reg] = value;
104         return i2c_smbus_write_byte_data(client, reg, value);
105 }
106
107 static inline int
108 adv7170_read (struct i2c_client *client,
109               u8                 reg)
110 {
111         return i2c_smbus_read_byte_data(client, reg);
112 }
113
114 static int
115 adv7170_write_block (struct i2c_client *client,
116                      const u8          *data,
117                      unsigned int       len)
118 {
119         int ret = -1;
120         u8 reg;
121
122         /* the adv7170 has an autoincrement function, use it if
123          * the adapter understands raw I2C */
124         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
125                 /* do raw I2C, not smbus compatible */
126                 struct adv7170 *encoder = i2c_get_clientdata(client);
127                 u8 block_data[32];
128                 int block_len;
129
130                 while (len >= 2) {
131                         block_len = 0;
132                         block_data[block_len++] = reg = data[0];
133                         do {
134                                 block_data[block_len++] =
135                                     encoder->reg[reg++] = data[1];
136                                 len -= 2;
137                                 data += 2;
138                         } while (len >= 2 && data[0] == reg &&
139                                  block_len < 32);
140                         if ((ret = i2c_master_send(client, block_data,
141                                                    block_len)) < 0)
142                                 break;
143                 }
144         } else {
145                 /* do some slow I2C emulation kind of thing */
146                 while (len >= 2) {
147                         reg = *data++;
148                         if ((ret = adv7170_write(client, reg,
149                                                  *data++)) < 0)
150                                 break;
151                         len -= 2;
152                 }
153         }
154
155         return ret;
156 }
157
158 /* ----------------------------------------------------------------------- */
159 // Output filter:  S-Video  Composite
160
161 #define MR050       0x11        //0x09
162 #define MR060       0x14        //0x0c
163
164 //---------------------------------------------------------------------------
165
166 #define TR0MODE     0x4c
167 #define TR0RST      0x80
168
169 #define TR1CAPT     0x00
170 #define TR1PLAY     0x00
171
172
173 static const unsigned char init_NTSC[] = {
174         0x00, 0x10,             // MR0
175         0x01, 0x20,             // MR1
176         0x02, 0x0e,             // MR2 RTC control: bits 2 and 1 
177         0x03, 0x80,             // MR3
178         0x04, 0x30,             // MR4
179         0x05, 0x00,             // Reserved
180         0x06, 0x00,             // Reserved
181         0x07, TR0MODE,          // TM0
182         0x08, TR1CAPT,          // TM1
183         0x09, 0x16,             // Fsc0
184         0x0a, 0x7c,             // Fsc1
185         0x0b, 0xf0,             // Fsc2
186         0x0c, 0x21,             // Fsc3
187         0x0d, 0x00,             // Subcarrier Phase
188         0x0e, 0x00,             // Closed Capt. Ext 0
189         0x0f, 0x00,             // Closed Capt. Ext 1
190         0x10, 0x00,             // Closed Capt. 0
191         0x11, 0x00,             // Closed Capt. 1
192         0x12, 0x00,             // Pedestal Ctl 0
193         0x13, 0x00,             // Pedestal Ctl 1
194         0x14, 0x00,             // Pedestal Ctl 2
195         0x15, 0x00,             // Pedestal Ctl 3
196         0x16, 0x00,             // CGMS_WSS_0
197         0x17, 0x00,             // CGMS_WSS_1
198         0x18, 0x00,             // CGMS_WSS_2
199         0x19, 0x00,             // Teletext Ctl 
200 };
201
202 static const unsigned char init_PAL[] = {
203         0x00, 0x71,             // MR0
204         0x01, 0x20,             // MR1
205         0x02, 0x0e,             // MR2 RTC control: bits 2 and 1
206         0x03, 0x80,             // MR3
207         0x04, 0x30,             // MR4
208         0x05, 0x00,             // Reserved
209         0x06, 0x00,             // Reserved
210         0x07, TR0MODE,          // TM0
211         0x08, TR1CAPT,          // TM1
212         0x09, 0xcb,             // Fsc0
213         0x0a, 0x8a,             // Fsc1
214         0x0b, 0x09,             // Fsc2
215         0x0c, 0x2a,             // Fsc3
216         0x0d, 0x00,             // Subcarrier Phase
217         0x0e, 0x00,             // Closed Capt. Ext 0
218         0x0f, 0x00,             // Closed Capt. Ext 1
219         0x10, 0x00,             // Closed Capt. 0
220         0x11, 0x00,             // Closed Capt. 1
221         0x12, 0x00,             // Pedestal Ctl 0
222         0x13, 0x00,             // Pedestal Ctl 1
223         0x14, 0x00,             // Pedestal Ctl 2
224         0x15, 0x00,             // Pedestal Ctl 3
225         0x16, 0x00,             // CGMS_WSS_0
226         0x17, 0x00,             // CGMS_WSS_1
227         0x18, 0x00,             // CGMS_WSS_2
228         0x19, 0x00,             // Teletext Ctl
229 };
230
231
232 static int
233 adv7170_command (struct i2c_client *client,
234                  unsigned int       cmd,
235                  void *             arg)
236 {
237         struct adv7170 *encoder = i2c_get_clientdata(client);
238
239         switch (cmd) {
240
241         case 0:
242 #if 0
243                 /* This is just for testing!!! */
244                 adv7170_write_block(client, init_common,
245                                     sizeof(init_common));
246                 adv7170_write(client, 0x07, TR0MODE | TR0RST);
247                 adv7170_write(client, 0x07, TR0MODE);
248 #endif
249                 break;
250
251         case ENCODER_GET_CAPABILITIES:
252         {
253                 struct video_encoder_capability *cap = arg;
254
255                 cap->flags = VIDEO_ENCODER_PAL |
256                              VIDEO_ENCODER_NTSC;
257                 cap->inputs = 2;
258                 cap->outputs = 1;
259         }
260                 break;
261
262         case ENCODER_SET_NORM:
263         {
264                 int iarg = *(int *) arg;
265
266                 dprintk(1, KERN_DEBUG "%s_command: set norm %d",
267                         I2C_NAME(client), iarg);
268
269                 switch (iarg) {
270
271                 case VIDEO_MODE_NTSC:
272                         adv7170_write_block(client, init_NTSC,
273                                             sizeof(init_NTSC));
274                         if (encoder->input == 0)
275                                 adv7170_write(client, 0x02, 0x0e);      // Enable genlock
276                         adv7170_write(client, 0x07, TR0MODE | TR0RST);
277                         adv7170_write(client, 0x07, TR0MODE);
278                         break;
279
280                 case VIDEO_MODE_PAL:
281                         adv7170_write_block(client, init_PAL,
282                                             sizeof(init_PAL));
283                         if (encoder->input == 0)
284                                 adv7170_write(client, 0x02, 0x0e);      // Enable genlock
285                         adv7170_write(client, 0x07, TR0MODE | TR0RST);
286                         adv7170_write(client, 0x07, TR0MODE);
287                         break;
288
289                 default:
290                         dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
291                                I2C_NAME(client), iarg);
292                         return -EINVAL;
293
294                 }
295                 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
296                         norms[iarg]);
297                 encoder->norm = iarg;
298         }
299                 break;
300
301         case ENCODER_SET_INPUT:
302         {
303                 int iarg = *(int *) arg;
304
305                 /* RJ: *iarg = 0: input is from decoder
306                  *iarg = 1: input is from ZR36060
307                  *iarg = 2: color bar */
308
309                 dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
310                         I2C_NAME(client),
311                         iarg == 0 ? "decoder" : "ZR36060");
312
313                 switch (iarg) {
314
315                 case 0:
316                         adv7170_write(client, 0x01, 0x20);
317                         adv7170_write(client, 0x08, TR1CAPT);   /* TR1 */
318                         adv7170_write(client, 0x02, 0x0e);      // Enable genlock
319                         adv7170_write(client, 0x07, TR0MODE | TR0RST);
320                         adv7170_write(client, 0x07, TR0MODE);
321                         //udelay(10);
322                         break;
323
324                 case 1:
325                         adv7170_write(client, 0x01, 0x00);
326                         adv7170_write(client, 0x08, TR1PLAY);   /* TR1 */
327                         adv7170_write(client, 0x02, 0x08);
328                         adv7170_write(client, 0x07, TR0MODE | TR0RST);
329                         adv7170_write(client, 0x07, TR0MODE);
330                         //udelay(10);
331                         break;
332
333                 default:
334                         dprintk(1, KERN_ERR "%s: illegal input: %d\n",
335                                 I2C_NAME(client), iarg);
336                         return -EINVAL;
337
338                 }
339                 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
340                         inputs[iarg]);
341                 encoder->input = iarg;
342         }
343                 break;
344
345         case ENCODER_SET_OUTPUT:
346         {
347                 int *iarg = arg;
348
349                 /* not much choice of outputs */
350                 if (*iarg != 0) {
351                         return -EINVAL;
352                 }
353         }
354                 break;
355
356         case ENCODER_ENABLE_OUTPUT:
357         {
358                 int *iarg = arg;
359
360                 encoder->enable = !!*iarg;
361         }
362                 break;
363
364         default:
365                 return -EINVAL;
366         }
367
368         return 0;
369 }
370
371 /* ----------------------------------------------------------------------- */
372
373 /*
374  * Generic i2c probe
375  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
376  */
377 static unsigned short normal_i2c[] =
378     { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
379         I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
380         I2C_CLIENT_END
381 };
382
383 static unsigned short ignore = I2C_CLIENT_END;
384                                                                                 
385 static struct i2c_client_address_data addr_data = {
386         .normal_i2c             = normal_i2c,
387         .probe                  = &ignore,
388         .ignore                 = &ignore,
389 };
390
391 static struct i2c_driver i2c_driver_adv7170;
392
393 static int
394 adv7170_detect_client (struct i2c_adapter *adapter,
395                        int                 address,
396                        int                 kind)
397 {
398         int i;
399         struct i2c_client *client;
400         struct adv7170 *encoder;
401         char *dname;
402
403         dprintk(1,
404                 KERN_INFO
405                 "adv7170.c: detecting adv7170 client on address 0x%x\n",
406                 address << 1);
407
408         /* Check if the adapter supports the needed features */
409         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
410                 return 0;
411
412         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
413         if (client == 0)
414                 return -ENOMEM;
415         client->addr = address;
416         client->adapter = adapter;
417         client->driver = &i2c_driver_adv7170;
418         if ((client->addr == I2C_ADV7170 >> 1) ||
419             (client->addr == (I2C_ADV7170 >> 1) + 1)) {
420                 dname = adv7170_name;
421         } else if ((client->addr == I2C_ADV7171 >> 1) ||
422                    (client->addr == (I2C_ADV7171 >> 1) + 1)) {
423                 dname = adv7171_name;
424         } else {
425                 /* We should never get here!!! */
426                 kfree(client);
427                 return 0;
428         }
429         strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
430
431         encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
432         if (encoder == NULL) {
433                 kfree(client);
434                 return -ENOMEM;
435         }
436         encoder->norm = VIDEO_MODE_NTSC;
437         encoder->input = 0;
438         encoder->enable = 1;
439         i2c_set_clientdata(client, encoder);
440
441         i = i2c_attach_client(client);
442         if (i) {
443                 kfree(client);
444                 kfree(encoder);
445                 return i;
446         }
447
448         i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
449         if (i >= 0) {
450                 i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
451                 i = adv7170_write(client, 0x07, TR0MODE);
452                 i = adv7170_read(client, 0x12);
453                 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
454                         I2C_NAME(client), i & 1, client->addr << 1);
455         }
456         if (i < 0) {
457                 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
458                        I2C_NAME(client), i);
459         }
460
461         return 0;
462 }
463
464 static int
465 adv7170_attach_adapter (struct i2c_adapter *adapter)
466 {
467         dprintk(1,
468                 KERN_INFO
469                 "adv7170.c: starting probe for adapter %s (0x%x)\n",
470                 I2C_NAME(adapter), adapter->id);
471         return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
472 }
473
474 static int
475 adv7170_detach_client (struct i2c_client *client)
476 {
477         struct adv7170 *encoder = i2c_get_clientdata(client);
478         int err;
479
480         err = i2c_detach_client(client);
481         if (err) {
482                 return err;
483         }
484
485         kfree(encoder);
486         kfree(client);
487
488         return 0;
489 }
490
491 /* ----------------------------------------------------------------------- */
492
493 static struct i2c_driver i2c_driver_adv7170 = {
494         .driver = {
495                 .name = "adv7170",      /* name */
496         },
497
498         .id = I2C_DRIVERID_ADV7170,
499
500         .attach_adapter = adv7170_attach_adapter,
501         .detach_client = adv7170_detach_client,
502         .command = adv7170_command,
503 };
504
505 static int __init
506 adv7170_init (void)
507 {
508         return i2c_add_driver(&i2c_driver_adv7170);
509 }
510
511 static void __exit
512 adv7170_exit (void)
513 {
514         i2c_del_driver(&i2c_driver_adv7170);
515 }
516
517 module_init(adv7170_init);
518 module_exit(adv7170_exit);