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);
43 static int detach_inform(struct i2c_client *client);
45 static int i2c_debug = 0;
46 static int i2c_hw = 0;
47 MODULE_PARM(i2c_debug,"i");
48 MODULE_PARM(i2c_hw,"i");
50 /* ----------------------------------------------------------------------- */
51 /* I2C functions - bitbanging adapter (software i2c) */
53 void bttv_bit_setscl(void *data, int state)
55 struct bttv *btv = (struct bttv*)data;
58 btv->i2c_state |= 0x02;
60 btv->i2c_state &= ~0x02;
61 btwrite(btv->i2c_state, BT848_I2C);
65 void bttv_bit_setsda(void *data, int state)
67 struct bttv *btv = (struct bttv*)data;
70 btv->i2c_state |= 0x01;
72 btv->i2c_state &= ~0x01;
73 btwrite(btv->i2c_state, BT848_I2C);
77 static int bttv_bit_getscl(void *data)
79 struct bttv *btv = (struct bttv*)data;
82 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
86 static int bttv_bit_getsda(void *data)
88 struct bttv *btv = (struct bttv*)data;
91 state = btread(BT848_I2C) & 0x01;
95 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
96 .setsda = bttv_bit_setsda,
97 .setscl = bttv_bit_setscl,
98 .getsda = bttv_bit_getsda,
99 .getscl = bttv_bit_getscl,
105 static struct i2c_adapter bttv_i2c_adap_sw_template = {
107 .owner = THIS_MODULE,
109 .inc_use = bttv_inc_use,
110 .dec_use = bttv_dec_use,
112 #ifdef I2C_CLASS_TV_ANALOG
113 .class = I2C_CLASS_TV_ANALOG,
115 I2C_DEVNAME("bt848"),
116 .id = I2C_HW_B_BT848,
117 .client_register = attach_inform,
118 .client_unregister = detach_inform,
121 /* ----------------------------------------------------------------------- */
122 /* I2C functions - hardware i2c */
124 static int algo_control(struct i2c_adapter *adapter,
125 unsigned int cmd, unsigned long arg)
130 static u32 functionality(struct i2c_adapter *adap)
132 return I2C_FUNC_SMBUS_EMUL;
136 bttv_i2c_wait_done(struct bttv *btv)
138 DECLARE_WAITQUEUE(wait, current);
141 add_wait_queue(&btv->i2c_queue, &wait);
142 set_current_state(TASK_INTERRUPTIBLE);
143 if (0 == btv->i2c_done)
144 schedule_timeout(HZ/50+1);
145 set_current_state(TASK_RUNNING);
146 remove_wait_queue(&btv->i2c_queue, &wait);
148 if (0 == btv->i2c_done)
151 if (btv->i2c_done & BT848_INT_RACK)
157 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
158 BT848_I2C_SCL | BT848_I2C_SDA)
161 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
170 /* start, address + first byte */
171 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
172 if (msg->len > 1 || !last)
173 xmit |= BT878_I2C_NOSTOP;
174 btwrite(xmit, BT848_I2C);
175 retval = bttv_i2c_wait_done(btv);
181 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
182 if (!(xmit & BT878_I2C_NOSTOP))
186 for (cnt = 1; cnt < msg->len; cnt++ ) {
187 /* following bytes */
188 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
189 if (cnt < msg->len-1 || !last)
190 xmit |= BT878_I2C_NOSTOP;
191 btwrite(xmit, BT848_I2C);
192 retval = bttv_i2c_wait_done(btv);
198 printk(" %02x", msg->buf[cnt]);
199 if (!(xmit & BT878_I2C_NOSTOP))
209 printk(" ERR: %d\n",retval);
214 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
220 for(cnt = 0; cnt < msg->len; cnt++) {
221 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
222 if (cnt < msg->len-1)
223 xmit |= BT848_I2C_W3B;
224 if (cnt < msg->len-1 || !last)
225 xmit |= BT878_I2C_NOSTOP;
227 xmit |= BT878_I2C_NOSTART;
228 btwrite(xmit, BT848_I2C);
229 retval = bttv_i2c_wait_done(btv);
234 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
236 if (!(xmit & BT878_I2C_NOSTART))
237 printk(" <R %02x", (msg->addr << 1) +1);
238 printk(" =%02x", msg->buf[cnt]);
239 if (!(xmit & BT878_I2C_NOSTOP))
249 printk(" ERR: %d\n",retval);
253 int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
255 struct bttv *btv = i2c_get_adapdata(i2c_adap);
261 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
262 for (i = 0 ; i < num; i++) {
263 if (msgs[i].flags & I2C_M_RD) {
265 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
270 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
281 static struct i2c_algorithm bttv_algo = {
283 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
284 .master_xfer = bttv_i2c_xfer,
285 .algo_control = algo_control,
286 .functionality = functionality,
289 static struct i2c_adapter bttv_i2c_adap_hw_template = {
291 .owner = THIS_MODULE,
293 .inc_use = bttv_inc_use,
294 .dec_use = bttv_dec_use,
296 #ifdef I2C_CLASS_TV_ANALOG
297 .class = I2C_CLASS_TV_ANALOG,
299 I2C_DEVNAME("bt878"),
300 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
302 .client_register = attach_inform,
303 .client_unregister = detach_inform,
306 /* ----------------------------------------------------------------------- */
307 /* I2C functions - common stuff */
310 static void bttv_inc_use(struct i2c_adapter *adap)
315 static void bttv_dec_use(struct i2c_adapter *adap)
321 static int attach_inform(struct i2c_client *client)
323 struct bttv *btv = i2c_get_adapdata(client->adapter);
325 if (btv->tuner_type != UNSET)
326 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
327 if (btv->pinnacle_id != UNSET)
328 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
330 bttv_i2c_info(&btv->c, client, 1);
333 printk("bttv%d: i2c attach [client=%s]\n",
334 btv->c.nr, i2c_clientname(client));
338 static int detach_inform(struct i2c_client *client)
340 struct bttv *btv = i2c_get_adapdata(client->adapter);
342 bttv_i2c_info(&btv->c, client, 0);
346 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
348 if (0 != btv->i2c_rc)
350 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
353 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
355 if (card >= bttv_num)
357 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
360 static struct i2c_client bttv_i2c_client_template = {
361 I2C_DEVNAME("bttv internal"),
367 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
369 unsigned char buffer = 0;
371 if (0 != btv->i2c_rc)
373 if (bttv_verbose && NULL != probe_for)
374 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
375 btv->c.nr,probe_for,addr);
376 btv->i2c_client.addr = addr >> 1;
377 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
378 if (NULL != probe_for) {
380 printk("not found\n");
382 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
386 if (bttv_verbose && NULL != probe_for)
392 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
393 unsigned char b2, int both)
395 unsigned char buffer[2];
396 int bytes = both ? 2 : 1;
398 if (0 != btv->i2c_rc)
400 btv->i2c_client.addr = addr >> 1;
403 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
408 /* read EEPROM content */
409 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
413 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
414 printk(KERN_WARNING "bttv: readee error\n");
417 btv->i2c_client.addr = addr >> 1;
418 for (i=0; i<256; i+=16) {
419 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
420 printk(KERN_WARNING "bttv: readee error\n");
426 /* init + register i2c algo-bit adapter */
427 int __devinit init_bttv_i2c(struct bttv *btv)
429 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
430 sizeof(bttv_i2c_client_template));
434 if (btv->use_i2c_hw) {
436 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
437 sizeof(bttv_i2c_adap_hw_template));
440 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
441 sizeof(bttv_i2c_adap_sw_template));
442 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
443 sizeof(bttv_i2c_algo_bit_template));
444 btv->i2c_algo.data = btv;
445 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
448 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
449 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
450 "bt%d #%d [%s]", btv->id, btv->c.nr,
451 btv->use_i2c_hw ? "hw" : "sw");
453 i2c_set_adapdata(&btv->c.i2c_adap, btv);
454 btv->i2c_client.adapter = &btv->c.i2c_adap;
456 if (btv->use_i2c_hw) {
457 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
459 bttv_bit_setscl(btv,1);
460 bttv_bit_setsda(btv,1);
461 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
466 int __devexit fini_bttv_i2c(struct bttv *btv)
468 if (0 != btv->i2c_rc)
471 if (btv->use_i2c_hw) {
472 return i2c_del_adapter(&btv->c.i2c_adap);
474 return i2c_bit_del_bus(&btv->c.i2c_adap);