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