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