2 bttv-if.c -- interfaces to other kernel modules
3 all the i2c code is here
4 also the gpio interface exported by bttv (used by lirc)
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>
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
;
42 static void bttv_inc_use(struct i2c_adapter
*adap
);
43 static void bttv_dec_use(struct i2c_adapter
*adap
);
45 static int attach_inform(struct i2c_client
*client
);
47 EXPORT_SYMBOL(bttv_get_cardinfo
);
48 EXPORT_SYMBOL(bttv_get_pcidev
);
49 EXPORT_SYMBOL(bttv_get_id
);
50 EXPORT_SYMBOL(bttv_gpio_enable
);
51 EXPORT_SYMBOL(bttv_read_gpio
);
52 EXPORT_SYMBOL(bttv_write_gpio
);
53 EXPORT_SYMBOL(bttv_get_gpio_queue
);
54 EXPORT_SYMBOL(bttv_i2c_call
);
56 static int i2c_debug
= 0;
57 static int i2c_hw
= 0;
58 MODULE_PARM(i2c_debug
,"i");
59 MODULE_PARM(i2c_hw
,"i");
61 /* ----------------------------------------------------------------------- */
62 /* Exported functions - for other modules which want to access the */
63 /* gpio ports (IR for example) */
64 /* see bttv.h for comments */
66 int bttv_get_cardinfo(unsigned int card
, int *type
, unsigned *cardid
)
68 if (card
>= bttv_num
) {
71 *type
= bttvs
[card
].type
;
72 *cardid
= bttvs
[card
].cardid
;
76 struct pci_dev
* bttv_get_pcidev(unsigned int card
)
80 return bttvs
[card
].dev
;
83 int bttv_get_id(unsigned int card
)
85 printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
86 if (card
>= bttv_num
) {
89 return bttvs
[card
].type
;
93 int bttv_gpio_enable(unsigned int card
, unsigned long mask
, unsigned long data
)
97 if (card
>= bttv_num
) {
102 btaor(data
, ~mask
, BT848_GPIO_OUT_EN
);
104 bttv_gpio_tracking(btv
,"extern enable");
108 int bttv_read_gpio(unsigned int card
, unsigned long *data
)
112 if (card
>= bttv_num
) {
122 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
123 because we set direct input on init */
124 *data
= btread(BT848_GPIO_DATA
);
128 int bttv_write_gpio(unsigned int card
, unsigned long mask
, unsigned long data
)
132 if (card
>= bttv_num
) {
138 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
139 because direct input is set on init */
140 btaor(data
& mask
, ~mask
, BT848_GPIO_DATA
);
142 bttv_gpio_tracking(btv
,"extern write");
146 wait_queue_head_t
* bttv_get_gpio_queue(unsigned int card
)
150 if (card
>= bttv_num
) {
155 if (bttvs
[card
].shutdown
) {
162 /* ----------------------------------------------------------------------- */
163 /* I2C functions - bitbanging adapter (software i2c) */
165 void bttv_bit_setscl(void *data
, int state
)
167 struct bttv
*btv
= (struct bttv
*)data
;
170 btv
->i2c_state
|= 0x02;
172 btv
->i2c_state
&= ~0x02;
173 btwrite(btv
->i2c_state
, BT848_I2C
);
177 void bttv_bit_setsda(void *data
, int state
)
179 struct bttv
*btv
= (struct bttv
*)data
;
182 btv
->i2c_state
|= 0x01;
184 btv
->i2c_state
&= ~0x01;
185 btwrite(btv
->i2c_state
, BT848_I2C
);
189 static int bttv_bit_getscl(void *data
)
191 struct bttv
*btv
= (struct bttv
*)data
;
194 state
= btread(BT848_I2C
) & 0x02 ? 1 : 0;
198 static int bttv_bit_getsda(void *data
)
200 struct bttv
*btv
= (struct bttv
*)data
;
203 state
= btread(BT848_I2C
) & 0x01;
207 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template
= {
208 .setsda
= bttv_bit_setsda
,
209 .setscl
= bttv_bit_setscl
,
210 .getsda
= bttv_bit_getsda
,
211 .getscl
= bttv_bit_getscl
,
217 static struct i2c_adapter bttv_i2c_adap_sw_template
= {
219 .owner
= THIS_MODULE
,
221 .inc_use
= bttv_inc_use
,
222 .dec_use
= bttv_dec_use
,
224 #ifdef I2C_ADAP_CLASS_TV_ANALOG
225 .class = I2C_ADAP_CLASS_TV_ANALOG
,
227 I2C_DEVNAME("bt848"),
228 .id
= I2C_HW_B_BT848
,
229 .client_register
= attach_inform
,
232 /* ----------------------------------------------------------------------- */
233 /* I2C functions - hardware i2c */
235 static int algo_control(struct i2c_adapter
*adapter
,
236 unsigned int cmd
, unsigned long arg
)
241 static u32
functionality(struct i2c_adapter
*adap
)
243 return I2C_FUNC_SMBUS_EMUL
;
247 bttv_i2c_wait_done(struct bttv
*btv
)
252 timeout
= jiffies
+ HZ
/100 + 1; /* 10ms */
254 stat
= btread(BT848_INT_STAT
);
255 if (stat
& BT848_INT_I2CDONE
)
257 if (time_after(jiffies
,timeout
))
261 btwrite(BT848_INT_I2CDONE
|BT848_INT_RACK
, BT848_INT_STAT
);
262 return ((stat
& BT848_INT_RACK
) ? 1 : 0);
265 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
266 BT848_I2C_SCL | BT848_I2C_SDA)
269 bttv_i2c_sendbytes(struct bttv
*btv
, const struct i2c_msg
*msg
, int last
)
274 /* start, address + first byte */
275 xmit
= (msg
->addr
<< 25) | (msg
->buf
[0] << 16) | I2C_HW
;
276 if (msg
->len
> 1 || !last
)
277 xmit
|= BT878_I2C_NOSTOP
;
278 btwrite(xmit
, BT848_I2C
);
279 retval
= bttv_i2c_wait_done(btv
);
285 printk(" <W %02x %02x", msg
->addr
<< 1, msg
->buf
[0]);
286 if (!(xmit
& BT878_I2C_NOSTOP
))
290 for (cnt
= 1; cnt
< msg
->len
; cnt
++ ) {
291 /* following bytes */
292 xmit
= (msg
->buf
[cnt
] << 24) | I2C_HW
| BT878_I2C_NOSTART
;
293 if (cnt
< msg
->len
-1 || !last
)
294 xmit
|= BT878_I2C_NOSTOP
;
295 btwrite(xmit
, BT848_I2C
);
296 retval
= bttv_i2c_wait_done(btv
);
302 printk(" %02x", msg
->buf
[cnt
]);
303 if (!(xmit
& BT878_I2C_NOSTOP
))
313 printk(" ERR: %d\n",retval
);
318 bttv_i2c_readbytes(struct bttv
*btv
, const struct i2c_msg
*msg
, int last
)
324 for(cnt
= 0; cnt
< msg
->len
; cnt
++) {
325 xmit
= (msg
->addr
<< 25) | (1 << 24) | I2C_HW
;
326 if (cnt
< msg
->len
-1)
327 xmit
|= BT848_I2C_W3B
;
328 if (cnt
< msg
->len
-1 || !last
)
329 xmit
|= BT878_I2C_NOSTOP
;
331 xmit
|= BT878_I2C_NOSTART
;
332 btwrite(xmit
, BT848_I2C
);
333 retval
= bttv_i2c_wait_done(btv
);
338 msg
->buf
[cnt
] = ((u32
)btread(BT848_I2C
) >> 8) & 0xff;
340 if (!(xmit
& BT878_I2C_NOSTART
))
341 printk(" <R %02x", (msg
->addr
<< 1) +1);
342 printk(" =%02x", msg
->buf
[cnt
]);
343 if (!(xmit
& BT878_I2C_NOSTOP
))
353 printk(" ERR: %d\n",retval
);
357 int bttv_i2c_xfer(struct i2c_adapter
*i2c_adap
, struct i2c_msg msgs
[], int num
)
359 struct bttv
*btv
= i2c_get_adapdata(i2c_adap
);
365 btwrite(BT848_INT_I2CDONE
|BT848_INT_RACK
, BT848_INT_STAT
);
366 for (i
= 0 ; i
< num
; i
++) {
367 if (msgs
[i
].flags
& I2C_M_RD
) {
369 retval
= bttv_i2c_readbytes(btv
, &msgs
[i
], i
+1 == num
);
374 retval
= bttv_i2c_sendbytes(btv
, &msgs
[i
], i
+1 == num
);
385 static struct i2c_algorithm bttv_algo
= {
387 .id
= I2C_ALGO_BIT
| I2C_HW_B_BT848
/* FIXME */,
388 .master_xfer
= bttv_i2c_xfer
,
389 .algo_control
= algo_control
,
390 .functionality
= functionality
,
393 static struct i2c_adapter bttv_i2c_adap_hw_template
= {
395 .owner
= THIS_MODULE
,
397 .inc_use
= bttv_inc_use
,
398 .dec_use
= bttv_dec_use
,
400 #ifdef I2C_ADAP_CLASS_TV_ANALOG
401 .class = I2C_ADAP_CLASS_TV_ANALOG
,
403 I2C_DEVNAME("bt878"),
404 .id
= I2C_ALGO_BIT
| I2C_HW_B_BT848
/* FIXME */,
406 .client_register
= attach_inform
,
409 /* ----------------------------------------------------------------------- */
410 /* I2C functions - common stuff */
413 static void bttv_inc_use(struct i2c_adapter
*adap
)
418 static void bttv_dec_use(struct i2c_adapter
*adap
)
424 static int attach_inform(struct i2c_client
*client
)
426 struct bttv
*btv
= i2c_get_adapdata(client
->adapter
);
428 if (btv
->tuner_type
!= UNSET
)
429 bttv_call_i2c_clients(btv
,TUNER_SET_TYPE
,&btv
->tuner_type
);
430 if (btv
->pinnacle_id
!= UNSET
)
431 bttv_call_i2c_clients(btv
,AUDC_CONFIG_PINNACLE
,
435 printk("bttv%d: i2c attach [client=%s]\n",
436 btv
->nr
, i2c_clientname(client
));
440 void bttv_call_i2c_clients(struct bttv
*btv
, unsigned int cmd
, void *arg
)
442 if (0 != btv
->i2c_rc
)
444 i2c_clients_command(&btv
->i2c_adap
, cmd
, arg
);
447 void bttv_i2c_call(unsigned int card
, unsigned int cmd
, void *arg
)
449 if (card
>= bttv_num
)
451 bttv_call_i2c_clients(&bttvs
[card
], cmd
, arg
);
454 static struct i2c_client bttv_i2c_client_template
= {
455 I2C_DEVNAME("bttv internal"),
461 int bttv_I2CRead(struct bttv
*btv
, unsigned char addr
, char *probe_for
)
463 unsigned char buffer
= 0;
465 if (0 != btv
->i2c_rc
)
467 if (bttv_verbose
&& NULL
!= probe_for
)
468 printk(KERN_INFO
"bttv%d: i2c: checking for %s @ 0x%02x... ",
469 btv
->nr
,probe_for
,addr
);
470 btv
->i2c_client
.addr
= addr
>> 1;
471 if (1 != i2c_master_recv(&btv
->i2c_client
, &buffer
, 1)) {
472 if (NULL
!= probe_for
) {
474 printk("not found\n");
476 printk(KERN_WARNING
"bttv%d: i2c read 0x%x: error\n",
480 if (bttv_verbose
&& NULL
!= probe_for
)
486 int bttv_I2CWrite(struct bttv
*btv
, unsigned char addr
, unsigned char b1
,
487 unsigned char b2
, int both
)
489 unsigned char buffer
[2];
490 int bytes
= both
? 2 : 1;
492 if (0 != btv
->i2c_rc
)
494 btv
->i2c_client
.addr
= addr
>> 1;
497 if (bytes
!= i2c_master_send(&btv
->i2c_client
, buffer
, bytes
))
502 /* read EEPROM content */
503 void __devinit
bttv_readee(struct bttv
*btv
, unsigned char *eedata
, int addr
)
507 if (bttv_I2CWrite(btv
, addr
, 0, -1, 0)<0) {
508 printk(KERN_WARNING
"bttv: readee error\n");
511 btv
->i2c_client
.addr
= addr
>> 1;
512 for (i
=0; i
<256; i
+=16) {
513 if (16 != i2c_master_recv(&btv
->i2c_client
,eedata
+i
,16)) {
514 printk(KERN_WARNING
"bttv: readee error\n");
520 /* init + register i2c algo-bit adapter */
521 int __devinit
init_bttv_i2c(struct bttv
*btv
)
523 int use_hw
= (btv
->id
== 878) && i2c_hw
;
525 memcpy(&btv
->i2c_client
, &bttv_i2c_client_template
,
526 sizeof(bttv_i2c_client_template
));
530 memcpy(&btv
->i2c_adap
, &bttv_i2c_adap_hw_template
,
531 sizeof(bttv_i2c_adap_hw_template
));
534 memcpy(&btv
->i2c_adap
, &bttv_i2c_adap_sw_template
,
535 sizeof(bttv_i2c_adap_sw_template
));
536 memcpy(&btv
->i2c_algo
, &bttv_i2c_algo_bit_template
,
537 sizeof(bttv_i2c_algo_bit_template
));
538 btv
->i2c_algo
.data
= btv
;
539 btv
->i2c_adap
.algo_data
= &btv
->i2c_algo
;
542 btv
->i2c_adap
.dev
.parent
= &btv
->dev
->dev
;
543 snprintf(btv
->i2c_adap
.name
, sizeof(btv
->i2c_adap
.name
),
544 "bt%d #%d [%s]", btv
->id
, btv
->nr
, use_hw
? "hw" : "sw");
546 i2c_set_adapdata(&btv
->i2c_adap
, btv
);
547 btv
->i2c_client
.adapter
= &btv
->i2c_adap
;
550 btv
->i2c_rc
= i2c_add_adapter(&btv
->i2c_adap
);
552 bttv_bit_setscl(btv
,1);
553 bttv_bit_setsda(btv
,1);
554 btv
->i2c_rc
= i2c_bit_add_bus(&btv
->i2c_adap
);
559 int __devexit
fini_bttv_i2c(struct bttv
*btv
)
561 int use_hw
= (btv
->id
== 878) && i2c_hw
;
563 if (0 != btv
->i2c_rc
)
567 return i2c_del_adapter(&btv
->i2c_adap
);
569 return i2c_bit_del_bus(&btv
->i2c_adap
);