Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / drivers / media / video / bt832.c
1 /* Driver for Bt832 CMOS Camera Video Processor
2     i2c-addresses: 0x88 or 0x8a
3
4   The BT832 interfaces to a Quartzsight Digital Camera (352x288, 25 or 30 fps)
5   via a 9 pin connector ( 4-wire SDATA, 2-wire i2c, SCLK, VCC, GND).
6   It outputs an 8-bit 4:2:2 YUV or YCrCb video signal which can be directly
7   connected to bt848/bt878 GPIO pins on this purpose.
8   (see: VLSI Vision Ltd. www.vvl.co.uk for camera datasheets)
9
10   Supported Cards:
11   -  Pixelview Rev.4E: 0x8a
12                 GPIO 0x400000 toggles Bt832 RESET, and the chip changes to i2c 0x88 !
13
14   (c) Gunther Mayer, 2002
15
16   STATUS:
17   - detect chip and hexdump
18   - reset chip and leave low power mode
19   - detect camera present
20
21   TODO:
22   - make it work (find correct setup for Bt832 and Bt878)
23 */
24
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/i2c.h>
28 #include <linux/types.h>
29 #include <linux/videodev.h>
30 #include <linux/init.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <media/audiochip.h>
34 #include <media/v4l2-common.h>
35
36 #include "bttv.h"
37 #include "bt832.h"
38
39 MODULE_LICENSE("GPL");
40
41 /* Addresses to scan */
42 static unsigned short normal_i2c[] = { I2C_BT832_ALT1>>1, I2C_BT832_ALT2>>1,
43                                        I2C_CLIENT_END };
44 I2C_CLIENT_INSMOD;
45
46 int debug    = 0;    /* debug output */
47 module_param(debug,            int, 0644);
48
49 /* ---------------------------------------------------------------------- */
50
51 static int bt832_detach(struct i2c_client *client);
52
53
54 static struct i2c_driver driver;
55 static struct i2c_client client_template;
56
57 struct bt832 {
58         struct i2c_client client;
59 };
60
61 int bt832_hexdump(struct i2c_client *i2c_client_s, unsigned char *buf)
62 {
63         int i,rc;
64         buf[0]=0x80; // start at register 0 with auto-increment
65         if (1 != (rc = i2c_master_send(i2c_client_s,buf,1)))
66                 v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 1)\n",rc);
67
68         for(i=0;i<65;i++)
69                 buf[i]=0;
70         if (65 != (rc=i2c_master_recv(i2c_client_s,buf,65)))
71                 v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 65)\n",rc);
72
73         // Note: On READ the first byte is the current index
74         //  (e.g. 0x80, what we just wrote)
75
76         if(debug>1) {
77                 int i;
78                 v4l_dbg(2,i2c_client_s,"hexdump:");
79                 for(i=1;i<65;i++) {
80                         if(i!=1) {
81                                 if(((i-1)%8)==0) printk(" ");
82                                 if(((i-1)%16)==0) {
83                                         printk("\n");
84                                         v4l_dbg(2,i2c_client_s,"hexdump:");
85                                 }
86                         }
87                         printk(" %02x",buf[i]);
88                 }
89                 printk("\n");
90         }
91         return 0;
92 }
93
94 // Return: 1 (is a bt832), 0 (No bt832 here)
95 int bt832_init(struct i2c_client *i2c_client_s)
96 {
97         unsigned char *buf;
98         int rc;
99
100         buf=kmalloc(65,GFP_KERNEL);
101         bt832_hexdump(i2c_client_s,buf);
102
103         if(buf[0x40] != 0x31) {
104                 v4l_err(i2c_client_s,"This i2c chip is no bt832 (id=%02x). Detaching.\n",buf[0x40]);
105                 kfree(buf);
106                 return 0;
107         }
108
109         v4l_err(i2c_client_s,"Write 0 tp VPSTATUS\n");
110         buf[0]=BT832_VP_STATUS; // Reg.52
111         buf[1]= 0x00;
112         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
113                 v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
114
115         bt832_hexdump(i2c_client_s,buf);
116
117
118         // Leave low power mode:
119         v4l_err(i2c_client_s,"leave low power mode.\n");
120         buf[0]=BT832_CAM_SETUP0; //0x39 57
121         buf[1]=0x08;
122         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
123                 v4l_err(i2c_client_s,"i2c i/o error LLPM: rc == %d (should be 2)\n",rc);
124
125         bt832_hexdump(i2c_client_s,buf);
126
127         v4l_info(i2c_client_s,"Write 0 tp VPSTATUS\n");
128         buf[0]=BT832_VP_STATUS; // Reg.52
129         buf[1]= 0x00;
130         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
131                 v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
132
133         bt832_hexdump(i2c_client_s,buf);
134
135
136         // Enable Output
137         v4l_info(i2c_client_s,"Enable Output\n");
138         buf[0]=BT832_VP_CONTROL1; // Reg.40
139         buf[1]= 0x27 & (~0x01); // Default | !skip
140         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
141                 v4l_err(i2c_client_s,"i2c i/o error EO: rc == %d (should be 2)\n",rc);
142
143         bt832_hexdump(i2c_client_s,buf);
144
145
146         // for testing (even works when no camera attached)
147         v4l_info(i2c_client_s,"*** Generate NTSC M Bars *****\n");
148         buf[0]=BT832_VP_TESTCONTROL0; // Reg. 42
149         buf[1]=3; // Generate NTSC System M bars, Generate Frame timing internally
150         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
151                 v4l_info(i2c_client_s,"i2c i/o error MBAR: rc == %d (should be 2)\n",rc);
152
153         v4l_info(i2c_client_s,"Camera Present: %s\n",
154                 (buf[1+BT832_CAM_STATUS] & BT832_56_CAMERA_PRESENT) ? "yes":"no");
155
156         bt832_hexdump(i2c_client_s,buf);
157         kfree(buf);
158         return 1;
159 }
160
161
162
163 static int bt832_attach(struct i2c_adapter *adap, int addr, int kind)
164 {
165         struct bt832 *t;
166
167         client_template.adapter = adap;
168         client_template.addr    = addr;
169
170         if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
171                 return -ENOMEM;
172         memset(t,0,sizeof(*t));
173         t->client = client_template;
174         i2c_set_clientdata(&t->client, t);
175         i2c_attach_client(&t->client);
176
177         v4l_info(&t->client,"chip found @ 0x%x\n", addr<<1);
178
179
180         if(! bt832_init(&t->client)) {
181                 bt832_detach(&t->client);
182                 return -1;
183         }
184
185         return 0;
186 }
187
188 static int bt832_probe(struct i2c_adapter *adap)
189 {
190         if (adap->class & I2C_CLASS_TV_ANALOG)
191                 return i2c_probe(adap, &addr_data, bt832_attach);
192         return 0;
193 }
194
195 static int bt832_detach(struct i2c_client *client)
196 {
197         struct bt832 *t = i2c_get_clientdata(client);
198
199         v4l_info(&t->client,"dettach\n");
200         i2c_detach_client(client);
201         kfree(t);
202         return 0;
203 }
204
205 static int
206 bt832_command(struct i2c_client *client, unsigned int cmd, void *arg)
207 {
208         struct bt832 *t = i2c_get_clientdata(client);
209
210         if (debug>1)
211                 v4l_i2c_print_ioctl(&t->client,cmd);
212
213         switch (cmd) {
214                 case BT832_HEXDUMP: {
215                         unsigned char *buf;
216                         buf=kmalloc(65,GFP_KERNEL);
217                         bt832_hexdump(&t->client,buf);
218                         kfree(buf);
219                 }
220                 break;
221                 case BT832_REATTACH:
222                         v4l_info(&t->client,"re-attach\n");
223                         i2c_del_driver(&driver);
224                         i2c_add_driver(&driver);
225                 break;
226         }
227         return 0;
228 }
229
230 /* ----------------------------------------------------------------------- */
231
232 static struct i2c_driver driver = {
233         .driver = {
234                 .name   = "bt832",
235         },
236         .id             = 0, /* FIXME */
237         .attach_adapter = bt832_probe,
238         .detach_client  = bt832_detach,
239         .command        = bt832_command,
240 };
241 static struct i2c_client client_template =
242 {
243         .name       = "bt832",
244         .driver     = &driver,
245 };
246
247
248 static int __init bt832_init_module(void)
249 {
250         return i2c_add_driver(&driver);
251 }
252
253 static void __exit bt832_cleanup_module(void)
254 {
255         i2c_del_driver(&driver);
256 }
257
258 module_init(bt832_init_module);
259 module_exit(bt832_cleanup_module);
260
261 /*
262  * Overrides for Emacs so that we follow Linus's tabbing style.
263  * ---------------------------------------------------------------------------
264  * Local variables:
265  * c-basic-offset: 8
266  * End:
267  */