added mtd driver
[linux-2.4.git] / drivers / media / video / bt832.c
1 /* Driver for Bt832 CMOS Camera Video Processor
2     i2c-adresses: 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
34 #include "id.h"
35 #include "audiochip.h"
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_CLIENT_END};
43 static unsigned short normal_i2c_range[] = {I2C_BT832_ALT1>>1,I2C_BT832_ALT2>>1,I2C_CLIENT_END};
44 I2C_CLIENT_INSMOD;
45
46 /* ---------------------------------------------------------------------- */
47
48 #define dprintk     if (debug) printk
49
50 static int bt832_detach(struct i2c_client *client);
51
52
53 static struct i2c_driver driver;
54 static struct i2c_client client_template;
55
56 struct bt832 {
57         struct i2c_client client;
58 };
59
60 int bt832_hexdump(struct i2c_client *i2c_client_s, unsigned char *buf)
61 {
62         int i,rc;
63         buf[0]=0x80; // start at register 0 with auto-increment
64         if (1 != (rc = i2c_master_send(i2c_client_s,buf,1)))
65                 printk("bt832: i2c i/o error: rc == %d (should be 1)\n",rc);
66
67         for(i=0;i<65;i++)
68                 buf[i]=0;
69         if (65 != (rc=i2c_master_recv(i2c_client_s,buf,65)))
70                 printk("bt832: i2c i/o error: rc == %d (should be 65)\n",rc);
71
72         // Note: On READ the first byte is the current index
73         //  (e.g. 0x80, what we just wrote)
74
75         if(1) {
76                 int i;
77                 printk("BT832 hexdump:\n");
78                 for(i=1;i<65;i++) {
79                         if(i!=1) {
80                           if(((i-1)%8)==0) printk(" ");
81                           if(((i-1)%16)==0) printk("\n");
82                         }
83                         printk(" %02x",buf[i]);
84                 }
85                 printk("\n");
86         }
87         return 0;
88 }
89
90 // Return: 1 (is a bt832), 0 (No bt832 here)
91 int bt832_init(struct i2c_client *i2c_client_s)
92 {
93         unsigned char *buf;
94         int rc;
95
96         buf=kmalloc(65,GFP_KERNEL);
97         bt832_hexdump(i2c_client_s,buf);
98         
99         if(buf[0x40] != 0x31) {
100                 printk("bt832: this i2c chip is no bt832 (id=%02x). Detaching.\n",buf[0x40]);
101                 kfree(buf);
102                 return 0;
103         }
104
105         printk("Write 0 tp VPSTATUS\n");
106         buf[0]=BT832_VP_STATUS; // Reg.52
107         buf[1]= 0x00;
108         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
109                 printk("bt832: i2c i/o error VPS: rc == %d (should be 2)\n",rc);
110
111         bt832_hexdump(i2c_client_s,buf);
112
113
114         // Leave low power mode:
115         printk("Bt832: leave low power mode.\n");
116         buf[0]=BT832_CAM_SETUP0; //0x39 57
117         buf[1]=0x08;
118         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
119                 printk("bt832: i2c i/o error LLPM: rc == %d (should be 2)\n",rc);
120
121         bt832_hexdump(i2c_client_s,buf);
122
123         printk("Write 0 tp VPSTATUS\n");
124         buf[0]=BT832_VP_STATUS; // Reg.52
125         buf[1]= 0x00;
126         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
127                 printk("bt832: i2c i/o error VPS: rc == %d (should be 2)\n",rc);
128
129         bt832_hexdump(i2c_client_s,buf);
130
131
132         // Enable Output
133         printk("Enable Output\n");
134         buf[0]=BT832_VP_CONTROL1; // Reg.40
135         buf[1]= 0x27 & (~0x01); // Default | !skip
136         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
137                 printk("bt832: i2c i/o error EO: rc == %d (should be 2)\n",rc);
138         
139         bt832_hexdump(i2c_client_s,buf);
140
141 #if 0
142         // Full 30/25 Frame rate
143         printk("Full 30/25 Frame rate\n");
144         buf[0]=BT832_VP_CONTROL0; // Reg.39
145         buf[1]= 0x00;
146         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
147                 printk("bt832: i2c i/o error FFR: rc == %d (should be 2)\n",rc);
148
149         bt832_hexdump(i2c_client_s,buf);
150 #endif
151
152 #if 1
153         // for testing (even works when no camera attached)
154         printk("bt832: *** Generate NTSC M Bars *****\n");
155         buf[0]=BT832_VP_TESTCONTROL0; // Reg. 42
156         buf[1]=3; // Generate NTSC System M bars, Generate Frame timing internally
157         if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
158                 printk("bt832: i2c i/o error MBAR: rc == %d (should be 2)\n",rc);
159 #endif
160
161         printk("Bt832: Camera Present: %s\n",
162                 (buf[1+BT832_CAM_STATUS] & BT832_56_CAMERA_PRESENT) ? "yes":"no");
163
164         bt832_hexdump(i2c_client_s,buf);
165         kfree(buf);
166         return 1;
167 }
168
169
170
171 static int bt832_attach(struct i2c_adapter *adap, int addr,
172                           unsigned short flags, int kind)
173 {
174         struct bt832 *t;
175
176         printk("bt832_attach\n");
177
178         client_template.adapter = adap;
179         client_template.addr    = addr;
180
181         printk("bt832: chip found @ 0x%x\n", addr<<1);
182
183         if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
184                 return -ENOMEM;
185         memset(t,0,sizeof(*t));
186         t->client = client_template;
187         t->client.data = t;
188         i2c_attach_client(&t->client);
189
190         MOD_INC_USE_COUNT;
191         if(! bt832_init(&t->client)) {
192                 bt832_detach(&t->client);
193                 return -1;
194         }
195         
196         return 0;
197 }
198
199 static int bt832_probe(struct i2c_adapter *adap)
200 {
201         int rc;
202
203         printk("bt832_probe\n");
204
205         switch (adap->id) {
206         case I2C_ALGO_BIT | I2C_HW_B_BT848:
207         case I2C_ALGO_BIT | I2C_HW_B_RIVA:
208         case I2C_ALGO_SAA7134:
209                 printk("bt832: probing %s i2c adapter [id=0x%x]\n",
210                        adap->name,adap->id);
211                 rc = i2c_probe(adap, &addr_data, bt832_attach);
212                 break;
213         default:
214                 printk("bt832: ignoring %s i2c adapter [id=0x%x]\n",
215                        adap->name,adap->id);
216                 rc = 0;
217                 /* nothing */
218         }
219         return rc;
220 }
221
222 static int bt832_detach(struct i2c_client *client)
223 {
224         struct bt832 *t = (struct bt832*)client->data;
225
226         printk("bt832: detach.\n");
227         i2c_detach_client(client);
228         kfree(t);
229         MOD_DEC_USE_COUNT;
230         return 0;
231 }
232
233 static int
234 bt832_command(struct i2c_client *client, unsigned int cmd, void *arg)
235 {
236         struct bt832 *t = (struct bt832*)client->data;
237
238         printk("bt832: command %x\n",cmd);
239
240         switch (cmd) {
241                 case BT832_HEXDUMP: {
242                         unsigned char *buf;
243                         buf=kmalloc(65,GFP_KERNEL);
244                         bt832_hexdump(&t->client,buf);
245                         kfree(buf);
246                 }
247                 break;
248                 case BT832_REATTACH:
249                         printk("bt832: re-attach\n");
250                         i2c_del_driver(&driver);
251                         i2c_add_driver(&driver);
252                 break;
253         }
254         return 0;
255 }
256
257 /* ----------------------------------------------------------------------- */
258
259 static struct i2c_driver driver = {
260         .name           = "i2c bt832 driver",
261         .id             = -1, /* FIXME */
262         .flags          = I2C_DF_NOTIFY,
263         .attach_adapter = bt832_probe,
264         .detach_client  = bt832_detach,
265         .command        = bt832_command,
266 };
267 static struct i2c_client client_template =
268 {
269         .name   = "bt832",
270         .flags  = I2C_CLIENT_ALLOW_USE,
271         .driver = &driver,
272 };
273
274
275 int bt832_init_module(void)
276 {
277         i2c_add_driver(&driver);
278         return 0;
279 }
280
281 static void bt832_cleanup_module(void)
282 {
283         i2c_del_driver(&driver);
284 }
285
286 module_init(bt832_init_module);
287 module_exit(bt832_cleanup_module);
288