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