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