3 bttv-i2c.c -- all the i2c code is here
5 bttv - Bt848 frame grabber driver
7 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
8 & Marcus Metzler (mocm@thp.uni-koeln.de)
9 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
33 #include <linux/jiffies.h>
36 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template
;
37 static struct i2c_adapter bttv_i2c_adap_sw_template
;
38 static struct i2c_adapter bttv_i2c_adap_hw_template
;
39 static struct i2c_client bttv_i2c_client_template
;
41 static int attach_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 static 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 static 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 .class = I2C_CLASS_TV_ANALOG
,
110 .id
= I2C_HW_B_BT848
,
111 .client_register
= attach_inform
,
114 /* ----------------------------------------------------------------------- */
115 /* I2C functions - hardware i2c */
117 static int algo_control(struct i2c_adapter
*adapter
,
118 unsigned int cmd
, unsigned long arg
)
123 static u32
functionality(struct i2c_adapter
*adap
)
125 return I2C_FUNC_SMBUS_EMUL
;
129 bttv_i2c_wait_done(struct bttv
*btv
)
134 if (wait_event_interruptible_timeout(btv
->i2c_queue
,
135 btv
->i2c_done
, msecs_to_jiffies(85)) == -ERESTARTSYS
)
139 if (btv
->i2c_done
& BT848_INT_RACK
)
145 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
146 BT848_I2C_SCL | BT848_I2C_SDA)
149 bttv_i2c_sendbytes(struct bttv
*btv
, const struct i2c_msg
*msg
, int last
)
158 /* start, address + first byte */
159 xmit
= (msg
->addr
<< 25) | (msg
->buf
[0] << 16) | I2C_HW
;
160 if (msg
->len
> 1 || !last
)
161 xmit
|= BT878_I2C_NOSTOP
;
162 btwrite(xmit
, BT848_I2C
);
163 retval
= bttv_i2c_wait_done(btv
);
169 printk(" <W %02x %02x", msg
->addr
<< 1, msg
->buf
[0]);
170 if (!(xmit
& BT878_I2C_NOSTOP
))
174 for (cnt
= 1; cnt
< msg
->len
; cnt
++ ) {
175 /* following bytes */
176 xmit
= (msg
->buf
[cnt
] << 24) | I2C_HW
| BT878_I2C_NOSTART
;
177 if (cnt
< msg
->len
-1 || !last
)
178 xmit
|= BT878_I2C_NOSTOP
;
179 btwrite(xmit
, BT848_I2C
);
180 retval
= bttv_i2c_wait_done(btv
);
186 printk(" %02x", msg
->buf
[cnt
]);
187 if (!(xmit
& BT878_I2C_NOSTOP
))
197 printk(" ERR: %d\n",retval
);
202 bttv_i2c_readbytes(struct bttv
*btv
, const struct i2c_msg
*msg
, int last
)
208 for(cnt
= 0; cnt
< msg
->len
; cnt
++) {
209 xmit
= (msg
->addr
<< 25) | (1 << 24) | I2C_HW
;
210 if (cnt
< msg
->len
-1)
211 xmit
|= BT848_I2C_W3B
;
212 if (cnt
< msg
->len
-1 || !last
)
213 xmit
|= BT878_I2C_NOSTOP
;
215 xmit
|= BT878_I2C_NOSTART
;
216 btwrite(xmit
, BT848_I2C
);
217 retval
= bttv_i2c_wait_done(btv
);
222 msg
->buf
[cnt
] = ((u32
)btread(BT848_I2C
) >> 8) & 0xff;
224 if (!(xmit
& BT878_I2C_NOSTART
))
225 printk(" <R %02x", (msg
->addr
<< 1) +1);
226 printk(" =%02x", msg
->buf
[cnt
]);
227 if (!(xmit
& BT878_I2C_NOSTOP
))
237 printk(" ERR: %d\n",retval
);
241 static int bttv_i2c_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg
*msgs
, int num
)
243 struct bttv
*btv
= i2c_get_adapdata(i2c_adap
);
249 btwrite(BT848_INT_I2CDONE
|BT848_INT_RACK
, BT848_INT_STAT
);
250 for (i
= 0 ; i
< num
; i
++) {
251 if (msgs
[i
].flags
& I2C_M_RD
) {
253 retval
= bttv_i2c_readbytes(btv
, &msgs
[i
], i
+1 == num
);
258 retval
= bttv_i2c_sendbytes(btv
, &msgs
[i
], i
+1 == num
);
269 static struct i2c_algorithm bttv_algo
= {
270 .master_xfer
= bttv_i2c_xfer
,
271 .algo_control
= algo_control
,
272 .functionality
= functionality
,
275 static struct i2c_adapter bttv_i2c_adap_hw_template
= {
276 .owner
= THIS_MODULE
,
277 .class = I2C_CLASS_TV_ANALOG
,
279 .id
= I2C_HW_B_BT848
/* FIXME */,
281 .client_register
= attach_inform
,
284 /* ----------------------------------------------------------------------- */
285 /* I2C functions - common stuff */
287 static int attach_inform(struct i2c_client
*client
)
289 struct bttv
*btv
= i2c_get_adapdata(client
->adapter
);
293 if (ADDR_UNSET
!= bttv_tvcards
[btv
->c
.type
].tuner_addr
)
294 addr
= bttv_tvcards
[btv
->c
.type
].tuner_addr
;
298 printk(KERN_DEBUG
"bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
299 btv
->c
.nr
, client
->driver
->driver
.name
, client
->addr
,
301 if (!client
->driver
->command
)
304 if (btv
->tuner_type
!= UNSET
) {
305 struct tuner_setup tun_setup
;
307 if ((addr
==ADDR_UNSET
) ||
308 (addr
==client
->addr
)) {
310 tun_setup
.mode_mask
= T_ANALOG_TV
| T_DIGITAL_TV
| T_RADIO
;
311 tun_setup
.type
= btv
->tuner_type
;
312 tun_setup
.addr
= addr
;
313 bttv_call_i2c_clients(btv
, TUNER_SET_TYPE_ADDR
, &tun_setup
);
321 void bttv_call_i2c_clients(struct bttv
*btv
, unsigned int cmd
, void *arg
)
323 if (0 != btv
->i2c_rc
)
325 i2c_clients_command(&btv
->c
.i2c_adap
, cmd
, arg
);
328 static struct i2c_client bttv_i2c_client_template
= {
329 .name
= "bttv internal",
334 int bttv_I2CRead(struct bttv
*btv
, unsigned char addr
, char *probe_for
)
336 unsigned char buffer
= 0;
338 if (0 != btv
->i2c_rc
)
340 if (bttv_verbose
&& NULL
!= probe_for
)
341 printk(KERN_INFO
"bttv%d: i2c: checking for %s @ 0x%02x... ",
342 btv
->c
.nr
,probe_for
,addr
);
343 btv
->i2c_client
.addr
= addr
>> 1;
344 if (1 != i2c_master_recv(&btv
->i2c_client
, &buffer
, 1)) {
345 if (NULL
!= probe_for
) {
347 printk("not found\n");
349 printk(KERN_WARNING
"bttv%d: i2c read 0x%x: error\n",
353 if (bttv_verbose
&& NULL
!= probe_for
)
359 int bttv_I2CWrite(struct bttv
*btv
, unsigned char addr
, unsigned char b1
,
360 unsigned char b2
, int both
)
362 unsigned char buffer
[2];
363 int bytes
= both
? 2 : 1;
365 if (0 != btv
->i2c_rc
)
367 btv
->i2c_client
.addr
= addr
>> 1;
370 if (bytes
!= i2c_master_send(&btv
->i2c_client
, buffer
, bytes
))
375 /* read EEPROM content */
376 void __devinit
bttv_readee(struct bttv
*btv
, unsigned char *eedata
, int addr
)
378 memset(eedata
, 0, 256);
379 if (0 != btv
->i2c_rc
)
381 btv
->i2c_client
.addr
= addr
>> 1;
382 tveeprom_read(&btv
->i2c_client
, eedata
, 256);
385 static char *i2c_devs
[128] = {
386 [ 0x1c >> 1 ] = "lgdt330x",
387 [ 0x30 >> 1 ] = "IR (hauppauge)",
388 [ 0x80 >> 1 ] = "msp34xx",
389 [ 0x86 >> 1 ] = "tda9887",
390 [ 0xa0 >> 1 ] = "eeprom",
391 [ 0xc0 >> 1 ] = "tuner (analog)",
392 [ 0xc2 >> 1 ] = "tuner (analog)",
395 static void do_i2c_scan(char *name
, struct i2c_client
*c
)
400 for (i
= 0; i
< 128; i
++) {
402 rc
= i2c_master_recv(c
,&buf
,0);
405 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
406 name
, i
<< 1, i2c_devs
[i
] ? i2c_devs
[i
] : "???");
410 /* init + register i2c algo-bit adapter */
411 int __devinit
init_bttv_i2c(struct bttv
*btv
)
413 memcpy(&btv
->i2c_client
, &bttv_i2c_client_template
,
414 sizeof(bttv_i2c_client_template
));
418 if (btv
->use_i2c_hw
) {
420 memcpy(&btv
->c
.i2c_adap
, &bttv_i2c_adap_hw_template
,
421 sizeof(bttv_i2c_adap_hw_template
));
424 memcpy(&btv
->c
.i2c_adap
, &bttv_i2c_adap_sw_template
,
425 sizeof(bttv_i2c_adap_sw_template
));
426 memcpy(&btv
->i2c_algo
, &bttv_i2c_algo_bit_template
,
427 sizeof(bttv_i2c_algo_bit_template
));
428 btv
->i2c_algo
.data
= btv
;
429 btv
->c
.i2c_adap
.algo_data
= &btv
->i2c_algo
;
432 btv
->c
.i2c_adap
.dev
.parent
= &btv
->c
.pci
->dev
;
433 snprintf(btv
->c
.i2c_adap
.name
, sizeof(btv
->c
.i2c_adap
.name
),
434 "bt%d #%d [%s]", btv
->id
, btv
->c
.nr
,
435 btv
->use_i2c_hw
? "hw" : "sw");
437 i2c_set_adapdata(&btv
->c
.i2c_adap
, btv
);
438 btv
->i2c_client
.adapter
= &btv
->c
.i2c_adap
;
440 if (bttv_tvcards
[btv
->c
.type
].no_video
)
441 btv
->c
.i2c_adap
.class &= ~I2C_CLASS_TV_ANALOG
;
442 if (bttv_tvcards
[btv
->c
.type
].has_dvb
)
443 btv
->c
.i2c_adap
.class |= I2C_CLASS_TV_DIGITAL
;
445 if (btv
->use_i2c_hw
) {
446 btv
->i2c_rc
= i2c_add_adapter(&btv
->c
.i2c_adap
);
448 bttv_bit_setscl(btv
,1);
449 bttv_bit_setsda(btv
,1);
450 btv
->i2c_rc
= i2c_bit_add_bus(&btv
->c
.i2c_adap
);
452 if (0 == btv
->i2c_rc
&& i2c_scan
)
453 do_i2c_scan(btv
->c
.name
,&btv
->i2c_client
);
457 int __devexit
fini_bttv_i2c(struct bttv
*btv
)
459 if (0 != btv
->i2c_rc
)
462 if (btv
->use_i2c_hw
) {
463 return i2c_del_adapter(&btv
->c
.i2c_adap
);
465 return i2c_bit_del_bus(&btv
->c
.i2c_adap
);