2 $Id: bttv-i2c.c,v 1.13 2004/11/07 13:17:15 kraxel Exp $
4 bttv-i2c.c -- all the i2c code is here
6 bttv - Bt848 frame grabber driver
8 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
9 & Marcus Metzler (mocm@thp.uni-koeln.de)
10 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
35 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
36 static struct i2c_adapter bttv_i2c_adap_sw_template;
37 static struct i2c_adapter bttv_i2c_adap_hw_template;
38 static struct i2c_client bttv_i2c_client_template;
40 static int attach_inform(struct i2c_client *client);
41 static int detach_inform(struct i2c_client *client);
43 static int i2c_debug = 0;
44 static int i2c_hw = 0;
45 static int i2c_scan = 0;
46 module_param(i2c_debug, int, 0644);
47 module_param(i2c_hw, int, 0444);
48 module_param(i2c_scan, int, 0444);
49 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
51 /* ----------------------------------------------------------------------- */
52 /* I2C functions - bitbanging adapter (software i2c) */
54 void bttv_bit_setscl(void *data, int state)
56 struct bttv *btv = (struct bttv*)data;
59 btv->i2c_state |= 0x02;
61 btv->i2c_state &= ~0x02;
62 btwrite(btv->i2c_state, BT848_I2C);
66 void bttv_bit_setsda(void *data, int state)
68 struct bttv *btv = (struct bttv*)data;
71 btv->i2c_state |= 0x01;
73 btv->i2c_state &= ~0x01;
74 btwrite(btv->i2c_state, BT848_I2C);
78 static int bttv_bit_getscl(void *data)
80 struct bttv *btv = (struct bttv*)data;
83 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
87 static int bttv_bit_getsda(void *data)
89 struct bttv *btv = (struct bttv*)data;
92 state = btread(BT848_I2C) & 0x01;
96 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
97 .setsda = bttv_bit_setsda,
98 .setscl = bttv_bit_setscl,
99 .getsda = bttv_bit_getsda,
100 .getscl = bttv_bit_getscl,
106 static struct i2c_adapter bttv_i2c_adap_sw_template = {
107 .owner = THIS_MODULE,
108 #ifdef I2C_CLASS_TV_ANALOG
109 .class = I2C_CLASS_TV_ANALOG,
111 I2C_DEVNAME("bt848"),
112 .id = I2C_HW_B_BT848,
113 .client_register = attach_inform,
114 .client_unregister = detach_inform,
117 /* ----------------------------------------------------------------------- */
118 /* I2C functions - hardware i2c */
120 static int algo_control(struct i2c_adapter *adapter,
121 unsigned int cmd, unsigned long arg)
126 static u32 functionality(struct i2c_adapter *adap)
128 return I2C_FUNC_SMBUS_EMUL;
132 bttv_i2c_wait_done(struct bttv *btv)
134 DECLARE_WAITQUEUE(wait, current);
137 add_wait_queue(&btv->i2c_queue, &wait);
138 if (0 == btv->i2c_done)
139 msleep_interruptible(20);
140 remove_wait_queue(&btv->i2c_queue, &wait);
142 if (0 == btv->i2c_done)
145 if (btv->i2c_done & BT848_INT_RACK)
151 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
152 BT848_I2C_SCL | BT848_I2C_SDA)
155 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
164 /* start, address + first byte */
165 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
166 if (msg->len > 1 || !last)
167 xmit |= BT878_I2C_NOSTOP;
168 btwrite(xmit, BT848_I2C);
169 retval = bttv_i2c_wait_done(btv);
175 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
176 if (!(xmit & BT878_I2C_NOSTOP))
180 for (cnt = 1; cnt < msg->len; cnt++ ) {
181 /* following bytes */
182 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
183 if (cnt < msg->len-1 || !last)
184 xmit |= BT878_I2C_NOSTOP;
185 btwrite(xmit, BT848_I2C);
186 retval = bttv_i2c_wait_done(btv);
192 printk(" %02x", msg->buf[cnt]);
193 if (!(xmit & BT878_I2C_NOSTOP))
203 printk(" ERR: %d\n",retval);
208 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
214 for(cnt = 0; cnt < msg->len; cnt++) {
215 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
216 if (cnt < msg->len-1)
217 xmit |= BT848_I2C_W3B;
218 if (cnt < msg->len-1 || !last)
219 xmit |= BT878_I2C_NOSTOP;
221 xmit |= BT878_I2C_NOSTART;
222 btwrite(xmit, BT848_I2C);
223 retval = bttv_i2c_wait_done(btv);
228 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
230 if (!(xmit & BT878_I2C_NOSTART))
231 printk(" <R %02x", (msg->addr << 1) +1);
232 printk(" =%02x", msg->buf[cnt]);
233 if (!(xmit & BT878_I2C_NOSTOP))
243 printk(" ERR: %d\n",retval);
247 int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
249 struct bttv *btv = i2c_get_adapdata(i2c_adap);
255 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
256 for (i = 0 ; i < num; i++) {
257 if (msgs[i].flags & I2C_M_RD) {
259 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
264 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
275 static struct i2c_algorithm bttv_algo = {
277 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
278 .master_xfer = bttv_i2c_xfer,
279 .algo_control = algo_control,
280 .functionality = functionality,
283 static struct i2c_adapter bttv_i2c_adap_hw_template = {
284 .owner = THIS_MODULE,
285 #ifdef I2C_CLASS_TV_ANALOG
286 .class = I2C_CLASS_TV_ANALOG,
288 I2C_DEVNAME("bt878"),
289 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
291 .client_register = attach_inform,
292 .client_unregister = detach_inform,
295 /* ----------------------------------------------------------------------- */
296 /* I2C functions - common stuff */
298 static int attach_inform(struct i2c_client *client)
300 struct bttv *btv = i2c_get_adapdata(client->adapter);
302 if (btv->tuner_type != UNSET)
303 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
304 if (btv->pinnacle_id != UNSET)
305 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
307 bttv_i2c_info(&btv->c, client, 1);
310 printk("bttv%d: i2c attach [client=%s]\n",
311 btv->c.nr, i2c_clientname(client));
315 static int detach_inform(struct i2c_client *client)
317 struct bttv *btv = i2c_get_adapdata(client->adapter);
319 bttv_i2c_info(&btv->c, client, 0);
323 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
325 if (0 != btv->i2c_rc)
327 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
330 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
332 if (card >= bttv_num)
334 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
337 static struct i2c_client bttv_i2c_client_template = {
338 I2C_DEVNAME("bttv internal"),
344 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
346 unsigned char buffer = 0;
348 if (0 != btv->i2c_rc)
350 if (bttv_verbose && NULL != probe_for)
351 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
352 btv->c.nr,probe_for,addr);
353 btv->i2c_client.addr = addr >> 1;
354 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
355 if (NULL != probe_for) {
357 printk("not found\n");
359 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
363 if (bttv_verbose && NULL != probe_for)
369 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
370 unsigned char b2, int both)
372 unsigned char buffer[2];
373 int bytes = both ? 2 : 1;
375 if (0 != btv->i2c_rc)
377 btv->i2c_client.addr = addr >> 1;
380 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
385 /* read EEPROM content */
386 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
390 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
391 printk(KERN_WARNING "bttv: readee error\n");
394 btv->i2c_client.addr = addr >> 1;
395 for (i=0; i<256; i+=16) {
396 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
397 printk(KERN_WARNING "bttv: readee error\n");
403 static char *i2c_devs[128] = {
404 [ 0x30 >> 1 ] = "IR (hauppauge)",
405 [ 0x80 >> 1 ] = "msp34xx",
406 [ 0x86 >> 1 ] = "tda9887",
407 [ 0xa0 >> 1 ] = "eeprom",
408 [ 0xc0 >> 1 ] = "tuner (analog)",
409 [ 0xc2 >> 1 ] = "tuner (analog)",
412 static void do_i2c_scan(char *name, struct i2c_client *c)
417 for (i = 0; i < 128; i++) {
419 rc = i2c_master_recv(c,&buf,0);
422 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
423 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
427 /* init + register i2c algo-bit adapter */
428 int __devinit init_bttv_i2c(struct bttv *btv)
430 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
431 sizeof(bttv_i2c_client_template));
435 if (btv->use_i2c_hw) {
437 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
438 sizeof(bttv_i2c_adap_hw_template));
441 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
442 sizeof(bttv_i2c_adap_sw_template));
443 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
444 sizeof(bttv_i2c_algo_bit_template));
445 btv->i2c_algo.data = btv;
446 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
449 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
450 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
451 "bt%d #%d [%s]", btv->id, btv->c.nr,
452 btv->use_i2c_hw ? "hw" : "sw");
454 i2c_set_adapdata(&btv->c.i2c_adap, btv);
455 btv->i2c_client.adapter = &btv->c.i2c_adap;
457 #ifdef I2C_CLASS_TV_ANALOG
458 if (bttv_tvcards[btv->c.type].no_video)
459 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
460 if (bttv_tvcards[btv->c.type].has_dvb)
461 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
464 if (btv->use_i2c_hw) {
465 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
467 bttv_bit_setscl(btv,1);
468 bttv_bit_setsda(btv,1);
469 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
471 if (0 == btv->i2c_rc && i2c_scan)
472 do_i2c_scan(btv->c.name,&btv->i2c_client);
476 int __devexit fini_bttv_i2c(struct bttv *btv)
478 if (0 != btv->i2c_rc)
481 if (btv->use_i2c_hw) {
482 return i2c_del_adapter(&btv->c.i2c_adap);
484 return i2c_bit_del_bus(&btv->c.i2c_adap);