Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-2.6] / 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/types.h>
42
43 #include <linux/videodev.h>
44 #include <asm/uaccess.h>
45
46 MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
47 MODULE_AUTHOR("Dave Perks");
48 MODULE_LICENSE("GPL");
49
50 #include <linux/i2c.h>
51
52 #define I2C_NAME(s) (s)->name
53
54 #include <linux/video_encoder.h>
55
56 static int debug = 0;
57 module_param(debug, int, 0);
58 MODULE_PARM_DESC(debug, "Debug level (0-1)");
59
60 #define dprintk(num, format, args...) \
61         do { \
62                 if (debug >= num) \
63                         printk(format, ##args); \
64         } while (0)
65
66 /* ----------------------------------------------------------------------- */
67
68 struct adv7175 {
69         int norm;
70         int input;
71         int enable;
72         int bright;
73         int contrast;
74         int hue;
75         int sat;
76 };
77
78 #define   I2C_ADV7175        0xd4
79 #define   I2C_ADV7176        0x54
80
81 static char adv7175_name[] = "adv7175";
82 static char adv7176_name[] = "adv7176";
83
84 static char *inputs[] = { "pass_through", "play_back", "color_bar" };
85 static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
86
87 /* ----------------------------------------------------------------------- */
88
89 static inline int
90 adv7175_write (struct i2c_client *client,
91                u8                 reg,
92                u8                 value)
93 {
94         return i2c_smbus_write_byte_data(client, reg, value);
95 }
96
97 static inline int
98 adv7175_read (struct i2c_client *client,
99               u8                 reg)
100 {
101         return i2c_smbus_read_byte_data(client, reg);
102 }
103
104 static int
105 adv7175_write_block (struct i2c_client *client,
106                      const u8          *data,
107                      unsigned int       len)
108 {
109         int ret = -1;
110         u8 reg;
111
112         /* the adv7175 has an autoincrement function, use it if
113          * the adapter understands raw I2C */
114         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
115                 /* do raw I2C, not smbus compatible */
116                 u8 block_data[32];
117                 int block_len;
118
119                 while (len >= 2) {
120                         block_len = 0;
121                         block_data[block_len++] = reg = data[0];
122                         do {
123                                 block_data[block_len++] = data[1];
124                                 reg++;
125                                 len -= 2;
126                                 data += 2;
127                         } while (len >= 2 && data[0] == reg &&
128                                  block_len < 32);
129                         if ((ret = i2c_master_send(client, block_data,
130                                                    block_len)) < 0)
131                                 break;
132                 }
133         } else {
134                 /* do some slow I2C emulation kind of thing */
135                 while (len >= 2) {
136                         reg = *data++;
137                         if ((ret = adv7175_write(client, reg,
138                                                  *data++)) < 0)
139                                 break;
140                         len -= 2;
141                 }
142         }
143
144         return ret;
145 }
146
147 static void
148 set_subcarrier_freq (struct i2c_client *client,
149                      int                pass_through)
150 {
151         /* for some reason pass_through NTSC needs
152          * a different sub-carrier freq to remain stable. */
153         if(pass_through)
154                 adv7175_write(client, 0x02, 0x00);
155         else
156                 adv7175_write(client, 0x02, 0x55);
157
158         adv7175_write(client, 0x03, 0x55);
159         adv7175_write(client, 0x04, 0x55);
160         adv7175_write(client, 0x05, 0x25);
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     0x46
172 #define TR0RST      0x80
173
174 #define TR1CAPT     0x80
175 #define TR1PLAY     0x00
176
177 static const unsigned char init_common[] = {
178
179         0x00, MR050,            /* MR0, PAL enabled */
180         0x01, 0x00,             /* MR1 */
181         0x02, 0x0c,             /* subc. freq. */
182         0x03, 0x8c,             /* subc. freq. */
183         0x04, 0x79,             /* subc. freq. */
184         0x05, 0x26,             /* subc. freq. */
185         0x06, 0x40,             /* subc. phase */
186
187         0x07, TR0MODE,          /* TR0, 16bit */
188         0x08, 0x21,             /*  */
189         0x09, 0x00,             /*  */
190         0x0a, 0x00,             /*  */
191         0x0b, 0x00,             /*  */
192         0x0c, TR1CAPT,          /* TR1 */
193         0x0d, 0x4f,             /* MR2 */
194         0x0e, 0x00,             /*  */
195         0x0f, 0x00,             /*  */
196         0x10, 0x00,             /*  */
197         0x11, 0x00,             /*  */
198 };
199
200 static const unsigned char init_pal[] = {
201         0x00, MR050,            /* MR0, PAL enabled */
202         0x01, 0x00,             /* MR1 */
203         0x02, 0x0c,             /* subc. freq. */
204         0x03, 0x8c,             /* subc. freq. */
205         0x04, 0x79,             /* subc. freq. */
206         0x05, 0x26,             /* subc. freq. */
207         0x06, 0x40,             /* subc. phase */
208 };
209
210 static const unsigned char init_ntsc[] = {
211         0x00, MR060,            /* MR0, NTSC enabled */
212         0x01, 0x00,             /* MR1 */
213         0x02, 0x55,             /* subc. freq. */
214         0x03, 0x55,             /* subc. freq. */
215         0x04, 0x55,             /* subc. freq. */
216         0x05, 0x25,             /* subc. freq. */
217         0x06, 0x1a,             /* subc. phase */
218 };
219
220 static int
221 adv7175_command (struct i2c_client *client,
222                  unsigned int       cmd,
223                  void              *arg)
224 {
225         struct adv7175 *encoder = i2c_get_clientdata(client);
226
227         switch (cmd) {
228
229         case 0:
230                 /* This is just for testing!!! */
231                 adv7175_write_block(client, init_common,
232                                     sizeof(init_common));
233                 adv7175_write(client, 0x07, TR0MODE | TR0RST);
234                 adv7175_write(client, 0x07, TR0MODE);
235                 break;
236
237         case ENCODER_GET_CAPABILITIES:
238         {
239                 struct video_encoder_capability *cap = arg;
240
241                 cap->flags = VIDEO_ENCODER_PAL |
242                              VIDEO_ENCODER_NTSC |
243                              VIDEO_ENCODER_SECAM; /* well, hacky */
244                 cap->inputs = 2;
245                 cap->outputs = 1;
246         }
247                 break;
248
249         case ENCODER_SET_NORM:
250         {
251                 int iarg = *(int *) arg;
252
253                 switch (iarg) {
254
255                 case VIDEO_MODE_NTSC:
256                         adv7175_write_block(client, init_ntsc,
257                                             sizeof(init_ntsc));
258                         if (encoder->input == 0)
259                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
260                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
261                         adv7175_write(client, 0x07, TR0MODE);
262                         break;
263
264                 case VIDEO_MODE_PAL:
265                         adv7175_write_block(client, init_pal,
266                                             sizeof(init_pal));
267                         if (encoder->input == 0)
268                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
269                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
270                         adv7175_write(client, 0x07, TR0MODE);
271                         break;
272
273                 case VIDEO_MODE_SECAM:  // WARNING! ADV7176 does not support SECAM.
274                         /* This is an attempt to convert
275                          * SECAM->PAL (typically it does not work
276                          * due to genlock: when decoder is in SECAM
277                          * and encoder in in PAL the subcarrier can
278                          * not be syncronized with horizontal
279                          * quency) */
280                         adv7175_write_block(client, init_pal,
281                                             sizeof(init_pal));
282                         if (encoder->input == 0)
283                                 adv7175_write(client, 0x0d, 0x49);      // Disable genlock
284                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
285                         adv7175_write(client, 0x07, TR0MODE);
286                         break;
287                 default:
288                         dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
289                                 I2C_NAME(client), iarg);
290                         return -EINVAL;
291
292                 }
293                 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
294                         norms[iarg]);
295                 encoder->norm = iarg;
296         }
297                 break;
298
299         case ENCODER_SET_INPUT:
300         {
301                 int iarg = *(int *) arg;
302
303                 /* RJ: *iarg = 0: input is from SAA7110
304                  *iarg = 1: input is from ZR36060
305                  *iarg = 2: color bar */
306
307                 switch (iarg) {
308
309                 case 0:
310                         adv7175_write(client, 0x01, 0x00);
311
312                         if (encoder->norm == VIDEO_MODE_NTSC)
313                                 set_subcarrier_freq(client, 1);
314
315                         adv7175_write(client, 0x0c, TR1CAPT);   /* TR1 */
316                         if (encoder->norm == VIDEO_MODE_SECAM)
317                                 adv7175_write(client, 0x0d, 0x49);      // Disable genlock
318                         else
319                                 adv7175_write(client, 0x0d, 0x4f);      // Enable genlock
320                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
321                         adv7175_write(client, 0x07, TR0MODE);
322                         //udelay(10);
323                         break;
324
325                 case 1:
326                         adv7175_write(client, 0x01, 0x00);
327
328                         if (encoder->norm == VIDEO_MODE_NTSC)
329                                 set_subcarrier_freq(client, 0);
330
331                         adv7175_write(client, 0x0c, TR1PLAY);   /* TR1 */
332                         adv7175_write(client, 0x0d, 0x49);
333                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
334                         adv7175_write(client, 0x07, TR0MODE);
335                         //udelay(10);
336                         break;
337
338                 case 2:
339                         adv7175_write(client, 0x01, 0x80);
340
341                         if (encoder->norm == VIDEO_MODE_NTSC)
342                                 set_subcarrier_freq(client, 0);
343
344                         adv7175_write(client, 0x0d, 0x49);
345                         adv7175_write(client, 0x07, TR0MODE | TR0RST);
346                         adv7175_write(client, 0x07, TR0MODE);
347                         //udelay(10);
348                         break;
349
350                 default:
351                         dprintk(1, KERN_ERR "%s: illegal input: %d\n",
352                                 I2C_NAME(client), iarg);
353                         return -EINVAL;
354
355                 }
356                 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
357                         inputs[iarg]);
358                 encoder->input = iarg;
359         }
360                 break;
361
362         case ENCODER_SET_OUTPUT:
363         {
364                 int *iarg = arg;
365
366                 /* not much choice of outputs */
367                 if (*iarg != 0) {
368                         return -EINVAL;
369                 }
370         }
371                 break;
372
373         case ENCODER_ENABLE_OUTPUT:
374         {
375                 int *iarg = arg;
376
377                 encoder->enable = !!*iarg;
378         }
379                 break;
380
381         default:
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 /* ----------------------------------------------------------------------- */
389
390 /*
391  * Generic i2c probe
392  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
393  */
394 static unsigned short normal_i2c[] =
395     { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
396         I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
397         I2C_CLIENT_END
398 };
399
400 static unsigned short ignore = I2C_CLIENT_END;
401
402 static struct i2c_client_address_data addr_data = {
403         .normal_i2c             = normal_i2c,
404         .probe                  = &ignore,
405         .ignore                 = &ignore,
406 };
407
408 static struct i2c_driver i2c_driver_adv7175;
409
410 static int
411 adv7175_detect_client (struct i2c_adapter *adapter,
412                        int                 address,
413                        int                 kind)
414 {
415         int i;
416         struct i2c_client *client;
417         struct adv7175 *encoder;
418         char *dname;
419
420         dprintk(1,
421                 KERN_INFO
422                 "adv7175.c: detecting adv7175 client on address 0x%x\n",
423                 address << 1);
424
425         /* Check if the adapter supports the needed features */
426         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
427                 return 0;
428
429         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
430         if (client == 0)
431                 return -ENOMEM;
432         client->addr = address;
433         client->adapter = adapter;
434         client->driver = &i2c_driver_adv7175;
435         if ((client->addr == I2C_ADV7175 >> 1) ||
436             (client->addr == (I2C_ADV7175 >> 1) + 1)) {
437                 dname = adv7175_name;
438         } else if ((client->addr == I2C_ADV7176 >> 1) ||
439                    (client->addr == (I2C_ADV7176 >> 1) + 1)) {
440                 dname = adv7176_name;
441         } else {
442                 /* We should never get here!!! */
443                 kfree(client);
444                 return 0;
445         }
446         strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
447
448         encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
449         if (encoder == NULL) {
450                 kfree(client);
451                 return -ENOMEM;
452         }
453         encoder->norm = VIDEO_MODE_PAL;
454         encoder->input = 0;
455         encoder->enable = 1;
456         i2c_set_clientdata(client, encoder);
457
458         i = i2c_attach_client(client);
459         if (i) {
460                 kfree(client);
461                 kfree(encoder);
462                 return i;
463         }
464
465         i = adv7175_write_block(client, init_common, sizeof(init_common));
466         if (i >= 0) {
467                 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
468                 i = adv7175_write(client, 0x07, TR0MODE);
469                 i = adv7175_read(client, 0x12);
470                 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
471                         I2C_NAME(client), i & 1, client->addr << 1);
472         }
473         if (i < 0) {
474                 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
475                         I2C_NAME(client), i);
476         }
477
478         return 0;
479 }
480
481 static int
482 adv7175_attach_adapter (struct i2c_adapter *adapter)
483 {
484         dprintk(1,
485                 KERN_INFO
486                 "adv7175.c: starting probe for adapter %s (0x%x)\n",
487                 I2C_NAME(adapter), adapter->id);
488         return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
489 }
490
491 static int
492 adv7175_detach_client (struct i2c_client *client)
493 {
494         struct adv7175 *encoder = i2c_get_clientdata(client);
495         int err;
496
497         err = i2c_detach_client(client);
498         if (err) {
499                 return err;
500         }
501
502         kfree(encoder);
503         kfree(client);
504
505         return 0;
506 }
507
508 /* ----------------------------------------------------------------------- */
509
510 static struct i2c_driver i2c_driver_adv7175 = {
511         .driver = {
512                 .name = "adv7175",      /* name */
513         },
514
515         .id = I2C_DRIVERID_ADV7175,
516
517         .attach_adapter = adv7175_attach_adapter,
518         .detach_client = adv7175_detach_client,
519         .command = adv7175_command,
520 };
521
522 static int __init
523 adv7175_init (void)
524 {
525         return i2c_add_driver(&i2c_driver_adv7175);
526 }
527
528 static void __exit
529 adv7175_exit (void)
530 {
531         i2c_del_driver(&i2c_driver_adv7175);
532 }
533
534 module_init(adv7175_init);
535 module_exit(adv7175_exit);