2 bttv-i2c.c -- all the i2c code is here
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
7 & Marcus Metzler (mocm@thp.uni-koeln.de)
8 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
33 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
34 static struct i2c_adapter bttv_i2c_adap_sw_template;
35 static struct i2c_adapter bttv_i2c_adap_hw_template;
36 static struct i2c_client bttv_i2c_client_template;
39 static void bttv_inc_use(struct i2c_adapter *adap);
40 static void bttv_dec_use(struct i2c_adapter *adap);
42 static int attach_inform(struct i2c_client *client);
44 static int i2c_debug = 0;
45 static int i2c_hw = 0;
46 MODULE_PARM(i2c_debug,"i");
47 MODULE_PARM(i2c_hw,"i");
49 /* ----------------------------------------------------------------------- */
50 /* I2C functions - bitbanging adapter (software i2c) */
52 void bttv_bit_setscl(void *data, int state)
54 struct bttv *btv = (struct bttv*)data;
57 btv->i2c_state |= 0x02;
59 btv->i2c_state &= ~0x02;
60 btwrite(btv->i2c_state, BT848_I2C);
64 void bttv_bit_setsda(void *data, int state)
66 struct bttv *btv = (struct bttv*)data;
69 btv->i2c_state |= 0x01;
71 btv->i2c_state &= ~0x01;
72 btwrite(btv->i2c_state, BT848_I2C);
76 static int bttv_bit_getscl(void *data)
78 struct bttv *btv = (struct bttv*)data;
81 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
85 static int bttv_bit_getsda(void *data)
87 struct bttv *btv = (struct bttv*)data;
90 state = btread(BT848_I2C) & 0x01;
94 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
95 .setsda = bttv_bit_setsda,
96 .setscl = bttv_bit_setscl,
97 .getsda = bttv_bit_getsda,
98 .getscl = bttv_bit_getscl,
104 static struct i2c_adapter bttv_i2c_adap_sw_template = {
106 .owner = THIS_MODULE,
108 .inc_use = bttv_inc_use,
109 .dec_use = bttv_dec_use,
111 #ifdef I2C_CLASS_TV_ANALOG
112 .class = I2C_CLASS_TV_ANALOG,
114 I2C_DEVNAME("bt848"),
115 .id = I2C_HW_B_BT848,
116 .client_register = attach_inform,
119 /* ----------------------------------------------------------------------- */
120 /* I2C functions - hardware i2c */
122 static int algo_control(struct i2c_adapter *adapter,
123 unsigned int cmd, unsigned long arg)
128 static u32 functionality(struct i2c_adapter *adap)
130 return I2C_FUNC_SMBUS_EMUL;
134 bttv_i2c_wait_done(struct bttv *btv)
136 DECLARE_WAITQUEUE(wait, current);
139 add_wait_queue(&btv->i2c_queue, &wait);
140 set_current_state(TASK_INTERRUPTIBLE);
141 if (0 == btv->i2c_done)
142 schedule_timeout(HZ/50+1);
143 set_current_state(TASK_RUNNING);
144 remove_wait_queue(&btv->i2c_queue, &wait);
146 if (0 == btv->i2c_done)
149 if (btv->i2c_done & BT848_INT_RACK)
155 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
156 BT848_I2C_SCL | BT848_I2C_SDA)
159 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
168 /* start, address + first byte */
169 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
170 if (msg->len > 1 || !last)
171 xmit |= BT878_I2C_NOSTOP;
172 btwrite(xmit, BT848_I2C);
173 retval = bttv_i2c_wait_done(btv);
179 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
180 if (!(xmit & BT878_I2C_NOSTOP))
184 for (cnt = 1; cnt < msg->len; cnt++ ) {
185 /* following bytes */
186 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
187 if (cnt < msg->len-1 || !last)
188 xmit |= BT878_I2C_NOSTOP;
189 btwrite(xmit, BT848_I2C);
190 retval = bttv_i2c_wait_done(btv);
196 printk(" %02x", msg->buf[cnt]);
197 if (!(xmit & BT878_I2C_NOSTOP))
207 printk(" ERR: %d\n",retval);
212 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
218 for(cnt = 0; cnt < msg->len; cnt++) {
219 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
220 if (cnt < msg->len-1)
221 xmit |= BT848_I2C_W3B;
222 if (cnt < msg->len-1 || !last)
223 xmit |= BT878_I2C_NOSTOP;
225 xmit |= BT878_I2C_NOSTART;
226 btwrite(xmit, BT848_I2C);
227 retval = bttv_i2c_wait_done(btv);
232 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
234 if (!(xmit & BT878_I2C_NOSTART))
235 printk(" <R %02x", (msg->addr << 1) +1);
236 printk(" =%02x", msg->buf[cnt]);
237 if (!(xmit & BT878_I2C_NOSTOP))
247 printk(" ERR: %d\n",retval);
251 int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
253 struct bttv *btv = i2c_get_adapdata(i2c_adap);
259 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
260 for (i = 0 ; i < num; i++) {
261 if (msgs[i].flags & I2C_M_RD) {
263 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
268 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
279 static struct i2c_algorithm bttv_algo = {
281 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
282 .master_xfer = bttv_i2c_xfer,
283 .algo_control = algo_control,
284 .functionality = functionality,
287 static struct i2c_adapter bttv_i2c_adap_hw_template = {
289 .owner = THIS_MODULE,
291 .inc_use = bttv_inc_use,
292 .dec_use = bttv_dec_use,
294 #ifdef I2C_CLASS_TV_ANALOG
295 .class = I2C_CLASS_TV_ANALOG,
297 I2C_DEVNAME("bt878"),
298 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
300 .client_register = attach_inform,
303 /* ----------------------------------------------------------------------- */
304 /* I2C functions - common stuff */
307 static void bttv_inc_use(struct i2c_adapter *adap)
312 static void bttv_dec_use(struct i2c_adapter *adap)
318 static int attach_inform(struct i2c_client *client)
320 struct bttv *btv = i2c_get_adapdata(client->adapter);
322 if (btv->tuner_type != UNSET)
323 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
324 if (btv->pinnacle_id != UNSET)
325 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
329 printk("bttv%d: i2c attach [client=%s]\n",
330 btv->c.nr, i2c_clientname(client));
334 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
336 if (0 != btv->i2c_rc)
338 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
341 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
343 if (card >= bttv_num)
345 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
348 static struct i2c_client bttv_i2c_client_template = {
349 I2C_DEVNAME("bttv internal"),
355 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
357 unsigned char buffer = 0;
359 if (0 != btv->i2c_rc)
361 if (bttv_verbose && NULL != probe_for)
362 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
363 btv->c.nr,probe_for,addr);
364 btv->i2c_client.addr = addr >> 1;
365 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
366 if (NULL != probe_for) {
368 printk("not found\n");
370 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
374 if (bttv_verbose && NULL != probe_for)
380 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
381 unsigned char b2, int both)
383 unsigned char buffer[2];
384 int bytes = both ? 2 : 1;
386 if (0 != btv->i2c_rc)
388 btv->i2c_client.addr = addr >> 1;
391 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
396 /* read EEPROM content */
397 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
401 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
402 printk(KERN_WARNING "bttv: readee error\n");
405 btv->i2c_client.addr = addr >> 1;
406 for (i=0; i<256; i+=16) {
407 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
408 printk(KERN_WARNING "bttv: readee error\n");
414 /* init + register i2c algo-bit adapter */
415 int __devinit init_bttv_i2c(struct bttv *btv)
417 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
418 sizeof(bttv_i2c_client_template));
422 if (btv->use_i2c_hw) {
424 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
425 sizeof(bttv_i2c_adap_hw_template));
428 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
429 sizeof(bttv_i2c_adap_sw_template));
430 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
431 sizeof(bttv_i2c_algo_bit_template));
432 btv->i2c_algo.data = btv;
433 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
436 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
437 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
438 "bt%d #%d [%s]", btv->id, btv->c.nr,
439 btv->use_i2c_hw ? "hw" : "sw");
441 i2c_set_adapdata(&btv->c.i2c_adap, btv);
442 btv->i2c_client.adapter = &btv->c.i2c_adap;
444 if (btv->use_i2c_hw) {
445 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
447 bttv_bit_setscl(btv,1);
448 bttv_bit_setsda(btv,1);
449 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
454 int __devexit fini_bttv_i2c(struct bttv *btv)
456 if (0 != btv->i2c_rc)
459 if (btv->use_i2c_hw) {
460 return i2c_del_adapter(&btv->c.i2c_adap);
462 return i2c_bit_del_bus(&btv->c.i2c_adap);