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