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