| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 9 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 34 | #include <media/audiochip.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "bttv.h" | 
 | 36 | #include "bt832.h" | 
 | 37 |  | 
 | 38 | MODULE_LICENSE("GPL"); | 
 | 39 |  | 
 | 40 | /* Addresses to scan */ | 
| Mauro Carvalho Chehab | f5bec39 | 2005-06-23 22:05:13 -0700 | [diff] [blame] | 41 | static unsigned short normal_i2c[] = { I2C_BT832_ALT1>>1, I2C_BT832_ALT2>>1, | 
 | 42 | 				       I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | I2C_CLIENT_INSMOD; | 
 | 44 |  | 
 | 45 | /* ---------------------------------------------------------------------- */ | 
 | 46 |  | 
 | 47 | #define dprintk     if (debug) printk | 
 | 48 |  | 
 | 49 | static int bt832_detach(struct i2c_client *client); | 
 | 50 |  | 
 | 51 |  | 
 | 52 | static struct i2c_driver driver; | 
 | 53 | static struct i2c_client client_template; | 
 | 54 |  | 
 | 55 | struct bt832 { | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 56 | 	struct i2c_client client; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; | 
 | 58 |  | 
 | 59 | int bt832_hexdump(struct i2c_client *i2c_client_s, unsigned char *buf) | 
 | 60 | { | 
 | 61 | 	int i,rc; | 
 | 62 | 	buf[0]=0x80; // start at register 0 with auto-increment | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 63 | 	if (1 != (rc = i2c_master_send(i2c_client_s,buf,1))) | 
 | 64 | 		printk("bt832: i2c i/o error: rc == %d (should be 1)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 66 | 	for(i=0;i<65;i++) | 
 | 67 | 		buf[i]=0; | 
 | 68 | 	if (65 != (rc=i2c_master_recv(i2c_client_s,buf,65))) | 
 | 69 | 		printk("bt832: i2c i/o error: rc == %d (should be 65)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 71 | 	// Note: On READ the first byte is the current index | 
 | 72 | 	//  (e.g. 0x80, what we just wrote) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 74 | 	if(1) { | 
 | 75 | 		int i; | 
 | 76 | 		printk("BT832 hexdump:\n"); | 
 | 77 | 		for(i=1;i<65;i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 			if(i!=1) { | 
 | 79 | 			  if(((i-1)%8)==0) printk(" "); | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 80 | 			  if(((i-1)%16)==0) printk("\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 			} | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 82 | 			printk(" %02x",buf[i]); | 
 | 83 | 		} | 
 | 84 | 		printk("\n"); | 
 | 85 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 	return 0; | 
 | 87 | } | 
 | 88 |  | 
 | 89 | // Return: 1 (is a bt832), 0 (No bt832 here) | 
 | 90 | int bt832_init(struct i2c_client *i2c_client_s) | 
 | 91 | { | 
 | 92 | 	unsigned char *buf; | 
 | 93 | 	int rc; | 
 | 94 |  | 
 | 95 | 	buf=kmalloc(65,GFP_KERNEL); | 
 | 96 | 	bt832_hexdump(i2c_client_s,buf); | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 97 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | 	if(buf[0x40] != 0x31) { | 
 | 99 | 		printk("bt832: this i2c chip is no bt832 (id=%02x). Detaching.\n",buf[0x40]); | 
 | 100 | 		kfree(buf); | 
 | 101 | 		return 0; | 
 | 102 | 	} | 
 | 103 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 104 | 	printk("Write 0 tp VPSTATUS\n"); | 
 | 105 | 	buf[0]=BT832_VP_STATUS; // Reg.52 | 
 | 106 | 	buf[1]= 0x00; | 
 | 107 | 	if (2 != (rc = i2c_master_send(i2c_client_s,buf,2))) | 
 | 108 | 		printk("bt832: i2c i/o error VPS: rc == %d (should be 2)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 110 | 	bt832_hexdump(i2c_client_s,buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
 | 112 |  | 
 | 113 | 	// Leave low power mode: | 
 | 114 | 	printk("Bt832: leave low power mode.\n"); | 
 | 115 | 	buf[0]=BT832_CAM_SETUP0; //0x39 57 | 
 | 116 | 	buf[1]=0x08; | 
 | 117 | 	if (2 != (rc = i2c_master_send(i2c_client_s,buf,2))) | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 118 | 		printk("bt832: i2c i/o error LLPM: rc == %d (should be 2)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 120 | 	bt832_hexdump(i2c_client_s,buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 |  | 
 | 122 | 	printk("Write 0 tp VPSTATUS\n"); | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 123 | 	buf[0]=BT832_VP_STATUS; // Reg.52 | 
 | 124 | 	buf[1]= 0x00; | 
 | 125 | 	if (2 != (rc = i2c_master_send(i2c_client_s,buf,2))) | 
 | 126 | 		printk("bt832: i2c i/o error VPS: rc == %d (should be 2)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 128 | 	bt832_hexdump(i2c_client_s,buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 |  | 
 | 130 |  | 
 | 131 | 	// Enable Output | 
 | 132 | 	printk("Enable Output\n"); | 
 | 133 | 	buf[0]=BT832_VP_CONTROL1; // Reg.40 | 
 | 134 | 	buf[1]= 0x27 & (~0x01); // Default | !skip | 
 | 135 | 	if (2 != (rc = i2c_master_send(i2c_client_s,buf,2))) | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 136 | 		printk("bt832: i2c i/o error EO: rc == %d (should be 2)\n",rc); | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 137 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 138 | 	bt832_hexdump(i2c_client_s,buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | 	// for testing (even works when no camera attached) | 
 | 142 | 	printk("bt832: *** Generate NTSC M Bars *****\n"); | 
 | 143 | 	buf[0]=BT832_VP_TESTCONTROL0; // Reg. 42 | 
 | 144 | 	buf[1]=3; // Generate NTSC System M bars, Generate Frame timing internally | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 145 | 	if (2 != (rc = i2c_master_send(i2c_client_s,buf,2))) | 
 | 146 | 		printk("bt832: i2c i/o error MBAR: rc == %d (should be 2)\n",rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |  | 
 | 148 | 	printk("Bt832: Camera Present: %s\n", | 
 | 149 | 		(buf[1+BT832_CAM_STATUS] & BT832_56_CAMERA_PRESENT) ? "yes":"no"); | 
 | 150 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 151 | 	bt832_hexdump(i2c_client_s,buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | 	kfree(buf); | 
 | 153 | 	return 1; | 
 | 154 | } | 
 | 155 |  | 
 | 156 |  | 
 | 157 |  | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 158 | static int bt832_attach(struct i2c_adapter *adap, int addr, int kind) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { | 
 | 160 | 	struct bt832 *t; | 
 | 161 |  | 
 | 162 | 	printk("bt832_attach\n"); | 
 | 163 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 164 | 	client_template.adapter = adap; | 
 | 165 | 	client_template.addr    = addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 167 | 	printk("bt832: chip found @ 0x%x\n", addr<<1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 169 | 	if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL))) | 
 | 170 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | 	memset(t,0,sizeof(*t)); | 
 | 172 | 	t->client = client_template; | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 173 | 	i2c_set_clientdata(&t->client, t); | 
 | 174 | 	i2c_attach_client(&t->client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 |  | 
 | 176 | 	if(! bt832_init(&t->client)) { | 
 | 177 | 		bt832_detach(&t->client); | 
 | 178 | 		return -1; | 
 | 179 | 	} | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 180 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | 	return 0; | 
 | 182 | } | 
 | 183 |  | 
 | 184 | static int bt832_probe(struct i2c_adapter *adap) | 
 | 185 | { | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 186 | #ifdef I2C_CLASS_TV_ANALOG | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | 	if (adap->class & I2C_CLASS_TV_ANALOG) | 
 | 188 | 		return i2c_probe(adap, &addr_data, bt832_attach); | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 189 | #else | 
| Jean Delvare | c7a4653 | 2005-08-11 23:41:56 +0200 | [diff] [blame] | 190 | 	if (adap->id == I2C_HW_B_BT848) | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 191 | 		return i2c_probe(adap, &addr_data, bt832_attach); | 
 | 192 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | 	return 0; | 
 | 194 | } | 
 | 195 |  | 
 | 196 | static int bt832_detach(struct i2c_client *client) | 
 | 197 | { | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 198 | 	struct bt832 *t = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
 | 200 | 	printk("bt832: detach.\n"); | 
 | 201 | 	i2c_detach_client(client); | 
 | 202 | 	kfree(t); | 
 | 203 | 	return 0; | 
 | 204 | } | 
 | 205 |  | 
 | 206 | static int | 
 | 207 | bt832_command(struct i2c_client *client, unsigned int cmd, void *arg) | 
 | 208 | { | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 209 | 	struct bt832 *t = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 |  | 
 | 211 | 	printk("bt832: command %x\n",cmd); | 
 | 212 |  | 
| Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 213 | 	switch (cmd) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 			printk("bt832: 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 | 	.owner          = THIS_MODULE, | 
| Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 234 | 	.name           = "i2c bt832 driver", | 
 | 235 | 	.id             = -1, /* FIXME */ | 
 | 236 | 	.flags          = I2C_DF_NOTIFY, | 
 | 237 | 	.attach_adapter = bt832_probe, | 
 | 238 | 	.detach_client  = bt832_detach, | 
 | 239 | 	.command        = bt832_command, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | }; | 
 | 241 | static struct i2c_client client_template = | 
 | 242 | { | 
| Jean Delvare | fae91e7 | 2005-08-15 19:57:04 +0200 | [diff] [blame] | 243 | 	.name       = "bt832", | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 244 | 	.flags      = I2C_CLIENT_ALLOW_USE, | 
| Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 245 | 	.driver     = &driver, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | }; | 
 | 247 |  | 
 | 248 |  | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 249 | static int __init bt832_init_module(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 251 | 	return i2c_add_driver(&driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } | 
 | 253 |  | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 254 | static void __exit bt832_cleanup_module(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { | 
 | 256 | 	i2c_del_driver(&driver); | 
 | 257 | } | 
 | 258 |  | 
 | 259 | module_init(bt832_init_module); | 
 | 260 | module_exit(bt832_cleanup_module); | 
 | 261 |  | 
| Mauro Carvalho Chehab | 56fc08c | 2005-06-23 22:05:07 -0700 | [diff] [blame] | 262 | /* | 
 | 263 |  * Overrides for Emacs so that we follow Linus's tabbing style. | 
 | 264 |  * --------------------------------------------------------------------------- | 
 | 265 |  * Local variables: | 
 | 266 |  * c-basic-offset: 8 | 
 | 267 |  * End: | 
 | 268 |  */ |