Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / bt8xx / bt832.c
blobf92f06dec0d0a2fce3c09965e5e1b8570ca34acd
1 /* Driver for Bt832 CMOS Camera Video Processor
2 i2c-addresses: 0x88 or 0x8a
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)
10 Supported Cards:
11 - Pixelview Rev.4E: 0x8a
12 GPIO 0x400000 toggles Bt832 RESET, and the chip changes to i2c 0x88 !
14 (c) Gunther Mayer, 2002
16 STATUS:
17 - detect chip and hexdump
18 - reset chip and leave low power mode
19 - detect camera present
21 TODO:
22 - make it work (find correct setup for Bt832 and Bt878)
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/v4l2-common.h>
35 #include "bttv.h"
36 #include "bt832.h"
38 MODULE_LICENSE("GPL");
40 /* Addresses to scan */
41 static unsigned short normal_i2c[] = { I2C_ADDR_BT832_ALT1>>1, I2C_ADDR_BT832_ALT2>>1,
42 I2C_CLIENT_END };
43 I2C_CLIENT_INSMOD;
45 int debug; /* debug output */
46 module_param(debug, int, 0644);
48 /* ---------------------------------------------------------------------- */
50 static int bt832_detach(struct i2c_client *client);
53 static struct i2c_driver driver;
54 static struct i2c_client client_template;
56 struct bt832 {
57 struct i2c_client client;
60 int bt832_hexdump(struct i2c_client *i2c_client_s, unsigned char *buf)
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 v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 1)\n",rc);
67 for(i=0;i<65;i++)
68 buf[i]=0;
69 if (65 != (rc=i2c_master_recv(i2c_client_s,buf,65)))
70 v4l_err(i2c_client_s,"i2c i/o error: rc == %d (should be 65)\n",rc);
72 // Note: On READ the first byte is the current index
73 // (e.g. 0x80, what we just wrote)
75 if(debug>1) {
76 int i;
77 v4l_dbg(2, debug,i2c_client_s,"hexdump:");
78 for(i=1;i<65;i++) {
79 if(i!=1) {
80 if(((i-1)%8)==0) printk(" ");
81 if(((i-1)%16)==0) {
82 printk("\n");
83 v4l_dbg(2, debug,i2c_client_s,"hexdump:");
86 printk(" %02x",buf[i]);
88 printk("\n");
90 return 0;
93 // Return: 1 (is a bt832), 0 (No bt832 here)
94 int bt832_init(struct i2c_client *i2c_client_s)
96 unsigned char *buf;
97 int rc;
99 buf=kmalloc(65,GFP_KERNEL);
100 if (!buf) {
101 v4l_err(&t->client,
102 "Unable to allocate memory. Detaching.\n");
103 return 0;
105 bt832_hexdump(i2c_client_s,buf);
107 if(buf[0x40] != 0x31) {
108 v4l_err(i2c_client_s,"This i2c chip is no bt832 (id=%02x). Detaching.\n",buf[0x40]);
109 kfree(buf);
110 return 0;
113 v4l_err(i2c_client_s,"Write 0 tp VPSTATUS\n");
114 buf[0]=BT832_VP_STATUS; // Reg.52
115 buf[1]= 0x00;
116 if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
117 v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
119 bt832_hexdump(i2c_client_s,buf);
122 // Leave low power mode:
123 v4l_err(i2c_client_s,"leave low power mode.\n");
124 buf[0]=BT832_CAM_SETUP0; //0x39 57
125 buf[1]=0x08;
126 if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
127 v4l_err(i2c_client_s,"i2c i/o error LLPM: rc == %d (should be 2)\n",rc);
129 bt832_hexdump(i2c_client_s,buf);
131 v4l_info(i2c_client_s,"Write 0 tp VPSTATUS\n");
132 buf[0]=BT832_VP_STATUS; // Reg.52
133 buf[1]= 0x00;
134 if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
135 v4l_err(i2c_client_s,"i2c i/o error VPS: rc == %d (should be 2)\n",rc);
137 bt832_hexdump(i2c_client_s,buf);
140 // Enable Output
141 v4l_info(i2c_client_s,"Enable Output\n");
142 buf[0]=BT832_VP_CONTROL1; // Reg.40
143 buf[1]= 0x27 & (~0x01); // Default | !skip
144 if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
145 v4l_err(i2c_client_s,"i2c i/o error EO: rc == %d (should be 2)\n",rc);
147 bt832_hexdump(i2c_client_s,buf);
150 // for testing (even works when no camera attached)
151 v4l_info(i2c_client_s,"*** Generate NTSC M Bars *****\n");
152 buf[0]=BT832_VP_TESTCONTROL0; // Reg. 42
153 buf[1]=3; // Generate NTSC System M bars, Generate Frame timing internally
154 if (2 != (rc = i2c_master_send(i2c_client_s,buf,2)))
155 v4l_info(i2c_client_s,"i2c i/o error MBAR: rc == %d (should be 2)\n",rc);
157 v4l_info(i2c_client_s,"Camera Present: %s\n",
158 (buf[1+BT832_CAM_STATUS] & BT832_56_CAMERA_PRESENT) ? "yes":"no");
160 bt832_hexdump(i2c_client_s,buf);
161 kfree(buf);
162 return 1;
167 static int bt832_attach(struct i2c_adapter *adap, int addr, int kind)
169 struct bt832 *t;
171 client_template.adapter = adap;
172 client_template.addr = addr;
174 if (NULL == (t = kzalloc(sizeof(*t), GFP_KERNEL)))
175 return -ENOMEM;
176 t->client = client_template;
177 i2c_set_clientdata(&t->client, t);
178 i2c_attach_client(&t->client);
180 v4l_info(&t->client,"chip found @ 0x%x\n", addr<<1);
183 if(! bt832_init(&t->client)) {
184 bt832_detach(&t->client);
185 return -1;
188 return 0;
191 static int bt832_probe(struct i2c_adapter *adap)
193 if (adap->class & I2C_CLASS_TV_ANALOG)
194 return i2c_probe(adap, &addr_data, bt832_attach);
195 return 0;
198 static int bt832_detach(struct i2c_client *client)
200 struct bt832 *t = i2c_get_clientdata(client);
202 v4l_info(&t->client,"dettach\n");
203 i2c_detach_client(client);
204 kfree(t);
205 return 0;
208 static int
209 bt832_command(struct i2c_client *client, unsigned int cmd, void *arg)
211 struct bt832 *t = i2c_get_clientdata(client);
213 if (debug>1)
214 v4l_i2c_print_ioctl(&t->client,cmd);
216 switch (cmd) {
217 case BT832_HEXDUMP: {
218 unsigned char *buf;
219 buf = kmalloc(65, GFP_KERNEL);
220 if (!buf) {
221 v4l_err(&t->client,
222 "Unable to allocate memory\n");
223 break;
225 bt832_hexdump(&t->client,buf);
226 kfree(buf);
228 break;
229 case BT832_REATTACH:
230 v4l_info(&t->client,"re-attach\n");
231 i2c_del_driver(&driver);
232 i2c_add_driver(&driver);
233 break;
235 return 0;
238 /* ----------------------------------------------------------------------- */
240 static struct i2c_driver driver = {
241 .driver = {
242 .name = "bt832",
244 .id = 0, /* FIXME */
245 .attach_adapter = bt832_probe,
246 .detach_client = bt832_detach,
247 .command = bt832_command,
249 static struct i2c_client client_template =
251 .name = "bt832",
252 .driver = &driver,
256 static int __init bt832_init_module(void)
258 return i2c_add_driver(&driver);
261 static void __exit bt832_cleanup_module(void)
263 i2c_del_driver(&driver);
266 module_init(bt832_init_module);
267 module_exit(bt832_cleanup_module);
270 * Overrides for Emacs so that we follow Linus's tabbing style.
271 * ---------------------------------------------------------------------------
272 * Local variables:
273 * c-basic-offset: 8
274 * End: