ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / adv7175.c
1 /* 
2  *  adv7175 - adv7175a video encoder driver version 0.0.3
3  *
4  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
6  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7  *    - some corrections for Pinnacle Systems Inc. DC10plus card.
8  *
9  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10  *    - moved over to linux>=2.4.x i2c protocol (9/9/2002)
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/major.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/pci.h>
37 #include <linux/signal.h>
38 #include <asm/io.h>
39 #include <asm/pgtable.h>
40 #include <asm/page.h>
41 #include <linux/sched.h>
42 #include <asm/segment.h>
43 #include <linux/types.h>
44
45 #include <linux/videodev.h>
46 #include <asm/uaccess.h>
47
48 MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
49 MODULE_AUTHOR("Dave Perks");
50 MODULE_LICENSE("GPL");
51
52 #include <linux/i2c.h>
53 #include <linux/i2c-dev.h>
54
55 #define I2C_NAME(s) (s)->name
56
57 #include <linux/video_encoder.h>
58
59 static int debug = 0;
60 MODULE_PARM(debug, "i");
61 MODULE_PARM_DESC(debug, "Debug level (0-1)");
62
63 #define dprintk(num, format, args...) \
64         do { \
65                 if (debug >= num) \
66                         printk(format, ##args); \
67         } while (0)
68
69 /* ----------------------------------------------------------------------- */
70
71 struct adv7175 {
72         unsigned char reg[128];
73
74         int norm;
75         int input;
76         int enable;
77         int bright;
78         int contrast;
79         int hue;
80         int sat;
81 };
82
83 #define   I2C_ADV7175        0xd4
84 #define   I2C_ADV7176        0x54
85
86 static char adv7175_name[] = "adv7175";
87 static char adv7176_name[] = "adv7176";
88
89 static char *inputs[] = { "pass_through", "play_back", "color_bar" };
90 static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
91
92 /* ----------------------------------------------------------------------- */
93
94 static inline int
95 adv7175_write (struct i2c_client *client,
96                u8                 reg,
97                u8                 value)
98 {
99         struct adv7175 *encoder = i2c_get_clientdata(client);
100
101         encoder->reg[reg] = value;
102         return i2c_smbus_write_byte_data(client, reg, value);
103 }
104
105 static inline int
106 adv7175_read (struct i2c_client *client,
107               u8                 reg)
108 {
109         return i2c_smbus_read_byte_data(client, reg);
110 }
111
112 static int
113 adv7175_write_block (struct i2c_client *client,
114                      const u8          *data,
115                      unsigned int       len)
116 {
117         int ret = -1;
118         u8 reg;
119
120         /* the adv7175 has an autoincrement function, use it if
121          * the adapter understands raw I2C */
122         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
123                 /* do raw I2C, not smbus compatible */
124                 struct adv7175 *encoder = i2c_get_clientdata(client);
125                 struct i2c_msg msg;
126                 u8 block_data[32];
127
128                 msg.addr = client->addr;
129                 msg.flags = client->flags;
130                 while (len >= 2) {
131                         msg.buf = (char *) block_data;
132                         msg.len = 0;
133                         block_data[msg.len++] = reg = data[0];
134                         do {
135                                 block_data[msg.len++] =
136                                     encoder->reg[reg++] = data[1];
137                                 len -= 2;
138                                 data += 2;
139                         } while (len >= 2 && data[0] == reg &&
140                                  msg.len < 32);
141                         if ((ret = i2c_transfer(client->adapter,
142                                                 &msg, 1)) < 0)
143                                 break;
144                 }
145         } else {
146                 /* do some slow I2C emulation kind of thing */
147                 while (len >= 2) {
148                         reg = *data++;
149                         if ((ret = adv7175_write(client, reg,
150                                                  *data++)) < 0)
151                                 break;
152                         len -= 2;
153                 }
154         }
155
156         return ret;
157 }
158
159 #ifdef ENCODER_DUMP
160 static void
161 dump (struct i2c_client *client)
162 {
163         struct adv7175 *encoder = i2c_get_clientdata(client);
164         int i, j;
165
166         printk(KERN_INFO "%s: registry dump\n", I2C_NAME(client));
167         for (i = 0; i < 182 / 8; i++) {
168                 printk("%s: 0x%02x -", I2C_NAME(client), i * 8);
169                 for (j = 0; j < 8; j++) {
170                         printk(" 0x%02x", encoder->reg[i * 8 + j]);
171                 }
172                 printk("\n");
173         }
174 }
175 #endif
176
177 /* ----------------------------------------------------------------------- */
178 // Output filter:  S-Video  Composite
179
180 #define MR050       0x11        //0x09
181 #define MR060       0x14        //0x0c
182
183 //---------------------------------------------------------------------------
184
185 #define TR0MODE     0x46
186 #define TR0RST      0x80
187
188 #define TR1CAPT     0x80
189 #define TR1PLAY     0x00
190
191 static const unsigned char init_common[] = {
192
193         0x00, MR050,            /* MR0, PAL enabled */
194         0x01, 0x00,             /* MR1 */
195         0x02, 0x0c,             /* subc. freq. */
196         0x03, 0x8c,             /* subc. freq. */
197         0x04, 0x79,             /* subc. freq. */
198         0x05, 0x26,             /* subc. freq. */
199         0x06, 0x40,             /* subc. phase */
200
201         0x07, TR0MODE,          /* TR0, 16bit */
202         0x08, 0x21,             /*  */
203         0x09, 0x00,             /*  */
204         0x0a, 0x00,             /*  */
205         0x0b, 0x00,             /*  */
206         0x0c, TR1CAPT,          /* TR1 */
207         0x0d, 0x4f,             /* MR2 */
208         0x0e, 0x00,             /*  */
209         0x0f, 0x00,             /*  */
210         0x10, 0x00,             /*  */
211         0x11, 0x00,             /*  */
212 };
213
214 static const unsigned char init_pal[] = {
215         0x00, MR050,            /* MR0, PAL enabled */
216         0x01, 0x00,             /* MR1 */
217         0x02, 0x0c,             /* subc. freq. */
218         0x03, 0x8c,             /* subc. freq. */
219         0x04, 0x79,             /* subc. freq. */
220         0x05, 0x26,             /* subc. freq. */
221         0x06, 0x40,             /* subc. phase */
222 };
223
224 static const unsigned char init_ntsc[] = {
225         0x00, MR060,            /* MR0, NTSC enabled */
226         0x01, 0x00,             /* MR1 */
227         0x02, 0x55,             /* subc. freq. */
228         0x03, 0x55,             /* subc. freq. */
229         0x04, 0x55,             /* subc. freq. */
230         0x05, 0x25,             /* subc. freq. */
231         0x06, 0x1a,             /* subc. phase */
232 };
233
234 static int
235 adv7175_command (struct i2c_client *client,
236                  unsigned int       cmd,
237                  void              *arg)
238 {
239         struct adv7175 *encoder = i2c_get_clientdata(client);
240
241         switch (cmd) {
242
243         case 0:
244                 /* This is just for testing!!! */
245                 adv7175_write_block(client, init_common,
246                                     sizeof(init_common));
247                 adv7175_write(client, 0x07, TR0MODE | TR0RST);
248                 adv7175_write(client, 0x07, TR0MODE);
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                              VIDEO_ENCODER_SECAM; /* well, hacky */
258                 cap->inputs = 2;
259                 cap->outputs = 1;
260         }
261                 break;
262
263         case ENCODER_SET_NORM:
264         {
265                 int iarg = *(int *) arg;
266
267                 switch (iarg) {
268
269                 case VIDEO_MODE_NTSC:
270                         adv7175_write_block(client, init_ntsc,
271                                             sizeof(init_ntsc));
272                         if (encoder->input == 0)
273                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
274                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
275                         adv7175_write(client, 0x07, TR0MODE);
276                         break;
277
278                 case VIDEO_MODE_PAL:
279                         adv7175_write_block(client, init_pal,
280                                             sizeof(init_pal));
281                         if (encoder->input == 0)
282                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
283                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
284                         adv7175_write(client, 0x07, TR0MODE);
285                         break;
286
287                 case VIDEO_MODE_SECAM:  // WARNING! ADV7176 does not support SECAM.
288                         /* This is an attempt to convert
289                          * SECAM->PAL (typically it does not work
290                          * due to genlock: when decoder is in SECAM
291                          * and encoder in in PAL the subcarrier can
292                          * not be syncronized with horizontal
293                          * quency) */
294                         adv7175_write_block(client, init_pal,
295                                             sizeof(init_pal));
296                         if (encoder->input == 0)
297                                 adv7175_write(client, 0x0d, 0x49);      // Disable genlock
298                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
299                         adv7175_write(client, 0x07, TR0MODE);
300                         break;
301                 default:
302                         dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
303                                 I2C_NAME(client), iarg);
304                         return -EINVAL;
305
306                 }
307                 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
308                         norms[iarg]);
309                 encoder->norm = iarg;
310         }
311                 break;
312
313         case ENCODER_SET_INPUT:
314         {
315                 int iarg = *(int *) arg;
316
317                 /* RJ: *iarg = 0: input is from SAA7110
318                  *iarg = 1: input is from ZR36060
319                  *iarg = 2: color bar */
320
321                 switch (iarg) {
322
323                 case 0:
324                         adv7175_write(client, 0x01, 0x00);
325                         adv7175_write(client, 0x0c, TR1CAPT);   /* TR1 */
326                         if (encoder->norm == VIDEO_MODE_SECAM)
327                                 adv7175_write(client, 0x0d, 0x49);      // Disable genlock
328                         else
329                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
330                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
331                         adv7175_write(client, 0x07, TR0MODE);
332                         //udelay(10);
333                         break;
334
335                 case 1:
336                         adv7175_write(client, 0x01, 0x00);
337                         adv7175_write(client, 0x0c, TR1PLAY);   /* TR1 */
338                         adv7175_write(client, 0x0d, 0x49);
339                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
340                         adv7175_write(client, 0x07, TR0MODE);
341                         //udelay(10);
342                         break;
343
344                 case 2:
345                         adv7175_write(client, 0x01, 0x80);
346                         adv7175_write(client, 0x0d, 0x49);
347                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
348                         adv7175_write(client, 0x07, TR0MODE);
349                         //udelay(10);
350                         break;
351
352                 default:
353                         dprintk(1, KERN_ERR "%s: illegal input: %d\n",
354                                 I2C_NAME(client), iarg);
355                         return -EINVAL;
356
357                 }
358                 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
359                         inputs[iarg]);
360                 encoder->input = iarg;
361         }
362                 break;
363
364         case ENCODER_SET_OUTPUT:
365         {
366                 int *iarg = arg;
367
368                 /* not much choice of outputs */
369                 if (*iarg != 0) {
370                         return -EINVAL;
371                 }
372         }
373                 break;
374
375         case ENCODER_ENABLE_OUTPUT:
376         {
377                 int *iarg = arg;
378
379                 encoder->enable = !!*iarg;
380         }
381                 break;
382
383 #ifdef ENCODER_DUMP
384         case ENCODER_DUMP:
385         {
386                 dump(client);
387         }
388                 break;
389 #endif
390
391         default:
392                 return -EINVAL;
393         }
394
395         return 0;
396 }
397
398 /* ----------------------------------------------------------------------- */
399
400 /*
401  * Generic i2c probe
402  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
403  */
404 static unsigned short normal_i2c[] =
405     { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
406         I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
407         I2C_CLIENT_END
408 };
409 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
410
411 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
412 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
413 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
414 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
415 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
416                                                                                 
417 static struct i2c_client_address_data addr_data = {
418         .normal_i2c             = normal_i2c,
419         .normal_i2c_range       = normal_i2c_range,
420         .probe                  = probe,
421         .probe_range            = probe_range,
422         .ignore                 = ignore,
423         .ignore_range           = ignore_range,
424         .force                  = force
425 };
426
427 static int adv7175_i2c_id = 0;
428 static struct i2c_driver i2c_driver_adv7175;
429
430 static int
431 adv7175_detect_client (struct i2c_adapter *adapter,
432                        int                 address,
433                        int                 kind)
434 {
435         int i;
436         struct i2c_client *client;
437         struct adv7175 *encoder;
438         char *dname;
439
440         dprintk(1,
441                 KERN_INFO
442                 "adv7175.c: detecting adv7175 client on address 0x%x\n",
443                 address << 1);
444
445         /* Check if the adapter supports the needed features */
446         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
447                 return 0;
448
449         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
450         if (client == 0)
451                 return -ENOMEM;
452         memset(client, 0, sizeof(struct i2c_client));
453         client->addr = address;
454         client->adapter = adapter;
455         client->driver = &i2c_driver_adv7175;
456         client->flags = I2C_CLIENT_ALLOW_USE;
457         client->id = adv7175_i2c_id++;
458         if ((client->addr == I2C_ADV7175 >> 1) ||
459             (client->addr == (I2C_ADV7175 >> 1) + 1)) {
460                 dname = adv7175_name;
461         } else if ((client->addr == I2C_ADV7176 >> 1) ||
462                    (client->addr == (I2C_ADV7176 >> 1) + 1)) {
463                 dname = adv7176_name;
464         } else {
465                 /* We should never get here!!! */
466                 kfree(client);
467                 return 0;
468         }
469         snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
470                 "%s[%d]", dname, client->id);
471
472         encoder = kmalloc(sizeof(struct adv7175), GFP_KERNEL);
473         if (encoder == NULL) {
474                 kfree(client);
475                 return -ENOMEM;
476         }
477         memset(encoder, 0, sizeof(struct adv7175));
478         encoder->norm = VIDEO_MODE_PAL;
479         encoder->input = 0;
480         encoder->enable = 1;
481         i2c_set_clientdata(client, encoder);
482
483         i = i2c_attach_client(client);
484         if (i) {
485                 kfree(client);
486                 kfree(encoder);
487                 return i;
488         }
489
490         i = adv7175_write_block(client, init_common, sizeof(init_common));
491         if (i >= 0) {
492                 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
493                 i = adv7175_write(client, 0x07, TR0MODE);
494                 i = adv7175_read(client, 0x12);
495                 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
496                         I2C_NAME(client), i & 1, client->addr << 1);
497         }
498         if (i < 0) {
499                 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
500                         I2C_NAME(client), i);
501         }
502
503         return 0;
504 }
505
506 static int
507 adv7175_attach_adapter (struct i2c_adapter *adapter)
508 {
509         dprintk(1,
510                 KERN_INFO
511                 "adv7175.c: starting probe for adapter %s (0x%x)\n",
512                 I2C_NAME(adapter), adapter->id);
513         return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
514 }
515
516 static int
517 adv7175_detach_client (struct i2c_client *client)
518 {
519         struct adv7175 *encoder = i2c_get_clientdata(client);
520         int err;
521
522         err = i2c_detach_client(client);
523         if (err) {
524                 return err;
525         }
526
527         kfree(encoder);
528         kfree(client);
529
530         return 0;
531 }
532
533 /* ----------------------------------------------------------------------- */
534
535 static struct i2c_driver i2c_driver_adv7175 = {
536         .owner = THIS_MODULE,
537         .name = "adv7175",      /* name */
538
539         .id = I2C_DRIVERID_ADV7175,
540         .flags = I2C_DF_NOTIFY,
541
542         .attach_adapter = adv7175_attach_adapter,
543         .detach_client = adv7175_detach_client,
544         .command = adv7175_command,
545 };
546
547 static int __init
548 adv7175_init (void)
549 {
550         return i2c_add_driver(&i2c_driver_adv7175);
551 }
552
553 static void __exit
554 adv7175_exit (void)
555 {
556         i2c_del_driver(&i2c_driver_adv7175);
557 }
558
559 module_init(adv7175_init);
560 module_exit(adv7175_exit);