ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / bt856.c
1 /* 
2  * bt856 - BT856A Digital Video Encoder (Rockwell Part)
3  *
4  * Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5  * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6  *
7  * Modifications for LML33/DC10plus unified driver
8  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9  *
10  * This code was modify/ported from the saa7111 driver written
11  * by Dave Perks.
12  *
13  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14  *   - moved over to linux>=2.4.x i2c protocol (9/9/2002)
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 <asm/segment.h>
47 #include <linux/types.h>
48
49 #include <linux/videodev.h>
50 #include <asm/uaccess.h>
51
52 MODULE_DESCRIPTION("Brooktree-856A video encoder driver");
53 MODULE_AUTHOR("Mike Bernson & Dave Perks");
54 MODULE_LICENSE("GPL");
55
56 #include <linux/i2c.h>
57 #include <linux/i2c-dev.h>
58
59 #define I2C_NAME(s) (s)->name
60
61 #include <linux/video_encoder.h>
62
63 static int debug = 0;
64 MODULE_PARM(debug, "i");
65 MODULE_PARM_DESC(debug, "Debug level (0-1)");
66
67 #define dprintk(num, format, args...) \
68         do { \
69                 if (debug >= num) \
70                         printk(format, ##args); \
71         } while (0)
72
73 /* ----------------------------------------------------------------------- */
74
75 #define REG_OFFSET  0xCE
76
77 struct bt856 {
78         unsigned char reg[32];
79
80         int norm;
81         int enable;
82         int bright;
83         int contrast;
84         int hue;
85         int sat;
86 };
87
88 #define   I2C_BT856        0x88
89
90 /* ----------------------------------------------------------------------- */
91
92 static inline int
93 bt856_write (struct i2c_client *client,
94              u8                 reg,
95              u8                 value)
96 {
97         struct bt856 *encoder = i2c_get_clientdata(client);
98
99         encoder->reg[reg - REG_OFFSET] = value;
100         return i2c_smbus_write_byte_data(client, reg, value);
101 }
102
103 static inline int
104 bt856_setbit (struct i2c_client *client,
105               u8                 reg,
106               u8                 bit,
107               u8                 value)
108 {
109         struct bt856 *encoder = i2c_get_clientdata(client);
110
111         return bt856_write(client, reg,
112                            (encoder->
113                             reg[reg - REG_OFFSET] & ~(1 << bit)) |
114                             (value ? (1 << bit) : 0));
115 }
116
117 static void
118 bt856_dump (struct i2c_client *client)
119 {
120         int i;
121         struct bt856 *encoder = i2c_get_clientdata(client);
122
123         printk(KERN_INFO "%s: register dump:", I2C_NAME(client));
124         for (i = 0xd6; i <= 0xde; i += 2)
125                 printk(" %02x", encoder->reg[i - REG_OFFSET]);
126         printk("\n");
127 }
128
129 /* ----------------------------------------------------------------------- */
130
131 static int
132 bt856_command (struct i2c_client *client,
133                unsigned int       cmd,
134                void              *arg)
135 {
136         struct bt856 *encoder = i2c_get_clientdata(client);
137
138         switch (cmd) {
139
140         case 0:
141                 /* This is just for testing!!! */
142                 dprintk(1, KERN_INFO "bt856: init\n");
143                 bt856_write(client, 0xdc, 0x18);
144                 bt856_write(client, 0xda, 0);
145                 bt856_write(client, 0xde, 0);
146
147                 bt856_setbit(client, 0xdc, 3, 1);
148                 //bt856_setbit(client, 0xdc, 6, 0);
149                 bt856_setbit(client, 0xdc, 4, 1);
150
151                 switch (encoder->norm) {
152
153                 case VIDEO_MODE_NTSC:
154                         bt856_setbit(client, 0xdc, 2, 0);
155                         break;
156
157                 case VIDEO_MODE_PAL:
158                         bt856_setbit(client, 0xdc, 2, 1);
159                         break;
160                 }
161
162                 bt856_setbit(client, 0xdc, 1, 1);
163                 bt856_setbit(client, 0xde, 4, 0);
164                 bt856_setbit(client, 0xde, 3, 1);
165                 if (debug != 0)
166                         bt856_dump(client);
167                 break;
168
169         case ENCODER_GET_CAPABILITIES:
170         {
171                 struct video_encoder_capability *cap = arg;
172
173                 dprintk(1, KERN_INFO "%s: get capabilities\n",
174                         I2C_NAME(client));
175
176                 cap->flags = VIDEO_ENCODER_PAL |
177                              VIDEO_ENCODER_NTSC |
178                              VIDEO_ENCODER_CCIR;
179                 cap->inputs = 2;
180                 cap->outputs = 1;
181         }
182                 break;
183
184         case ENCODER_SET_NORM:
185         {
186                 int *iarg = arg;
187
188                 dprintk(1, KERN_INFO "%s: set norm %d\n", I2C_NAME(client),
189                         *iarg);
190
191                 switch (*iarg) {
192
193                 case VIDEO_MODE_NTSC:
194                         bt856_setbit(client, 0xdc, 2, 0);
195                         break;
196
197                 case VIDEO_MODE_PAL:
198                         bt856_setbit(client, 0xdc, 2, 1);
199                         bt856_setbit(client, 0xda, 0, 0);
200                         //bt856_setbit(client, 0xda, 0, 1);
201                         break;
202
203                 default:
204                         return -EINVAL;
205
206                 }
207                 encoder->norm = *iarg;
208                 if (debug != 0)
209                         bt856_dump(client);
210         }
211                 break;
212
213         case ENCODER_SET_INPUT:
214         {
215                 int *iarg = arg;
216
217                 dprintk(1, KERN_INFO "%s: set input %d\n", I2C_NAME(client),
218                         *iarg);
219
220                 /* We only have video bus.
221                  * iarg = 0: input is from bt819
222                  * iarg = 1: input is from ZR36060 */
223
224                 switch (*iarg) {
225
226                 case 0:
227                         bt856_setbit(client, 0xde, 4, 0);
228                         bt856_setbit(client, 0xde, 3, 1);
229                         bt856_setbit(client, 0xdc, 3, 1);
230                         bt856_setbit(client, 0xdc, 6, 0);
231                         break;
232                 case 1:
233                         bt856_setbit(client, 0xde, 4, 0);
234                         bt856_setbit(client, 0xde, 3, 1);
235                         bt856_setbit(client, 0xdc, 3, 1);
236                         bt856_setbit(client, 0xdc, 6, 1);
237                         break;
238                 case 2: // Color bar
239                         bt856_setbit(client, 0xdc, 3, 0);
240                         bt856_setbit(client, 0xde, 4, 1);
241                         break;
242                 default:
243                         return -EINVAL;
244
245                 }
246
247                 if (debug != 0)
248                         bt856_dump(client);
249         }
250                 break;
251
252         case ENCODER_SET_OUTPUT:
253         {
254                 int *iarg = arg;
255
256                 dprintk(1, KERN_INFO "%s: set output %d\n", I2C_NAME(client),
257                         *iarg);
258
259                 /* not much choice of outputs */
260                 if (*iarg != 0) {
261                         return -EINVAL;
262                 }
263         }
264                 break;
265
266         case ENCODER_ENABLE_OUTPUT:
267         {
268                 int *iarg = arg;
269
270                 encoder->enable = !!*iarg;
271
272                 dprintk(1, KERN_INFO "%s: enable output %d\n",
273                         I2C_NAME(client), encoder->enable);
274         }
275                 break;
276
277         default:
278                 return -EINVAL;
279         }
280
281         return 0;
282 }
283
284 /* ----------------------------------------------------------------------- */
285
286 /*
287  * Generic i2c probe
288  * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
289  */
290 static unsigned short normal_i2c[] = { I2C_BT856 >> 1, I2C_CLIENT_END };
291 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
292
293 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
294 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
295 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
296 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
297 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
298                                                                                 
299 static struct i2c_client_address_data addr_data = {
300         .normal_i2c             = normal_i2c,
301         .normal_i2c_range       = normal_i2c_range,
302         .probe                  = probe,
303         .probe_range            = probe_range,
304         .ignore                 = ignore,
305         .ignore_range           = ignore_range,
306         .force                  = force
307 };
308
309 static int bt856_i2c_id = 0;
310 static struct i2c_driver i2c_driver_bt856;
311
312 static int
313 bt856_detect_client (struct i2c_adapter *adapter,
314                      int                 address,
315                      int                 kind)
316 {
317         int i;
318         struct i2c_client *client;
319         struct bt856 *encoder;
320
321         dprintk(1,
322                 KERN_INFO
323                 "bt856.c: detecting bt856 client on address 0x%x\n",
324                 address << 1);
325
326         /* Check if the adapter supports the needed features */
327         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
328                 return 0;
329
330         client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
331         if (client == 0)
332                 return -ENOMEM;
333         memset(client, 0, sizeof(struct i2c_client));
334         client->addr = address;
335         client->adapter = adapter;
336         client->driver = &i2c_driver_bt856;
337         client->flags = I2C_CLIENT_ALLOW_USE;
338         client->id = bt856_i2c_id++;
339         snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
340                 "bt856[%d]", client->id);
341
342         encoder = kmalloc(sizeof(struct bt856), GFP_KERNEL);
343         if (encoder == NULL) {
344                 kfree(client);
345                 return -ENOMEM;
346         }
347         memset(encoder, 0, sizeof(struct bt856));
348         encoder->norm = VIDEO_MODE_NTSC;
349         encoder->enable = 1;
350         i2c_set_clientdata(client, encoder);
351
352         i = i2c_attach_client(client);
353         if (i) {
354                 kfree(client);
355                 kfree(encoder);
356                 return i;
357         }
358
359         bt856_write(client, 0xdc, 0x18);
360         bt856_write(client, 0xda, 0);
361         bt856_write(client, 0xde, 0);
362
363         bt856_setbit(client, 0xdc, 3, 1);
364         //bt856_setbit(client, 0xdc, 6, 0);
365         bt856_setbit(client, 0xdc, 4, 1);
366
367         switch (encoder->norm) {
368
369         case VIDEO_MODE_NTSC:
370                 bt856_setbit(client, 0xdc, 2, 0);
371                 break;
372
373         case VIDEO_MODE_PAL:
374                 bt856_setbit(client, 0xdc, 2, 1);
375                 break;
376         }
377
378         bt856_setbit(client, 0xdc, 1, 1);
379         bt856_setbit(client, 0xde, 4, 0);
380         bt856_setbit(client, 0xde, 3, 1);
381
382         if (debug != 0)
383                 bt856_dump(client);
384
385         dprintk(1, KERN_INFO "%s_attach: at address 0x%x\n", I2C_NAME(client),
386                 client->addr << 1);
387
388         return 0;
389 }
390
391 static int
392 bt856_attach_adapter (struct i2c_adapter *adapter)
393 {
394         dprintk(1,
395                 KERN_INFO
396                 "bt856.c: starting probe for adapter %s (0x%x)\n",
397                 I2C_NAME(adapter), adapter->id);
398         return i2c_probe(adapter, &addr_data, &bt856_detect_client);
399 }
400
401 static int
402 bt856_detach_client (struct i2c_client *client)
403 {
404         struct bt856 *encoder = i2c_get_clientdata(client);
405         int err;
406
407         err = i2c_detach_client(client);
408         if (err) {
409                 return err;
410         }
411
412         kfree(encoder);
413         kfree(client);
414
415         return 0;
416 }
417
418 /* ----------------------------------------------------------------------- */
419
420 static struct i2c_driver i2c_driver_bt856 = {
421         .owner = THIS_MODULE,
422         .name = "bt856",
423
424         .id = I2C_DRIVERID_BT856,
425         .flags = I2C_DF_NOTIFY,
426
427         .attach_adapter = bt856_attach_adapter,
428         .detach_client = bt856_detach_client,
429         .command = bt856_command,
430 };
431
432 static int __init
433 bt856_init (void)
434 {
435         return i2c_add_driver(&i2c_driver_bt856);
436 }
437
438 static void __exit
439 bt856_exit (void)
440 {
441         i2c_del_driver(&i2c_driver_bt856);
442 }
443
444 module_init(bt856_init);
445 module_exit(bt856_exit);