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>
35 static struct i2c_algo_bit_data bttv_i2c_algo_template;
36 static struct i2c_adapter bttv_i2c_adap_template;
37 static struct i2c_client bttv_i2c_client_template;
39 EXPORT_SYMBOL(bttv_get_cardinfo);
40 EXPORT_SYMBOL(bttv_get_pcidev);
41 EXPORT_SYMBOL(bttv_get_id);
42 EXPORT_SYMBOL(bttv_gpio_enable);
43 EXPORT_SYMBOL(bttv_read_gpio);
44 EXPORT_SYMBOL(bttv_write_gpio);
45 EXPORT_SYMBOL(bttv_get_gpio_queue);
46 EXPORT_SYMBOL(bttv_i2c_call);
48 /* ----------------------------------------------------------------------- */
49 /* Exported functions - for other modules which want to access the */
50 /* gpio ports (IR for example) */
51 /* see bttv.h for comments */
53 int bttv_get_cardinfo(unsigned int card, int *type, unsigned *cardid)
55 if (card >= bttv_num) {
58 *type = bttvs[card].type;
59 *cardid = bttvs[card].cardid;
63 struct pci_dev* bttv_get_pcidev(unsigned int card)
67 return bttvs[card].dev;
70 int bttv_get_id(unsigned int card)
72 printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n");
73 if (card >= bttv_num) {
76 return bttvs[card].type;
79 int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data)
83 if (card >= bttv_num) {
88 btaor(data, ~mask, BT848_GPIO_OUT_EN);
90 bttv_gpio_tracking(btv,"extern enable");
94 int bttv_read_gpio(unsigned int card, unsigned long *data)
98 if (card >= bttv_num) {
108 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
109 because we set direct input on init */
110 *data = btread(BT848_GPIO_DATA);
114 int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data)
118 if (card >= bttv_num) {
124 /* prior setting BT848_GPIO_REG_INP is (probably) not needed
125 because direct input is set on init */
126 btaor(data & mask, ~mask, BT848_GPIO_DATA);
128 bttv_gpio_tracking(btv,"extern write");
132 wait_queue_head_t* bttv_get_gpio_queue(unsigned int card)
136 if (card >= bttv_num) {
141 if (bttvs[card].shutdown) {
148 /* ----------------------------------------------------------------------- */
151 void bttv_bit_setscl(void *data, int state)
153 struct bttv *btv = (struct bttv*)data;
156 btv->i2c_state |= 0x02;
158 btv->i2c_state &= ~0x02;
159 btwrite(btv->i2c_state, BT848_I2C);
163 void bttv_bit_setsda(void *data, int state)
165 struct bttv *btv = (struct bttv*)data;
168 btv->i2c_state |= 0x01;
170 btv->i2c_state &= ~0x01;
171 btwrite(btv->i2c_state, BT848_I2C);
175 static int bttv_bit_getscl(void *data)
177 struct bttv *btv = (struct bttv*)data;
180 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
184 static int bttv_bit_getsda(void *data)
186 struct bttv *btv = (struct bttv*)data;
189 state = btread(BT848_I2C) & 0x01;
193 static void bttv_inc_use(struct i2c_adapter *adap)
198 static void bttv_dec_use(struct i2c_adapter *adap)
203 static int attach_inform(struct i2c_client *client)
205 struct bttv *btv = i2c_get_adapdata(client->adapter);
207 if (btv->tuner_type != UNSET)
208 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
209 if (btv->pinnacle_id != UNSET)
210 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
214 printk("bttv%d: i2c attach [client=%s]\n",
215 btv->nr, i2c_clientname(client));
219 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
221 if (0 != btv->i2c_rc)
223 i2c_clients_command(&btv->i2c_adap, cmd, arg);
226 void bttv_i2c_call(unsigned int card, unsigned int cmd, void *arg)
228 if (card >= bttv_num)
230 bttv_call_i2c_clients(&bttvs[card], cmd, arg);
233 static struct i2c_algo_bit_data bttv_i2c_algo_template = {
234 .setsda = bttv_bit_setsda,
235 .setscl = bttv_bit_setscl,
236 .getsda = bttv_bit_getsda,
237 .getscl = bttv_bit_getscl,
243 static struct i2c_adapter bttv_i2c_adap_template = {
244 .inc_use = bttv_inc_use,
245 .dec_use = bttv_dec_use,
246 #ifdef I2C_ADAP_CLASS_TV_ANALOG
247 .class = I2C_ADAP_CLASS_TV_ANALOG,
249 I2C_DEVNAME("bt848"),
250 .id = I2C_HW_B_BT848,
251 .client_register = attach_inform,
254 static struct i2c_client bttv_i2c_client_template = {
255 I2C_DEVNAME("bttv internal"),
261 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
263 unsigned char buffer = 0;
265 if (0 != btv->i2c_rc)
267 if (bttv_verbose && NULL != probe_for)
268 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
269 btv->nr,probe_for,addr);
270 btv->i2c_client.addr = addr >> 1;
271 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
272 if (NULL != probe_for) {
274 printk("not found\n");
276 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
280 if (bttv_verbose && NULL != probe_for)
286 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
287 unsigned char b2, int both)
289 unsigned char buffer[2];
290 int bytes = both ? 2 : 1;
292 if (0 != btv->i2c_rc)
294 btv->i2c_client.addr = addr >> 1;
297 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
302 /* read EEPROM content */
303 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
307 if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) {
308 printk(KERN_WARNING "bttv: readee error\n");
311 btv->i2c_client.addr = addr >> 1;
312 for (i=0; i<256; i+=16) {
313 if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) {
314 printk(KERN_WARNING "bttv: readee error\n");
320 /* init + register i2c algo-bit adapter */
321 int __devinit init_bttv_i2c(struct bttv *btv)
323 memcpy(&btv->i2c_adap, &bttv_i2c_adap_template,
324 sizeof(struct i2c_adapter));
325 memcpy(&btv->i2c_algo, &bttv_i2c_algo_template,
326 sizeof(struct i2c_algo_bit_data));
327 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
328 sizeof(struct i2c_client));
330 sprintf(btv->i2c_adap.name, "bt848 #%d", btv->nr);
332 btv->i2c_algo.data = btv;
333 i2c_set_adapdata(&btv->i2c_adap, btv);
334 btv->i2c_adap.algo_data = &btv->i2c_algo;
335 btv->i2c_client.adapter = &btv->i2c_adap;
337 bttv_bit_setscl(btv,1);
338 bttv_bit_setsda(btv,1);
340 btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap);