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